r237192 - Add a new error for unexpected semi-colon before closing delimiter.

Richard Trieu rtrieu at google.com
Tue May 12 14:36:35 PDT 2015


Author: rtrieu
Date: Tue May 12 16:36:35 2015
New Revision: 237192

URL: http://llvm.org/viewvc/llvm-project?rev=237192&view=rev
Log:
Add a new error for unexpected semi-colon before closing delimiter.

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.


Added:
    cfe/trunk/test/Parser/extra-semi.cpp
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td
    cfe/trunk/lib/Parse/RAIIObjectsForParser.h
    cfe/trunk/test/Parser/cxx-class.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td?rev=237192&r1=237191&r2=237192&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td Tue May 12 16:36:35 2015
@@ -170,6 +170,7 @@ def err_function_declared_typedef : Erro
   "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">;

Modified: cfe/trunk/lib/Parse/RAIIObjectsForParser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/RAIIObjectsForParser.h?rev=237192&r1=237191&r2=237192&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/RAIIObjectsForParser.h (original)
+++ cfe/trunk/lib/Parse/RAIIObjectsForParser.h Tue May 12 16:36:35 2015
@@ -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();
     }

Modified: cfe/trunk/test/Parser/cxx-class.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Parser/cxx-class.cpp?rev=237192&r1=237191&r2=237192&view=diff
==============================================================================
--- cfe/trunk/test/Parser/cxx-class.cpp (original)
+++ cfe/trunk/test/Parser/cxx-class.cpp Tue May 12 16:36:35 2015
@@ -210,9 +210,9 @@ class X2 { a::a; }; // expected-error {{
 
 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 ')'}}
           ));
 };
 

Added: cfe/trunk/test/Parser/extra-semi.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Parser/extra-semi.cpp?rev=237192&view=auto
==============================================================================
--- cfe/trunk/test/Parser/extra-semi.cpp (added)
+++ cfe/trunk/test/Parser/extra-semi.cpp Tue May 12 16:36:35 2015
@@ -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 '}'}}
+}





More information about the cfe-commits mailing list