[clang] 2183fe2 - [clang-format] Parse the else part of `#if 0`

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


Author: sstwcw
Date: 2022-09-16T13:30:46Z
New Revision: 2183fe2160fbcb754893e98829f2bff4d0fccfa3

URL: https://github.com/llvm/llvm-project/commit/2183fe2160fbcb754893e98829f2bff4d0fccfa3
DIFF: https://github.com/llvm/llvm-project/commit/2183fe2160fbcb754893e98829f2bff4d0fccfa3.diff

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

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

Previously things outside of `#if` blocks were parsed as if only the
first branch of the conditional compilation branch existed, unless the
first condition is 0.  In that case the outer parts would be parsed as
if nothing inside the conditional parts existed.  Now we use the second
conditional branch if the first condition is 0.

Reviewed By: owenpan

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

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 699d2d37c71dc..fce50d65fc364 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -1124,7 +1124,9 @@ void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
   ++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);

diff  --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index aae300e67f62a..8d9ccfd0cfe14 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -5993,6 +5993,16 @@ TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
                          ");\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 @@ TEST_F(FormatTest, InsertBraces) {
 
   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;
 


        


More information about the cfe-commits mailing list