[cfe-commits] r99980 - in /cfe/trunk: lib/Sema/Sema.h lib/Sema/SemaDecl.cpp test/Sema/init.c test/SemaCXX/invalid-member-expr.cpp

John McCall rjmccall at apple.com
Tue Mar 30 19:13:20 PDT 2010


Author: rjmccall
Date: Tue Mar 30 21:13:20 2010
New Revision: 99980

URL: http://llvm.org/viewvc/llvm-project?rev=99980&view=rev
Log:
Fix PR6327:  restore invariants when there's a parse error in an initializer.


Modified:
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/Sema/init.c
    cfe/trunk/test/SemaCXX/invalid-member-expr.cpp

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=99980&r1=99979&r2=99980&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Tue Mar 30 21:13:20 2010
@@ -828,6 +828,7 @@
   virtual void AddInitializerToDecl(DeclPtrTy dcl, ExprArg init);
   void AddInitializerToDecl(DeclPtrTy dcl, ExprArg init, bool DirectInit);
   void ActOnUninitializedDecl(DeclPtrTy dcl, bool TypeContainsUndeducedAuto);
+  virtual void ActOnInitializerError(DeclPtrTy Dcl);
   virtual void SetDeclDeleted(DeclPtrTy dcl, SourceLocation DelLoc);
   virtual DeclGroupPtrTy FinalizeDeclaratorGroup(Scope *S, const DeclSpec &DS,
                                                  DeclPtrTy *Group,

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=99980&r1=99979&r2=99980&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Tue Mar 30 21:13:20 2010
@@ -3797,6 +3797,41 @@
   return;
 }
 
+/// ActOnInitializerError - Given that there was an error parsing an
+/// initializer for the given declaration, try to return to some form
+/// of sanity.
+void Sema::ActOnInitializerError(DeclPtrTy dcl) {
+  // Our main concern here is re-establishing invariants like "a
+  // variable's type is either dependent or complete".
+  Decl *D = dcl.getAs<Decl>();
+  if (!D || D->isInvalidDecl()) return;
+
+  VarDecl *VD = dyn_cast<VarDecl>(D);
+  if (!VD) return;
+
+  QualType Ty = VD->getType();
+  if (Ty->isDependentType()) return;
+
+  // Require a complete type.
+  if (RequireCompleteType(VD->getLocation(), 
+                          Context.getBaseElementType(Ty),
+                          diag::err_typecheck_decl_incomplete_type)) {
+    VD->setInvalidDecl();
+    return;
+  }
+
+  // Require an abstract type.
+  if (RequireNonAbstractType(VD->getLocation(), Ty,
+                             diag::err_abstract_type_in_decl,
+                             AbstractVariableType)) {
+    VD->setInvalidDecl();
+    return;
+  }
+
+  // Don't bother complaining about constructors or destructors,
+  // though.
+}
+
 void Sema::ActOnUninitializedDecl(DeclPtrTy dcl,
                                   bool TypeContainsUndeducedAuto) {
   Decl *RealDecl = dcl.getAs<Decl>();

Modified: cfe/trunk/test/Sema/init.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/init.c?rev=99980&r1=99979&r2=99980&view=diff
==============================================================================
--- cfe/trunk/test/Sema/init.c (original)
+++ cfe/trunk/test/Sema/init.c Tue Mar 30 21:13:20 2010
@@ -75,7 +75,8 @@
 };
 
 // PR3001
-struct s1 s2 = {
+struct s1 s2 = { // expected-error {{variable has incomplete type 'struct s1'}}  \
+                 // expected-note {{forward declaration of 'struct s1'}}
     .a = sizeof(struct s3), // expected-error {{invalid application of 'sizeof'}} \
                             // expected-note{{forward declaration of 'struct s3'}}
     .b = bogus // expected-error {{use of undeclared identifier 'bogus'}}

Modified: cfe/trunk/test/SemaCXX/invalid-member-expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/invalid-member-expr.cpp?rev=99980&r1=99979&r2=99980&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/invalid-member-expr.cpp (original)
+++ cfe/trunk/test/SemaCXX/invalid-member-expr.cpp Tue Mar 30 21:13:20 2010
@@ -19,3 +19,21 @@
   x->operator; // expected-error{{missing type specifier after 'operator'}}
   x->operator typedef; // expected-error{{missing type specifier after 'operator'}}
 }
+
+// PR6327
+namespace test3 {
+  template <class A, class B> struct pair {};
+
+  void test0() {
+    pair<int, int> z = minmax({}); // expected-error {{expected expression}}
+  }
+
+  struct string {
+    class iterator {};
+  };
+
+  void test1() {
+    string s;
+    string::iterator i = s.foo(); // expected-error {{no member named 'foo'}}
+  }
+}





More information about the cfe-commits mailing list