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

Andre Sun via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 3 20:01:50 PST 2026


https://github.com/and2049 updated https://github.com/llvm/llvm-project/pull/225184

>From d033af06999bc4bd9d085a147c1fb3076abf13c2 Mon Sep 17 00:00:00 2001
From: Andre Sun <andsun9999 at gmail.com>
Date: Sun, 20 Sep 2026 13:07:59 -0400
Subject: [PATCH 1/2] [clang-format] Fix InsertBraces inserting braces around
 directives

Fixes #224942.
---
 clang/lib/Format/UnwrappedLineParser.cpp      | 21 ++++++++-
 clang/unittests/Format/BracesInserterTest.cpp | 45 +++++++++++++++++++
 2 files changed, 64 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 2d2fe729fbc2f1..86445cc02cfa66 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -2910,6 +2910,7 @@ void UnwrappedLineParser::parseUnbracedBody(bool CheckEOF) {
   }
 
   addUnwrappedLine();
+  const size_t BodyStart = CurrentLines->size();
   ++Line->Level;
   ++Line->UnbracedBodyLevel;
   parseStructuralElement();
@@ -2917,15 +2918,31 @@ void UnwrappedLineParser::parseUnbracedBody(bool CheckEOF) {
 
   if (Tok) {
     assert(!Line->InPPDirective);
+    FormatToken *const LBraceTok = Tok;
     Tok = nullptr;
-    for (const auto &L : llvm::reverse(*CurrentLines)) {
+    size_t BodyEnd = CurrentLines->size();
+    while (BodyEnd > 0) {
+      const auto &L = (*CurrentLines)[--BodyEnd];
       if (!L.InPPDirective && getLastNonComment(L)) {
         Tok = L.Tokens.back().Tok;
         break;
       }
     }
     assert(Tok);
-    ++Tok->BraceCount;
+    // Don't insert the braces if they would enclose preprocessor directives.
+    bool EnclosesPPDirective = false;
+    for (size_t I = BodyStart; I <= BodyEnd && !EnclosesPPDirective; ++I) {
+      const auto &L = (*CurrentLines)[I];
+      EnclosesPPDirective =
+          L.InPPDirective ||
+          llvm::any_of(L.Tokens, [](const UnwrappedLineNode &Node) {
+            return Node.Tok->FirstAfterPPLine;
+          });
+    }
+    if (EnclosesPPDirective)
+      LBraceTok->BraceCount = 0;
+    else
+      ++Tok->BraceCount;
   }
 
   if (CheckEOF && eof())
diff --git a/clang/unittests/Format/BracesInserterTest.cpp b/clang/unittests/Format/BracesInserterTest.cpp
index 572e53e595e37d..11c7c17b9aeb44 100644
--- a/clang/unittests/Format/BracesInserterTest.cpp
+++ b/clang/unittests/Format/BracesInserterTest.cpp
@@ -195,6 +195,51 @@ TEST_F(BracesInserterTest, InsertBraces) {
                "#endif",
                Style);
 
+  verifyFormat("if (a)\n"
+               "  if (b)\n"
+               "#ifdef A\n"
+               "    f();\n"
+               "#else\n"
+               "    g();\n"
+               "#endif",
+               Style);
+
+  verifyFormat("while (a)\n"
+               "  if (b)\n"
+               "    f(\n"
+               "#ifdef A\n"
+               "        1);\n"
+               "#else\n"
+               "        2);\n"
+               "#endif",
+               Style);
+
+  verifyFormat("if (a)\n"
+               "  f(\n"
+               "#ifdef A\n"
+               "      1\n"
+               "#else\n"
+               "      2\n"
+               "#endif\n"
+               "  );",
+               Style);
+
+  verifyFormat("#if A\n"
+               "if (a) {\n"
+               "  f();\n"
+               "}\n"
+               "#else\n"
+               "if (b) {\n"
+               "  g();\n"
+               "}\n"
+               "#endif",
+               "#if A\n"
+               "if (a) f();\n"
+               "#else\n"
+               "if (b) g();\n"
+               "#endif",
+               Style);
+
   verifyFormat("#if 0\n"
                "#elif 1\n"
                "#endif\n"

>From dd781761a34b9de24c403a2b97477c35a26b380b Mon Sep 17 00:00:00 2001
From: Andre Sun <andsun9999 at gmail.com>
Date: Mon, 21 Sep 2026 16:06:21 -0400
Subject: [PATCH 2/2] [clang-format] Fix InsertBraces after preprocessor
 directives in lambdas

Depends on #224947.

Fixes #199648.
---
 clang/lib/Format/UnwrappedLineParser.cpp      |  8 ++-
 clang/unittests/Format/BracesInserterTest.cpp | 61 +++++++++++++++++++
 2 files changed, 68 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 86445cc02cfa66..2cb1fc2cf9c462 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -2892,11 +2892,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 11c7c17b9aeb44..4c0f873dd4b8ef 100644
--- a/clang/unittests/Format/BracesInserterTest.cpp
+++ b/clang/unittests/Format/BracesInserterTest.cpp
@@ -265,6 +265,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