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 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) { @@ -25367,27 +25377,12 @@ verifyFormat("do {\n" "#if 0\n" - " if (a) {\n" - "#else\n" - " if (b) {\n" - "#endif\n" - "}\n" - "}\n" - "while (0)\n" - " ;", - Style); - // TODO: Replace the test above with the one below after #57539 is fixed. -#if 0 - verifyFormat("do {\n" - "#if 0\n" - " if (a) {\n" "#else\n" " if (b) {\n" "#endif\n" " }\n" "} while (0);", Style); -#endif Style.ColumnLimit = 15;