sstwcw created this revision.
sstwcw added reviewers: HazardyKnusperkeks, MyDeveloperDay, curdeius, owenpan, 
sammccall.
sstwcw added a project: clang-format.
Herald added a project: All.
sstwcw requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This is intended to provide a hint for how to fix the bug.   The patch fixes 
the bug itself, but a problem remains.


Repository:
  rG LLVM Github Monorepo

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 =
+      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() {


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 =
+      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() {
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to