Author: Jacob Abraham Date: 2022-10-10T19:57:58-07:00 New Revision: b0758f62afb6945edfb2046da10b338bb6ced474
URL: https://github.com/llvm/llvm-project/commit/b0758f62afb6945edfb2046da10b338bb6ced474 DIFF: https://github.com/llvm/llvm-project/commit/b0758f62afb6945edfb2046da10b338bb6ced474.diff LOG: [clang-format] Fix mis-attributing preprocessor directives to macros This solves the issue where a case statement inside a macro greedily adds preprocessor lines such as #include to the macro even if they are not a part of the macro to begin with. Fixes #58214. Differential Revision: https://reviews.llvm.org/D135422 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 7076a9f5cb3f4..d3aa0625d471a 100644 --- a/clang/lib/Format/UnwrappedLineFormatter.cpp +++ b/clang/lib/Format/UnwrappedLineFormatter.cpp @@ -643,6 +643,7 @@ class LineJoiner { unsigned Length = 0; bool EndsWithComment = false; bool InPPDirective = I[0]->InPPDirective; + bool InMacroBody = I[0]->InMacroBody; const unsigned Level = I[0]->Level; for (; NumStmts < 3; ++NumStmts) { if (I + 1 + NumStmts == E) @@ -650,6 +651,8 @@ class LineJoiner { const AnnotatedLine *Line = I[1 + NumStmts]; if (Line->InPPDirective != InPPDirective) break; + if (Line->InMacroBody != InMacroBody) + break; if (Line->First->isOneOf(tok::kw_case, tok::kw_default, tok::r_brace)) break; if (Line->First->isOneOf(tok::kw_if, tok::kw_for, tok::kw_switch, diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index e29fe1066623e..a777382e762a0 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -2999,6 +2999,10 @@ TEST_F(FormatTest, ShortCaseLabels) { "}", Style); Style.ColumnLimit = 21; + verifyFormat("#define X \\\n" + " case 0: break;\n" + "#include \"f\"", + Style); verifyFormat("switch (a) {\n" "case 1: x = 1; break;\n" "case 2: return;\n" _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits