[clang] 27d5049 - [clang][dataflow] Fix `getResultObjectLocation()` on `CXXDefaultArgExpr`. (#85072)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 18 05:36:25 PDT 2024
Author: martinboehme
Date: 2024-03-18T13:36:20+01:00
New Revision: 27d504998ec7ec596bc9ff5d16333aea7a1bac18
URL: https://github.com/llvm/llvm-project/commit/27d504998ec7ec596bc9ff5d16333aea7a1bac18
DIFF: https://github.com/llvm/llvm-project/commit/27d504998ec7ec596bc9ff5d16333aea7a1bac18.diff
LOG: [clang][dataflow] Fix `getResultObjectLocation()` on `CXXDefaultArgExpr`. (#85072)
This patch includes a test that causes an assertion failure without the
other
changes in this patch.
Added:
Modified:
clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
clang/lib/Analysis/FlowSensitive/Transfer.cpp
clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
index 1d2bd9a9b08af3..cc1ebd511191a9 100644
--- a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
+++ b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
@@ -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
diff --git a/clang/lib/Analysis/FlowSensitive/Transfer.cpp b/clang/lib/Analysis/FlowSensitive/Transfer.cpp
index 04aa2831df0558..8b44bcb4e3caeb 100644
--- a/clang/lib/Analysis/FlowSensitive/Transfer.cpp
+++ b/clang/lib/Analysis/FlowSensitive/Transfer.cpp
@@ -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);
diff --git a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
index a8c282f140b4cd..86c7f32f0104be 100644
--- a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
@@ -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 {};
More information about the cfe-commits
mailing list