]> granicus.if.org Git - clang/commitdiff
PR10101: Recover better from a common copy-paste error: if a function
authorRichard Smith <richard-llvm@metafoo.co.uk>
Tue, 29 Nov 2011 05:27:40 +0000 (05:27 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Tue, 29 Nov 2011 05:27:40 +0000 (05:27 +0000)
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

include/clang/Basic/DiagnosticParseKinds.td
lib/Parse/ParseDecl.cpp
lib/Parse/ParseTemplate.cpp
test/FixIt/fixit-cxx0x.cpp
test/FixIt/fixit.c
test/FixIt/fixit.cpp
test/SemaCXX/typedef-redecl.cpp

index e0b10fa87c2c0f84a83566d6cc627c8dfaf64f8b..9b5540d4c99dcf06fcc06eba376c3088392083f8 100644 (file)
@@ -138,6 +138,8 @@ def ext_expected_semi_decl_list : ExtWarn<
   "expected ';' at end of declaration list">;
 def err_expected_member_name_or_semi : Error<
   "expected member name or ';' after declaration specifiers">;
+def err_stray_semi_function_definition : Error<
+  "stray ';' in function definition">;
 def err_function_declared_typedef : Error<
   "function definition declared 'typedef'">;
 def err_iboutletcollection_builtintype : Error<
index 3bddd3de13d06a49389aca61a76fa6ef3eb0b061..75c423340c36620090099898f7ca8a8817785a3d 100644 (file)
@@ -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);
index b485f1e97771a0411522df7738236ea10476da5d..91cf8e81c2a48bff5ae1418af4aab8ebbf481d78 100644 (file)
@@ -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);
   }
index 9fb647d03fcd6ba7cc19db5781bbc4d028df8fb4..d9b8763b056c3dd6b7ba15ce2c3bf74c313bf438 100644 (file)
@@ -58,3 +58,26 @@ namespace SemiCommaTypo {
   n [[]], // expected-error {{expected ';' at end of declaration}}
   int o;
 }
+
+int extraSemi(); // expected-error {{stray ';' in function definition}}
+  = delete;
+
+class ExtraSemi {
+public:
+  ExtraSemi();
+  ExtraSemi(const ExtraSemi &);
+  int n;
+};
+ExtraSemi::ExtraSemi(); // expected-error {{stray ';'}}
+ : n(0) {
+}
+ExtraSemi::ExtraSemi(const ExtraSemi &); // expected-error {{stray ';'}}
+  = default;
+
+template<typename T> T extraSemi(T t);
+
+template<typename T> T extraSemi(T t); // expected-error {{stray ';'}}
+{
+  return t;
+}
+template int extraSemi(int);
index 967ae23c186c047707418a41b9f940bc153cca87..da1144da0fe9c80313cd82d9be32f86584988c70 100644 (file)
@@ -77,3 +77,11 @@ void oopsMoreCommas() {
   static int b[] = { 3, 4, 5 },
   &a == &b ? oopsMoreCommas() : removeUnusedLabels(a[0]);
 }
+
+void extraSemicolon();
+{
+  void extraSemicolon();
+  {
+    return;
+  }
+}
index 86b397777c2a7b6cd37ad685576368f1bd0ec78c..7c8ba9ac6f1a353931c293c2f5a388cc9f915609 100644 (file)
@@ -125,3 +125,13 @@ AD oopsMoreCommas() {
   AD ad, // expected-error {{expected ';' at end of declaration}}
   return ad;
 }
+
+int extraSemi1(); // expected-error {{stray ';' in function definition}}
+{
+  return 0;
+}
+
+int extraSemi2(); // expected-error {{stray ';' in function definition}}
+try {
+} catch (...) {
+}
index 7db1970a70edf40659cba5553b6d5d7fdbcd36ab..31de9c00c105aa5f5b04c6754cdf71879fb18e84 100644 (file)
@@ -54,3 +54,8 @@ namespace PR7462 {
   typedef int operator! (A); // expected-error{{typedef name must be an identifier}}
   int i = !A(); // expected-error{{invalid argument type}}
 }
+
+template<typename T>
+typedef T f(T t) { return t; } // expected-error {{function definition declared 'typedef'}}
+int k = f(0);
+int k2 = k;