Skip to content

Commit 2724faa

Browse files
committed
[𝘀𝗽𝗿] initial version
Created using spr 1.3.7
1 parent 01c0cb9 commit 2724faa

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

clang/include/clang/Analysis/FlowSensitive/CachedConstAccessorsLattice.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ template <typename Base> class CachedConstAccessorsLattice : public Base {
7373
///
7474
/// - `Callee` should return a location (return type is a reference type or a
7575
/// record type).
76+
StorageLocation &getOrCreateConstMethodReturnStorageLocation(
77+
const RecordStorageLocation &RecordLoc, const FunctionDecl *Callee,
78+
Environment &Env,
79+
llvm::function_ref<void(QualType, StorageLocation &)> Initialize);
7680
StorageLocation &getOrCreateConstMethodReturnStorageLocation(
7781
const RecordStorageLocation &RecordLoc, const FunctionDecl *Callee,
7882
Environment &Env, llvm::function_ref<void(StorageLocation &)> Initialize);
@@ -196,7 +200,8 @@ template <typename Base>
196200
StorageLocation &
197201
CachedConstAccessorsLattice<Base>::getOrCreateConstMethodReturnStorageLocation(
198202
const RecordStorageLocation &RecordLoc, const FunctionDecl *Callee,
199-
Environment &Env, llvm::function_ref<void(StorageLocation &)> Initialize) {
203+
Environment &Env,
204+
llvm::function_ref<void(QualType, StorageLocation &)> Initialize) {
200205
assert(Callee != nullptr);
201206
QualType Type = Callee->getReturnType();
202207
assert(!Type.isNull());
@@ -206,13 +211,24 @@ CachedConstAccessorsLattice<Base>::getOrCreateConstMethodReturnStorageLocation(
206211
if (it != ObjMap.end())
207212
return *it->second;
208213

214+
auto T = Type.getNonReferenceType();
209215
StorageLocation &Loc = Env.createStorageLocation(Type.getNonReferenceType());
210-
Initialize(Loc);
216+
Initialize(T, Loc);
211217

212218
ObjMap.insert({Callee, &Loc});
213219
return Loc;
214220
}
215221

222+
template <typename Base>
223+
StorageLocation &
224+
CachedConstAccessorsLattice<Base>::getOrCreateConstMethodReturnStorageLocation(
225+
const RecordStorageLocation &RecordLoc, const FunctionDecl *Callee,
226+
Environment &Env, llvm::function_ref<void(StorageLocation &)> Initialize) {
227+
return getOrCreateConstMethodReturnStorageLocation(
228+
RecordLoc, Callee, Env,
229+
[Initialize](QualType T, StorageLocation &Loc) { Initialize(Loc); });
230+
}
231+
216232
} // namespace dataflow
217233
} // namespace clang
218234

clang/include/clang/Analysis/FlowSensitive/SmartPointerAccessorCaching.h

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,18 @@ template <typename LatticeT>
9191
void transferSmartPointerLikeCachedDeref(
9292
const CallExpr *DerefExpr, RecordStorageLocation *SmartPointerLoc,
9393
TransferState<LatticeT> &State,
94-
llvm::function_ref<void(StorageLocation &)> InitializeLoc);
94+
llvm::function_ref<void(QualType, StorageLocation &)> InitializeLoc);
95+
template <typename LatticeT>
96+
void transferSmartPointerLikeCachedDeref(
97+
const CallExpr *DerefExpr, RecordStorageLocation *SmartPointerLoc,
98+
TransferState<LatticeT> &State,
99+
llvm::function_ref<void(StorageLocation &)> InitializeLoc) {
100+
transferSmartPointerLikeCachedDeref<LatticeT>(
101+
DerefExpr, SmartPointerLoc, State,
102+
[InitializeLoc](QualType T, StorageLocation &Loc) {
103+
InitializeLoc(Loc);
104+
});
105+
}
95106

96107
/// A transfer function for `operator->` (and `get`) calls that can be cached.
97108
/// Runs the `InitializeLoc` callback to initialize any new StorageLocations.
@@ -103,13 +114,24 @@ template <typename LatticeT>
103114
void transferSmartPointerLikeCachedGet(
104115
const CallExpr *GetExpr, RecordStorageLocation *SmartPointerLoc,
105116
TransferState<LatticeT> &State,
106-
llvm::function_ref<void(StorageLocation &)> InitializeLoc);
117+
llvm::function_ref<void(QualType, StorageLocation &)> InitializeLoc);
118+
template <typename LatticeT>
119+
void transferSmartPointerLikeCachedGet(
120+
const CallExpr *GetExpr, RecordStorageLocation *SmartPointerLoc,
121+
TransferState<LatticeT> &State,
122+
llvm::function_ref<void(StorageLocation &)> InitializeLoc) {
123+
transferSmartPointerLikeCachedGet<LatticeT>(
124+
GetExpr, SmartPointerLoc, State,
125+
[InitializeLoc](QualType T, StorageLocation &Loc) {
126+
InitializeLoc(Loc);
127+
});
128+
}
107129

108130
template <typename LatticeT>
109131
void transferSmartPointerLikeCachedDeref(
110132
const CallExpr *DerefExpr, RecordStorageLocation *SmartPointerLoc,
111133
TransferState<LatticeT> &State,
112-
llvm::function_ref<void(StorageLocation &)> InitializeLoc) {
134+
llvm::function_ref<void(QualType, StorageLocation &)> InitializeLoc) {
113135
if (State.Env.getStorageLocation(*DerefExpr) != nullptr)
114136
return;
115137
if (SmartPointerLoc == nullptr)
@@ -145,7 +167,7 @@ template <typename LatticeT>
145167
void transferSmartPointerLikeCachedGet(
146168
const CallExpr *GetExpr, RecordStorageLocation *SmartPointerLoc,
147169
TransferState<LatticeT> &State,
148-
llvm::function_ref<void(StorageLocation &)> InitializeLoc) {
170+
llvm::function_ref<void(QualType, StorageLocation &)> InitializeLoc) {
149171
if (SmartPointerLoc == nullptr)
150172
return;
151173

clang/unittests/Analysis/FlowSensitive/CachedConstAccessorsLatticeTest.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,5 +307,23 @@ TEST_F(CachedConstAccessorsLatticeTest, ProducesNewValueAfterJoinDistinct) {
307307
EXPECT_NE(ValAfterJoin2, Val3);
308308
}
309309

310+
TEST_F(CachedConstAccessorsLatticeTest, TypePassed) {
311+
CommonTestInputs Inputs;
312+
auto *CE = Inputs.CallRef;
313+
RecordStorageLocation Loc(Inputs.SType, RecordStorageLocation::FieldToLoc(),
314+
{});
315+
316+
LatticeT Lattice;
317+
318+
const FunctionDecl *Callee = CE->getDirectCallee();
319+
auto RetType = Callee->getReturnType().getNonReferenceType();
320+
auto CheckedInit = [RetType](QualType T, StorageLocation &) {
321+
ASSERT_EQ(T, RetType);
322+
};
323+
ASSERT_NE(Callee, nullptr);
324+
Lattice.getOrCreateConstMethodReturnStorageLocation(Loc, Callee, Env,
325+
CheckedInit);
326+
}
327+
310328
} // namespace
311329
} // namespace clang::dataflow

0 commit comments

Comments
 (0)