Skip to content

Keep the semicolon inside #if for cfg'd struct and union fields - #1193

Open
thelastbackspace wants to merge 1 commit into
mozilla:mainfrom
thelastbackspace:fix/1058-cfg-field-semicolon
Open

thelastbackspace wants to merge 1 commit into
mozilla:mainfrom
thelastbackspace:fix/1058-cfg-field-semicolon

Conversation

@thelastbackspace

Copy link
Copy Markdown

Fixes #1058

When a struct or union field has a #[cfg] that maps to a define, the ; ended up after the #endif:

struct Foobar {
  uint32_t a;
#if defined(XYZ)
  uint32_t b
#endif
  ;
  uint32_t c;
};

That gives -Wextra-semi warnings when the define isn't set. As ogoffart pointed out in the issue, the cause is write_vertical_source_list with ListType::Cap(";"): it adds the ; only after write_field has already closed the condition.

I split write_field so that struct and union members write their ; before condition.write_after(). The list now uses Join(""), so it doesn't add a second one. Uncfg'd fields come out the same as before. The C++ constructor argument list still uses write_field with Join(","), so it keeps the existing newline workaround from #634.

Now it's generated as:

#if defined(XYZ)
  uint32_t b;
#endif

The cfg, cfg_both and cfg_tag expectations already contained the stray ; line. I regenerated them, and they're the only expectation files that change. If I keep the new expectations and revert the source change, test_cfg fails.

Testing: CBINDGEN_TEST_VERIFY=1 cargo +nightly test (161 passed, so all expectations compile with gcc, g++ and Cython 3.0.2), plus cargo fmt --check and cargo clippy --all-targets (no new warnings).

A field with a #[cfg] that maps to a define was written as

    #if defined(XYZ)
      uint32_t b
    #endif
      ;

because the list writer appended the ';' after write_field had already
closed the condition. Write the terminator for struct and union members
before the #endif instead. The C++ constructor argument list still uses
the old path since it joins with ','.

Fixes mozilla#1058
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.

Extra semicolon with define

1 participant