Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format output closer to what clang-format would output #35

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No biggie but this can apparently still leave empty pairs of curly braces, like this example from Zeek's websocket_pac.cc:

WebSocket_FrameHeader::~WebSocket_FrameHeader() {
    switch ( maybe_more_len_case_index() ) {
        case ((uint8)126):
            // Clean up "payload_len2"
            {
            }
            break;
        case ((uint8)127):
            // Clean up "payload_len8"
            {
            }
            break;
        default:
            // Clean up "short_len"
            {
            }
            break;
    }

Just in case you want to iterate on that one.

Copy link
Member Author

@timwoj timwoj Jul 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't even generate those case blocks if they're not doing anything. Unfortunately we can't know they're not going to be blank until we call GenCleanUpCode on the object, which does it's own output to the file. There's a lot of places in binpac where that kind of thing happens.

In this case though, I can't squash those because the check there is for an anonymous field. These cleanup blocks are for named fields in binpac, and it has to call GenCleanUpCode on them even if it's going to return nothing.

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()); }
5 changes: 2 additions & 3 deletions src/pac_dataptr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ void DataPtr::GenBoundaryCheck(Output* out_cc, Env* env, const char* data_size,
ASSERT(id_);

out_cc->println("// Checking out-of-bound for \"%s\"", data_name);
out_cc->println("if ( %s + (%s) > %s || %s + (%s) < %s )", ptr_expr(), data_size, env->RValue(end_of_data),
out_cc->println("if ( %s + (%s) > %s || %s + (%s) < %s ) {", ptr_expr(), data_size, env->RValue(end_of_data),
ptr_expr(), data_size, ptr_expr());

out_cc->inc_indent();
out_cc->println("{");

char* data_offset = AbsOffsetExpr(env, begin_of_data);

Expand All @@ -47,6 +46,6 @@ void DataPtr::GenBoundaryCheck(Output* out_cc, Env* env, const char* data_size,

delete[] data_offset;

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