Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/topic/timw/format-output-code'
Browse files Browse the repository at this point in the history
* origin/topic/timw/format-output-code:
  Format output closer to what clang-format would output
  • Loading branch information
timwoj committed Jul 24, 2024
2 parents a5c8f19 + 5d477e5 commit 6e494ed
Show file tree
Hide file tree
Showing 22 changed files with 138 additions and 172 deletions.
4 changes: 4 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
0.61.0-33 | 2024-07-24 13:30:00 -0700

* Format output closer to what clang-format would output (Tim Wojtulewicz, Corelight)

0.61.0-28 | 2024-05-28 15:24:22 -0700

* CI: update FreeBSD 13 to 13.3 (Christian Kreibich, Corelight)
Expand Down
2 changes: 1 addition & 1 deletion README
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.. -*- mode: rst-mode -*-
..
.. Version number is filled in automatically.
.. |version| replace:: 0.61.0-28
.. |version| replace:: 0.61.0-33

======
BinPAC
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.61.0-28
0.61.0-33
5 changes: 2 additions & 3 deletions src/pac_action.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@ void AnalyzerAction::GenCode(Output* out_h, Output* out_cc, AnalyzerDecl* decl)

out_h->println("void %s;", action_func_proto.c_str());

out_cc->println("void %s::%s", decl->class_name().c_str(), action_func_proto.c_str());
out_cc->println("void %s::%s {", decl->class_name().c_str(), action_func_proto.c_str());
out_cc->inc_indent();
out_cc->println("{");

code_->GenCode(out_cc, &action_func_env);

out_cc->println("");
out_cc->println("}");
out_cc->dec_indent();
out_cc->println("}");
out_cc->println("");
}

