r200381 - Fix crash on unmatched #endif's.

Manuel Klimek klimek at google.com
Wed Jan 29 00:49:03 PST 2014


Author: klimek
Date: Wed Jan 29 02:49:02 2014
New Revision: 200381

URL: http://llvm.org/viewvc/llvm-project?rev=200381&view=rev
Log:
Fix crash on unmatched #endif's.

The following snippet would crash:
  #endif
  #if A

Modified:
    cfe/trunk/lib/Format/UnwrappedLineParser.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=200381&r1=200380&r2=200381&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Wed Jan 29 02:49:02 2014
@@ -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())

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=200381&r1=200380&r2=200381&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Jan 29 02:49:02 2014
@@ -2427,6 +2427,11 @@ TEST_F(FormatTest, LayoutStatementsAroun
                "#endif");
 }
 
+TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
+  verifyFormat("#endif\n"
+               "#if B");
+}
+
 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
   FormatStyle SingleLine = getLLVMStyle();
   SingleLine.AllowShortIfStatementsOnASingleLine = true;





More information about the cfe-commits mailing list