aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--clang/lib/Format/UnwrappedLineParser.cpp4
-rw-r--r--clang/unittests/Format/FormatTest.cpp5
2 files changed, 8 insertions, 1 deletions
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index dbd8d9d..b56140e 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -509,7 +509,9 @@ void UnwrappedLineParser::parsePPEndIf() {
PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
}
}
- --PPBranchLevel;
+ // Guard against #endif's without #if.
+ if (PPBranchLevel > 0)
+ --PPBranchLevel;
if (!PPChainBranchIndex.empty())
PPChainBranchIndex.pop();
if (!PPStack.empty())
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index efc563b..9b0a11c 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -2427,6 +2427,11 @@ TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
"#endif");
}
+TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
+ verifyFormat("#endif\n"
+ "#if B");
+}
+
TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
FormatStyle SingleLine = getLLVMStyle();
SingleLine.AllowShortIfStatementsOnASingleLine = true;