-
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] Convert SpecialBoolAnalysis
to synthetic fields.
#74706
Merged
martinboehme
merged 1 commit into
llvm:main
from
martinboehme:piper_export_cl_588703718
Dec 11, 2023
Merged
[clang][dataflow] Convert SpecialBoolAnalysis
to synthetic fields.
#74706
martinboehme
merged 1 commit into
llvm:main
from
martinboehme:piper_export_cl_588703718
Dec 11, 2023
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
We're working towards eliminating `RecordValue`; this eliminates one more blocker on that path.
llvmbot
added
clang
Clang issues not falling into any other category
clang:dataflow
Clang Dataflow Analysis framework - https://clang.llvm.org/docs/DataFlowAnalysisIntro.html
labels
Dec 7, 2023
@llvm/pr-subscribers-clang Author: None (martinboehme) ChangesWe're working towards eliminating Full diff: https://github.com/llvm/llvm-project/pull/74706.diff 1 Files Affected:
diff --git a/clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp b/clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp
index f92afd8c3d84a..4c3cb322eacfb 100644
--- a/clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/TypeErasedDataflowAnalysisTest.cpp
@@ -514,8 +514,17 @@ TEST_F(NoreturnDestructorTest, ConditionalOperatorNestedBranchReturns) {
class SpecialBoolAnalysis final
: public DataflowAnalysis<SpecialBoolAnalysis, NoopLattice> {
public:
- explicit SpecialBoolAnalysis(ASTContext &Context)
- : DataflowAnalysis<SpecialBoolAnalysis, NoopLattice>(Context) {}
+ explicit SpecialBoolAnalysis(ASTContext &Context, Environment &Env)
+ : DataflowAnalysis<SpecialBoolAnalysis, NoopLattice>(Context) {
+ Env.getDataflowAnalysisContext().setSyntheticFieldCallback(
+ [](QualType Ty) -> llvm::StringMap<QualType> {
+ RecordDecl *RD = Ty->getAsRecordDecl();
+ if (RD == nullptr || RD->getIdentifier() == nullptr ||
+ RD->getName() != "SpecialBool")
+ return {};
+ return {{"is_set", RD->getASTContext().BoolTy}};
+ });
+ }
static NoopLattice initialElement() { return {}; }
@@ -530,67 +539,18 @@ class SpecialBoolAnalysis final
if (const auto *E = selectFirst<CXXConstructExpr>(
"call", match(cxxConstructExpr(HasSpecialBoolType).bind("call"), *S,
getASTContext()))) {
- cast<RecordValue>(Env.getValue(*E))
- ->setProperty("is_set", Env.getBoolLiteralValue(false));
+ Env.setValue(Env.getResultObjectLocation(*E).getSyntheticField("is_set"),
+ Env.getBoolLiteralValue(false));
} else if (const auto *E = selectFirst<CXXMemberCallExpr>(
"call", match(cxxMemberCallExpr(callee(cxxMethodDecl(ofClass(
SpecialBoolRecordDecl))))
.bind("call"),
*S, getASTContext()))) {
- auto &ObjectLoc =
- *cast<RecordStorageLocation>(getImplicitObjectLocation(*E, Env));
-
- refreshRecordValue(ObjectLoc, Env)
- .setProperty("is_set", Env.getBoolLiteralValue(true));
+ if (RecordStorageLocation *ObjectLoc = getImplicitObjectLocation(*E, Env))
+ Env.setValue(ObjectLoc->getSyntheticField("is_set"),
+ Env.getBoolLiteralValue(true));
}
}
-
- ComparisonResult compare(QualType Type, const Value &Val1,
- const Environment &Env1, const Value &Val2,
- const Environment &Env2) override {
- const auto *Decl = Type->getAsCXXRecordDecl();
- if (Decl == nullptr || Decl->getIdentifier() == nullptr ||
- Decl->getName() != "SpecialBool")
- return ComparisonResult::Unknown;
-
- auto *IsSet1 = cast_or_null<BoolValue>(Val1.getProperty("is_set"));
- auto *IsSet2 = cast_or_null<BoolValue>(Val2.getProperty("is_set"));
- if (IsSet1 == nullptr)
- return IsSet2 == nullptr ? ComparisonResult::Same
- : ComparisonResult::Different;
-
- if (IsSet2 == nullptr)
- return ComparisonResult::Different;
-
- return Env1.proves(IsSet1->formula()) == Env2.proves(IsSet2->formula())
- ? ComparisonResult::Same
- : ComparisonResult::Different;
- }
-
- // Always returns `true` to accept the `MergedVal`.
- bool merge(QualType Type, const Value &Val1, const Environment &Env1,
- const Value &Val2, const Environment &Env2, Value &MergedVal,
- Environment &MergedEnv) override {
- const auto *Decl = Type->getAsCXXRecordDecl();
- if (Decl == nullptr || Decl->getIdentifier() == nullptr ||
- Decl->getName() != "SpecialBool")
- return true;
-
- auto *IsSet1 = cast_or_null<BoolValue>(Val1.getProperty("is_set"));
- if (IsSet1 == nullptr)
- return true;
-
- auto *IsSet2 = cast_or_null<BoolValue>(Val2.getProperty("is_set"));
- if (IsSet2 == nullptr)
- return true;
-
- auto &IsSet = MergedEnv.makeAtomicBoolValue();
- MergedVal.setProperty("is_set", IsSet);
- if (Env1.proves(IsSet1->formula()) && Env2.proves(IsSet2->formula()))
- MergedEnv.assume(IsSet.formula());
-
- return true;
- }
};
class JoinFlowConditionsTest : public Test {
@@ -602,7 +562,7 @@ class JoinFlowConditionsTest : public Test {
AnalysisInputs<SpecialBoolAnalysis>(
Code, ast_matchers::hasName("target"),
[](ASTContext &Context, Environment &Env) {
- return SpecialBoolAnalysis(Context);
+ return SpecialBoolAnalysis(Context, Env);
})
.withASTBuildArgs({"-fsyntax-only", "-std=c++17"}),
/*VerifyResults=*/[&Match](const llvm::StringMap<
@@ -650,7 +610,9 @@ TEST_F(JoinFlowConditionsTest, JoinDistinctButProvablyEquivalentValues) {
ASSERT_THAT(FooDecl, NotNull());
auto GetFoo = [FooDecl](const Environment &Env) -> const Formula & {
- return cast<BoolValue>(Env.getValue(*FooDecl)->getProperty("is_set"))
+ auto *Loc =
+ cast<RecordStorageLocation>(Env.getStorageLocation(*FooDecl));
+ return cast<BoolValue>(Env.getValue(Loc->getSyntheticField("is_set")))
->formula();
};
|
Xazax-hun
approved these changes
Dec 9, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
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're working towards eliminating
RecordValue
; this eliminates one moreblocker on that path.