Skip to content

Commit 68761a9

Browse files
authored
[clang][nullability] Propagate storage location / value of ++/-- operators. (#94217)
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.
1 parent deab451 commit 68761a9

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

clang/lib/Analysis/FlowSensitive/Transfer.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,20 @@ class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {
382382
Env.setValue(*S, Env.makeNot(*SubExprVal));
383383
break;
384384
}
385+
case UO_PreInc:
386+
case UO_PreDec:
387+
// Propagate the storage location, but don't create a new value; to
388+
// avoid generating unnecessary values, we leave it to the specific
389+
// analysis to do this if desired.
390+
propagateStorageLocation(*S->getSubExpr(), *S, Env);
391+
break;
392+
case UO_PostInc:
393+
case UO_PostDec:
394+
// Propagate the old value, but don't create a new value; to avoid
395+
// generating unnecessary values, we leave it to the specific analysis
396+
// to do this if desired.
397+
propagateValue(*S->getSubExpr(), *S, Env);
398+
break;
385399
default:
386400
break;
387401
}

clang/unittests/Analysis/FlowSensitive/TransferTest.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3760,6 +3760,42 @@ TEST(TransferTest, AddrOfReference) {
37603760
});
37613761
}
37623762

3763+
TEST(TransferTest, Preincrement) {
3764+
std::string Code = R"(
3765+
void target(int I) {
3766+
int &IRef = ++I;
3767+
// [[p]]
3768+
}
3769+
)";
3770+
runDataflow(
3771+
Code,
3772+
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
3773+
ASTContext &ASTCtx) {
3774+
const Environment &Env = getEnvironmentAtAnnotation(Results, "p");
3775+
3776+
EXPECT_EQ(&getLocForDecl(ASTCtx, Env, "IRef"),
3777+
&getLocForDecl(ASTCtx, Env, "I"));
3778+
});
3779+
}
3780+
3781+
TEST(TransferTest, Postincrement) {
3782+
std::string Code = R"(
3783+
void target(int I) {
3784+
int OldVal = I++;
3785+
// [[p]]
3786+
}
3787+
)";
3788+
runDataflow(
3789+
Code,
3790+
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
3791+
ASTContext &ASTCtx) {
3792+
const Environment &Env = getEnvironmentAtAnnotation(Results, "p");
3793+
3794+
EXPECT_EQ(&getValueForDecl(ASTCtx, Env, "OldVal"),
3795+
&getValueForDecl(ASTCtx, Env, "I"));
3796+
});
3797+
}
3798+
37633799
TEST(TransferTest, CannotAnalyzeFunctionTemplate) {
37643800
std::string Code = R"(
37653801
template <typename T>

0 commit comments

Comments
 (0)