Skip to content

[clang][dataflow] Consider CXXDefaultInitExpr to be an "original record ctor". #78423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 18, 2024
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
1 change: 1 addition & 0 deletions clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,7 @@ static bool isOriginalRecordConstructor(const Expr &RecordPRValue) {
return !Init->isSemanticForm() || !Init->isTransparent();
return isa<CXXConstructExpr>(RecordPRValue) || isa<CallExpr>(RecordPRValue) ||
isa<LambdaExpr>(RecordPRValue) ||
isa<CXXDefaultInitExpr>(RecordPRValue) ||
// The framework currently does not propagate the objects created in
// the two branches of a `ConditionalOperator` because there is no way
// to reconcile their storage locations, which are different. We
Expand Down
42 changes: 42 additions & 0 deletions clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2684,6 +2684,48 @@ TEST(TransferTest, ResultObjectLocation) {
});
}

TEST(TransferTest, ResultObjectLocationForDefaultInitExpr) {
std::string Code = R"(
struct S {};
struct target {
target () {
(void)0;
// [[p]]
}
S s = {};
};
)";

using ast_matchers::cxxCtorInitializer;
using ast_matchers::match;
using ast_matchers::selectFirst;
runDataflow(
Code,
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
ASTContext &ASTCtx) {
const Environment &Env = getEnvironmentAtAnnotation(Results, "p");

const ValueDecl *SField = findValueDecl(ASTCtx, "s");

auto *CtorInit = selectFirst<CXXCtorInitializer>(
"ctor_initializer",
match(cxxCtorInitializer().bind("ctor_initializer"), ASTCtx));
ASSERT_NE(CtorInit, nullptr);

auto *DefaultInit = cast<CXXDefaultInitExpr>(CtorInit->getInit());

RecordStorageLocation &Loc = Env.getResultObjectLocation(*DefaultInit);

// FIXME: The result object location for the `CXXDefaultInitExpr` should
// be the location of the member variable being initialized, but we
// don't do this correctly yet; see also comments in
// `builtinTransferInitializer()`.
// For the time being, we just document the current erroneous behavior
// here (this should be `EXPECT_EQ` when the behavior is fixed).
EXPECT_NE(&Loc, Env.getThisPointeeStorageLocation()->getChild(*SField));
});
}

TEST(TransferTest, StaticCast) {
std::string Code = R"(
void target(int Foo) {
Expand Down