Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion clang/lib/Analysis/FlowSensitive/Transfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,12 @@ class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {
if (LocSrc == nullptr || LocDst == nullptr)
return;

copyRecord(*LocSrc, *LocDst, Env);
// If the destination object here is of a derived class, `Arg0` may be a
// cast of that object to a base class, and the source object may be of a
// sibling derived class. To handle these cases, ensure we are copying
// only the fields for `Arg0`'s type, not the type of the underlying
// `RecordStorageLocation`.
copyRecord(*LocSrc, *LocDst, Env, Arg0->getType());

// The assignment operator can have an arbitrary return type. We model the
// return value only if the return type is the same as or a base class of
Expand Down
33 changes: 30 additions & 3 deletions clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1554,8 +1554,8 @@ TEST(TransferTest, BaseClassInitializerFromSiblingDerivedInstance) {
struct DerivedTwo : public Base {
int DerivedTwoField;

DerivedTwo(const DerivedOne& d1)
: Base(d1), DerivedTwoField(d1.DerivedOneField) {
DerivedTwo(const DerivedOne& D1)
: Base(D1), DerivedTwoField(D1.DerivedOneField) {
(void)BaseField;
}
};
Expand All @@ -1565,7 +1565,34 @@ TEST(TransferTest, BaseClassInitializerFromSiblingDerivedInstance) {
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
ASTContext &ASTCtx) {
// Regression test only; we used to crash when transferring the base
// class initializer from the DerivedToBase-cast `d1`.
// class initializer from the DerivedToBase-cast `D1`.
},
LangStandard::lang_cxx17, /*ApplyBuiltinTransfer=*/true, "DerivedTwo");
}

TEST(TransferTest, CopyAssignmentToDerivedToBase) {
std::string Code = R"cc(
struct Base {};

struct DerivedOne : public Base {
int DerivedOneField;
};

struct DerivedTwo : public Base {
int DerivedTwoField;

explicit DerivedTwo(const DerivedOne& D1) {
*static_cast<Base*>(this) = D1;
}
};
)cc";

runDataflow(
Code,
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
ASTContext &ASTCtx) {
// Regression test only; we used to crash when transferring the copy
// assignment operator in the constructor for `DerivedTwo`.
},
LangStandard::lang_cxx17, /*ApplyBuiltinTransfer=*/true, "DerivedTwo");
}
Expand Down