[clang-format] Don't insert braces for loops with a null statement
authorowenca <owenpiano@gmail.com>
Sat, 10 Sep 2022 06:27:22 +0000 (23:27 -0700)
committerowenca <owenpiano@gmail.com>
Mon, 12 Sep 2022 07:37:47 +0000 (00:37 -0700)
This is a workaround for #57539.

Fixes #57509.

Differential Revision: https://reviews.llvm.org/D133635

clang/lib/Format/UnwrappedLineParser.cpp
clang/unittests/Format/FormatTest.cpp

index 7ef1e82..699d2d3 100644 (file)
@@ -2605,8 +2605,7 @@ void UnwrappedLineParser::parseUnbracedBody(bool CheckEOF) {
   FormatToken *Tok = nullptr;
 
   if (Style.InsertBraces && !Line->InPPDirective && !Line->Tokens.empty() &&
-      PreprocessorDirectives.empty()) {
-    assert(!Line->Tokens.empty());
+      PreprocessorDirectives.empty() && FormatTok->isNot(tok::semi)) {
     Tok = Style.BraceWrapping.AfterControlStatement == FormatStyle::BWACS_Never
               ? getLastNonComment(*Line)
               : Line->Tokens.back().Tok;
index d3e657b..aae300e 100644 (file)
@@ -25365,6 +25365,30 @@ TEST_F(FormatTest, InsertBraces) {
                "}",
                Style);
 
+  verifyFormat("do {\n"
+               "#if 0\n"
+               " if (a) {\n"
+               "#else\n"
+               "  if (b) {\n"
+               "#endif\n"
+               "}\n"
+               "}\n"
+               "while (0)\n"
+               "  ;",
+               Style);
+  // TODO: Replace the test above with the one below after #57539 is fixed.
+#if 0
+  verifyFormat("do {\n"
+               "#if 0\n"
+               "  if (a) {\n"
+               "#else\n"
+               "  if (b) {\n"
+               "#endif\n"
+               "  }\n"
+               "} while (0);",
+               Style);
+#endif
+
   Style.ColumnLimit = 15;
 
   verifyFormat("#define A     \\\n"