[clang] f14ebe9 - [clang-format] Skip preprocessor lines when finding the record lbrace
Arthur Eubanks via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 14 09:31:32 PDT 2022
Author: Arthur Eubanks
Date: 2022-04-14T09:31:15-07:00
New Revision: f14ebe91c5dd6be5b64a45e479291cd08676be0c
URL: https://github.com/llvm/llvm-project/commit/f14ebe91c5dd6be5b64a45e479291cd08676be0c
DIFF: https://github.com/llvm/llvm-project/commit/f14ebe91c5dd6be5b64a45e479291cd08676be0c.diff
LOG: [clang-format] Skip preprocessor lines when finding the record lbrace
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 https://github.com/llvm/llvm-project/issues/54901.
Reviewed By: curdeius, owenpan
Differential Revision: https://reviews.llvm.org/D123737
Added:
Modified:
clang/lib/Format/UnwrappedLineFormatter.cpp
clang/unittests/Format/FormatTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index e2dbc35c22004..9154999e2644f 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -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;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index e837dbd566957..bf4ba3d4738ee 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -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;
More information about the cfe-commits
mailing list