Expand Down
32 changes: 13 additions & 19 deletions src/pac_array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void ArrayType::init() {

vector_str_ = strfmt("vector<%s>", elemtype_->DataTypeStr().c_str());

datatype_str_ = strfmt("%s *", vector_str_.c_str());
datatype_str_ = strfmt("%s*", vector_str_.c_str());

attr_generic_until_expr_ = nullptr;
attr_until_element_expr_ = nullptr;
Expand Down Expand Up @@ -210,14 +210,13 @@ void ArrayType::GenArrayLength(Output* out_cc, Env* env, const DataPtr& data) {
env->SetEvaluated(arraylength_var());

// Check negative array length
out_cc->println("if ( %s < 0 )", env->LValue(arraylength_var()));
out_cc->println("if ( %s < 0 ) {", env->LValue(arraylength_var()));
out_cc->inc_indent();
out_cc->println("{");
out_cc->println("throw binpac::ExceptionOutOfBound(\"%s\",", data_id_str_.c_str());
out_cc->println(" %s, (%s) - (%s));", env->LValue(arraylength_var()), env->RValue(end_of_data),
env->RValue(begin_of_data));
out_cc->println("}");
out_cc->dec_indent();
out_cc->println("}");

int element_size;

Expand Down Expand Up @@ -309,19 +308,17 @@ void ArrayType::GenCleanUpCode(Output* out_cc, Env* env) {
elem_var_field_->Prepare(env);
}

out_cc->println("if ( %s )", env->RValue(value_var()));
out_cc->println("if ( %s ) {", env->RValue(value_var()));
out_cc->inc_indent();
out_cc->println("{");

out_cc->println("for ( auto* %s : *%s )", env->LValue(elem_var()), env->RValue(value_var()));
out_cc->println("for ( auto* %s : *%s ) {", env->LValue(elem_var()), env->RValue(value_var()));
out_cc->inc_indent();
out_cc->println("{");
elemtype_->GenCleanUpCode(out_cc, env);
out_cc->println("}");
out_cc->dec_indent();

out_cc->println("}");

out_cc->dec_indent();
out_cc->println("}");
}
out_cc->println("delete %s;", lvalue());
}
Expand All @@ -331,9 +328,8 @@ string ArrayType::GenArrayInit(Output* out_cc, Env* env, bool known_array_length

array_str = lvalue();
if ( incremental_parsing() ) {
out_cc->println("if ( %s < 0 )", env->LValue(elem_it_var()));
out_cc->println("if ( %s < 0 ) {", env->LValue(elem_it_var()));
out_cc->inc_indent();
out_cc->println("{");
out_cc->println("// Initialize only once");
out_cc->println("%s = 0;", env->LValue(elem_it_var()));
}
Expand All @@ -345,8 +341,8 @@ string ArrayType::GenArrayInit(Output* out_cc, Env* env, bool known_array_length
}

if ( incremental_parsing() ) {
out_cc->println("}");
out_cc->dec_indent();
out_cc->println("}");
}

return array_str;
Expand Down Expand Up @@ -426,9 +422,8 @@ void ArrayType::DoGenParseCode(Output* out_cc, Env* env, const DataPtr& data, in
strfmt("%s < %s", env->LValue(elem_it_var()), env->RValue(arraylength_var())) :
"/* forever */";

out_cc->println("for (; %s; ++%s)", for_condition.c_str(), env->LValue(elem_it_var()));
out_cc->println("for (; %s; ++%s) {", for_condition.c_str(), env->LValue(elem_it_var()));
out_cc->inc_indent();
out_cc->println("{");

if ( attr_generic_until_expr_ )
GenUntilCheck(out_cc, env, attr_generic_until_expr_, true);
Expand Down Expand Up @@ -472,8 +467,8 @@ void ArrayType::DoGenParseCode(Output* out_cc, Env* env, const DataPtr& data, in
if ( elemtype_->IsPointerType() )
out_cc->println("%s = nullptr;", env->LValue(elem_var()));

out_cc->println("}");
out_cc->dec_indent();
out_cc->println("}");

out_cc->dec_indent();
out_cc->println("%s: ;", end_of_array_loop_label_.c_str());
Expand Down Expand Up @@ -508,9 +503,8 @@ void ArrayType::GenUntilCheck(Output* out_cc, Env* env, Expr* until_expr, bool d
}

out_cc->println("// Check &until(%s)", until_expr->orig());
out_cc->println("if ( %s )", until_expr->EvalExpr(out_cc, &check_env));
out_cc->println("if ( %s ) {", until_expr->EvalExpr(out_cc, &check_env));
out_cc->inc_indent();
out_cc->println("{");
if ( parsing_complete_var() ) {
out_cc->println("%s = true;", env->LValue(parsing_complete_var()));
}
Expand All @@ -523,8 +517,8 @@ void ArrayType::GenUntilCheck(Output* out_cc, Env* env, Expr* until_expr, bool d
}

out_cc->println("goto %s;", end_of_array_loop_label_.c_str());
out_cc->println("}");
out_cc->dec_indent();
out_cc->println("}");
}

void ArrayType::GenDynamicSize(Output* out_cc, Env* env, const DataPtr& data) {
Expand Down
10 changes: 5 additions & 5 deletions src/pac_btype.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void BuiltInType::DoGenParseCode(Output* out_cc, Env* env, const DataPtr& data,

case INT8:
case UINT8:
out_cc->println("%s = *((%s const *) (%s));", lvalue(), DataTypeStr().c_str(), data.ptr_expr());
out_cc->println("%s = *((%s const*)(%s));", lvalue(), DataTypeStr().c_str(), data.ptr_expr());
break;
case INT16:
case UINT16:
Expand All @@ -104,13 +104,13 @@ void BuiltInType::DoGenParseCode(Output* out_cc, Env* env, const DataPtr& data,
case UINT64:
#if 0
out_cc->println("%s = UnMarshall<%s>(%s, %s);",
lvalue(),
DataTypeStr().c_str(),
lvalue(),
DataTypeStr().c_str(),
data.ptr_expr(),
EvalByteOrder(out_cc, env).c_str());
#else
out_cc->println("%s = FixByteOrder(%s, *((%s const *) (%s)));", lvalue(),
EvalByteOrder(out_cc, env).c_str(), DataTypeStr().c_str(), data.ptr_expr());
out_cc->println("%s = FixByteOrder(%s, *((%s const*)(%s)));", lvalue(), EvalByteOrder(out_cc, env).c_str(),
DataTypeStr().c_str(), data.ptr_expr());
#endif
break;
}
Expand Down
41 changes: 22 additions & 19 deletions src/pac_case.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ bool CaseType::DefineValueVar() const { return false; }

string CaseType::DataTypeStr() const {
ASSERT(type_decl());
return strfmt("%s *", type_decl()->class_name().c_str());
return strfmt("%s*", type_decl()->class_name().c_str());
}

Type* CaseType::ValueType() const {
Expand Down Expand Up @@ -117,15 +117,14 @@ void CaseType::GenCleanUpCode(Output* out_cc, Env* env) {
Type::GenCleanUpCode(out_cc, env);

env->set_in_branch(true);
out_cc->println("switch ( %s )", env->RValue(index_var_));
out_cc->println("switch ( %s ) {", env->RValue(index_var_));
out_cc->inc_indent();
out_cc->println("{");
foreach (i, CaseFieldList, cases_) {
CaseField* c = *i;
c->GenCleanUpCode(out_cc, env);
}
out_cc->println("}");
out_cc->dec_indent();
out_cc->println("}");
env->set_in_branch(false);
}

Expand All @@ -142,9 +141,8 @@ void CaseType::DoGenParseCode(Output* out_cc, Env* env, const DataPtr& data, int
env->SetEvaluated(index_var_);

env->set_in_branch(true);
out_cc->println("switch ( %s )", env->RValue(index_var_));
out_cc->println("switch ( %s ) {", env->RValue(index_var_));
out_cc->inc_indent();
out_cc->println("{");
bool has_default_case = false;
foreach (i, CaseFieldList, cases_) {
CaseField* c = *i;
Expand All @@ -161,8 +159,8 @@ void CaseType::DoGenParseCode(Output* out_cc, Env* env, const DataPtr& data, int
out_cc->println("break;");
out_cc->dec_indent();
}
out_cc->println("}");
out_cc->dec_indent();
out_cc->println("}");
env->set_in_branch(false);

if ( compute_size_var )
Expand Down Expand Up @@ -282,7 +280,7 @@ void GenCaseStr(ExprList* index_list, Output* out_cc, Env* env, Type* switch_typ
// We're always using "int" for storage, so ok to just
// cast into the type used by the switch statement since
// some unsafe stuff is already checked above.
out_cc->println("case ((%s) %d):", switch_type->DataTypeStr().c_str(), index_const);
out_cc->println("case ((%s)%d):", switch_type->DataTypeStr().c_str(), index_const);
}
}
else {
Expand All @@ -303,17 +301,14 @@ void CaseField::GenPubDecls(Output* out_h, Env* env) {
if ( type_->DataTypeStr().empty() )
return;

out_h->println("%s %s const", type_->DataTypeConstRefStr().c_str(), env->RValue(id_));

out_h->println("%s %s const {", type_->DataTypeConstRefStr().c_str(), env->RValue(id_));
out_h->inc_indent();
out_h->println("{");

if ( ! index_ )
out_h->println("return %s;", lvalue());
else {
out_h->println("switch ( %s )", env->RValue(index_var_));
out_h->println("switch ( %s ) {", env->RValue(index_var_));
out_h->inc_indent();
out_h->println("{");
GenCaseStr(index_, out_h, env, case_type()->IndexExpr()->DataType(env));
out_h->inc_indent();
out_h->println("break; // OK");
Expand All @@ -326,14 +321,14 @@ void CaseField::GenPubDecls(Output* out_h, Env* env) {
out_h->println("break;");
out_h->dec_indent();

out_h->println("}");
out_h->dec_indent();
out_h->println("}");

out_h->println("return %s;", lvalue());
}

out_h->println("}");
out_h->dec_indent();
out_h->println("}");
}

void CaseField::GenInitCode(Output* out_cc, Env* env) {
Expand All @@ -351,10 +346,16 @@ void CaseField::GenCleanUpCode(Output* out_cc, Env* env) {
GenCaseStr(index_, out_cc, env, case_type()->IndexExpr()->DataType(env));
out_cc->inc_indent();
out_cc->println("// Clean up \"%s\"", id_->Name());
out_cc->println("{");
if ( ! anonymous_field() )
if ( ! anonymous_field() ) {
out_cc->println("{");
out_cc->inc_indent();
type_->GenCleanUpCode(out_cc, env);
out_cc->println("}");
out_cc->dec_indent();
out_cc->println("}");
}
else
out_cc->println("{}");

out_cc->println("break;");
out_cc->dec_indent();
}
Expand All @@ -364,6 +365,7 @@ void CaseField::GenParseCode(Output* out_cc, Env* env, const DataPtr& data, cons
out_cc->inc_indent();
out_cc->println("// Parse \"%s\"", id_->Name());
out_cc->println("{");
out_cc->inc_indent();

{
Env case_env(env, this);
Expand All @@ -378,9 +380,10 @@ void CaseField::GenParseCode(Output* out_cc, Env* env, const DataPtr& data, cons
out_cc->println("%s = %s;", case_env.LValue(case_type()->parsing_complete_var()),
case_env.RValue(type_->parsing_complete_var()));
}
out_cc->println("}");
}

out_cc->dec_indent();
out_cc->println("}");
out_cc->println("break;");
out_cc->dec_indent();
}
Expand Down
15 changes: 6 additions & 9 deletions src/pac_conn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ void ConnDecl::GenEOFFunc(Output* out_h, Output* out_cc) {

out_h->println("void %s;", proto.c_str());

out_cc->println("void %s::%s", class_name().c_str(), proto.c_str());
out_cc->println("void %s::%s {", class_name().c_str(), proto.c_str());
out_cc->inc_indent();
out_cc->println("{");

out_cc->println("if ( is_orig )");
out_cc->inc_indent();
Expand All @@ -81,8 +80,8 @@ void ConnDecl::GenEOFFunc(Output* out_h, Output* out_cc) {

out_cc->dec_indent();

out_cc->println("}");
out_cc->dec_indent();
out_cc->println("}");
out_cc->println("");
}

Expand All @@ -91,9 +90,8 @@ void ConnDecl::GenGapFunc(Output* out_h, Output* out_cc) {

out_h->println("void %s;", proto.c_str());

out_cc->println("void %s::%s", class_name().c_str(), proto.c_str());
out_cc->println("void %s::%s {", class_name().c_str(), proto.c_str());
out_cc->inc_indent();
out_cc->println("{");

out_cc->println("if ( is_orig )");
out_cc->inc_indent();
Expand All @@ -104,8 +102,8 @@ void ConnDecl::GenGapFunc(Output* out_h, Output* out_cc) {
out_cc->println("%s->%s(gap_length);", env_->LValue(downflow_id), kFlowGap);
out_cc->dec_indent();

out_cc->println("}");
out_cc->dec_indent();
out_cc->println("}");
out_cc->println("");
}

Expand All @@ -114,9 +112,8 @@ void ConnDecl::GenProcessFunc(Output* out_h, Output* out_cc) {

out_h->println("void %s;", proto.c_str());

out_cc->println("void %s::%s", class_name().c_str(), proto.c_str());
out_cc->println("void %s::%s {", class_name().c_str(), proto.c_str());
out_cc->inc_indent();
out_cc->println("{");

out_cc->println("if ( is_orig )");
out_cc->inc_indent();
Expand All @@ -127,7 +124,7 @@ void ConnDecl::GenProcessFunc(Output* out_h, Output* out_cc) {
out_cc->println("%s->%s(begin, end);", env_->LValue(downflow_id), kNewData);
out_cc->dec_indent();

out_cc->println("}");
out_cc->dec_indent();
out_cc->println("}");
out_cc->println("");
}
6 changes: 3 additions & 3 deletions src/pac_ctype.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
string CType::DeclareInstance(const string& var) const { return strfmt("%s %s", name().c_str(), var.c_str()); }

string CType::DeclareConstReference(const string& var) const {
return strfmt("%s const &%s", name().c_str(), var.c_str());
return strfmt("%s const& %s", name().c_str(), var.c_str());
}

string CType::DeclareConstPointer(const string& var) const {
return strfmt("%s const *%s", name().c_str(), var.c_str());
return strfmt("%s const* %s", name().c_str(), var.c_str());
}

string CType::DeclarePointer(const string& var) const { return strfmt("%s *%s", name().c_str(), var.c_str()); }
string CType::DeclarePointer(const string& var) const { return strfmt("%s* %s", name().c_str(), var.c_str()); }
Loading

0 comments on commit 6e494ed

Please sign in to comment.