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

Owen Pan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 9 23:41:31 PDT 2022


owenpan created this revision.
owenpan added reviewers: MyDeveloperDay, curdeius, HazardyKnusperkeks.
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.

This is a workaround for https://github.com/llvm/llvm-project/issues/57539.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D133635

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


Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -25327,6 +25327,30 @@
                "}",
                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"
Index: clang/lib/Format/UnwrappedLineParser.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineParser.cpp
+++ clang/lib/Format/UnwrappedLineParser.cpp
@@ -2605,8 +2605,7 @@
   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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D133635.459265.patch
Type: text/x-patch
Size: 1614 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220910/964e7139/attachment.bin>


More information about the cfe-commits mailing list