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

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


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


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


More information about the cfe-commits mailing list