aboutsummaryrefslogtreecommitdiff
path: root/clang
diff options
context:
space:
mode:
authormartinboehme <mboehme@google.com>2024-06-04 17:08:20 +0200
committerGitHub <noreply@github.com>2024-06-04 17:08:20 +0200
commit492417278d986ddd8206b2b9bba626ce690ea244 (patch)
treea1751a72271e92c35ae40553021331c6bffc7cbd /clang
parent2464f1cef3d28182da42debe6abf9bad8ab5a4d2 (diff)
downloadllvm-492417278d986ddd8206b2b9bba626ce690ea244.zip
llvm-492417278d986ddd8206b2b9bba626ce690ea244.tar.gz
llvm-492417278d986ddd8206b2b9bba626ce690ea244.tar.bz2
[clang][dataflow] Propagate storage location of compound assignment operators. (#94332)
To avoid generating unnecessary values, we don't create a new value but instead leave it to the specific analysis to do this if desired.
Diffstat (limited to 'clang')
-rw-r--r--clang/lib/Analysis/FlowSensitive/Transfer.cpp7
-rw-r--r--clang/unittests/Analysis/FlowSensitive/TransferTest.cpp20
2 files changed, 27 insertions, 0 deletions
diff --git a/clang/lib/Analysis/FlowSensitive/Transfer.cpp b/clang/lib/Analysis/FlowSensitive/Transfer.cpp
index d161b23..8109ac6 100644
--- a/clang/lib/Analysis/FlowSensitive/Transfer.cpp
+++ b/clang/lib/Analysis/FlowSensitive/Transfer.cpp
@@ -147,6 +147,13 @@ public:
const Expr *RHS = S->getRHS();
assert(RHS != nullptr);
+ // Do compound assignments up-front, as there are so many of them and we
+ // don't want to list all of them in the switch statement below.
+ // To avoid generating unnecessary values, we don't create a new value but
+ // instead leave it to the specific analysis to do this if desired.
+ if (S->isCompoundAssignmentOp())
+ propagateStorageLocation(*S->getLHS(), *S, Env);
+
switch (S->getOpcode()) {
case BO_Assign: {
auto *LHSLoc = Env.getStorageLocation(*LHS);
diff --git a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
index cdaee9b..f7e6b0c 100644
--- a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
@@ -3796,6 +3796,26 @@ TEST(TransferTest, Postincrement) {
});
}
+// We test just one of the compound assignment operators because we know the
+// code for propagating the storage location is shared among all of them.
+TEST(TransferTest, AddAssign) {
+ std::string Code = R"(
+ void target(int I) {
+ int &IRef = (I += 1);
+ // [[p]]
+ }
+ )";
+ runDataflow(
+ Code,
+ [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
+ ASTContext &ASTCtx) {
+ const Environment &Env = getEnvironmentAtAnnotation(Results, "p");
+
+ EXPECT_EQ(&getLocForDecl(ASTCtx, Env, "IRef"),
+ &getLocForDecl(ASTCtx, Env, "I"));
+ });
+}
+
TEST(TransferTest, CannotAnalyzeFunctionTemplate) {
std::string Code = R"(
template <typename T>