Skip to content

Commit c8797bb

Browse files
committed
[clang][dataflow] Treat BuiltinBitCastExpr correctly in PropagateResultObject().
This patch includes a test that assert-fails without the fix.
1 parent 36b3c26 commit c8797bb

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
@@ -505,7 +505,11 @@ class ResultObjectVisitor : public RecursiveASTVisitor<ResultObjectVisitor> {
505505
// below them can initialize the same object (or part of it).
506506
if (isa<CXXConstructExpr>(E) || isa<CallExpr>(E) || isa<LambdaExpr>(E) ||
507507
isa<CXXDefaultArgExpr>(E) || isa<CXXDefaultInitExpr>(E) ||
508-
isa<CXXStdInitializerListExpr>(E)) {
508+
isa<CXXStdInitializerListExpr>(E) ||
509+
// We treat `BuiltinBitCastExpr` as an "original initializer" too as
510+
// it may not even be casting from a record type -- and even if it is,
511+
// the two objects are in general of unrelated type.
512+
isa<BuiltinBitCastExpr>(E)) {
509513
return;
510514
}
511515
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
@@ -3182,6 +3182,32 @@ TEST(TransferTest, ResultObjectLocationForStdInitializerListExpr) {
31823182
});
31833183
}
31843184

3185+
TEST(TransferTest, ResultObjectLocationForBuiltinBitCastExpr) {
3186+
std::string Code = R"(
3187+
struct S { int i; };
3188+
void target(int i) {
3189+
S s = __builtin_bit_cast(S, i);
3190+
// [[p]]
3191+
}
3192+
)";
3193+
using ast_matchers::explicitCastExpr;
3194+
using ast_matchers::match;
3195+
using ast_matchers::selectFirst;
3196+
using ast_matchers::traverse;
3197+
runDataflow(
3198+
Code,
3199+
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
3200+
ASTContext &ASTCtx) {
3201+
const Environment &Env = getEnvironmentAtAnnotation(Results, "p");
3202+
3203+
auto *BuiltinBitCast = selectFirst<BuiltinBitCastExpr>(
3204+
"cast", match(explicitCastExpr().bind("cast"), ASTCtx));
3205+
3206+
EXPECT_EQ(&Env.getResultObjectLocation(*BuiltinBitCast),
3207+
&getLocForDecl<RecordStorageLocation>(ASTCtx, Env, "s"));
3208+
});
3209+
}
3210+
31853211
TEST(TransferTest, ResultObjectLocationPropagatesThroughConditionalOperator) {
31863212
std::string Code = R"(
31873213
struct A {

0 commit comments

Comments
 (0)