[clang] [clang-format] Restore InsertBraces around interior preprocessor directives (PR #239459)

Andre Sun via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 29 17:57:22 PST 2026


================
@@ -3078,29 +3163,19 @@ void UnwrappedLineParser::parseUnbracedBody(bool CheckEOF) {
     assert(!Line->InPPDirective);
     FormatToken *const LBraceTok = Tok;
     Tok = nullptr;
-    auto BodyEnd = CurrentLines->size();
-    while (BodyEnd > 0) {
-      const auto &L = (*CurrentLines)[--BodyEnd];
-      if (!L.InPPDirective && getLastNonComment(L)) {
-        Tok = L.Tokens.back().Tok;
+    for (const auto &BodyLine : llvm::reverse(*CurrentLines)) {
+      if (!BodyLine.InPPDirective && getLastNonComment(BodyLine)) {
+        Tok = BodyLine.Tokens.back().Tok;
         break;
       }
     }
     assert(Tok);
-    // 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 &BodyLine = (*CurrentLines)[I];
-      EnclosesPPDirective =
-          BodyLine.InPPDirective ||
-          llvm::any_of(BodyLine.Tokens, [](const UnwrappedLineNode &Node) {
-            return Node.Tok->FirstAfterPPLine;
-          });
-    }
-    if (EnclosesPPDirective || FormatTok == TokenAfterIfWithConditionalElse)
-      LBraceTok->BraceCount = 0;
-    else
+    if (FormatTok != TokenAfterIfWithConditionalElse &&
+        canInsertBraces(*LBraceTok, *Tok)) {
----------------
and2049 wrote:

`Style.InsertBraces` is already checked at https://github.com/llvm/llvm-project/blob/b4039f3a70b7605065bdc553e2c00e97881317eb/clang/lib/Format/UnwrappedLineParser.cpp#L3139-L3147

https://github.com/llvm/llvm-project/pull/239459


More information about the cfe-commits mailing list