Skip to content

[clang][dataflow] Fix getResultObjectLocation() on CXXDefaultArgExpr. #85072

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 2 commits into from
Mar 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 @@ -771,6 +771,7 @@ static bool isOriginalRecordConstructor(const Expr &RecordPRValue) {
return !Init->isSemanticForm() || !Init->isTransparent();
return isa<CXXConstructExpr>(RecordPRValue) || isa<CallExpr>(RecordPRValue) ||
isa<LambdaExpr>(RecordPRValue) ||
isa<CXXDefaultArgExpr>(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
Expand Down
19 changes: 19 additions & 0 deletions clang/lib/Analysis/FlowSensitive/Transfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,25 @@ class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {
Env.setStorageLocation(*S, *MemberLoc);
}

void VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *S) {
const Expr *ArgExpr = S->getExpr();
assert(ArgExpr != nullptr);
propagateValueOrStorageLocation(*ArgExpr, *S, Env);

// If this is a prvalue of record type, we consider it to be an "original
// record constructor", which we always require to have a `RecordValue`.
// So make sure we have a value if we didn't propagate one above.
if (S->isPRValue() && S->getType()->isRecordType()) {
if (Env.getValue(*S) == nullptr) {
Value *Val = Env.createValue(S->getType());
// We're guaranteed to always be able to create a value for record
// types.
assert(Val != nullptr);
Env.setValue(*S, *Val);
}
}
}

void VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *S) {
const Expr *InitExpr = S->getExpr();
assert(InitExpr != nullptr);
Expand Down
30 changes: 30 additions & 0 deletions clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2924,6 +2924,36 @@ TEST(TransferTest, ResultObjectLocation) {
});
}

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

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

auto *DefaultArg = selectFirst<CXXDefaultArgExpr>(
"default_arg",
match(cxxDefaultArgExpr().bind("default_arg"), ASTCtx));
ASSERT_NE(DefaultArg, nullptr);

// The values for default arguments aren't modeled; we merely verify
// that we can get a result object location for a default arg.
Env.getResultObjectLocation(*DefaultArg);
});
}

TEST(TransferTest, ResultObjectLocationForDefaultInitExpr) {
std::string Code = R"(
struct S {};
Expand Down