Diagnose multiple initialzation of data-member/base

in the ctor-initializer list. More to come.

llvm-svn: 74465
This commit is contained in:
Fariborz Jahanian 2009-06-29 22:33:26 +00:00
parent 911edef65b
commit 2be8bf44cd
4 changed files with 45 additions and 0 deletions

View File

@ -609,6 +609,10 @@ public:
/// arguments.
typedef Expr * const * arg_const_iterator;
/// getBaseOrMember - get the generic 'member' representing either the field
/// or a base class.
uintptr_t getBaseOrMember() const { return BaseOrMember; }
/// isBaseInitializer - Returns true when this initializer is
/// initializing a base class.
bool isBaseInitializer() const { return (BaseOrMember & 0x1) != 0; }

View File

@ -1579,6 +1579,12 @@ def err_overload_multiple_match : Error<
def err_only_constructors_take_base_inits : Error<
"only constructors take base initializers">;
def error_multiple_mem_initialization : Error <
"multiple initializations given for non-static member '%0'">;
def error_multiple_base_initialization : Error <
"multiple initializations given for base %0">;
def err_mem_init_not_member_or_class : Error<
"member initializer %0 does not name a non-static data member or base "
"class">;

View File

@ -766,6 +766,24 @@ void Sema::ActOnMemInitializers(DeclPtrTy ConstructorDecl,
Diag(ColonLoc, diag::err_only_constructors_take_base_inits);
return;
}
llvm::DenseSet<uintptr_t>Members;
for (unsigned i = 0; i < NumMemInits; i++) {
CXXBaseOrMemberInitializer *Member =
static_cast<CXXBaseOrMemberInitializer*>(MemInits[i]);
if (Members.count(Member->getBaseOrMember()) == 0)
Members.insert(Member->getBaseOrMember());
else {
if (FieldDecl *Field = Member->getMember())
Diag(ColonLoc, diag::error_multiple_mem_initialization)
<< Field->getNameAsString();
else if (Type *BaseClass = Member->getBaseClass())
Diag(ColonLoc, diag::error_multiple_base_initialization)
<< BaseClass->getDesugaredType(true);
else
assert(false && "ActOnMemInitializers - neither field or base");
}
}
}
namespace {

View File

@ -0,0 +1,17 @@
// RUN: clang-cc -fsyntax-only -verify %s
class S {
public:
S ();
};
struct D : S {
D() : b1(0), b2(1), b1(0), S(), S() {} // expected-error {{multiple initializations given for non-static member 'b1'}} \
// expected-error {{multiple initializations given for base 'class S'}}
int b1;
int b2;
};