[PATCH] D133647: [clang-format] Parse the else part of `#if 0`

sstwcw via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 16 06:35:52 PDT 2022


This revision was automatically updated to reflect the committed changes.
Closed by commit rG2183fe2160fb: [clang-format] Parse the else part of `#if 0` (authored by sstwcw).

Changed prior to commit:
  https://reviews.llvm.org/D133647?vs=459305&id=460729#toc

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D133647

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
@@ -5993,6 +5993,16 @@
                          ");\n"
                          "#else\n"
                          "#endif");
+
+  // Verify that indentation is correct when there is an `#if 0` with an
+  // `#else`.
+  verifyFormat("#if 0\n"
+               "{\n"
+               "#else\n"
+               "{\n"
+               "#endif\n"
+               "  x;\n"
+               "}");
 }
 
 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
@@ -25367,27 +25377,12 @@
 
   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;
 
Index: clang/lib/Format/UnwrappedLineParser.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineParser.cpp
+++ clang/lib/Format/UnwrappedLineParser.cpp
@@ -1124,7 +1124,9 @@
   ++PPBranchLevel;
   assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
   if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
-    PPLevelBranchIndex.push_back(0);
+    // If the first branch is unreachable, set the BranchIndex to 1.  This way
+    // the next branch will be parsed if there is one.
+    PPLevelBranchIndex.push_back(Unreachable ? 1 : 0);
     PPLevelBranchCount.push_back(0);
   }
   PPChainBranchIndex.push(0);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D133647.460729.patch
Type: text/x-patch
Size: 2038 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220916/ad692f8c/attachment-0001.bin>


More information about the cfe-commits mailing list