]> granicus.if.org Git - clang/commitdiff
Improve parser error recovery after a constructor initializer
authorDouglas Gregor <dgregor@apple.com>
Mon, 10 Nov 2008 16:59:40 +0000 (16:59 +0000)
committerDouglas Gregor <dgregor@apple.com>
Mon, 10 Nov 2008 16:59:40 +0000 (16:59 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@58989 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Parse/Parser.h
lib/Parse/ParseCXXInlineMethods.cpp
test/SemaCXX/constructor-initializer.cpp

index d9014b6a77c3715a24596029dc323696c29538d3..4071172743a14066ebed80784624a1f0d4d7ca32 100644 (file)
@@ -397,7 +397,8 @@ private:
 
   DeclTy *ParseCXXInlineMethodDef(AccessSpecifier AS, Declarator &D);
   void ParseLexedMethodDefs();
-  bool ConsumeAndStoreUntil(tok::TokenKind T, TokensTy &Toks);
+  bool ConsumeAndStoreUntil(tok::TokenKind T, TokensTy &Toks,
+                            tok::TokenKind EarlyAbortIf = tok::unknown);
 
   //===--------------------------------------------------------------------===//
   // C99 6.9: External Definitions.
index 7d977c1251b056533736b69f4b609a2cb29b31c5..2f5014ba50403d14b8712060a293e2cbcde3fba6 100644 (file)
@@ -11,6 +11,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "clang/Basic/Diagnostic.h"
 #include "clang/Parse/Parser.h"
 #include "clang/Parse/DeclSpec.h"
 #include "clang/Parse/Scope.h"
@@ -36,7 +37,19 @@ Parser::ParseCXXInlineMethodDef(AccessSpecifier AS, Declarator &D) {
   // We may have a constructor initializer here.
   if (Tok.is(tok::colon)) {
     // Consume everything up to (and including) the left brace.
-    ConsumeAndStoreUntil(tok::l_brace, Toks);
+    if (!ConsumeAndStoreUntil(tok::l_brace, Toks, tok::semi)) {
+      // We didn't find the left-brace we expected after the
+      // constructor initializer. 
+      if (Tok.is(tok::semi)) {
+        // We found a semicolon; complain, consume the semicolon, and
+        // don't try to parse this method later.
+        Diag(Tok.getLocation(), diag::err_expected_lbrace);
+        ConsumeAnyToken();
+        getCurTopClassStack().pop();
+        return FnD;
+      }
+    }
+
   } else {
     // Begin by storing the '{' token. 
     Toks.push_back(Tok);
@@ -82,9 +95,12 @@ void Parser::ParseLexedMethodDefs() {
 
 /// ConsumeAndStoreUntil - Consume and store the token at the passed token
 /// container until the token 'T' is reached (which gets consumed/stored too).
+/// If EarlyAbortIf is specified, then we will stop early if we find that
+/// token at the top level.
 /// Returns true if token 'T' was found.
 /// NOTE: This is a specialized version of Parser::SkipUntil.
-bool Parser::ConsumeAndStoreUntil(tok::TokenKind T, TokensTy &Toks) {
+bool Parser::ConsumeAndStoreUntil(tok::TokenKind T, TokensTy &Toks,
+                                  tok::TokenKind EarlyAbortIf) {
   // We always want this function to consume at least one token if the first
   // token isn't T and if not at EOF.
   bool isFirstTokenConsumed = true;
@@ -96,6 +112,10 @@ bool Parser::ConsumeAndStoreUntil(tok::TokenKind T, TokensTy &Toks) {
       return true;
     }
 
+    // If we found the early-abort token, return.
+    if (Tok.is(EarlyAbortIf))
+      return false;
+
     switch (Tok.getKind()) {
     case tok::eof:
       // Ran out of tokens.
index 6b450b097ac7df9a296543f2520c69e2c2d61ebc..a79b6caac983f28d03b2fd2a675bb159e8ce16b0 100644 (file)
@@ -41,3 +41,7 @@ public:
   { 
   }
 };
+
+class G : A {
+  G() : A(10); // expected-error{{expected '{'}}
+};