Skip to content

Commit

Permalink
Fix a crash in cppcoreguidelines-pro-type-member-init related to miss…
Browse files Browse the repository at this point in the history
…ing constructor bodies.

Summary: Fixes a crash in cppcoreguidelines-pro-type-member-init when checking some record types with a constructor without a body. We now check to make sure the constructor has a body before looking for missing members and base initializers.

Patch by Michael Miller!

Reviewers: aaron.ballman, alexfh, hokein

Subscribers: cfe-commits

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

llvm-svn: 266862
  • Loading branch information
hokein committed Apr 20, 2016
1 parent 117625a commit 257914e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ void ProTypeMemberInitCheck::registerMatchers(MatchFinder *Finder) {

void ProTypeMemberInitCheck::check(const MatchFinder::MatchResult &Result) {
if (const auto *Ctor = Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor")) {
// Skip declarations delayed by late template parsing without a body.
if (!Ctor->getBody())
return;
checkMissingMemberInitializer(*Result.Context, Ctor);
checkMissingBaseClassInitializer(*Result.Context, Ctor);
} else if (const auto *Var = Result.Nodes.getNodeAs<VarDecl>("var")) {
Expand All @@ -304,11 +307,6 @@ void ProTypeMemberInitCheck::checkMissingMemberInitializer(
if (IsUnion && ClassDecl->hasInClassInitializer())
return;

// Skip declarations delayed by late template parsing without a body.
const Stmt *Body = Ctor->getBody();
if (!Body)
return;

SmallPtrSet<const FieldDecl *, 16> FieldsToInit;
fieldsRequiringInit(ClassDecl->fields(), Context, FieldsToInit);
if (FieldsToInit.empty())
Expand All @@ -323,7 +321,7 @@ void ProTypeMemberInitCheck::checkMissingMemberInitializer(
FieldsToInit.erase(Init->getMember());
}
}
removeFieldsInitializedInBody(*Body, Context, FieldsToInit);
removeFieldsInitializedInBody(*Ctor->getBody(), Context, FieldsToInit);

// Collect all fields in order, both direct fields and indirect fields from
// anonmyous record types.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,10 @@ struct PositiveAnonymousUnionAndStruct {
int X;
// CHECK-FIXES: int X{};
};

// This check results in a CXXConstructorDecl with no body.
struct NegativeDeletedConstructor : NegativeAggregateType {
NegativeDeletedConstructor() = delete;

Template<int> F;
};

0 comments on commit 257914e

Please sign in to comment.