-
Notifications
You must be signed in to change notification settings - Fork 14.2k
[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
martinboehme
merged 1 commit into
llvm:main
from
martinboehme:piper_export_cl_646074374
Jun 26, 2024
Merged
[clang][nullability] Improve modeling of ++
/--
operators.
#96601
martinboehme
merged 1 commit into
llvm:main
from
martinboehme:piper_export_cl_646074374
Jun 26, 2024
Conversation
This file contains hidden or 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
We definitely know that these operations change the value of their operand, so clear out any value associated with it. We don't create a new value, instead leaving it to the analysis to do this if desired.
@llvm/pr-subscribers-clang-analysis @llvm/pr-subscribers-clang Author: None (martinboehme) ChangesWe definitely know that these operations change the value of their operand, so Full diff: https://github.com/llvm/llvm-project/pull/96601.diff 2 Files Affected:
diff --git a/clang/lib/Analysis/FlowSensitive/Transfer.cpp b/clang/lib/Analysis/FlowSensitive/Transfer.cpp
index 8109ac6a781e7..3c896d373a211 100644
--- a/clang/lib/Analysis/FlowSensitive/Transfer.cpp
+++ b/clang/lib/Analysis/FlowSensitive/Transfer.cpp
@@ -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;
diff --git a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
index cfbc64c77b0cc..e743eefa5d458 100644
--- a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
@@ -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);
});
}
|
Xazax-hun
approved these changes
Jun 25, 2024
lravenclaw
pushed a commit
to lravenclaw/llvm-project
that referenced
this pull request
Jul 3, 2024
…6601) We definitely know that these operations change the value of their operand, so clear out any value associated with it. We don't create a new value, instead leaving it to the analysis to do this if desired.
AlexisPerry
pushed a commit
to llvm-project-tlp/llvm-project
that referenced
this pull request
Jul 9, 2024
…6601) We definitely know that these operations change the value of their operand, so clear out any value associated with it. We don't create a new value, instead leaving it to the analysis to do this if desired.
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.
We definitely know that these operations change the value of their operand, so
clear out any value associated with it. We don't create a new value, instead
leaving it to the analysis to do this if desired.