[cfe-commits] r67672 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Parse/Parser.cpp lib/Sema/Sema.h lib/Sema/SemaDeclCXX.cpp test/SemaCXX/constructor-initializer.cpp test/SemaCXX/nested-name-spec.cpp

Anders Carlsson andersca at mac.com
Tue Mar 24 19:58:17 PDT 2009


Author: andersca
Date: Tue Mar 24 21:58:17 2009
New Revision: 67672

URL: http://llvm.org/viewvc/llvm-project?rev=67672&view=rev
Log:
Improve handling of base initializers. We now parse initializers in out of line decls, such as:

class C {
    C() { }
    
    int a;
};

C::C() : a(10) { }

We also diagnose when initializers are used on declarations that aren't constructors:

t.cpp:1:10: error: only constructors take base initializers
void f() : a(10) { }
         ^

Doug and/or Sebastian: I'd appreciate a review, especially the nested-name-spec test results (from the looks of it we now match gcc in that test.)

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Parse/Parser.cpp
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaDeclCXX.cpp
    cfe/trunk/test/SemaCXX/constructor-initializer.cpp
    cfe/trunk/test/SemaCXX/nested-name-spec.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=67672&r1=67671&r2=67672&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Mar 24 21:58:17 2009
@@ -1219,6 +1219,9 @@
   "more than one matching function found in __builtin_overload">;
 
 // C++ member initializers.
+def err_only_constructors_take_base_inits : Error<
+  "only constructors take base initializers">;
+
 def err_mem_init_not_member_or_class : Error<
   "member initializer %0 does not name a non-static data member or base "
   "class">;

Modified: cfe/trunk/lib/Parse/Parser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/Parser.cpp?rev=67672&r1=67671&r2=67672&view=diff

==============================================================================
--- cfe/trunk/lib/Parse/Parser.cpp (original)
+++ cfe/trunk/lib/Parse/Parser.cpp Tue Mar 24 21:58:17 2009
@@ -505,7 +505,9 @@
   } else if (DeclaratorInfo.isFunctionDeclarator() &&
              (Tok.is(tok::l_brace) ||             // int X() {}
               (!getLang().CPlusPlus &&
-               isDeclarationSpecifier()))) {    // int X(f) int f; {}
+               isDeclarationSpecifier()) ||   // int X(f) int f; {}
+              (getLang().CPlusPlus &&
+               Tok.is(tok::colon)))) { // X() : Base() {} (used for ctors)
     if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
       Diag(Tok, diag::err_function_declared_typedef);
 

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=67672&r1=67671&r2=67672&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Tue Mar 24 21:58:17 2009
@@ -1588,6 +1588,10 @@
 
   void AddImplicitlyDeclaredMembersToClass(CXXRecordDecl *ClassDecl);
 
+  virtual void ActOnMemInitializers(DeclTy *ConstructorDecl, 
+                                    SourceLocation ColonLoc,
+                                    MemInitTy **MemInits, unsigned NumMemInits);
+  
   virtual void ActOnFinishCXXMemberSpecification(Scope* S, SourceLocation RLoc,
                                                  DeclTy *TagDecl,
                                                  SourceLocation LBrac,

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Tue Mar 24 21:58:17 2009
@@ -706,6 +706,18 @@
   return new CXXBaseOrMemberInitializer(BaseType, (Expr **)Args, NumArgs);
 }
 
+void Sema::ActOnMemInitializers(DeclTy *ConstructorDecl, 
+                                SourceLocation ColonLoc,
+                                MemInitTy **MemInits, unsigned NumMemInits) {
+  CXXConstructorDecl *Constructor = 
+  dyn_cast<CXXConstructorDecl>((Decl *)ConstructorDecl);
+  
+  if (!Constructor) {
+    Diag(ColonLoc, diag::err_only_constructors_take_base_inits);
+    return;
+  }
+}
+
 namespace {
   /// PureVirtualMethodCollector - traverses a class and its superclasses
   /// and determines if it has any pure virtual methods.

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

==============================================================================
--- cfe/trunk/test/SemaCXX/constructor-initializer.cpp (original)
+++ cfe/trunk/test/SemaCXX/constructor-initializer.cpp Tue Mar 24 21:58:17 2009
@@ -45,3 +45,12 @@
 class G : A {
   G() : A(10); // expected-error{{expected '{'}}
 };
+
+void f() : a(242) { } // expected-error{{only constructors take base initializers}}
+
+class H : A {
+  H();
+};
+
+H::H() : A(10) { }
+

Modified: cfe/trunk/test/SemaCXX/nested-name-spec.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/nested-name-spec.cpp?rev=67672&r1=67671&r2=67672&view=diff

==============================================================================
--- cfe/trunk/test/SemaCXX/nested-name-spec.cpp (original)
+++ cfe/trunk/test/SemaCXX/nested-name-spec.cpp Tue Mar 24 21:58:17 2009
@@ -168,5 +168,6 @@
          // FIXME: ugly: expected-error{{invalid token after top level declarator}}
 
 X::X() : a(5) { } // expected-error{{use of undeclared identifier 'X'}} \
-      // expected-error{{expected function body after function declarator}}
+      // expected-error{{C++ requires a type specifier for all declarations}} \
+      // expected-error{{only constructors take base initializers}}
 





More information about the cfe-commits mailing list