]> granicus.if.org Git - clang/commitdiff
Fix crash on unmatched #endif's.
authorManuel Klimek <klimek@google.com>
Wed, 29 Jan 2014 08:49:02 +0000 (08:49 +0000)
committerManuel Klimek <klimek@google.com>
Wed, 29 Jan 2014 08:49:02 +0000 (08:49 +0000)
The following snippet would crash:
  #endif
  #if A

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@200381 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/UnwrappedLineParser.cpp
unittests/Format/FormatTest.cpp

index dbd8d9dbc1b6dddfed6687cdca6a32f9ca51f4a3..b56140e763d3bba77e2b8181b86d83649e546ce0 100644 (file)
@@ -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())
index efc563b4f25c346800809079a4cbff582364cb40..9b0a11c3bc248577025db21b750a8c5022cadb22 100644 (file)
@@ -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;