Skip to content

Commit

Permalink
[clang-format] Skip preprocessor lines when finding the record lbrace
Browse files Browse the repository at this point in the history
With D117142, we would now format

```
struct A {
#define A
  void f() { a(); }
#endif
};
```

into

```
struct A {
#ifdef A
  void f() {
    a();
  }
#endif
};
```

because we were looking for the record lbrace without skipping preprocess lines.

Fixes llvm/llvm-project#54901.

Reviewed By: curdeius, owenpan

Differential Revision: https://reviews.llvm.org/D123737
  • Loading branch information
aeubanks authored and memfrob committed Oct 4, 2022
1 parent eafe6c3 commit e1f3986
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clang/lib/Format/UnwrappedLineFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ class LineJoiner {
// Find the last line with lower level.
auto J = I - 1;
for (; J != AnnotatedLines.begin(); --J)
if ((*J)->Level < TheLine->Level)
if (!(*J)->InPPDirective && (*J)->Level < TheLine->Level)
break;
if ((*J)->Level >= TheLine->Level)
return false;
Expand Down
7 changes: 7 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12766,6 +12766,13 @@ TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
"};",
MergeInlineOnly);

verifyFormat("class C {\n"
"#ifdef A\n"
" int f() { return 42; }\n"
"#endif\n"
"};",
MergeInlineOnly);

// Also verify behavior when BraceWrapping.AfterFunction = true
MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
MergeInlineOnly.BraceWrapping.AfterFunction = true;
Expand Down

0 comments on commit e1f3986

Please sign in to comment.