diff options
author | martinboehme <mboehme@google.com> | 2024-04-17 08:17:56 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-17 08:17:56 +0200 |
commit | 1bccbe1f49abc39b9f980cf3f1b171da5541d1a4 (patch) | |
tree | 65ec2f771c9887ca64631cdfb356494fe7e6a9a6 /clang | |
parent | 47148832d4e3bf4901430732f1af6673147accb2 (diff) | |
download | llvm-1bccbe1f49abc39b9f980cf3f1b171da5541d1a4.zip llvm-1bccbe1f49abc39b9f980cf3f1b171da5541d1a4.tar.gz llvm-1bccbe1f49abc39b9f980cf3f1b171da5541d1a4.tar.bz2 |
[clang][dataflow] Treat `BuiltinBitCastExpr` correctly in `PropagateResultObject()`. (#88875)
This patch includes a test that assert-fails without the fix.
Diffstat (limited to 'clang')
-rw-r--r-- | clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp | 6 | ||||
-rw-r--r-- | clang/unittests/Analysis/FlowSensitive/TransferTest.cpp | 26 |
2 files changed, 31 insertions, 1 deletions
diff --git a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp index f2b4a67..3f1600d 100644 --- a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp +++ b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp @@ -419,7 +419,11 @@ public: // below them can initialize the same object (or part of it). if (isa<CXXConstructExpr>(E) || isa<CallExpr>(E) || isa<LambdaExpr>(E) || isa<CXXDefaultArgExpr>(E) || isa<CXXDefaultInitExpr>(E) || - isa<CXXStdInitializerListExpr>(E)) { + isa<CXXStdInitializerListExpr>(E) || + // We treat `BuiltinBitCastExpr` as an "original initializer" too as + // it may not even be casting from a record type -- and even if it is, + // the two objects are in general of unrelated type. + isa<BuiltinBitCastExpr>(E)) { return; } if (auto *Op = dyn_cast<BinaryOperator>(E); diff --git a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp index d7a51b0..97ec321 100644 --- a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp +++ b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp @@ -3208,6 +3208,32 @@ TEST(TransferTest, ResultObjectLocationForStmtExpr) { }); } +TEST(TransferTest, ResultObjectLocationForBuiltinBitCastExpr) { + std::string Code = R"( + struct S { int i; }; + void target(int i) { + S s = __builtin_bit_cast(S, i); + // [[p]] + } + )"; + using ast_matchers::explicitCastExpr; + using ast_matchers::match; + using ast_matchers::selectFirst; + using ast_matchers::traverse; + runDataflow( + Code, + [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results, + ASTContext &ASTCtx) { + const Environment &Env = getEnvironmentAtAnnotation(Results, "p"); + + auto *BuiltinBitCast = selectFirst<BuiltinBitCastExpr>( + "cast", match(explicitCastExpr().bind("cast"), ASTCtx)); + + EXPECT_EQ(&Env.getResultObjectLocation(*BuiltinBitCast), + &getLocForDecl<RecordStorageLocation>(ASTCtx, Env, "s")); + }); +} + TEST(TransferTest, ResultObjectLocationPropagatesThroughConditionalOperator) { std::string Code = R"( struct A { |