Skip to content

[clang][nullability] Improve modeling of ++/-- operators. #96601

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

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions clang/lib/Analysis/FlowSensitive/Transfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,17 +391,22 @@ class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {
}
case UO_PreInc:
case UO_PreDec:
// Propagate the storage location, but don't create a new value; to
// avoid generating unnecessary values, we leave it to the specific
// analysis to do this if desired.
// Propagate the storage location and clear out any value associated with
// it (to represent the fact that the value has definitely changed).
// To avoid generating unnecessary values, we leave it to the specific
// analysis to create a new value if desired.
propagateStorageLocation(*S->getSubExpr(), *S, Env);
if (StorageLocation *Loc = Env.getStorageLocation(*S->getSubExpr()))
Env.clearValue(*Loc);
break;
case UO_PostInc:
case UO_PostDec:
// Propagate the old value, but don't create a new value; to avoid
// generating unnecessary values, we leave it to the specific analysis
// to do this if desired.
// Propagate the old value, then clear out any value associated with the
// storage location (to represent the fact that the value has definitely
// changed). See above for rationale.
propagateValue(*S->getSubExpr(), *S, Env);
if (StorageLocation *Loc = Env.getStorageLocation(*S->getSubExpr()))
Env.clearValue(*Loc);
break;
default:
break;
Expand Down
31 changes: 23 additions & 8 deletions clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3789,36 +3789,51 @@ TEST(TransferTest, AddrOfReference) {
TEST(TransferTest, Preincrement) {
std::string Code = R"(
void target(int I) {
(void)0; // [[before]]
int &IRef = ++I;
// [[p]]
// [[after]]
}
)";
runDataflow(
Code,
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
ASTContext &ASTCtx) {
const Environment &Env = getEnvironmentAtAnnotation(Results, "p");
const Environment &EnvBefore =
getEnvironmentAtAnnotation(Results, "before");
const Environment &EnvAfter =
getEnvironmentAtAnnotation(Results, "after");

EXPECT_EQ(&getLocForDecl(ASTCtx, Env, "IRef"),
&getLocForDecl(ASTCtx, Env, "I"));
EXPECT_EQ(&getLocForDecl(ASTCtx, EnvAfter, "IRef"),
&getLocForDecl(ASTCtx, EnvBefore, "I"));

const ValueDecl *IDecl = findValueDecl(ASTCtx, "I");
EXPECT_NE(EnvBefore.getValue(*IDecl), nullptr);
EXPECT_EQ(EnvAfter.getValue(*IDecl), nullptr);
});
}

TEST(TransferTest, Postincrement) {
std::string Code = R"(
void target(int I) {
(void)0; // [[before]]
int OldVal = I++;
// [[p]]
// [[after]]
}
)";
runDataflow(
Code,
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
ASTContext &ASTCtx) {
const Environment &Env = getEnvironmentAtAnnotation(Results, "p");
const Environment &EnvBefore =
getEnvironmentAtAnnotation(Results, "before");
const Environment &EnvAfter =
getEnvironmentAtAnnotation(Results, "after");

EXPECT_EQ(&getValueForDecl(ASTCtx, EnvBefore, "I"),
&getValueForDecl(ASTCtx, EnvAfter, "OldVal"));

EXPECT_EQ(&getValueForDecl(ASTCtx, Env, "OldVal"),
&getValueForDecl(ASTCtx, Env, "I"));
const ValueDecl *IDecl = findValueDecl(ASTCtx, "I");
EXPECT_EQ(EnvAfter.getValue(*IDecl), nullptr);
});
}

Expand Down
Loading