[clang] 5889ed8 - [clang-format] Don't insert braces for loops with a null statement

via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 12 00:38:06 PDT 2022


Author: owenca
Date: 2022-09-12T00:37:47-07:00
New Revision: 5889ed83a0ffa71f2cf010b73bbbf210f9690778

URL: https://github.com/llvm/llvm-project/commit/5889ed83a0ffa71f2cf010b73bbbf210f9690778
DIFF: https://github.com/llvm/llvm-project/commit/5889ed83a0ffa71f2cf010b73bbbf210f9690778.diff

LOG: [clang-format] Don't insert braces for loops with a null statement

This is a workaround for #57539.

Fixes #57509.

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 7ef1e82d754fe..699d2d37c71dc 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -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;

diff  --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index d3e657b018b77..aae300e67f62a 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -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"


        


More information about the cfe-commits mailing list