-
Notifications
You must be signed in to change notification settings - Fork 10.8k
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
[clang][dataflow] Treat BuiltinBitCastExpr
correctly in PropagateResultObject()
.
#88875
Merged
martinboehme
merged 1 commit into
llvm:main
from
martinboehme:piper_export_cl_625278418
Apr 17, 2024
Merged
[clang][dataflow] Treat BuiltinBitCastExpr
correctly in PropagateResultObject()
.
#88875
martinboehme
merged 1 commit into
llvm:main
from
martinboehme:piper_export_cl_625278418
Apr 17, 2024
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
llvmbot
added
clang
Clang issues not falling into any other category
clang:dataflow
Clang Dataflow Analysis framework - https://clang.llvm.org/docs/DataFlowAnalysisIntro.html
clang:analysis
labels
Apr 16, 2024
@llvm/pr-subscribers-clang-analysis Author: None (martinboehme) ChangesThis patch includes a test that assert-fails without the fix. Full diff: https://github.com/llvm/llvm-project/pull/88875.diff 2 Files Affected:
diff --git a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
index ee2581143e1141..90eea420258764 100644
--- a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
+++ b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
@@ -505,7 +505,11 @@ class ResultObjectVisitor : public RecursiveASTVisitor<ResultObjectVisitor> {
// 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 d8bcc3da4b8b1c..54744a2762e773 100644
--- a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
@@ -3182,6 +3182,32 @@ TEST(TransferTest, ResultObjectLocationForStdInitializerListExpr) {
});
}
+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 {
|
@llvm/pr-subscribers-clang Author: None (martinboehme) ChangesThis patch includes a test that assert-fails without the fix. Full diff: https://github.com/llvm/llvm-project/pull/88875.diff 2 Files Affected:
diff --git a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
index ee2581143e1141..90eea420258764 100644
--- a/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
+++ b/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
@@ -505,7 +505,11 @@ class ResultObjectVisitor : public RecursiveASTVisitor<ResultObjectVisitor> {
// 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 d8bcc3da4b8b1c..54744a2762e773 100644
--- a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
@@ -3182,6 +3182,32 @@ TEST(TransferTest, ResultObjectLocationForStdInitializerListExpr) {
});
}
+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 {
|
ymand
approved these changes
Apr 16, 2024
Xazax-hun
approved these changes
Apr 16, 2024
…esultObject()`. This patch includes a test that assert-fails without the fix.
martinboehme
force-pushed
the
piper_export_cl_625278418
branch
from
April 17, 2024 06:17
c8797bb
to
0cacc5b
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
clang:analysis
clang:dataflow
Clang Dataflow Analysis framework - https://clang.llvm.org/docs/DataFlowAnalysisIntro.html
clang
Clang issues not falling into any other category
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This patch includes a test that assert-fails without the fix.