Skip to content

Commit 8a4c40b

Browse files
committed
[clang][dataflow] Store DeclContext of block being analysed in Environment if available.
Differential Revision: https://reviews.llvm.org/D131065
1 parent 286d59e commit 8a4c40b

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

clang/include/clang/Analysis/FlowSensitive/DataflowEnvironment.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,13 @@ class Environment {
347347
/// imply that `Val` is true.
348348
bool flowConditionImplies(BoolValue &Val) const;
349349

350+
/// Returns the `DeclContext` of the block being analysed, if any. Otherwise,
351+
/// returns null.
352+
const DeclContext *getDeclCtx() { return DeclCtx; }
353+
354+
/// Sets the `DeclContext` of the block being analysed.
355+
void setDeclCtx(const DeclContext *Ctx) { DeclCtx = Ctx; }
356+
350357
/// Returns the `ControlFlowContext` registered for `F`, if any. Otherwise,
351358
/// returns null.
352359
const ControlFlowContext *getControlFlowContext(const FunctionDecl *F) {
@@ -377,6 +384,9 @@ class Environment {
377384
// `DACtx` is not null and not owned by this object.
378385
DataflowAnalysisContext *DACtx;
379386

387+
// `DeclContext` of the block being analysed if provided.
388+
const DeclContext *DeclCtx;
389+
380390
// In a properly initialized `Environment`, `ReturnLoc` should only be null if
381391
// its `DeclContext` could not be cast to a `FunctionDecl`.
382392
StorageLocation *ReturnLoc = nullptr;

clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ Environment::Environment(DataflowAnalysisContext &DACtx)
154154
: DACtx(&DACtx), FlowConditionToken(&DACtx.makeFlowConditionToken()) {}
155155

156156
Environment::Environment(const Environment &Other)
157-
: DACtx(Other.DACtx), ReturnLoc(Other.ReturnLoc),
157+
: DACtx(Other.DACtx), DeclCtx(Other.DeclCtx), ReturnLoc(Other.ReturnLoc),
158158
ThisPointeeLoc(Other.ThisPointeeLoc), DeclToLoc(Other.DeclToLoc),
159159
ExprToLoc(Other.ExprToLoc), LocToVal(Other.LocToVal),
160160
MemberLocToStruct(Other.MemberLocToStruct),
@@ -168,9 +168,11 @@ Environment &Environment::operator=(const Environment &Other) {
168168
}
169169

170170
Environment::Environment(DataflowAnalysisContext &DACtx,
171-
const DeclContext &DeclCtx)
171+
const DeclContext &DeclCtxArg)
172172
: Environment(DACtx) {
173-
if (const auto *FuncDecl = dyn_cast<FunctionDecl>(&DeclCtx)) {
173+
setDeclCtx(&DeclCtxArg);
174+
175+
if (const auto *FuncDecl = dyn_cast<FunctionDecl>(DeclCtx)) {
174176
assert(FuncDecl->getBody() != nullptr);
175177
initGlobalVars(*FuncDecl->getBody(), *this);
176178
for (const auto *ParamDecl : FuncDecl->parameters()) {
@@ -185,7 +187,7 @@ Environment::Environment(DataflowAnalysisContext &DACtx,
185187
ReturnLoc = &createStorageLocation(ReturnType);
186188
}
187189

188-
if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(&DeclCtx)) {
190+
if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(DeclCtx)) {
189191
auto *Parent = MethodDecl->getParent();
190192
assert(Parent != nullptr);
191193
if (Parent->isLambda())
@@ -210,6 +212,9 @@ Environment Environment::pushCall(const CallExpr *Call) const {
210212

211213
const auto *FuncDecl = Call->getDirectCallee();
212214
assert(FuncDecl != nullptr);
215+
216+
Env.setDeclCtx(FuncDecl);
217+
213218
// FIXME: In order to allow the callee to reference globals, we probably need
214219
// to call `initGlobalVars` here in some way.
215220

@@ -252,12 +257,12 @@ Environment Environment::pushCall(const CallExpr *Call) const {
252257

253258
void Environment::popCall(const Environment &CalleeEnv) {
254259
// We ignore `DACtx` because it's already the same in both. We don't want the
255-
// callee's `ReturnLoc` or `ThisPointeeLoc`. We don't bring back `DeclToLoc`
256-
// and `ExprToLoc` because we want to be able to later analyze the same callee
257-
// in a different context, and `setStorageLocation` requires there to not
258-
// already be a storage location assigned. Conceptually, these maps capture
259-
// information from the local scope, so when popping that scope, we do not
260-
// propagate the maps.
260+
// callee's `DeclCtx`, `ReturnLoc` or `ThisPointeeLoc`. We don't bring back
261+
// `DeclToLoc` and `ExprToLoc` because we want to be able to later analyze the
262+
// same callee in a different context, and `setStorageLocation` requires there
263+
// to not already be a storage location assigned. Conceptually, these maps
264+
// capture information from the local scope, so when popping that scope, we do
265+
// not propagate the maps.
261266
this->LocToVal = std::move(CalleeEnv.LocToVal);
262267
this->MemberLocToStruct = std::move(CalleeEnv.MemberLocToStruct);
263268
this->FlowConditionToken = std::move(CalleeEnv.FlowConditionToken);
@@ -304,11 +309,13 @@ LatticeJoinEffect Environment::join(const Environment &Other,
304309
assert(DACtx == Other.DACtx);
305310
assert(ReturnLoc == Other.ReturnLoc);
306311
assert(ThisPointeeLoc == Other.ThisPointeeLoc);
312+
assert(DeclCtx == Other.DeclCtx);
307313

308314
auto Effect = LatticeJoinEffect::Unchanged;
309315

310316
Environment JoinedEnv(*DACtx);
311317

318+
JoinedEnv.setDeclCtx(DeclCtx);
312319
JoinedEnv.ReturnLoc = ReturnLoc;
313320
JoinedEnv.ThisPointeeLoc = ThisPointeeLoc;
314321

0 commit comments

Comments
 (0)