[PATCH] Add context to "expected unqualified-id" message

Richard Trieu rtrieu at google.com
Mon Jun 24 16:58:23 PDT 2013


When the error message "expected unqualified-id" appears in the middle of parsing a declarator, and the token pointed to begins a new line, add a note that points back to the rest of the declarator.  This is to prevent the confusing case where an "expected unqualified-id" message shows up and points to a line of code where there is a single '}'.

http://llvm-reviews.chandlerc.com/D1032

Files:
  lib/Parse/ParseDecl.cpp
  test/Parser/cxx-decl.cpp
  include/clang/Basic/DiagnosticParseKinds.td

Index: lib/Parse/ParseDecl.cpp
===================================================================
--- lib/Parse/ParseDecl.cpp
+++ lib/Parse/ParseDecl.cpp
@@ -4737,8 +4737,14 @@
     else if (getLangOpts().CPlusPlus) {
       if (Tok.is(tok::period) || Tok.is(tok::arrow))
         Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
-      else
+      else {
         Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
+        if (Tok.isAtStartOfLine()) {
+          SourceLocation Loc = D.getCXXScopeSpec().getEndLoc();
+          if (Loc.isValid())
+            Diag(PP.getLocForEndOfToken(Loc), diag::note_after_here);
+        }
+      }
     } else
       Diag(Tok, diag::err_expected_ident_lparen);
     D.SetIdentifier(0, Tok.getLocation());
Index: test/Parser/cxx-decl.cpp
===================================================================
--- test/Parser/cxx-decl.cpp
+++ test/Parser/cxx-decl.cpp
@@ -187,6 +187,22 @@
 // Ensure we produce at least some diagnostic for attributes in C++98.
 [[]] struct S; // expected-error 2{{}}
 
+namespace test7 {
+  struct Foo {
+    int a();
+    int b();
+  };
+
+  int Foo::
+  // Comment!
+  a() {}
+
+
+  int Foo::
+  // expected-note at -1 {{after here}}
+  // Comment!
+} // expected-error {{expected unqualified-id}}
+
 // PR8380
 extern ""      // expected-error {{unknown linkage language}}
 test6a { ;// expected-error {{C++ requires a type specifier for all declarations}} \
Index: include/clang/Basic/DiagnosticParseKinds.td
===================================================================
--- include/clang/Basic/DiagnosticParseKinds.td
+++ include/clang/Basic/DiagnosticParseKinds.td
@@ -450,6 +450,7 @@
   "cannot use %select{dot|arrow}0 operator on a type">;
 def err_expected_unqualified_id : Error<
   "expected %select{identifier|unqualified-id}0">;
+def note_after_here : Note<"after here">;
 def err_func_def_no_params : Error<
   "function definition does not declare parameters">;
 def err_expected_lparen_after_type : Error<
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1032.1.patch
Type: text/x-patch
Size: 2039 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130624/378ae090/attachment.bin>


More information about the cfe-commits mailing list