owenpan created this revision.
owenpan added reviewers: HazardyKnusperkeks, MyDeveloperDay, rymiel.
owenpan added a project: clang-format.
Herald added a project: All.
owenpan requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

But keep the added test case and also add another test case.

Fixes https://github.com/llvm/llvm-project/issues/59417.

This reverts commit e33243c950ac 
<https://reviews.llvm.org/rGe33243c950ac40d027ad8facbf7ccf0624604a16>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D139760

Files:
  clang/lib/Format/UnwrappedLineParser.cpp
  clang/unittests/Format/BracesRemoverTest.cpp

Index: clang/unittests/Format/BracesRemoverTest.cpp
===================================================================
--- clang/unittests/Format/BracesRemoverTest.cpp
+++ clang/unittests/Format/BracesRemoverTest.cpp
@@ -683,6 +683,41 @@
                "return a;",
                Style);
 
+  verifyFormat("if (a)\n"
+               "#ifdef FOO\n"
+               "  if (b)\n"
+               "    bar = c;\n"
+               "  else\n"
+               "#endif\n"
+               "  {\n"
+               "    foo = d;\n"
+               "#ifdef FOO\n"
+               "    bar = e;\n"
+               "#else\n"
+               "  bar = f;\n" // FIXME: should be indented 1 more level.
+               "#endif\n"
+               "  }\n"
+               "else\n"
+               "  bar = g;",
+               "if (a)\n"
+               "#ifdef FOO\n"
+               "  if (b)\n"
+               "    bar = c;\n"
+               "  else\n"
+               "#endif\n"
+               "  {\n"
+               "    foo = d;\n"
+               "#ifdef FOO\n"
+               "    bar = e;\n"
+               "#else\n"
+               "    bar = f;\n"
+               "#endif\n"
+               "  }\n"
+               "else {\n"
+               "  bar = g;\n"
+               "}",
+               Style);
+
   Style.ColumnLimit = 65;
   verifyFormat("if (condition) {\n"
                "  ff(Indices,\n"
Index: clang/lib/Format/UnwrappedLineParser.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineParser.cpp
+++ clang/lib/Format/UnwrappedLineParser.cpp
@@ -934,9 +934,6 @@
     return IfLBrace;
   }
 
-  Tok->MatchingParen = FormatTok;
-  FormatTok->MatchingParen = Tok;
-
   const bool IsFunctionRBrace =
       FormatTok->is(tok::r_brace) && Tok->is(TT_FunctionLBrace);
 
@@ -970,7 +967,10 @@
     }
     return mightFitOnOneLine((*CurrentLines)[Index], Tok);
   };
-  Tok->Optional = RemoveBraces();
+  if (RemoveBraces()) {
+    Tok->MatchingParen = FormatTok;
+    FormatTok->MatchingParen = Tok;
+  }
 
   size_t PPEndHash = computePPHash();
 
@@ -2707,20 +2707,10 @@
 
   assert(RightBrace->is(tok::r_brace));
   assert(RightBrace->MatchingParen == LeftBrace);
+  assert(LeftBrace->Optional == RightBrace->Optional);
 
-  RightBrace->Optional = LeftBrace->Optional;
-}
-
-static void resetOptional(FormatToken *LeftBrace) {
-  if (!LeftBrace)
-    return;
-
-  const auto *RightBrace = LeftBrace->MatchingParen;
-  const bool IsOptionalRightBrace = RightBrace && RightBrace->Optional;
-  assert(LeftBrace->Optional || !IsOptionalRightBrace);
-
-  if (!IsOptionalRightBrace)
-    LeftBrace->Optional = false;
+  LeftBrace->Optional = true;
+  RightBrace->Optional = true;
 }
 
 void UnwrappedLineParser::handleAttributes() {
@@ -2788,7 +2778,8 @@
 
   if (Style.RemoveBracesLLVM) {
     assert(!NestedTooDeep.empty());
-    KeepIfBraces = KeepIfBraces || (IfLeftBrace && !IfLeftBrace->Optional) ||
+    KeepIfBraces = KeepIfBraces ||
+                   (IfLeftBrace && !IfLeftBrace->MatchingParen) ||
                    NestedTooDeep.back() || IfBlockKind == IfStmtKind::IfOnly ||
                    IfBlockKind == IfStmtKind::IfElseIf;
   }
@@ -2819,9 +2810,8 @@
                          ElseBlockKind == IfStmtKind::IfElseIf;
       } else if (FollowedByIf && IfLBrace && !IfLBrace->Optional) {
         KeepElseBraces = true;
-        assert(ElseLeftBrace->Optional);
         assert(ElseLeftBrace->MatchingParen);
-        ElseLeftBrace->MatchingParen->Optional = true;
+        markOptionalBraces(ElseLeftBrace);
       }
       addUnwrappedLine();
     } else if (FormatTok->is(tok::kw_if)) {
@@ -2856,7 +2846,7 @@
 
   assert(!NestedTooDeep.empty());
   KeepElseBraces = KeepElseBraces ||
-                   (ElseLeftBrace && !ElseLeftBrace->Optional) ||
+                   (ElseLeftBrace && !ElseLeftBrace->MatchingParen) ||
                    NestedTooDeep.back();
 
   NestedTooDeep.pop_back();
@@ -2864,11 +2854,17 @@
   if (!KeepIfBraces && !KeepElseBraces) {
     markOptionalBraces(IfLeftBrace);
     markOptionalBraces(ElseLeftBrace);
+  } else if (IfLeftBrace) {
+    FormatToken *IfRightBrace = IfLeftBrace->MatchingParen;
+    if (IfRightBrace) {
+      assert(IfRightBrace->MatchingParen == IfLeftBrace);
+      assert(!IfLeftBrace->Optional);
+      assert(!IfRightBrace->Optional);
+      IfLeftBrace->MatchingParen = nullptr;
+      IfRightBrace->MatchingParen = nullptr;
+    }
   }
 
-  resetOptional(IfLeftBrace);
-  resetOptional(ElseLeftBrace);
-
   if (IfKind)
     *IfKind = Kind;
 
@@ -3083,7 +3079,6 @@
       if (!NestedTooDeep.back())
         markOptionalBraces(LeftBrace);
     }
-    resetOptional(LeftBrace);
     if (WrapRightBrace)
       addUnwrappedLine();
   } else {
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to