]> granicus.if.org Git - clang/commitdiff
Improve handling of base initializers. We now parse initializers in out of line decls...
authorAnders Carlsson <andersca@mac.com>
Wed, 25 Mar 2009 02:58:17 +0000 (02:58 +0000)
committerAnders Carlsson <andersca@mac.com>
Wed, 25 Mar 2009 02:58:17 +0000 (02:58 +0000)
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.)

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@67672 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 82f2001ede6a15efa9ee5d24de32b3b1d471cb3c..bdfbffa9b30cf3b7f7879ce3fc298e84d9af20f2 100644 (file)
@@ -1219,6 +1219,9 @@ def err_overload_multiple_match : Error<
   "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">;
index a26c310c204b1d357bf5e07989452d3cfda12b40..135faf4e9c9faa77799c5c0b5467069c8e3043b0 100644 (file)
@@ -505,7 +505,9 @@ Parser::ParseDeclarationOrFunctionDefinition(
   } 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);
 
index 6b9012b487a4acf69a9aa4ac56aaa99d07297026..42b0205bacbe47aef92f0b86b9835887f3526e0c 100644 (file)
@@ -1588,6 +1588,10 @@ public:
 
   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,
index a85d62bc41591a15c4c9cbe4f241ce4f0610aa97..d734ffe5cfaf5838f20704a1dea40f8b30c7e9f4 100644 (file)
@@ -706,6 +706,18 @@ Sema::ActOnMemInitializer(DeclTy *ConstructorD,
   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.
index 81d56eac9c394f74ce68bf42c9ced5d2cec4339e..d0c978a80d15d52f4c9cfa58d5b0f346a2f197a3 100644 (file)
@@ -45,3 +45,12 @@ public:
 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) { }
+
index 7aaa99175f0aad15913c98cbfca0d1b3b5350e19..f575fb82b41e3a5a341494ca5eea7e54fb90e5fe 100644 (file)
@@ -168,5 +168,6 @@ Y::foo y; // expected-error{{incomplete type 'struct Y' named in nested name spe
          // 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}}