diff options
author | Richard Smith <richard-llvm@metafoo.co.uk> | 2011-11-29 05:27:40 +0000 |
---|---|---|
committer | Richard Smith <richard-llvm@metafoo.co.uk> | 2011-11-29 05:27:40 +0000 |
commit | 874d253668f9ed183ca409cdff9d424925ee7800 (patch) | |
tree | 637fc2dc1c9b792795c6f63d5cf68061c347caa2 /lib | |
parent | b49e728a4d1a84b72f3aebf60ff494684f9cb004 (diff) |
PR10101: Recover better from a common copy-paste error: if a function
declaration at namespace scope is followed by a semicolon and an open-brace
(or in C++, a 'try', ':' or '='), then the error is probably a function
definition with a spurious ';', rather than a mysterious '{'.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@145372 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Parse/ParseDecl.cpp | 17 | ||||
-rw-r--r-- | lib/Parse/ParseTemplate.cpp | 26 |
2 files changed, 32 insertions, 11 deletions
diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp index 3bddd3de13..75c423340c 100644 --- a/lib/Parse/ParseDecl.cpp +++ b/lib/Parse/ParseDecl.cpp @@ -1041,13 +1041,28 @@ Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, return DeclGroupPtrTy(); } + // Do we have a stray semicolon in the middle of a function definition? + if (AllowFunctionDefinitions && D.isFunctionDeclarator() && + Tok.is(tok::semi) && Context == Declarator::FileContext) { + const Token &Next = NextToken(); + if (Next.is(tok::l_brace) || Next.is(tok::kw_try) || + (getLang().CPlusPlus && + (Next.is(tok::colon) || Next.is(tok::equal)))) { + // Pretend we didn't see the semicolon. + SourceLocation SemiLoc = ConsumeToken(); + Diag(SemiLoc, diag::err_stray_semi_function_definition) + << FixItHint::CreateRemoval(SemiLoc); + assert(isStartOfFunctionDefinition(D) && "expected a function defn"); + } + } + // Check to see if we have a function *definition* which must have a body. if (AllowFunctionDefinitions && D.isFunctionDeclarator() && // Look at the next token to make sure that this isn't a function // declaration. We have to check this because __attribute__ might be the // start of a function definition in GCC-extended K&R C. !isDeclarationAfterDeclarator()) { - + if (isStartOfFunctionDefinition(D)) { if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { Diag(Tok, diag::err_function_declared_typedef); diff --git a/lib/Parse/ParseTemplate.cpp b/lib/Parse/ParseTemplate.cpp index b485f1e977..91cf8e81c2 100644 --- a/lib/Parse/ParseTemplate.cpp +++ b/lib/Parse/ParseTemplate.cpp @@ -240,6 +240,20 @@ Parser::ParseSingleDeclarationAfterTemplate( return 0; } + // Check for a stray semicolon in a function definition. + if (DeclaratorInfo.isFunctionDeclarator() && Tok.is(tok::semi) && + Context == Declarator::FileContext) { + const Token &Next = NextToken(); + if (Next.is(tok::l_brace) || Next.is(tok::kw_try) || + Next.is(tok::equal) || Next.is(tok::colon)) { + SourceLocation SemiLoc = ConsumeToken(); + Diag(SemiLoc, diag::err_stray_semi_function_definition) + << FixItHint::CreateRemoval(SemiLoc); + assert(!isDeclarationAfterDeclarator() && + isStartOfFunctionDefinition(DeclaratorInfo)); + } + } + // If we have a declaration or declarator list, handle it. if (isDeclarationAfterDeclarator()) { // Parse this declaration. @@ -264,16 +278,8 @@ Parser::ParseSingleDeclarationAfterTemplate( if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { Diag(Tok, diag::err_function_declared_typedef); - if (Tok.is(tok::l_brace)) { - // This recovery skips the entire function body. It would be nice - // to simply call ParseFunctionDefinition() below, however Sema - // assumes the declarator represents a function, not a typedef. - ConsumeBrace(); - SkipUntil(tok::r_brace, true); - } else { - SkipUntil(tok::semi); - } - return 0; + // Recover by ignoring the 'typedef'. + DS.ClearStorageClassSpecs(); } return ParseFunctionDefinition(DeclaratorInfo, TemplateInfo); } |