[PATCH] D154755: [clang-format] Fix formatting of if statements with BlockIndent

Gedare Bloom via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 18 19:14:38 PDT 2023


gedare updated this revision to Diff 541813.
gedare marked an inline comment as done.
gedare added a comment.

Revert deleted line in test.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D154755/new/

https://reviews.llvm.org/D154755

Files:
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/FormatTest.cpp


Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -25494,12 +25494,56 @@
                "}",
                Style);
 
+  // Treating if clauses as block indents causes a known bug (#54808, #63383)
+  // breaking the following test. It gets formatted instead as:
+  // "if (quitelongarg != (alsolongarg - 1)\n"
+  // ") { // ABC is a very longgggggggggggg comment"
+  // "return;"
+  // "}"
+
+#if 0
   verifyFormat("if (quitelongarg !=\n"
                "    (alsolongarg - 1)) { // ABC is a very longgggggggggggg "
                "comment\n"
                "  return;\n"
                "}",
                Style);
+#endif
+
+  verifyFormat("void foo() {\n"
+               "  if (quitelongname < alsolongname ||\n"
+               "      anotherevenlongername <=\n"
+               "          thisreallyreallyreallyreallyreallyreallylongername ||"
+               "\n"
+               "      othername < thislastname) {\n"
+               "    return;\n"
+               "  } else if (\n"
+               "      quitelongname < alsolongname ||\n"
+               "      anotherevenlongername <=\n"
+               "          thisreallyreallyreallyreallyreallyreallylongername ||"
+               "\n"
+               "      othername < thislastname\n"
+               "  ) {\n"
+               "    return;\n"
+               "  }\n"
+               "}",
+               Style);
+
+  Style.ContinuationIndentWidth = 2;
+  verifyFormat("void foo() {\n"
+               "  if (\n"
+               "    ThisIsRatherALongIfClause && thatIExpectToBeBroken ||\n"
+               "    ontoMultipleLines && whenFormattedCorrectly\n"
+               "  ) {\n"
+               "    if (false) {\n"
+               "    } else if (\n"
+               "      thisIsRatherALongIfClause && thatIExpectToBeBroken ||\n"
+               "      ontoMultipleLines && whenFormattedCorrectly\n"
+               "    ) {\n"
+               "    }\n"
+               "  }\n"
+               "}",
+               Style);
 }
 
 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -5499,7 +5499,7 @@
     if (Next && Next->is(tok::l_paren))
       return false;
     const FormatToken *Previous = Right.MatchingParen->Previous;
-    return !(Previous && (Previous->is(tok::kw_for) || Previous->isIf()));
+    return !(Previous && Previous->is(tok::kw_for));
   }
 
   // Allow breaking after a trailing annotation, e.g. after a method


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154755.541813.patch
Type: text/x-patch
Size: 2776 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230719/dbf47b6f/attachment.bin>


More information about the cfe-commits mailing list