Skip to content

Commit 1bccbe1

Browse files
authored
[clang][dataflow] Treat BuiltinBitCastExpr correctly in PropagateResultObject(). (#88875)
This patch includes a test that assert-fails without the fix.
1 parent 4714883 commit 1bccbe1

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,11 @@ class ResultObjectVisitor : public RecursiveASTVisitor<ResultObjectVisitor> {
419419
// below them can initialize the same object (or part of it).
420420
if (isa<CXXConstructExpr>(E) || isa<CallExpr>(E) || isa<LambdaExpr>(E) ||
421421
isa<CXXDefaultArgExpr>(E) || isa<CXXDefaultInitExpr>(E) ||
422-
isa<CXXStdInitializerListExpr>(E)) {
422+
isa<CXXStdInitializerListExpr>(E) ||
423+
// We treat `BuiltinBitCastExpr` as an "original initializer" too as
424+
// it may not even be casting from a record type -- and even if it is,
425+
// the two objects are in general of unrelated type.
426+
isa<BuiltinBitCastExpr>(E)) {
423427
return;
424428
}
425429
if (auto *Op = dyn_cast<BinaryOperator>(E);

clang/unittests/Analysis/FlowSensitive/TransferTest.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3208,6 +3208,32 @@ TEST(TransferTest, ResultObjectLocationForStmtExpr) {
32083208
});
32093209
}
32103210

3211+
TEST(TransferTest, ResultObjectLocationForBuiltinBitCastExpr) {
3212+
std::string Code = R"(
3213+
struct S { int i; };
3214+
void target(int i) {
3215+
S s = __builtin_bit_cast(S, i);
3216+
// [[p]]
3217+
}
3218+
)";
3219+
using ast_matchers::explicitCastExpr;
3220+
using ast_matchers::match;
3221+
using ast_matchers::selectFirst;
3222+
using ast_matchers::traverse;
3223+
runDataflow(
3224+
Code,
3225+
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
3226+
ASTContext &ASTCtx) {
3227+
const Environment &Env = getEnvironmentAtAnnotation(Results, "p");
3228+
3229+
auto *BuiltinBitCast = selectFirst<BuiltinBitCastExpr>(
3230+
"cast", match(explicitCastExpr().bind("cast"), ASTCtx));
3231+
3232+
EXPECT_EQ(&Env.getResultObjectLocation(*BuiltinBitCast),
3233+
&getLocForDecl<RecordStorageLocation>(ASTCtx, Env, "s"));
3234+
});
3235+
}
3236+
32113237
TEST(TransferTest, ResultObjectLocationPropagatesThroughConditionalOperator) {
32123238
std::string Code = R"(
32133239
struct A {

0 commit comments

Comments
 (0)