[clang] aac6c9f - [clang-format] Fix InsertBraces after preprocessor directives in lambdas (#225184)

via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 4 11:53:14 PST 2026


Author: Andre Sun
Date: 2026-11-04T20:53:09+01:00
New Revision: aac6c9f8cb74e0bd75db0f590b764c04dcf262ab

URL: https://github.com/llvm/llvm-project/commit/aac6c9f8cb74e0bd75db0f590b764c04dcf262ab
DIFF: https://github.com/llvm/llvm-project/commit/aac6c9f8cb74e0bd75db0f590b764c04dcf262ab.diff

LOG: [clang-format] Fix InsertBraces after preprocessor directives in lambdas (#225184)

Check for directives within the current control-statement line instead
of the pending directive buffer, which can retain earlier directives
inside lambdas and suppress brace insertion for subsequent statements.

Fixes #199648.

Added: 
    

Modified: 
    clang/lib/Format/UnwrappedLineParser.cpp
    clang/unittests/Format/BracesInserterTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 27974cf25cc2e..ae507a0e96a57 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -2927,11 +2927,17 @@ static FormatToken *getLastNonComment(const UnwrappedLine &Line) {
   return nullptr;
 }
 
+static bool containsPPDirective(const UnwrappedLine &Line) {
+  return llvm::any_of(
+      llvm::drop_begin(Line.Tokens),
+      [](const UnwrappedLineNode &Node) { return Node.Tok->FirstAfterPPLine; });
+}
+
 void UnwrappedLineParser::parseUnbracedBody(bool CheckEOF) {
   FormatToken *Tok = nullptr;
 
   if (Style.InsertBraces && !Line->InPPDirective && !Line->Tokens.empty() &&
-      PreprocessorDirectives.empty() && FormatTok->isNot(tok::semi)) {
+      !containsPPDirective(*Line) && FormatTok->isNot(tok::semi)) {
     Tok = Style.BraceWrapping.AfterControlStatement == FormatStyle::BWACS_Never
               ? getLastNonComment(*Line)
               : Line->Tokens.back().Tok;

diff  --git a/clang/unittests/Format/BracesInserterTest.cpp b/clang/unittests/Format/BracesInserterTest.cpp
index b5013dfb6ffa7..6244ed5508654 100644
--- a/clang/unittests/Format/BracesInserterTest.cpp
+++ b/clang/unittests/Format/BracesInserterTest.cpp
@@ -308,6 +308,67 @@ TEST_F(BracesInserterTest, InsertBraces) {
                "} while (0);",
                Style);
 
+  verifyFormat("auto l = [] {\n"
+               "  if (a) {\n"
+               "    f();\n"
+               "  }\n"
+               "#if X\n"
+               "#endif\n"
+               "  if (b) {\n"
+               "    g();\n"
+               "  }\n"
+               "};",
+               "auto l = [] {\n"
+               "  if (a) f();\n"
+               "#if X\n"
+               "#endif\n"
+               "  if (b) g();\n"
+               "};",
+               Style);
+
+  verifyFormat("auto l = [] {\n"
+               "  if (a)\n"
+               "#ifdef A\n"
+               "    f();\n"
+               "#else\n"
+               "    g();\n"
+               "#endif\n"
+               "  if (b) {\n"
+               "    h();\n"
+               "  }\n"
+               "};",
+               "auto l = [] {\n"
+               "  if (a)\n"
+               "#ifdef A\n"
+               "    f();\n"
+               "#else\n"
+               "    g();\n"
+               "#endif\n"
+               "  if (b) h();\n"
+               "};",
+               Style);
+
+  verifyFormat("auto l = [&] {\n"
+               "#if X\n"
+               "#endif\n"
+               "  if (a)\n"
+               "    if (b)\n"
+               "#if Y\n"
+               "      f();\n"
+               "#else\n"
+               "      g();\n"
+               "#endif\n"
+               "};",
+               Style);
+
+  verifyFormat("if (a\n"
+               "#if X\n"
+               "    && b\n"
+               "#endif\n"
+               ")\n"
+               "  f();",
+               Style);
+
   Style.RemoveBracesLLVM = true;
   verifyFormat("if (a) //\n"
                "  return b;",


        


More information about the cfe-commits mailing list