summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManuel Klimek <klimek@google.com>2014-01-29 08:49:02 +0000
committerManuel Klimek <klimek@google.com>2014-01-29 08:49:02 +0000
commit14bd917470dab03e3a77b16225c88c492e6d84bb (patch)
tree51d0f30c62f3a5ad403f46a6b31fd8f33ae836db
parent8a4e2e41de4589696c0de021fdb0827b39959021 (diff)
downloadbcm5719-llvm-14bd917470dab03e3a77b16225c88c492e6d84bb.tar.gz
bcm5719-llvm-14bd917470dab03e3a77b16225c88c492e6d84bb.zip
Fix crash on unmatched #endif's.
The following snippet would crash: #endif #if A llvm-svn: 200381
-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 dbd8d9dbc1b..b56140e763d 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 efc563b4f25..9b0a11c3bc2 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;
OpenPOWER on IntegriCloud