diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/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() {