A more detailed diagnosis of ill-formed ctor-initializer
list.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@74480 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/AST/DeclCXX.h b/include/clang/AST/DeclCXX.h
index 2754c70..05383d8 100644
--- a/include/clang/AST/DeclCXX.h
+++ b/include/clang/AST/DeclCXX.h
@@ -588,15 +588,20 @@
/// Args - The arguments used to initialize the base or member.
Expr **Args;
unsigned NumArgs;
+
+ /// IdLoc - Location of the id in ctor-initializer list.
+ SourceLocation IdLoc;
public:
/// CXXBaseOrMemberInitializer - Creates a new base-class initializer.
explicit
- CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs);
+ CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs,
+ SourceLocation L);
/// CXXBaseOrMemberInitializer - Creates a new member initializer.
explicit
- CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs);
+ CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs,
+ SourceLocation L);
/// ~CXXBaseOrMemberInitializer - Destroy the base or member initializer.
~CXXBaseOrMemberInitializer();
@@ -653,6 +658,8 @@
return 0;
}
+ SourceLocation getSourceLocation() const { return IdLoc; }
+
/// begin() - Retrieve an iterator to the first initializer argument.
arg_iterator begin() { return Args; }
/// begin() - Retrieve an iterator to the first initializer argument.
diff --git a/lib/AST/DeclCXX.cpp b/lib/AST/DeclCXX.cpp
index bffa70f..8b596e1 100644
--- a/lib/AST/DeclCXX.cpp
+++ b/lib/AST/DeclCXX.cpp
@@ -294,8 +294,9 @@
}
CXXBaseOrMemberInitializer::
-CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs)
- : Args(0), NumArgs(0) {
+CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs,
+ SourceLocation L)
+ : Args(0), NumArgs(0), IdLoc(L) {
BaseOrMember = reinterpret_cast<uintptr_t>(BaseType.getTypePtr());
assert((BaseOrMember & 0x01) == 0 && "Invalid base class type pointer");
BaseOrMember |= 0x01;
@@ -309,8 +310,9 @@
}
CXXBaseOrMemberInitializer::
-CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs)
- : Args(0), NumArgs(0) {
+CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs,
+ SourceLocation L)
+ : Args(0), NumArgs(0), IdLoc(L) {
BaseOrMember = reinterpret_cast<uintptr_t>(Member);
assert((BaseOrMember & 0x01) == 0 && "Invalid member pointer");
diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp
index fffa66c..e59236c 100644
--- a/lib/Sema/SemaDeclCXX.cpp
+++ b/lib/Sema/SemaDeclCXX.cpp
@@ -688,7 +688,8 @@
if (Member) {
// FIXME: Perform direct initialization of the member.
- return new CXXBaseOrMemberInitializer(Member, (Expr **)Args, NumArgs);
+ return new CXXBaseOrMemberInitializer(Member, (Expr **)Args, NumArgs,
+ IdLoc);
}
// It didn't name a member, so see if it names a class.
@@ -750,7 +751,8 @@
return Diag(IdLoc, diag::err_base_init_direct_and_virtual)
<< MemberOrBase << SourceRange(IdLoc, RParenLoc);
- return new CXXBaseOrMemberInitializer(BaseType, (Expr **)Args, NumArgs);
+ return new CXXBaseOrMemberInitializer(BaseType, (Expr **)Args, NumArgs,
+ IdLoc);
}
void Sema::ActOnMemInitializers(DeclPtrTy ConstructorDecl,
@@ -766,22 +768,27 @@
Diag(ColonLoc, diag::err_only_constructors_take_base_inits);
return;
}
- llvm::DenseSet<uintptr_t>Members;
+ llvm::DenseMap<uintptr_t, CXXBaseOrMemberInitializer *>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());
+ CXXBaseOrMemberInitializer *&PrevMember = Members[Member->getBaseOrMember()];
+ if (!PrevMember)
+ PrevMember = Member;
else {
if (FieldDecl *Field = Member->getMember())
- Diag(ColonLoc, diag::error_multiple_mem_initialization)
- << Field->getNameAsString();
+ Diag(Member->getSourceLocation(),
+ diag::error_multiple_mem_initialization)
+ << Field->getNameAsString();
else if (Type *BaseClass = Member->getBaseClass())
- Diag(ColonLoc, diag::error_multiple_base_initialization)
+ Diag(Member->getSourceLocation(),
+ diag::error_multiple_base_initialization)
<< BaseClass->getDesugaredType(true);
else
assert(false && "ActOnMemInitializers - neither field or base");
+ Diag(PrevMember->getSourceLocation(), diag::note_previous_initializer)
+ << 0;
}
}
}
diff --git a/test/SemaCXX/class-base-member-init.cpp b/test/SemaCXX/class-base-member-init.cpp
index eca9fc3..c38c3d3 100644
--- a/test/SemaCXX/class-base-member-init.cpp
+++ b/test/SemaCXX/class-base-member-init.cpp
@@ -7,7 +7,9 @@
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'}}
+ // expected-note {{previous initialization is here}} \
+ // expected-error {{multiple initializations given for base 'class S'}} \
+ // expected-note {{previous initialization is here}}
int b1;
int b2;