Fix a crash in cppcoreguidelines-pro-type-member-init when checking a type with a template parameter as a base class.

Summary: Fixed a crash in cppcoreguidelines-pro-type-member-init when encountering a type that uses one of its template parameters as a base when compiling for C++98.

Patch by Michael Miller!

Reviewers: aaron.ballman, alexfh, hokein

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D19539

llvm-svn: 267700
This commit is contained in:
Haojian Wu 2016-04-27 12:17:04 +00:00
parent f23aa2a9c9
commit 20d4c20bca
2 changed files with 24 additions and 10 deletions

View File

@ -208,8 +208,12 @@ computeInsertions(const CXXConstructorDecl::init_const_range &Inits,
void getInitializationsInOrder(const CXXRecordDecl *ClassDecl,
SmallVectorImpl<const NamedDecl *> &Decls) {
Decls.clear();
for (const auto &Base : ClassDecl->bases())
Decls.emplace_back(getCanonicalRecordDecl(Base.getType()));
for (const auto &Base : ClassDecl->bases()) {
// Decl may be null if the base class is a template parameter.
if (const NamedDecl *Decl = getCanonicalRecordDecl(Base.getType())) {
Decls.emplace_back(Decl);
}
}
Decls.append(ClassDecl->fields().begin(), ClassDecl->fields().end());
}

View File

@ -70,26 +70,36 @@ struct NegativeAggregateType {
int Z;
};
struct NonTrivialType {
struct TrivialType {
int X;
int Y;
};
struct PositiveUninitializedBaseOrdering : public NegativeAggregateType,
public NonTrivialType {
PositiveUninitializedBaseOrdering() : NegativeAggregateType(), NonTrivialType(), B() {}
public TrivialType {
PositiveUninitializedBaseOrdering() : NegativeAggregateType(), TrivialType(), B() {}
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: constructor does not initialize these fields: A
// CHECK-FIXES: PositiveUninitializedBaseOrdering() : NegativeAggregateType(), NonTrivialType(), A(), B() {}
// CHECK-FIXES: PositiveUninitializedBaseOrdering() : NegativeAggregateType(), TrivialType(), A(), B() {}
// This is somewhat pathological with the base class initializer at the end...
PositiveUninitializedBaseOrdering(int) : B(), NonTrivialType(), A() {}
PositiveUninitializedBaseOrdering(int) : B(), TrivialType(), A() {}
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: constructor does not initialize these bases: NegativeAggregateType
// CHECK-FIXES: PositiveUninitializedBaseOrdering(int) : B(), NegativeAggregateType(), NonTrivialType(), A() {}
// CHECK-FIXES: PositiveUninitializedBaseOrdering(int) : B(), NegativeAggregateType(), TrivialType(), A() {}
PositiveUninitializedBaseOrdering(float) : NegativeAggregateType(), A() {}
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: constructor does not initialize these bases: NonTrivialType
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: constructor does not initialize these bases: TrivialType
// CHECK-MESSAGES: :[[@LINE-2]]:3: warning: constructor does not initialize these fields: B
// CHECK-FIXES: PositiveUninitializedBaseOrdering(float) : NegativeAggregateType(), NonTrivialType(), A(), B() {}
// CHECK-FIXES: PositiveUninitializedBaseOrdering(float) : NegativeAggregateType(), TrivialType(), A(), B() {}
int A, B;
};
template <class T>
class PositiveTemplateBase : T {
public:
PositiveTemplateBase() {}
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: constructor does not initialize these fields: X
// CHECK-FIXES: PositiveTemplateBase() : X() {}
int X;
};