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

Conversation

timwoj
Copy link
Member

@timwoj timwoj commented Apr 25, 2024

While working directly on a pac.cc file generated from a large analyzer, my Emacs session reformatted the whole file based on the clang-format configuration used by all of the Zeek projects. This took a very long time, since the change set was pretty large. This PR changes the output from binpac to be much closer to what clang-format would output. It's not perfect, since clang-format will do things like compressing empty blocks or splitting long lines, and we can't do that easily in binpac.

Copy link
Member

@ckreibich ckreibich left a comment

Choose a reason for hiding this comment

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

Thank you!

@@ -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.

@timwoj timwoj force-pushed the topic/timw/format-output-code branch from 40e216a to 5d477e5 Compare July 24, 2024 19:54
@timwoj timwoj merged commit 6e494ed into master Jul 24, 2024
18 checks passed
@timwoj timwoj deleted the topic/timw/format-output-code branch July 24, 2024 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants