r214018 - Parse: Don't crash on trailing whitespace before EOF

David Majnemer david.majnemer at gmail.com
Fri Jul 25 22:41:31 PDT 2014


Author: majnemer
Date: Sat Jul 26 00:41:31 2014
New Revision: 214018

URL: http://llvm.org/viewvc/llvm-project?rev=214018&view=rev
Log:
Parse: Don't crash on trailing whitespace before EOF

Parser::ParseDeclarationSpecifiers eagerly updates the source range of
the DeclSpec with the current token position.  However, it might not
consume any more tokens.

Fix this by only setting the start of the range, not the end.  This way
the SourceRange will be invalid if we don't consume any more tokens.

This fixes PR20413.

Differential Revision: http://reviews.llvm.org/D4646

Added:
    cfe/trunk/test/Parser/eof2.cpp
Modified:
    cfe/trunk/lib/Parse/ParseDecl.cpp

Modified: cfe/trunk/lib/Parse/ParseDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseDecl.cpp?rev=214018&r1=214017&r2=214018&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParseDecl.cpp (original)
+++ cfe/trunk/lib/Parse/ParseDecl.cpp Sat Jul 26 00:41:31 2014
@@ -2449,8 +2449,11 @@ void Parser::ParseDeclarationSpecifiers(
                                         DeclSpecContext DSContext,
                                         LateParsedAttrList *LateAttrs) {
   if (DS.getSourceRange().isInvalid()) {
+    // Start the range at the current token but make the end of the range
+    // invalid.  This will make the entire range invalid unless we successfully
+    // consume a token.
     DS.SetRangeStart(Tok.getLocation());
-    DS.SetRangeEnd(Tok.getLocation());
+    DS.SetRangeEnd(SourceLocation());
   }
 
   bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);

Added: cfe/trunk/test/Parser/eof2.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Parser/eof2.cpp?rev=214018&view=auto
==============================================================================
--- cfe/trunk/test/Parser/eof2.cpp (added)
+++ cfe/trunk/test/Parser/eof2.cpp Sat Jul 26 00:41:31 2014
@@ -0,0 +1,15 @@
+// RUN: not %clang_cc1 %s -fsyntax-only 2>&1 | FileCheck %s
+
+// CHECK: error: expected expression
+// CHECK: error: expected member name or ';' after declaration specifiers
+// CHECK: error: expected '}'
+// CHECK: note: to match this '{'
+// CHECK: error: expected ';' after class
+// CHECK: 4 errors generated.
+
+// Do not add anything to the end of this file.  This requires the whitespace
+// plus EOF after the '<' token.
+
+template <typename T>
+class a {
+  a< 





More information about the cfe-commits mailing list