-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[FlowSensitive] [PtrCaching] optionally pass Type to initializer #164030
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
Open
fmayer
wants to merge
1
commit into
main
Choose a base branch
from
users/fmayer/spr/flowsensitive-ptrcaching-optionally-pass-type-to-initializer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[FlowSensitive] [PtrCaching] optionally pass Type to initializer #164030
fmayer
wants to merge
1
commit into
main
from
users/fmayer/spr/flowsensitive-ptrcaching-optionally-pass-type-to-initializer
+62
−6
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
Created using spr 1.3.7
@llvm/pr-subscribers-clang @llvm/pr-subscribers-clang-analysis Author: Florian Mayer (fmayer) ChangesThis makes it easier to write the initialization function, because the Full diff: https://github.com/llvm/llvm-project/pull/164030.diff 3 Files Affected:
diff --git a/clang/include/clang/Analysis/FlowSensitive/CachedConstAccessorsLattice.h b/clang/include/clang/Analysis/FlowSensitive/CachedConstAccessorsLattice.h
index 6496771ad037e..4e316aaf09bdb 100644
--- a/clang/include/clang/Analysis/FlowSensitive/CachedConstAccessorsLattice.h
+++ b/clang/include/clang/Analysis/FlowSensitive/CachedConstAccessorsLattice.h
@@ -73,6 +73,10 @@ template <typename Base> class CachedConstAccessorsLattice : public Base {
///
/// - `Callee` should return a location (return type is a reference type or a
/// record type).
+ StorageLocation &getOrCreateConstMethodReturnStorageLocation(
+ const RecordStorageLocation &RecordLoc, const FunctionDecl *Callee,
+ Environment &Env,
+ llvm::function_ref<void(QualType, StorageLocation &)> Initialize);
StorageLocation &getOrCreateConstMethodReturnStorageLocation(
const RecordStorageLocation &RecordLoc, const FunctionDecl *Callee,
Environment &Env, llvm::function_ref<void(StorageLocation &)> Initialize);
@@ -196,7 +200,8 @@ template <typename Base>
StorageLocation &
CachedConstAccessorsLattice<Base>::getOrCreateConstMethodReturnStorageLocation(
const RecordStorageLocation &RecordLoc, const FunctionDecl *Callee,
- Environment &Env, llvm::function_ref<void(StorageLocation &)> Initialize) {
+ Environment &Env,
+ llvm::function_ref<void(QualType, StorageLocation &)> Initialize) {
assert(Callee != nullptr);
QualType Type = Callee->getReturnType();
assert(!Type.isNull());
@@ -206,13 +211,24 @@ CachedConstAccessorsLattice<Base>::getOrCreateConstMethodReturnStorageLocation(
if (it != ObjMap.end())
return *it->second;
+ auto T = Type.getNonReferenceType();
StorageLocation &Loc = Env.createStorageLocation(Type.getNonReferenceType());
- Initialize(Loc);
+ Initialize(T, Loc);
ObjMap.insert({Callee, &Loc});
return Loc;
}
+template <typename Base>
+StorageLocation &
+CachedConstAccessorsLattice<Base>::getOrCreateConstMethodReturnStorageLocation(
+ const RecordStorageLocation &RecordLoc, const FunctionDecl *Callee,
+ Environment &Env, llvm::function_ref<void(StorageLocation &)> Initialize) {
+ return getOrCreateConstMethodReturnStorageLocation(
+ RecordLoc, Callee, Env,
+ [Initialize](QualType T, StorageLocation &Loc) { Initialize(Loc); });
+}
+
} // namespace dataflow
} // namespace clang
diff --git a/clang/include/clang/Analysis/FlowSensitive/SmartPointerAccessorCaching.h b/clang/include/clang/Analysis/FlowSensitive/SmartPointerAccessorCaching.h
index e55b83aa845d4..b5bdff2df8ed6 100644
--- a/clang/include/clang/Analysis/FlowSensitive/SmartPointerAccessorCaching.h
+++ b/clang/include/clang/Analysis/FlowSensitive/SmartPointerAccessorCaching.h
@@ -91,7 +91,18 @@ template <typename LatticeT>
void transferSmartPointerLikeCachedDeref(
const CallExpr *DerefExpr, RecordStorageLocation *SmartPointerLoc,
TransferState<LatticeT> &State,
- llvm::function_ref<void(StorageLocation &)> InitializeLoc);
+ llvm::function_ref<void(QualType, StorageLocation &)> InitializeLoc);
+template <typename LatticeT>
+void transferSmartPointerLikeCachedDeref(
+ const CallExpr *DerefExpr, RecordStorageLocation *SmartPointerLoc,
+ TransferState<LatticeT> &State,
+ llvm::function_ref<void(StorageLocation &)> InitializeLoc) {
+ transferSmartPointerLikeCachedDeref<LatticeT>(
+ DerefExpr, SmartPointerLoc, State,
+ [InitializeLoc](QualType T, StorageLocation &Loc) {
+ InitializeLoc(Loc);
+ });
+}
/// A transfer function for `operator->` (and `get`) calls that can be cached.
/// Runs the `InitializeLoc` callback to initialize any new StorageLocations.
@@ -103,13 +114,24 @@ template <typename LatticeT>
void transferSmartPointerLikeCachedGet(
const CallExpr *GetExpr, RecordStorageLocation *SmartPointerLoc,
TransferState<LatticeT> &State,
- llvm::function_ref<void(StorageLocation &)> InitializeLoc);
+ llvm::function_ref<void(QualType, StorageLocation &)> InitializeLoc);
+template <typename LatticeT>
+void transferSmartPointerLikeCachedGet(
+ const CallExpr *GetExpr, RecordStorageLocation *SmartPointerLoc,
+ TransferState<LatticeT> &State,
+ llvm::function_ref<void(StorageLocation &)> InitializeLoc) {
+ transferSmartPointerLikeCachedGet<LatticeT>(
+ GetExpr, SmartPointerLoc, State,
+ [InitializeLoc](QualType T, StorageLocation &Loc) {
+ InitializeLoc(Loc);
+ });
+}
template <typename LatticeT>
void transferSmartPointerLikeCachedDeref(
const CallExpr *DerefExpr, RecordStorageLocation *SmartPointerLoc,
TransferState<LatticeT> &State,
- llvm::function_ref<void(StorageLocation &)> InitializeLoc) {
+ llvm::function_ref<void(QualType, StorageLocation &)> InitializeLoc) {
if (State.Env.getStorageLocation(*DerefExpr) != nullptr)
return;
if (SmartPointerLoc == nullptr)
@@ -145,7 +167,7 @@ template <typename LatticeT>
void transferSmartPointerLikeCachedGet(
const CallExpr *GetExpr, RecordStorageLocation *SmartPointerLoc,
TransferState<LatticeT> &State,
- llvm::function_ref<void(StorageLocation &)> InitializeLoc) {
+ llvm::function_ref<void(QualType, StorageLocation &)> InitializeLoc) {
if (SmartPointerLoc == nullptr)
return;
diff --git a/clang/unittests/Analysis/FlowSensitive/CachedConstAccessorsLatticeTest.cpp b/clang/unittests/Analysis/FlowSensitive/CachedConstAccessorsLatticeTest.cpp
index 67b471e328b5e..5a195ed6cfc32 100644
--- a/clang/unittests/Analysis/FlowSensitive/CachedConstAccessorsLatticeTest.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/CachedConstAccessorsLatticeTest.cpp
@@ -307,5 +307,23 @@ TEST_F(CachedConstAccessorsLatticeTest, ProducesNewValueAfterJoinDistinct) {
EXPECT_NE(ValAfterJoin2, Val3);
}
+TEST_F(CachedConstAccessorsLatticeTest, TypePassed) {
+ CommonTestInputs Inputs;
+ auto *CE = Inputs.CallRef;
+ RecordStorageLocation Loc(Inputs.SType, RecordStorageLocation::FieldToLoc(),
+ {});
+
+ LatticeT Lattice;
+
+ const FunctionDecl *Callee = CE->getDirectCallee();
+ auto RetType = Callee->getReturnType().getNonReferenceType();
+ auto CheckedInit = [RetType](QualType T, StorageLocation &) {
+ ASSERT_EQ(T, RetType);
+ };
+ ASSERT_NE(Callee, nullptr);
+ Lattice.getOrCreateConstMethodReturnStorageLocation(Loc, Callee, Env,
+ CheckedInit);
+}
+
} // namespace
} // namespace clang::dataflow
|
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.
This makes it easier to write the initialization function, because the
callee does not have to find out the type again.