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

Björn Schäpers via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 25 14:05:46 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)) {
----------------
HazardyKnusperkeks wrote:

This now seems a bit expensive, I think we should check for `Style.InsertBraces` before doing any of those loops.

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


More information about the cfe-commits mailing list