From 412e0cc52ea644d804dcfa87194800371f91a977 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Tue, 27 Mar 2012 00:56:56 +0000 Subject: [PATCH] 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@153488 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Parse/ParseDecl.cpp | 37 +++++++++++++++++++++++++++++++- test/Parser/cxx-class.cpp | 24 +++++++++++++++++++++ test/SemaCXX/copy-assignment.cpp | 9 ++------ 3 files changed, 62 insertions(+), 8 deletions(-) diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp index 24386d08ba..75abcae282 100644 --- a/lib/Parse/ParseDecl.cpp +++ b/lib/Parse/ParseDecl.cpp @@ -3379,7 +3379,42 @@ bool Parser::isConstructorDeclarator() { // 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; } diff --git a/test/Parser/cxx-class.cpp b/test/Parser/cxx-class.cpp index 36f08d52d5..8b8caa585f 100644 --- a/test/Parser/cxx-class.cpp +++ b/test/Parser/cxx-class.cpp @@ -61,6 +61,30 @@ class F { 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}} diff --git a/test/SemaCXX/copy-assignment.cpp b/test/SemaCXX/copy-assignment.cpp index 7aca9d6a80..798582c149 100644 --- a/test/SemaCXX/copy-assignment.cpp +++ b/test/SemaCXX/copy-assignment.cpp @@ -98,18 +98,13 @@ void test() { } // : Don't crash -// FIXME: the recovery here is really bad. namespace test1 { template 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'}} + A(UndeclaredType n) : X(n) {} // expected-error {{unknown type name 'UndeclaredType'}} }; template class B : public A { virtual void foo() {} }; - extern template class A; // expected-note {{in instantiation of template class 'test1::A' requested here}} \ - // expected-note {{definition of 'test1::A' is not complete until the closing '}'}} + extern template class A; extern template class B; } -- 2.40.0