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 @@ -1124,7 +1124,9 @@ ++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 it 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 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -5993,6 +5993,16 @@ ");\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) {