[cfe-commits] r67818 - in /cfe/trunk: lib/Sema/SemaDecl.cpp lib/Sema/SemaDeclCXX.cpp test/SemaCXX/constructor-recovery.cpp test/SemaCXX/constructor.cpp

Douglas Gregor dgregor at apple.com
Thu Mar 26 21:38:57 PDT 2009


Author: dgregor
Date: Thu Mar 26 23:38:56 2009
New Revision: 67818

URL: http://llvm.org/viewvc/llvm-project?rev=67818&view=rev
Log:
Improve recovery when a constructor fails to type-check. Test case from Anders

Added:
    cfe/trunk/test/SemaCXX/constructor-recovery.cpp   (with props)
Modified:
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/lib/Sema/SemaDeclCXX.cpp
    cfe/trunk/test/SemaCXX/constructor.cpp

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=67818&r1=67817&r2=67818&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Thu Mar 26 23:38:56 2009
@@ -2294,7 +2294,7 @@
 
         // A class is abstract if at least one function is pure virtual.
         cast<CXXRecordDecl>(CurContext)->setAbstract(true);
-      } else {
+      } else if (!Method->isInvalidDecl()) {
         Diag(Method->getLocation(), diag::err_non_virtual_pure)
           << Method->getDeclName() << Init->getSourceRange();
         Method->setInvalidDecl();

Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=67818&r1=67817&r2=67818&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Thu Mar 26 23:38:56 2009
@@ -1289,20 +1289,22 @@
 /// well-formedness, issuing any diagnostics required. Returns true if
 /// the constructor declarator is invalid.
 bool Sema::CheckConstructor(CXXConstructorDecl *Constructor) {
-  if (Constructor->isInvalidDecl())
+  CXXRecordDecl *ClassDecl 
+    = dyn_cast<CXXRecordDecl>(Constructor->getDeclContext());
+  if (!ClassDecl)
     return true;
 
-  CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Constructor->getDeclContext());
-  bool Invalid = false;
+  bool Invalid = Constructor->isInvalidDecl();
 
   // C++ [class.copy]p3:
   //   A declaration of a constructor for a class X is ill-formed if
   //   its first parameter is of type (optionally cv-qualified) X and
   //   either there are no other parameters or else all other
   //   parameters have default arguments.
-  if ((Constructor->getNumParams() == 1) || 
-      (Constructor->getNumParams() > 1 && 
-       Constructor->getParamDecl(1)->getDefaultArg() != 0)) {
+  if (!Constructor->isInvalidDecl() &&
+      ((Constructor->getNumParams() == 1) || 
+       (Constructor->getNumParams() > 1 && 
+        Constructor->getParamDecl(1)->getDefaultArg() != 0))) {
     QualType ParamType = Constructor->getParamDecl(0)->getType();
     QualType ClassTy = Context.getTagDeclType(ClassDecl);
     if (Context.getCanonicalType(ParamType).getUnqualifiedType() == ClassTy) {

Added: cfe/trunk/test/SemaCXX/constructor-recovery.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/constructor-recovery.cpp?rev=67818&view=auto

==============================================================================
--- cfe/trunk/test/SemaCXX/constructor-recovery.cpp (added)
+++ cfe/trunk/test/SemaCXX/constructor-recovery.cpp Thu Mar 26 23:38:56 2009
@@ -0,0 +1,9 @@
+// RUN: clang-cc -fsyntax-only -verify %s
+
+struct C {
+  virtual C() = 0; // expected-error{{constructor cannot be declared 'virtual'}}
+};
+
+void f() {
+ C c;
+}

Propchange: cfe/trunk/test/SemaCXX/constructor-recovery.cpp

------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/trunk/test/SemaCXX/constructor-recovery.cpp

------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cfe/trunk/test/SemaCXX/constructor-recovery.cpp

------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: cfe/trunk/test/SemaCXX/constructor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/constructor.cpp?rev=67818&r1=67817&r2=67818&view=diff

==============================================================================
--- cfe/trunk/test/SemaCXX/constructor.cpp (original)
+++ cfe/trunk/test/SemaCXX/constructor.cpp Thu Mar 26 23:38:56 2009
@@ -9,7 +9,7 @@
 
   ((Foo))(INT); // expected-error{{cannot be redeclared}}
 
-  Foo(Foo foo, int i = 17, int j = 42); // expected-error {{copy constructor must pass its first argument by reference}}
+  Foo(Foo foo, int i = 17, int j = 42); // expected-error{{copy constructor must pass its first argument by reference}}
 
   static Foo(short, short); // expected-error{{constructor cannot be declared 'static'}}
   virtual Foo(double); // expected-error{{constructor cannot be declared 'virtual'}}





More information about the cfe-commits mailing list