aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnders Carlsson <andersca@mac.com>2009-03-25 02:58:17 +0000
committerAnders Carlsson <andersca@mac.com>2009-03-25 02:58:17 +0000
commita7b3521ef52d983bd0e7fa562bb9ef1393f14634 (patch)
treef8bc187e9642a63ae61fcdeee48577e5ae274efe
parent0cf6891270c59b458a1dd36d80d475adbf49ae7e (diff)
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.) git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@67672 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/clang/Basic/DiagnosticSemaKinds.td3
-rw-r--r--lib/Parse/Parser.cpp4
-rw-r--r--lib/Sema/Sema.h4
-rw-r--r--lib/Sema/SemaDeclCXX.cpp12
-rw-r--r--test/SemaCXX/constructor-initializer.cpp9
-rw-r--r--test/SemaCXX/nested-name-spec.cpp3
6 files changed, 33 insertions, 2 deletions
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td
index 82f2001ede..bdfbffa9b3 100644
--- a/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/include/clang/Basic/DiagnosticSemaKinds.td
@@ -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">;
diff --git a/lib/Parse/Parser.cpp b/lib/Parse/Parser.cpp
index a26c310c20..135faf4e9c 100644
--- a/lib/Parse/Parser.cpp
+++ b/lib/Parse/Parser.cpp
@@ -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);
diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h
index 6b9012b487..42b0205bac 100644
--- a/lib/Sema/Sema.h
+++ b/lib/Sema/Sema.h
@@ -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,
diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp
index a85d62bc41..d734ffe5cf 100644
--- a/lib/Sema/SemaDeclCXX.cpp
+++ b/lib/Sema/SemaDeclCXX.cpp
@@ -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.
diff --git a/test/SemaCXX/constructor-initializer.cpp b/test/SemaCXX/constructor-initializer.cpp
index 81d56eac9c..d0c978a80d 100644
--- a/test/SemaCXX/constructor-initializer.cpp
+++ b/test/SemaCXX/constructor-initializer.cpp
@@ -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) { }
+
diff --git a/test/SemaCXX/nested-name-spec.cpp b/test/SemaCXX/nested-name-spec.cpp
index 7aaa99175f..f575fb82b4 100644
--- a/test/SemaCXX/nested-name-spec.cpp
+++ b/test/SemaCXX/nested-name-spec.cpp
@@ -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}}