[cfe-commits] r153488 - in /cfe/trunk: lib/Parse/ParseDecl.cpp test/Parser/cxx-class.cpp test/SemaCXX/copy-assignment.cpp

Richard Smith richard-llvm at metafoo.co.uk
Mon Mar 26 17:56:56 PDT 2012


Author: rsmith
Date: Mon Mar 26 19:56:56 2012
New Revision: 153488

URL: http://llvm.org/viewvc/llvm-project?rev=153488&view=rev
Log:
When we see 'Class(X' or 'Class::Class(X' and we suspect that it names a
constructor, but X is not a known typename, check whether the tokens could
possibly match the syntax of a declarator before concluding that it isn't
a constructor. If it's definitely ill-formed, assume it is a constructor.

Empirical evidence suggests that this pattern is much more often a
constructor with a typoed (or not-yet-declared) type name than any of the
other possibilities, so the extra cost of the check is not expected to be
problematic.

Modified:
    cfe/trunk/lib/Parse/ParseDecl.cpp
    cfe/trunk/test/Parser/cxx-class.cpp
    cfe/trunk/test/SemaCXX/copy-assignment.cpp

Modified: cfe/trunk/lib/Parse/ParseDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseDecl.cpp?rev=153488&r1=153487&r2=153488&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParseDecl.cpp (original)
+++ cfe/trunk/lib/Parse/ParseDecl.cpp Mon Mar 26 19:56:56 2012
@@ -3379,7 +3379,42 @@
   // Check whether the next token(s) are part of a declaration
   // specifier, in which case we have the start of a parameter and,
   // therefore, we know that this is a constructor.
-  bool IsConstructor = isDeclarationSpecifier();
+  bool IsConstructor = false;
+  if (isDeclarationSpecifier())
+    IsConstructor = true;
+  else if (Tok.is(tok::identifier) ||
+           (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
+    // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
+    // This might be a parenthesized member name, but is more likely to
+    // be a constructor declaration with an invalid argument type. Keep
+    // looking.
+    if (Tok.is(tok::annot_cxxscope))
+      ConsumeToken();
+    ConsumeToken();
+
+    // If this is not a constructor, we must be parsing a declarator,
+    // which must have one of the following syntactic forms:
+    switch (Tok.getKind()) {
+    case tok::l_paren:
+      // C(X   (   int));
+    case tok::l_square:
+      // C(X   [   5]);
+      // C(X   [   [attribute]]);
+    case tok::coloncolon:
+      // C(X   ::   Y);
+      // C(X   ::   *p);
+    case tok::r_paren:
+      // C(X   )
+      // Assume this isn't a constructor, rather than assuming it's a
+      // constructor with an unnamed parameter of an ill-formed type.
+      break;
+
+    default:
+      IsConstructor = true;
+      break;
+    }
+  }
+
   TPA.Revert();
   return IsConstructor;
 }

Modified: cfe/trunk/test/Parser/cxx-class.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Parser/cxx-class.cpp?rev=153488&r1=153487&r2=153488&view=diff
==============================================================================
--- cfe/trunk/test/Parser/cxx-class.cpp (original)
+++ cfe/trunk/test/Parser/cxx-class.cpp Mon Mar 26 19:56:56 2012
@@ -61,6 +61,30 @@
     typedef void F4() {} // expected-error{{function definition declared 'typedef'}}
 };
 
+namespace ctor_error {
+  class Foo {};
+  // By [class.qual]p2, this is a constructor declaration.
+  Foo::Foo (F) = F(); // expected-error{{does not match any declaration in 'ctor_error::Foo'}}
+
+  class Ctor { // expected-note{{not complete until the closing '}'}}
+    Ctor(f)(int); // ok
+    Ctor(g(int)); // ok
+    Ctor(x[5]); // expected-error{{incomplete type}}
+
+    Ctor(UnknownType *); // expected-error{{unknown type name 'UnknownType'}}
+  };
+
+  Ctor::Ctor (x) = { 0 }; // \
+    // expected-error{{qualified reference to 'Ctor' is a constructor name}}
+
+  // FIXME: These diagnostics are terrible.
+  Ctor::Ctor(UnknownType *) {} // \
+    // expected-error{{'Ctor' cannot be the name of a variable or data member}} \
+    // expected-error{{use of undeclared identifier 'UnknownType'}} \
+    // expected-error{{expected expression}} \
+    // expected-error{{expected ';' after top level declarator}}
+}
+
 // PR11109 must appear at the end of the source file
 class pr11109r3 { // expected-note{{to match this '{'}}
   public // expected-error{{expected ':'}} expected-error{{expected '}'}} expected-error{{expected ';' after class}}

Modified: cfe/trunk/test/SemaCXX/copy-assignment.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/copy-assignment.cpp?rev=153488&r1=153487&r2=153488&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/copy-assignment.cpp (original)
+++ cfe/trunk/test/SemaCXX/copy-assignment.cpp Mon Mar 26 19:56:56 2012
@@ -98,18 +98,13 @@
 }
 
 // <rdar://problem/8315440>: Don't crash
-// FIXME: the recovery here is really bad.
 namespace test1 {
   template<typename T> class A : public unknown::X { // expected-error {{undeclared identifier 'unknown'}} expected-error {{expected class name}}
-    A(UndeclaredType n) : X(n) {} // expected-error{{expected ')'}} expected-note{{to match this '('}} \
-    // expected-error{{use of undeclared identifier 'n'}} \
-    // expected-error{{expected ';' at end of declaration list}} \
-    // expected-error{{field has incomplete type 'test1::A<char>'}}
+    A(UndeclaredType n) : X(n) {} // expected-error {{unknown type name 'UndeclaredType'}}
   };
   template<typename T> class B : public A<T>     {
     virtual void foo() {}
   };
-  extern template class A<char>; // expected-note {{in instantiation of template class 'test1::A<char>' requested here}} \
-                                 // expected-note {{definition of 'test1::A<char>' is not complete until the closing '}'}}
+  extern template class A<char>;
   extern template class B<char>;
 }





More information about the cfe-commits mailing list