]> granicus.if.org Git - clang/commitdiff
Add a new error for unexpected semi-colon before closing delimiter.
authorRichard Trieu <rtrieu@google.com>
Tue, 12 May 2015 21:36:35 +0000 (21:36 +0000)
committerRichard Trieu <rtrieu@google.com>
Tue, 12 May 2015 21:36:35 +0000 (21:36 +0000)
Previously, if a semi-colon is unexpectedly added before a closing ')', ']' or
'}', two errors and one note would emitted, and the parsing would get confused
to which scope it was in.  This change consumes the semi-colon, recovers
parsing better, and emits only one error with a fix-it.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@237192 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticParseKinds.td
lib/Parse/RAIIObjectsForParser.h
test/Parser/cxx-class.cpp
test/Parser/extra-semi.cpp [new file with mode: 0644]

index 56cfb0eb5bd65bacf6370fc7031ad2120cfed56b..f00a3b39d23b167f756f7ce61c4c40c0f7eb9272 100644 (file)
@@ -170,6 +170,7 @@ def err_function_declared_typedef : Error<
   "function definition declared 'typedef'">;
 def err_at_defs_cxx : Error<"@defs is not supported in Objective-C++">;
 def err_at_in_class : Error<"unexpected '@' in member specification">;
+def err_unexpected_semi : Error<"unexpected ';' before %0">;
 
 def err_expected_fn_body : Error<
   "expected function body after function declarator">;
index c2f498041958be7dae004c317ef407a6abcb50d2..36d87ebd8acad6b2e684016506b2ea2b70e7b135 100644 (file)
@@ -429,7 +429,13 @@ namespace clang {
       if (P.Tok.is(Close)) {
         LClose = (P.*Consumer)();
         return false;
-      } 
+      } else if (P.Tok.is(tok::semi) && P.NextToken().is(Close)) {
+        SourceLocation SemiLoc = P.ConsumeToken();
+        P.Diag(SemiLoc, diag::err_unexpected_semi)
+            << Close << FixItHint::CreateRemoval(SourceRange(SemiLoc, SemiLoc));
+        LClose = (P.*Consumer)();
+        return false;
+      }
       
       return diagnoseMissingClose();
     }
index f46e752bcb09395828b008b4c0f68043e4d94827..38eef1756d0f23291e251e02624d74bf891829fd 100644 (file)
@@ -210,9 +210,9 @@ class X2 { a::a; }; // expected-error {{undeclared identifier 'a'}}
 
 class BadExceptionSpec {
   void f() throw(int; // expected-error {{expected ')'}} expected-note {{to match}}
-  void g() throw( // expected-note {{to match}}
-      int( // expected-note {{to match}}
-          ; // expected-error 2{{expected ')'}} expected-error {{unexpected end of exception specification}}
+  void g() throw(
+      int(
+          ; // expected-error {{unexpected ';' before ')'}}
           ));
 };
 
diff --git a/test/Parser/extra-semi.cpp b/test/Parser/extra-semi.cpp
new file mode 100644 (file)
index 0000000..1a44dae
--- /dev/null
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+// RUN: cp %s %t.cpp
+// RUN: not %clang_cc1 -fsyntax-only %t.cpp -fixit
+// RUN: %clang_cc1 -fsyntax-only %t.cpp
+
+void test1(int a;) { // expected-error{{unexpected ';' before ')'}}
+  while (a > 5;) {} // expected-error{{unexpected ';' before ')'}}
+  if (int b = 10;) {} // expected-error{{unexpected ';' before ')'}}
+  for (int c  = 0; c < 21; ++c;) {} // expected-error{{unexpected ';' before ')'}}
+  int d = int(3 + 4;); // expected-error{{unexpected ';' before ')'}}
+  int e[5;]; // expected-error{{unexpected ';' before ']'}}
+  e[a+1;] = 4; // expected-error{{unexpected ';' before ']'}}
+  int f[] = {1,2,3;}; // expected-error{{unexpected ';' before '}'}}
+}