[clang] [clang-format] Insert braces around balanced preprocessor conditionals (PR #235797)

Andre Sun via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 26 08:09:56 PDT 2026


https://github.com/and2049 created https://github.com/llvm/llvm-project/pull/235797

Choose a safe closing-brace position instead of skipping insertion whenever an unbraced body contains preprocessor directives. Preserve insertion around interior conditionals and place the closing brace after `#endif` when each branch ends the body.

Follow-up to #224947.

Depends on #235083. (first commit)


>From 25591925c1a4e10f4cc83589bfa692939b701318 Mon Sep 17 00:00:00 2001
From: Andre Sun <andsun9999 at gmail.com>
Date: Fri, 23 Oct 2026 15:53:55 -0400
Subject: [PATCH 1/2] [clang-format] Fix duplicate brace insertion across
 preprocessor runs

Keep the opening-brace marker after emitting it so that later
preprocessor runs don't insert the braces again.

Fixes #235036
---
 clang/lib/Format/Format.cpp                   |  7 ++-
 clang/lib/Format/FormatToken.h                |  4 ++
 clang/unittests/Format/BracesInserterTest.cpp | 43 +++++++++++++++++++
 3 files changed, 53 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index d83ca4a558b7f8..f1e50621ea2b7a 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -2751,10 +2751,15 @@ class BracesInserter : public TokenAnalyzer {
         std::string Brace;
         if (BraceCount < 0) {
           assert(BraceCount == -1);
+          // Keep the marker so that the parser doesn't mark the token again in
+          // a later run.
+          if (Token->LBraceInserted)
+            continue;
           if (!Line->Affected)
             break;
           Brace = Token->is(tok::comment) ? "\n{" : "{";
           ++OpeningBraceSurplus;
+          Token->LBraceInserted = true;
         } else {
           if (OpeningBraceSurplus == 0)
             break;
@@ -2762,8 +2767,8 @@ class BracesInserter : public TokenAnalyzer {
             BraceCount = OpeningBraceSurplus;
           Brace = '\n' + std::string(BraceCount, '}');
           OpeningBraceSurplus -= BraceCount;
+          Token->BraceCount = 0;
         }
-        Token->BraceCount = 0;
         const auto Start = Token->Tok.getEndLoc();
         cantFail(Result.add(tooling::Replacement(SourceMgr, Start, 0, Brace)));
       }
diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index 5bf0d4dfd3292c..ea5db44d77b004 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -631,6 +631,10 @@ struct FormatToken {
   ///   >0: number of right braces
   int8_t BraceCount = 0;
 
+  /// Whether the left brace was already inserted after this token when an
+  /// earlier run was analyzed.
+  bool LBraceInserted = false;
+
   /// If this token starts a block, this contains all the unwrapped lines
   /// in it.
   SmallVector<AnnotatedLine *, 1> Children;
diff --git a/clang/unittests/Format/BracesInserterTest.cpp b/clang/unittests/Format/BracesInserterTest.cpp
index 11c7c17b9aeb44..b5013dfb6ffa7f 100644
--- a/clang/unittests/Format/BracesInserterTest.cpp
+++ b/clang/unittests/Format/BracesInserterTest.cpp
@@ -240,6 +240,49 @@ TEST_F(BracesInserterTest, InsertBraces) {
                "#endif",
                Style);
 
+  verifyFormat("void f() {\n"
+               "#if A\n"
+               "  a();\n"
+               "#else\n"
+               "  b();\n"
+               "#endif\n"
+               "  if (c) {\n"
+               "    h();\n"
+               "  }\n"
+               "}",
+               "void f() {\n"
+               "#if A\n"
+               "  a();\n"
+               "#else\n"
+               "  b();\n"
+               "#endif\n"
+               "  if (c) h();\n"
+               "}",
+               Style);
+
+  verifyFormat("void f() {\n"
+               "#if A\n"
+               "  a();\n"
+               "#else\n"
+               "  b();\n"
+               "#endif\n"
+               "  if (c) {\n"
+               "    if (d) {\n"
+               "      h();\n"
+               "    }\n"
+               "  }\n"
+               "}",
+               "void f() {\n"
+               "#if A\n"
+               "  a();\n"
+               "#else\n"
+               "  b();\n"
+               "#endif\n"
+               "  if (c)\n"
+               "    if (d) h();\n"
+               "}",
+               Style);
+
   verifyFormat("#if 0\n"
                "#elif 1\n"
                "#endif\n"

>From 94d28042b47063ee616adf766d70c8e07ed25bb8 Mon Sep 17 00:00:00 2001
From: Andre Sun <andsun9999 at gmail.com>
Date: Mon, 26 Oct 2026 11:01:18 -0400
Subject: [PATCH 2/2] [clang-format] Insert braces around balanced preprocessor
 conditionals

Follow-up to #224947.
---
 clang/lib/Format/UnwrappedLineParser.cpp      | 190 +++++++++++++--
 clang/lib/Format/UnwrappedLineParser.h        |  36 +++
 clang/unittests/Format/BracesInserterTest.cpp | 220 +++++++++++++++++-
 3 files changed, 425 insertions(+), 21 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index c4e7faa055135b..51d0d5a12de183 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -1081,6 +1081,26 @@ void UnwrappedLineParser::parsePPDirective() {
   }
 }
 
+void UnwrappedLineParser::PPBranchSummary::add(const FormatToken &Tok) {
+  if (Tok.isOneOf(tok::l_paren, tok::l_square, tok::l_brace)) {
+    ++Depth;
+  } else if (Tok.isOneOf(tok::r_paren, tok::r_square, tok::r_brace)) {
+    --Depth;
+    MinDepth = std::min(MinDepth, Depth);
+  }
+  EndsWithTerminator = false;
+  if (Tok.isNoneOf(tok::semi, tok::r_brace))
+    return;
+  if (Terminators == 0 || Depth < TerminatorDepth) {
+    TerminatorDepth = Depth;
+    Terminators = 0;
+  }
+  if (Depth == TerminatorDepth) {
+    ++Terminators;
+    EndsWithTerminator = true;
+  }
+}
+
 void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
   size_t Line = CurrentLines->size();
   if (CurrentLines == &PreprocessorDirectives)
@@ -1095,6 +1115,8 @@ void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
 }
 
 void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
+  if (!PP.Stack.empty())
+    PP.Stack.back().Summary.HasNestedConditional = true;
   ++PP.BranchLevel;
   assert(PP.BranchLevel >= 0 &&
          PP.BranchLevel <= (int)PP.LevelBranchIndex.size());
@@ -1135,6 +1157,8 @@ void UnwrappedLineParser::conditionalCompilationEnd() {
 
 void UnwrappedLineParser::parsePPIf(bool IfDef) {
   bool IfNDef = FormatTok->is(tok::pp_ifndef);
+  if (!PP.Stack.empty())
+    PPConditionalDepths[FormatTok] = PP.Stack.back().Summary.Depth;
   nextToken();
   bool Unreachable = false;
   if (!IfDef && (FormatTok->is(tok::kw_false) || FormatTok->TokenText == "0"))
@@ -1172,6 +1196,7 @@ void UnwrappedLineParser::parsePPElse() {
   assert(PP.BranchLevel >= -1);
   if (PP.BranchLevel == -1)
     conditionalCompilationStart(/*Unreachable=*/true);
+  PPBranchSummaries[FormatTok] = PP.Stack.back().Summary;
   conditionalCompilationAlternative();
   --PP.BranchLevel;
   parsePPUnknown();
@@ -1179,6 +1204,8 @@ void UnwrappedLineParser::parsePPElse() {
 }
 
 void UnwrappedLineParser::parsePPEndIf() {
+  if (!PP.Stack.empty())
+    PPBranchSummaries[FormatTok] = PP.Stack.back().Summary;
   conditionalCompilationEnd();
   parsePPUnknown();
 }
@@ -2917,6 +2944,20 @@ void UnwrappedLineParser::keepAncestorBraces() {
   NestedTooDeep.push_back(false);
 }
 
+static const FormatToken *getPPKeyword(const UnwrappedLine &Line) {
+  if (!Line.InPPDirective)
+    return nullptr;
+  const auto IsNotComment = [](const UnwrappedLineNode &Node) {
+    return Node.Tok->isNot(tok::comment);
+  };
+  const auto End = Line.Tokens.end();
+  auto Hash = llvm::find_if(Line.Tokens, IsNotComment);
+  if (Hash == End || Hash->Tok->isNot(tok::hash))
+    return nullptr;
+  const auto Keyword = std::find_if(std::next(Hash), End, IsNotComment);
+  return Keyword == End ? nullptr : Keyword->Tok;
+}
+
 static FormatToken *getLastNonComment(const UnwrappedLine &Line) {
   for (const auto &Token : llvm::reverse(Line.Tokens))
     if (Token.Tok->isNot(tok::comment))
@@ -2925,6 +2966,123 @@ static FormatToken *getLastNonComment(const UnwrappedLine &Line) {
   return nullptr;
 }
 
+static int getBracketDepthBefore(const SmallVectorImpl<UnwrappedLine> &Lines,
+                                 size_t BodyStart, size_t End,
+                                 SourceLocation Loc) {
+  int Depth = 0;
+  for (size_t I = BodyStart; I < End; ++I) {
+    if (Lines[I].InPPDirective)
+      continue;
+    for (const auto &Node : Lines[I].Tokens) {
+      const auto &Tok = *Node.Tok;
+      if (Tok.Tok.getLocation() >= Loc)
+        break;
+      if (Tok.isOneOf(tok::l_paren, tok::l_square, tok::l_brace))
+        ++Depth;
+      else if (Tok.isOneOf(tok::r_paren, tok::r_square, tok::r_brace))
+        --Depth;
+    }
+  }
+  return Depth;
+}
+
+// Find a closing brace position that encloses the same body in every branch.
+// Keep BodyEnd if it is safe, or move past #endif if every branch ends the body
+// there. Return null if neither is safe. Directives remain in source order in
+// CurrentLines, even when stored after the code line that contains them.
+FormatToken *UnwrappedLineParser::findRBraceToken(size_t BodyStart,
+                                                  FormatToken *BodyEnd) const {
+  struct Conditional {
+    // Bracket depth at #if, relative to the start of the body.
+    int Depth;
+    bool HasElse = false;
+    // A nested conditional changed the depth, but the branch summary omits it.
+    bool UnbalancedNested = false;
+    const PPBranchSummary *BodyBranch = nullptr;
+    SmallVector<const PPBranchSummary *, 2> Branches;
+  };
+  SmallVector<Conditional, 4> Open;
+  const auto BodyEndLoc = BodyEnd->Tok.getLocation();
+  for (size_t I = BodyStart, E = CurrentLines->size(); I < E; ++I) {
+    const auto &L = (*CurrentLines)[I];
+    const auto *Keyword = getPPKeyword(L);
+    if (!Keyword)
+      continue;
+    const bool Opens =
+        Keyword->isOneOf(tok::pp_if, tok::pp_ifdef, tok::pp_ifndef);
+    const bool Closes = Keyword->is(tok::pp_endif);
+    if (!Opens && !Closes &&
+        !Keyword->isOneOf(tok::pp_else, tok::pp_elif, tok::pp_elifdef,
+                          tok::pp_elifndef)) {
+      continue;
+    }
+    const auto Loc = Keyword->Tok.getLocation();
+    const bool InBody = Loc < BodyEndLoc;
+    if (!InBody && Open.empty())
+      return BodyEnd;
+    if (Opens) {
+      int Depth;
+      if (Open.empty()) {
+        Depth = getBracketDepthBefore(*CurrentLines, BodyStart, I, Loc);
+      } else {
+        // Inactive branches aren't in the body lines. Use their recorded depth.
+        const auto It = PPConditionalDepths.find(Keyword);
+        if (It == PPConditionalDepths.end())
+          return nullptr;
+        Depth = Open.back().Depth + It->second;
+      }
+      Open.push_back({Depth});
+      continue;
+    }
+    // The body crosses a branch boundary of an enclosing conditional.
+    if (Open.empty())
+      return nullptr;
+    auto &C = Open.back();
+    const auto Summary = PPBranchSummaries.find(Keyword);
+    if (Summary == PPBranchSummaries.end())
+      return nullptr;
+    if (Summary->second.HasNestedConditional && C.UnbalancedNested)
+      return nullptr;
+    C.UnbalancedNested = false;
+    if (!InBody && !C.BodyBranch)
+      C.BodyBranch = &Summary->second;
+    else
+      C.Branches.push_back(&Summary->second);
+    if (Keyword->is(tok::pp_else))
+      C.HasElse = true;
+    if (!Closes)
+      continue;
+    if (InBody) {
+      // A missing #else acts as an empty branch.
+      static const PPBranchSummary Empty;
+      if (!C.HasElse)
+        C.Branches.push_back(&Empty);
+      const auto *Reference = C.Branches.front();
+      for (const auto *Branch : C.Branches) {
+        if (Branch->Depth != Reference->Depth ||
+            (Branch->Terminators > 0 &&
+             Branch->TerminatorDepth + C.Depth <= 0)) {
+          return nullptr;
+        }
+      }
+      Open.pop_back();
+      if (Reference->Depth != 0 && !Open.empty())
+        Open.back().UnbalancedNested = true;
+      continue;
+    }
+    // Every branch must end the body here; a missing #else cannot do so.
+    if (!C.HasElse || !C.BodyBranch->endsOneStatement())
+      return nullptr;
+    for (const auto *Branch : C.Branches)
+      if (!(*Branch == *C.BodyBranch))
+        return nullptr;
+    if (Open.size() == 1)
+      return L.Tokens.back().Tok;
+    Open.pop_back();
+  }
+  return Open.empty() ? BodyEnd : nullptr;
+}
+
 void UnwrappedLineParser::parseUnbracedBody(bool CheckEOF) {
   FormatToken *Tok = nullptr;
 
@@ -2934,8 +3092,8 @@ void UnwrappedLineParser::parseUnbracedBody(bool CheckEOF) {
               ? getLastNonComment(*Line)
               : Line->Tokens.back().Tok;
     assert(Tok);
-    if (Tok->BraceCount < 0) {
-      assert(Tok->BraceCount == -1);
+    if (Tok->BraceCount < 0 || RejectedBraceInsertions.contains(Tok)) {
+      assert(Tok->BraceCount >= -1);
       Tok = nullptr;
     } else {
       Tok->BraceCount = -1;
@@ -2953,29 +3111,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];
+    for (const auto &L : llvm::reverse(*CurrentLines)) {
       if (!L.InPPDirective && getLastNonComment(L)) {
         Tok = L.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)
+    if (FormatToken *RBraceTok = findRBraceToken(BodyStart, Tok)) {
+      ++RBraceTok->BraceCount;
+    } else {
       LBraceTok->BraceCount = 0;
-    else
-      ++Tok->BraceCount;
+      RejectedBraceInsertions.insert(LBraceTok);
+    }
   }
 
   if (CheckEOF && eof())
@@ -5198,6 +5346,12 @@ void UnwrappedLineParser::readToken(int LevelDifference) {
       }
     }
 
+    // Include inactive branches in the summaries.
+    if (!PP.Stack.empty() && !Line->InPPDirective &&
+        FormatTok->isNoneOf(tok::comment, tok::eof)) {
+      PP.Stack.back().Summary.add(*FormatTok);
+    }
+
     if (!PP.Stack.empty() && (PP.Stack.back().Kind == PP_Unreachable) &&
         !Line->InPPDirective) {
       continue;
diff --git a/clang/lib/Format/UnwrappedLineParser.h b/clang/lib/Format/UnwrappedLineParser.h
index 4a47f8158a0fe7..20ae96beb09b85 100644
--- a/clang/lib/Format/UnwrappedLineParser.h
+++ b/clang/lib/Format/UnwrappedLineParser.h
@@ -161,6 +161,7 @@ class UnwrappedLineParser {
   void parseSquare(bool SkipLambda = false);
   void keepAncestorBraces();
   void parseUnbracedBody(bool CheckEOF = false);
+  FormatToken *findRBraceToken(size_t BodyStart, FormatToken *BodyEnd) const;
   void handleAttributes();
   bool handleCppAttributes();
   bool isBlockBegin(const FormatToken &Tok) const;
@@ -368,10 +369,36 @@ class UnwrappedLineParser {
     PP_Unreachable  // #if 0 or a conditional preprocessor block inside #if 0
   };
 
+  struct PPBranchSummary {
+    // Depths are relative to the start of the branch.
+    int Depth = 0;
+    int MinDepth = 0;
+    // Track `;` and `}` at the lowest depth where either occurs.
+    int TerminatorDepth = 0;
+    unsigned Terminators = 0;
+    bool EndsWithTerminator = false;
+    bool HasNestedConditional = false;
+
+    void add(const FormatToken &Tok);
+    // The branch may start partway through the statement.
+    bool endsOneStatement() const {
+      return !HasNestedConditional && Terminators == 1 && EndsWithTerminator &&
+             Depth == MinDepth && TerminatorDepth == MinDepth;
+    }
+    bool operator==(const PPBranchSummary &Other) const {
+      return Depth == Other.Depth && MinDepth == Other.MinDepth &&
+             TerminatorDepth == Other.TerminatorDepth &&
+             Terminators == Other.Terminators &&
+             EndsWithTerminator == Other.EndsWithTerminator &&
+             HasNestedConditional == Other.HasNestedConditional;
+    }
+  };
+
   struct PPBranch {
     PPBranch(PPBranchKind Kind, size_t Line) : Kind(Kind), Line(Line) {}
     PPBranchKind Kind;
     size_t Line;
+    PPBranchSummary Summary;
   };
 
   // Include guard search state. Used to fixup preprocessor indent levels
@@ -433,6 +460,15 @@ class UnwrappedLineParser {
     bool AtEndOfPPLine = false;
   } PP;
 
+  // Branch summaries keyed by the ending keyword, kept across runs.
+  llvm::DenseMap<const FormatToken *, PPBranchSummary> PPBranchSummaries;
+
+  // Parent branch depth at each nested #if, including inactive branches.
+  llvm::DenseMap<const FormatToken *, int> PPConditionalDepths;
+
+  // Later preprocessor runs must not reverse a rejected insertion.
+  llvm::SmallPtrSet<const FormatToken *, 8> RejectedBraceInsertions;
+
   // The hash tokens of the parsed preprocessor directives. The lines of a
   // directive are kept when the token stream is rewound over it, so it is then
   // parsed again only for its effect on the preprocessor bookkeeping. See
diff --git a/clang/unittests/Format/BracesInserterTest.cpp b/clang/unittests/Format/BracesInserterTest.cpp
index b5013dfb6ffa7f..b7ce49edb98ea8 100644
--- a/clang/unittests/Format/BracesInserterTest.cpp
+++ b/clang/unittests/Format/BracesInserterTest.cpp
@@ -195,7 +195,15 @@ TEST_F(BracesInserterTest, InsertBraces) {
                "#endif",
                Style);
 
-  verifyFormat("if (a)\n"
+  verifyFormat("if (a) {\n"
+               "  if (b)\n"
+               "#ifdef A\n"
+               "    f();\n"
+               "#else\n"
+               "    g();\n"
+               "#endif\n"
+               "}",
+               "if (a)\n"
                "  if (b)\n"
                "#ifdef A\n"
                "    f();\n"
@@ -204,7 +212,71 @@ TEST_F(BracesInserterTest, InsertBraces) {
                "#endif",
                Style);
 
-  verifyFormat("while (a)\n"
+  verifyFormat("if (a) {\n"
+               "  if (b)\n"
+               "#if A\n"
+               "    f();\n"
+               "#elif B\n"
+               "    g();\n"
+               "#else\n"
+               "    h();\n"
+               "#endif // A\n"
+               "}",
+               "if (a)\n"
+               "  if (b)\n"
+               "#if A\n"
+               "    f();\n"
+               "#elif B\n"
+               "    g();\n"
+               "#else\n"
+               "    h();\n"
+               "#endif // A",
+               Style);
+
+  verifyFormat("if (a) {\n"
+               "  if (b) {\n"
+               "#ifdef A\n"
+               "    f();\n"
+               "#endif\n"
+               "  }\n"
+               "}",
+               "if (a)\n"
+               "  if (b) {\n"
+               "#ifdef A\n"
+               "    f();\n"
+               "#endif\n"
+               "  }",
+               Style);
+
+  verifyFormat("do {\n"
+               "  if (b)\n"
+               "#ifdef A\n"
+               "    f();\n"
+               "#else\n"
+               "    g();\n"
+               "#endif\n"
+               "} while (a);",
+               "do\n"
+               "  if (b)\n"
+               "#ifdef A\n"
+               "    f();\n"
+               "#else\n"
+               "    g();\n"
+               "#endif\n"
+               "while (a);",
+               Style);
+
+  verifyFormat("while (a) {\n"
+               "  if (b) {\n"
+               "    f(\n"
+               "#ifdef A\n"
+               "        1);\n"
+               "#else\n"
+               "        2);\n"
+               "#endif\n"
+               "  }\n"
+               "}",
+               "while (a)\n"
                "  if (b)\n"
                "    f(\n"
                "#ifdef A\n"
@@ -214,7 +286,16 @@ TEST_F(BracesInserterTest, InsertBraces) {
                "#endif",
                Style);
 
-  verifyFormat("if (a)\n"
+  verifyFormat("if (a) {\n"
+               "  f(\n"
+               "#ifdef A\n"
+               "      1\n"
+               "#else\n"
+               "      2\n"
+               "#endif\n"
+               "  );\n"
+               "}",
+               "if (a)\n"
                "  f(\n"
                "#ifdef A\n"
                "      1\n"
@@ -224,6 +305,139 @@ TEST_F(BracesInserterTest, InsertBraces) {
                "  );",
                Style);
 
+  verifyFormat("if (a)\n"
+               "  if (b)\n"
+               "#ifdef A\n"
+               "    f();\n"
+               "x();\n"
+               "#else\n"
+               "    g();\n"
+               "#endif",
+               Style);
+
+  verifyFormat("if (a)\n"
+               "  if (b)\n"
+               "#ifdef A\n"
+               "    f();\n"
+               "#endif\n"
+               "g();",
+               Style);
+
+  verifyFormat("if (a)\n"
+               "  if (b)\n"
+               "#if A\n"
+               "    f();\n"
+               "#elif B\n"
+               "#else\n"
+               "    g();\n"
+               "#endif\n"
+               "h();",
+               Style);
+
+  verifyFormat("if (a)\n"
+               "  if (b)\n"
+               "#ifdef A\n"
+               "#else\n"
+               "    f();\n"
+               "#endif\n"
+               "    g();",
+               Style);
+
+  verifyFormat("if (a)\n"
+               "  if (b)\n"
+               "#ifdef A\n"
+               "    f();\n"
+               "#else\n"
+               "    g();\n"
+               "h();\n"
+               "#endif",
+               Style);
+
+  verifyFormat("if (a)\n"
+               "  f(\n"
+               "#if A\n"
+               "      1\n"
+               "#else\n"
+               "      2)\n"
+               "#if B\n"
+               "      ;\n"
+               "h();\n"
+               "#else\n"
+               "      ;\n"
+               "g();\n"
+               "#endif\n"
+               "f(3\n"
+               "#endif\n"
+               "  );",
+               Style);
+
+  verifyFormat("if (a) {\n"
+               "  f(\n"
+               "#if A\n"
+               "      1\n"
+               "#else\n"
+               "#if B\n"
+               "      2\n"
+               "#else\n"
+               "      3\n"
+               "#endif\n"
+               "#endif\n"
+               "  );\n"
+               "}",
+               "if (a)\n"
+               "  f(\n"
+               "#if A\n"
+               "      1\n"
+               "#else\n"
+               "#if B\n"
+               "      2\n"
+               "#else\n"
+               "      3\n"
+               "#endif\n"
+               "#endif\n"
+               "  );",
+               Style);
+
+  verifyFormat("if (a) {\n"
+               "  if (b)\n"
+               "/* comment */ #ifdef A\n"
+               "    f();\n"
+               "#else\n"
+               "    g();\n"
+               "#endif\n"
+               "}",
+               "if (a)\n"
+               "  if (b)\n"
+               "/* comment */ #ifdef A\n"
+               "    f();\n"
+               "#else\n"
+               "    g();\n"
+               "#endif",
+               Style);
+
+  verifyFormat("#ifdef A\n"
+               "if (a)\n"
+               "  f(\n"
+               "#else\n"
+               "if (b)\n"
+               "  g(\n"
+               "#endif\n"
+               "      1);",
+               Style);
+
+  verifyFormat("if (a) {\n"
+               "  f();\n"
+               "}\n"
+               "#ifdef A\n"
+               "g();\n"
+               "#endif",
+               "if (a)\n"
+               "  f();\n"
+               "#ifdef A\n"
+               "g();\n"
+               "#endif",
+               Style);
+
   verifyFormat("#if A\n"
                "if (a) {\n"
                "  f();\n"



More information about the cfe-commits mailing list