[PATCH] D135372: [clang-format] Fix for #58188

sstwcw via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 6 09:27:36 PDT 2022


sstwcw updated this revision to Diff 465772.

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D135372

Files:
  clang/lib/Format/UnwrappedLineParser.cpp


Index: clang/lib/Format/UnwrappedLineParser.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineParser.cpp
+++ clang/lib/Format/UnwrappedLineParser.cpp
@@ -1127,9 +1127,7 @@
   ++PPBranchLevel;
   assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
   if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
-    // 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);
+    PPLevelBranchIndex.push_back(0);
     PPLevelBranchCount.push_back(0);
   }
   PPChainBranchIndex.push(0);
@@ -1138,14 +1136,24 @@
 }
 
 void UnwrappedLineParser::conditionalCompilationAlternative() {
+  bool FirstBranchSkipped =
+      !PPStack.empty() && PPLevelBranchIndex[PPBranchLevel] == 0 &&
+      PPStack.back().Kind == PP_Unreachable &&
+      !(PPStack.size() > 1 &&
+        PPStack[PPStack.size() - 2].Kind == PP_Unreachable);
   if (!PPStack.empty())
     PPStack.pop_back();
   assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
   if (!PPChainBranchIndex.empty())
     ++PPChainBranchIndex.top();
-  conditionalCompilationCondition(
-      PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
-      PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
+  // If the first branch is skipped because the condition is 0, parse the second
+  // branch.
+  conditionalCompilationCondition(/*Unreachable=*/
+                                  PPBranchLevel >= 0 &&
+                                  !PPChainBranchIndex.empty() &&
+                                  PPLevelBranchIndex[PPBranchLevel] !=
+                                      PPChainBranchIndex.top() &&
+                                  !FirstBranchSkipped);
 }
 
 void UnwrappedLineParser::conditionalCompilationEnd() {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D135372.465772.patch
Type: text/x-patch
Size: 1899 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221006/4229112f/attachment.bin>


More information about the cfe-commits mailing list