1*12c85518Srobert //===-- DataflowEnvironment.cpp ---------------------------------*- C++ -*-===//
2*12c85518Srobert //
3*12c85518Srobert // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*12c85518Srobert // See https://llvm.org/LICENSE.txt for license information.
5*12c85518Srobert // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*12c85518Srobert //
7*12c85518Srobert //===----------------------------------------------------------------------===//
8*12c85518Srobert //
9*12c85518Srobert // This file defines an Environment class that is used by dataflow analyses
10*12c85518Srobert // that run over Control-Flow Graphs (CFGs) to keep track of the state of the
11*12c85518Srobert // program at given program points.
12*12c85518Srobert //
13*12c85518Srobert //===----------------------------------------------------------------------===//
14*12c85518Srobert
15*12c85518Srobert #include "clang/Analysis/FlowSensitive/DataflowEnvironment.h"
16*12c85518Srobert #include "clang/AST/Decl.h"
17*12c85518Srobert #include "clang/AST/DeclCXX.h"
18*12c85518Srobert #include "clang/AST/Type.h"
19*12c85518Srobert #include "clang/Analysis/FlowSensitive/DataflowLattice.h"
20*12c85518Srobert #include "clang/Analysis/FlowSensitive/Value.h"
21*12c85518Srobert #include "llvm/ADT/DenseMap.h"
22*12c85518Srobert #include "llvm/ADT/DenseSet.h"
23*12c85518Srobert #include "llvm/ADT/STLExtras.h"
24*12c85518Srobert #include "llvm/Support/Casting.h"
25*12c85518Srobert #include "llvm/Support/ErrorHandling.h"
26*12c85518Srobert #include <cassert>
27*12c85518Srobert #include <memory>
28*12c85518Srobert #include <utility>
29*12c85518Srobert
30*12c85518Srobert namespace clang {
31*12c85518Srobert namespace dataflow {
32*12c85518Srobert
33*12c85518Srobert // FIXME: convert these to parameters of the analysis or environment. Current
34*12c85518Srobert // settings have been experimentaly validated, but only for a particular
35*12c85518Srobert // analysis.
36*12c85518Srobert static constexpr int MaxCompositeValueDepth = 3;
37*12c85518Srobert static constexpr int MaxCompositeValueSize = 1000;
38*12c85518Srobert
39*12c85518Srobert /// Returns a map consisting of key-value entries that are present in both maps.
40*12c85518Srobert template <typename K, typename V>
intersectDenseMaps(const llvm::DenseMap<K,V> & Map1,const llvm::DenseMap<K,V> & Map2)41*12c85518Srobert llvm::DenseMap<K, V> intersectDenseMaps(const llvm::DenseMap<K, V> &Map1,
42*12c85518Srobert const llvm::DenseMap<K, V> &Map2) {
43*12c85518Srobert llvm::DenseMap<K, V> Result;
44*12c85518Srobert for (auto &Entry : Map1) {
45*12c85518Srobert auto It = Map2.find(Entry.first);
46*12c85518Srobert if (It != Map2.end() && Entry.second == It->second)
47*12c85518Srobert Result.insert({Entry.first, Entry.second});
48*12c85518Srobert }
49*12c85518Srobert return Result;
50*12c85518Srobert }
51*12c85518Srobert
compareDistinctValues(QualType Type,Value & Val1,const Environment & Env1,Value & Val2,const Environment & Env2,Environment::ValueModel & Model)52*12c85518Srobert static bool compareDistinctValues(QualType Type, Value &Val1,
53*12c85518Srobert const Environment &Env1, Value &Val2,
54*12c85518Srobert const Environment &Env2,
55*12c85518Srobert Environment::ValueModel &Model) {
56*12c85518Srobert // Note: Potentially costly, but, for booleans, we could check whether both
57*12c85518Srobert // can be proven equivalent in their respective environments.
58*12c85518Srobert
59*12c85518Srobert // FIXME: move the reference/pointers logic from `areEquivalentValues` to here
60*12c85518Srobert // and implement separate, join/widen specific handling for
61*12c85518Srobert // reference/pointers.
62*12c85518Srobert switch (Model.compare(Type, Val1, Env1, Val2, Env2)) {
63*12c85518Srobert case ComparisonResult::Same:
64*12c85518Srobert return true;
65*12c85518Srobert case ComparisonResult::Different:
66*12c85518Srobert return false;
67*12c85518Srobert case ComparisonResult::Unknown:
68*12c85518Srobert switch (Val1.getKind()) {
69*12c85518Srobert case Value::Kind::Integer:
70*12c85518Srobert case Value::Kind::Reference:
71*12c85518Srobert case Value::Kind::Pointer:
72*12c85518Srobert case Value::Kind::Struct:
73*12c85518Srobert // FIXME: this choice intentionally introduces unsoundness to allow
74*12c85518Srobert // for convergence. Once we have widening support for the
75*12c85518Srobert // reference/pointer and struct built-in models, this should be
76*12c85518Srobert // `false`.
77*12c85518Srobert return true;
78*12c85518Srobert default:
79*12c85518Srobert return false;
80*12c85518Srobert }
81*12c85518Srobert }
82*12c85518Srobert llvm_unreachable("All cases covered in switch");
83*12c85518Srobert }
84*12c85518Srobert
85*12c85518Srobert /// Attempts to merge distinct values `Val1` and `Val2` in `Env1` and `Env2`,
86*12c85518Srobert /// respectively, of the same type `Type`. Merging generally produces a single
87*12c85518Srobert /// value that (soundly) approximates the two inputs, although the actual
88*12c85518Srobert /// meaning depends on `Model`.
mergeDistinctValues(QualType Type,Value & Val1,const Environment & Env1,Value & Val2,const Environment & Env2,Environment & MergedEnv,Environment::ValueModel & Model)89*12c85518Srobert static Value *mergeDistinctValues(QualType Type, Value &Val1,
90*12c85518Srobert const Environment &Env1, Value &Val2,
91*12c85518Srobert const Environment &Env2,
92*12c85518Srobert Environment &MergedEnv,
93*12c85518Srobert Environment::ValueModel &Model) {
94*12c85518Srobert // Join distinct boolean values preserving information about the constraints
95*12c85518Srobert // in the respective path conditions.
96*12c85518Srobert if (isa<BoolValue>(&Val1) && isa<BoolValue>(&Val2)) {
97*12c85518Srobert // FIXME: Checking both values should be unnecessary, since they should have
98*12c85518Srobert // a consistent shape. However, right now we can end up with BoolValue's in
99*12c85518Srobert // integer-typed variables due to our incorrect handling of
100*12c85518Srobert // boolean-to-integer casts (we just propagate the BoolValue to the result
101*12c85518Srobert // of the cast). So, a join can encounter an integer in one branch but a
102*12c85518Srobert // bool in the other.
103*12c85518Srobert // For example:
104*12c85518Srobert // ```
105*12c85518Srobert // std::optional<bool> o;
106*12c85518Srobert // int x;
107*12c85518Srobert // if (o.has_value())
108*12c85518Srobert // x = o.value();
109*12c85518Srobert // ```
110*12c85518Srobert auto *Expr1 = cast<BoolValue>(&Val1);
111*12c85518Srobert auto *Expr2 = cast<BoolValue>(&Val2);
112*12c85518Srobert auto &MergedVal = MergedEnv.makeAtomicBoolValue();
113*12c85518Srobert MergedEnv.addToFlowCondition(MergedEnv.makeOr(
114*12c85518Srobert MergedEnv.makeAnd(Env1.getFlowConditionToken(),
115*12c85518Srobert MergedEnv.makeIff(MergedVal, *Expr1)),
116*12c85518Srobert MergedEnv.makeAnd(Env2.getFlowConditionToken(),
117*12c85518Srobert MergedEnv.makeIff(MergedVal, *Expr2))));
118*12c85518Srobert return &MergedVal;
119*12c85518Srobert }
120*12c85518Srobert
121*12c85518Srobert // FIXME: Consider destroying `MergedValue` immediately if `ValueModel::merge`
122*12c85518Srobert // returns false to avoid storing unneeded values in `DACtx`.
123*12c85518Srobert // FIXME: Creating the value based on the type alone creates misshapen values
124*12c85518Srobert // for lvalues, since the type does not reflect the need for `ReferenceValue`.
125*12c85518Srobert if (Value *MergedVal = MergedEnv.createValue(Type))
126*12c85518Srobert if (Model.merge(Type, Val1, Env1, Val2, Env2, *MergedVal, MergedEnv))
127*12c85518Srobert return MergedVal;
128*12c85518Srobert
129*12c85518Srobert return nullptr;
130*12c85518Srobert }
131*12c85518Srobert
132*12c85518Srobert // When widening does not change `Current`, return value will equal `&Prev`.
widenDistinctValues(QualType Type,Value & Prev,const Environment & PrevEnv,Value & Current,Environment & CurrentEnv,Environment::ValueModel & Model)133*12c85518Srobert static Value &widenDistinctValues(QualType Type, Value &Prev,
134*12c85518Srobert const Environment &PrevEnv, Value &Current,
135*12c85518Srobert Environment &CurrentEnv,
136*12c85518Srobert Environment::ValueModel &Model) {
137*12c85518Srobert // Boolean-model widening.
138*12c85518Srobert if (isa<BoolValue>(&Prev)) {
139*12c85518Srobert assert(isa<BoolValue>(Current));
140*12c85518Srobert // Widen to Top, because we know they are different values. If previous was
141*12c85518Srobert // already Top, re-use that to (implicitly) indicate that no change occured.
142*12c85518Srobert if (isa<TopBoolValue>(Prev))
143*12c85518Srobert return Prev;
144*12c85518Srobert return CurrentEnv.makeTopBoolValue();
145*12c85518Srobert }
146*12c85518Srobert
147*12c85518Srobert // FIXME: Add other built-in model widening.
148*12c85518Srobert
149*12c85518Srobert // Custom-model widening.
150*12c85518Srobert if (auto *W = Model.widen(Type, Prev, PrevEnv, Current, CurrentEnv))
151*12c85518Srobert return *W;
152*12c85518Srobert
153*12c85518Srobert // Default of widening is a no-op: leave the current value unchanged.
154*12c85518Srobert return Current;
155*12c85518Srobert }
156*12c85518Srobert
157*12c85518Srobert /// Initializes a global storage value.
insertIfGlobal(const Decl & D,llvm::DenseSet<const FieldDecl * > & Fields,llvm::DenseSet<const VarDecl * > & Vars)158*12c85518Srobert static void insertIfGlobal(const Decl &D,
159*12c85518Srobert llvm::DenseSet<const FieldDecl *> &Fields,
160*12c85518Srobert llvm::DenseSet<const VarDecl *> &Vars) {
161*12c85518Srobert if (auto *V = dyn_cast<VarDecl>(&D))
162*12c85518Srobert if (V->hasGlobalStorage())
163*12c85518Srobert Vars.insert(V);
164*12c85518Srobert }
165*12c85518Srobert
getFieldsAndGlobalVars(const Decl & D,llvm::DenseSet<const FieldDecl * > & Fields,llvm::DenseSet<const VarDecl * > & Vars)166*12c85518Srobert static void getFieldsAndGlobalVars(const Decl &D,
167*12c85518Srobert llvm::DenseSet<const FieldDecl *> &Fields,
168*12c85518Srobert llvm::DenseSet<const VarDecl *> &Vars) {
169*12c85518Srobert insertIfGlobal(D, Fields, Vars);
170*12c85518Srobert if (const auto *Decomp = dyn_cast<DecompositionDecl>(&D))
171*12c85518Srobert for (const auto *B : Decomp->bindings())
172*12c85518Srobert if (auto *ME = dyn_cast_or_null<MemberExpr>(B->getBinding()))
173*12c85518Srobert // FIXME: should we be using `E->getFoundDecl()`?
174*12c85518Srobert if (const auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
175*12c85518Srobert Fields.insert(FD);
176*12c85518Srobert }
177*12c85518Srobert
178*12c85518Srobert /// Traverses `S` and inserts into `Vars` any global storage values that are
179*12c85518Srobert /// declared in or referenced from sub-statements.
getFieldsAndGlobalVars(const Stmt & S,llvm::DenseSet<const FieldDecl * > & Fields,llvm::DenseSet<const VarDecl * > & Vars)180*12c85518Srobert static void getFieldsAndGlobalVars(const Stmt &S,
181*12c85518Srobert llvm::DenseSet<const FieldDecl *> &Fields,
182*12c85518Srobert llvm::DenseSet<const VarDecl *> &Vars) {
183*12c85518Srobert for (auto *Child : S.children())
184*12c85518Srobert if (Child != nullptr)
185*12c85518Srobert getFieldsAndGlobalVars(*Child, Fields, Vars);
186*12c85518Srobert
187*12c85518Srobert if (auto *DS = dyn_cast<DeclStmt>(&S)) {
188*12c85518Srobert if (DS->isSingleDecl())
189*12c85518Srobert getFieldsAndGlobalVars(*DS->getSingleDecl(), Fields, Vars);
190*12c85518Srobert else
191*12c85518Srobert for (auto *D : DS->getDeclGroup())
192*12c85518Srobert getFieldsAndGlobalVars(*D, Fields, Vars);
193*12c85518Srobert } else if (auto *E = dyn_cast<DeclRefExpr>(&S)) {
194*12c85518Srobert insertIfGlobal(*E->getDecl(), Fields, Vars);
195*12c85518Srobert } else if (auto *E = dyn_cast<MemberExpr>(&S)) {
196*12c85518Srobert // FIXME: should we be using `E->getFoundDecl()`?
197*12c85518Srobert const ValueDecl *VD = E->getMemberDecl();
198*12c85518Srobert insertIfGlobal(*VD, Fields, Vars);
199*12c85518Srobert if (const auto *FD = dyn_cast<FieldDecl>(VD))
200*12c85518Srobert Fields.insert(FD);
201*12c85518Srobert }
202*12c85518Srobert }
203*12c85518Srobert
204*12c85518Srobert // FIXME: Add support for resetting globals after function calls to enable
205*12c85518Srobert // the implementation of sound analyses.
initVars(llvm::DenseSet<const VarDecl * > Vars)206*12c85518Srobert void Environment::initVars(llvm::DenseSet<const VarDecl *> Vars) {
207*12c85518Srobert for (const VarDecl *D : Vars) {
208*12c85518Srobert if (getStorageLocation(*D, SkipPast::None) != nullptr)
209*12c85518Srobert continue;
210*12c85518Srobert auto &Loc = createStorageLocation(*D);
211*12c85518Srobert setStorageLocation(*D, Loc);
212*12c85518Srobert if (auto *Val = createValue(D->getType()))
213*12c85518Srobert setValue(Loc, *Val);
214*12c85518Srobert }
215*12c85518Srobert }
216*12c85518Srobert
Environment(DataflowAnalysisContext & DACtx)217*12c85518Srobert Environment::Environment(DataflowAnalysisContext &DACtx)
218*12c85518Srobert : DACtx(&DACtx), FlowConditionToken(&DACtx.makeFlowConditionToken()) {}
219*12c85518Srobert
Environment(const Environment & Other)220*12c85518Srobert Environment::Environment(const Environment &Other)
221*12c85518Srobert : DACtx(Other.DACtx), CallStack(Other.CallStack),
222*12c85518Srobert ReturnLoc(Other.ReturnLoc), ThisPointeeLoc(Other.ThisPointeeLoc),
223*12c85518Srobert DeclToLoc(Other.DeclToLoc), ExprToLoc(Other.ExprToLoc),
224*12c85518Srobert LocToVal(Other.LocToVal), MemberLocToStruct(Other.MemberLocToStruct),
225*12c85518Srobert FlowConditionToken(&DACtx->forkFlowCondition(*Other.FlowConditionToken)) {
226*12c85518Srobert }
227*12c85518Srobert
operator =(const Environment & Other)228*12c85518Srobert Environment &Environment::operator=(const Environment &Other) {
229*12c85518Srobert Environment Copy(Other);
230*12c85518Srobert *this = std::move(Copy);
231*12c85518Srobert return *this;
232*12c85518Srobert }
233*12c85518Srobert
Environment(DataflowAnalysisContext & DACtx,const DeclContext & DeclCtx)234*12c85518Srobert Environment::Environment(DataflowAnalysisContext &DACtx,
235*12c85518Srobert const DeclContext &DeclCtx)
236*12c85518Srobert : Environment(DACtx) {
237*12c85518Srobert CallStack.push_back(&DeclCtx);
238*12c85518Srobert
239*12c85518Srobert if (const auto *FuncDecl = dyn_cast<FunctionDecl>(&DeclCtx)) {
240*12c85518Srobert assert(FuncDecl->getBody() != nullptr);
241*12c85518Srobert
242*12c85518Srobert llvm::DenseSet<const FieldDecl *> Fields;
243*12c85518Srobert llvm::DenseSet<const VarDecl *> Vars;
244*12c85518Srobert
245*12c85518Srobert // Look for global variable references in the constructor-initializers.
246*12c85518Srobert if (const auto *CtorDecl = dyn_cast<CXXConstructorDecl>(&DeclCtx)) {
247*12c85518Srobert for (const auto *Init : CtorDecl->inits()) {
248*12c85518Srobert if (const auto *M = Init->getAnyMember())
249*12c85518Srobert Fields.insert(M);
250*12c85518Srobert const Expr *E = Init->getInit();
251*12c85518Srobert assert(E != nullptr);
252*12c85518Srobert getFieldsAndGlobalVars(*E, Fields, Vars);
253*12c85518Srobert }
254*12c85518Srobert }
255*12c85518Srobert getFieldsAndGlobalVars(*FuncDecl->getBody(), Fields, Vars);
256*12c85518Srobert
257*12c85518Srobert // These have to be added before the lines that follow to ensure that
258*12c85518Srobert // `create*` work correctly for structs.
259*12c85518Srobert DACtx.addModeledFields(Fields);
260*12c85518Srobert
261*12c85518Srobert initVars(Vars);
262*12c85518Srobert
263*12c85518Srobert for (const auto *ParamDecl : FuncDecl->parameters()) {
264*12c85518Srobert assert(ParamDecl != nullptr);
265*12c85518Srobert auto &ParamLoc = createStorageLocation(*ParamDecl);
266*12c85518Srobert setStorageLocation(*ParamDecl, ParamLoc);
267*12c85518Srobert if (Value *ParamVal = createValue(ParamDecl->getType()))
268*12c85518Srobert setValue(ParamLoc, *ParamVal);
269*12c85518Srobert }
270*12c85518Srobert
271*12c85518Srobert QualType ReturnType = FuncDecl->getReturnType();
272*12c85518Srobert ReturnLoc = &createStorageLocation(ReturnType);
273*12c85518Srobert }
274*12c85518Srobert
275*12c85518Srobert if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(&DeclCtx)) {
276*12c85518Srobert auto *Parent = MethodDecl->getParent();
277*12c85518Srobert assert(Parent != nullptr);
278*12c85518Srobert if (Parent->isLambda())
279*12c85518Srobert MethodDecl = dyn_cast<CXXMethodDecl>(Parent->getDeclContext());
280*12c85518Srobert
281*12c85518Srobert // FIXME: Initialize the ThisPointeeLoc of lambdas too.
282*12c85518Srobert if (MethodDecl && !MethodDecl->isStatic()) {
283*12c85518Srobert QualType ThisPointeeType = MethodDecl->getThisObjectType();
284*12c85518Srobert ThisPointeeLoc = &createStorageLocation(ThisPointeeType);
285*12c85518Srobert if (Value *ThisPointeeVal = createValue(ThisPointeeType))
286*12c85518Srobert setValue(*ThisPointeeLoc, *ThisPointeeVal);
287*12c85518Srobert }
288*12c85518Srobert }
289*12c85518Srobert }
290*12c85518Srobert
canDescend(unsigned MaxDepth,const DeclContext * Callee) const291*12c85518Srobert bool Environment::canDescend(unsigned MaxDepth,
292*12c85518Srobert const DeclContext *Callee) const {
293*12c85518Srobert return CallStack.size() <= MaxDepth && !llvm::is_contained(CallStack, Callee);
294*12c85518Srobert }
295*12c85518Srobert
pushCall(const CallExpr * Call) const296*12c85518Srobert Environment Environment::pushCall(const CallExpr *Call) const {
297*12c85518Srobert Environment Env(*this);
298*12c85518Srobert
299*12c85518Srobert // FIXME: Support references here.
300*12c85518Srobert Env.ReturnLoc = getStorageLocation(*Call, SkipPast::Reference);
301*12c85518Srobert
302*12c85518Srobert if (const auto *MethodCall = dyn_cast<CXXMemberCallExpr>(Call)) {
303*12c85518Srobert if (const Expr *Arg = MethodCall->getImplicitObjectArgument()) {
304*12c85518Srobert if (!isa<CXXThisExpr>(Arg))
305*12c85518Srobert Env.ThisPointeeLoc = getStorageLocation(*Arg, SkipPast::Reference);
306*12c85518Srobert // Otherwise (when the argument is `this`), retain the current
307*12c85518Srobert // environment's `ThisPointeeLoc`.
308*12c85518Srobert }
309*12c85518Srobert }
310*12c85518Srobert
311*12c85518Srobert Env.pushCallInternal(Call->getDirectCallee(),
312*12c85518Srobert llvm::ArrayRef(Call->getArgs(), Call->getNumArgs()));
313*12c85518Srobert
314*12c85518Srobert return Env;
315*12c85518Srobert }
316*12c85518Srobert
pushCall(const CXXConstructExpr * Call) const317*12c85518Srobert Environment Environment::pushCall(const CXXConstructExpr *Call) const {
318*12c85518Srobert Environment Env(*this);
319*12c85518Srobert
320*12c85518Srobert // FIXME: Support references here.
321*12c85518Srobert Env.ReturnLoc = getStorageLocation(*Call, SkipPast::Reference);
322*12c85518Srobert
323*12c85518Srobert Env.ThisPointeeLoc = Env.ReturnLoc;
324*12c85518Srobert
325*12c85518Srobert Env.pushCallInternal(Call->getConstructor(),
326*12c85518Srobert llvm::ArrayRef(Call->getArgs(), Call->getNumArgs()));
327*12c85518Srobert
328*12c85518Srobert return Env;
329*12c85518Srobert }
330*12c85518Srobert
pushCallInternal(const FunctionDecl * FuncDecl,ArrayRef<const Expr * > Args)331*12c85518Srobert void Environment::pushCallInternal(const FunctionDecl *FuncDecl,
332*12c85518Srobert ArrayRef<const Expr *> Args) {
333*12c85518Srobert CallStack.push_back(FuncDecl);
334*12c85518Srobert
335*12c85518Srobert // FIXME: Share this code with the constructor, rather than duplicating it.
336*12c85518Srobert llvm::DenseSet<const FieldDecl *> Fields;
337*12c85518Srobert llvm::DenseSet<const VarDecl *> Vars;
338*12c85518Srobert // Look for global variable references in the constructor-initializers.
339*12c85518Srobert if (const auto *CtorDecl = dyn_cast<CXXConstructorDecl>(FuncDecl)) {
340*12c85518Srobert for (const auto *Init : CtorDecl->inits()) {
341*12c85518Srobert if (const auto *M = Init->getAnyMember())
342*12c85518Srobert Fields.insert(M);
343*12c85518Srobert const Expr *E = Init->getInit();
344*12c85518Srobert assert(E != nullptr);
345*12c85518Srobert getFieldsAndGlobalVars(*E, Fields, Vars);
346*12c85518Srobert }
347*12c85518Srobert }
348*12c85518Srobert getFieldsAndGlobalVars(*FuncDecl->getBody(), Fields, Vars);
349*12c85518Srobert
350*12c85518Srobert // These have to be added before the lines that follow to ensure that
351*12c85518Srobert // `create*` work correctly for structs.
352*12c85518Srobert DACtx->addModeledFields(Fields);
353*12c85518Srobert
354*12c85518Srobert initVars(Vars);
355*12c85518Srobert
356*12c85518Srobert const auto *ParamIt = FuncDecl->param_begin();
357*12c85518Srobert
358*12c85518Srobert // FIXME: Parameters don't always map to arguments 1:1; examples include
359*12c85518Srobert // overloaded operators implemented as member functions, and parameter packs.
360*12c85518Srobert for (unsigned ArgIndex = 0; ArgIndex < Args.size(); ++ParamIt, ++ArgIndex) {
361*12c85518Srobert assert(ParamIt != FuncDecl->param_end());
362*12c85518Srobert
363*12c85518Srobert const Expr *Arg = Args[ArgIndex];
364*12c85518Srobert auto *ArgLoc = getStorageLocation(*Arg, SkipPast::Reference);
365*12c85518Srobert if (ArgLoc == nullptr)
366*12c85518Srobert continue;
367*12c85518Srobert
368*12c85518Srobert const VarDecl *Param = *ParamIt;
369*12c85518Srobert auto &Loc = createStorageLocation(*Param);
370*12c85518Srobert setStorageLocation(*Param, Loc);
371*12c85518Srobert
372*12c85518Srobert QualType ParamType = Param->getType();
373*12c85518Srobert if (ParamType->isReferenceType()) {
374*12c85518Srobert auto &Val = takeOwnership(std::make_unique<ReferenceValue>(*ArgLoc));
375*12c85518Srobert setValue(Loc, Val);
376*12c85518Srobert } else if (auto *ArgVal = getValue(*ArgLoc)) {
377*12c85518Srobert setValue(Loc, *ArgVal);
378*12c85518Srobert } else if (Value *Val = createValue(ParamType)) {
379*12c85518Srobert setValue(Loc, *Val);
380*12c85518Srobert }
381*12c85518Srobert }
382*12c85518Srobert }
383*12c85518Srobert
popCall(const Environment & CalleeEnv)384*12c85518Srobert void Environment::popCall(const Environment &CalleeEnv) {
385*12c85518Srobert // We ignore `DACtx` because it's already the same in both. We don't want the
386*12c85518Srobert // callee's `DeclCtx`, `ReturnLoc` or `ThisPointeeLoc`. We don't bring back
387*12c85518Srobert // `DeclToLoc` and `ExprToLoc` because we want to be able to later analyze the
388*12c85518Srobert // same callee in a different context, and `setStorageLocation` requires there
389*12c85518Srobert // to not already be a storage location assigned. Conceptually, these maps
390*12c85518Srobert // capture information from the local scope, so when popping that scope, we do
391*12c85518Srobert // not propagate the maps.
392*12c85518Srobert this->LocToVal = std::move(CalleeEnv.LocToVal);
393*12c85518Srobert this->MemberLocToStruct = std::move(CalleeEnv.MemberLocToStruct);
394*12c85518Srobert this->FlowConditionToken = std::move(CalleeEnv.FlowConditionToken);
395*12c85518Srobert }
396*12c85518Srobert
equivalentTo(const Environment & Other,Environment::ValueModel & Model) const397*12c85518Srobert bool Environment::equivalentTo(const Environment &Other,
398*12c85518Srobert Environment::ValueModel &Model) const {
399*12c85518Srobert assert(DACtx == Other.DACtx);
400*12c85518Srobert
401*12c85518Srobert if (ReturnLoc != Other.ReturnLoc)
402*12c85518Srobert return false;
403*12c85518Srobert
404*12c85518Srobert if (ThisPointeeLoc != Other.ThisPointeeLoc)
405*12c85518Srobert return false;
406*12c85518Srobert
407*12c85518Srobert if (DeclToLoc != Other.DeclToLoc)
408*12c85518Srobert return false;
409*12c85518Srobert
410*12c85518Srobert if (ExprToLoc != Other.ExprToLoc)
411*12c85518Srobert return false;
412*12c85518Srobert
413*12c85518Srobert // Compare the contents for the intersection of their domains.
414*12c85518Srobert for (auto &Entry : LocToVal) {
415*12c85518Srobert const StorageLocation *Loc = Entry.first;
416*12c85518Srobert assert(Loc != nullptr);
417*12c85518Srobert
418*12c85518Srobert Value *Val = Entry.second;
419*12c85518Srobert assert(Val != nullptr);
420*12c85518Srobert
421*12c85518Srobert auto It = Other.LocToVal.find(Loc);
422*12c85518Srobert if (It == Other.LocToVal.end())
423*12c85518Srobert continue;
424*12c85518Srobert assert(It->second != nullptr);
425*12c85518Srobert
426*12c85518Srobert if (!areEquivalentValues(*Val, *It->second) &&
427*12c85518Srobert !compareDistinctValues(Loc->getType(), *Val, *this, *It->second, Other,
428*12c85518Srobert Model))
429*12c85518Srobert return false;
430*12c85518Srobert }
431*12c85518Srobert
432*12c85518Srobert return true;
433*12c85518Srobert }
434*12c85518Srobert
widen(const Environment & PrevEnv,Environment::ValueModel & Model)435*12c85518Srobert LatticeJoinEffect Environment::widen(const Environment &PrevEnv,
436*12c85518Srobert Environment::ValueModel &Model) {
437*12c85518Srobert assert(DACtx == PrevEnv.DACtx);
438*12c85518Srobert assert(ReturnLoc == PrevEnv.ReturnLoc);
439*12c85518Srobert assert(ThisPointeeLoc == PrevEnv.ThisPointeeLoc);
440*12c85518Srobert assert(CallStack == PrevEnv.CallStack);
441*12c85518Srobert
442*12c85518Srobert auto Effect = LatticeJoinEffect::Unchanged;
443*12c85518Srobert
444*12c85518Srobert // By the API, `PrevEnv` is a previous version of the environment for the same
445*12c85518Srobert // block, so we have some guarantees about its shape. In particular, it will
446*12c85518Srobert // be the result of a join or widen operation on previous values for this
447*12c85518Srobert // block. For `DeclToLoc` and `ExprToLoc`, join guarantees that these maps are
448*12c85518Srobert // subsets of the maps in `PrevEnv`. So, as long as we maintain this property
449*12c85518Srobert // here, we don't need change their current values to widen.
450*12c85518Srobert //
451*12c85518Srobert // FIXME: `MemberLocToStruct` does not share the above property, because
452*12c85518Srobert // `join` can cause the map size to increase (when we add fresh data in places
453*12c85518Srobert // of conflict). Once this issue with join is resolved, re-enable the
454*12c85518Srobert // assertion below or replace with something that captures the desired
455*12c85518Srobert // invariant.
456*12c85518Srobert assert(DeclToLoc.size() <= PrevEnv.DeclToLoc.size());
457*12c85518Srobert assert(ExprToLoc.size() <= PrevEnv.ExprToLoc.size());
458*12c85518Srobert // assert(MemberLocToStruct.size() <= PrevEnv.MemberLocToStruct.size());
459*12c85518Srobert
460*12c85518Srobert llvm::DenseMap<const StorageLocation *, Value *> WidenedLocToVal;
461*12c85518Srobert for (auto &Entry : LocToVal) {
462*12c85518Srobert const StorageLocation *Loc = Entry.first;
463*12c85518Srobert assert(Loc != nullptr);
464*12c85518Srobert
465*12c85518Srobert Value *Val = Entry.second;
466*12c85518Srobert assert(Val != nullptr);
467*12c85518Srobert
468*12c85518Srobert auto PrevIt = PrevEnv.LocToVal.find(Loc);
469*12c85518Srobert if (PrevIt == PrevEnv.LocToVal.end())
470*12c85518Srobert continue;
471*12c85518Srobert assert(PrevIt->second != nullptr);
472*12c85518Srobert
473*12c85518Srobert if (areEquivalentValues(*Val, *PrevIt->second)) {
474*12c85518Srobert WidenedLocToVal.insert({Loc, Val});
475*12c85518Srobert continue;
476*12c85518Srobert }
477*12c85518Srobert
478*12c85518Srobert Value &WidenedVal = widenDistinctValues(Loc->getType(), *PrevIt->second,
479*12c85518Srobert PrevEnv, *Val, *this, Model);
480*12c85518Srobert WidenedLocToVal.insert({Loc, &WidenedVal});
481*12c85518Srobert if (&WidenedVal != PrevIt->second)
482*12c85518Srobert Effect = LatticeJoinEffect::Changed;
483*12c85518Srobert }
484*12c85518Srobert LocToVal = std::move(WidenedLocToVal);
485*12c85518Srobert // FIXME: update the equivalence calculation for `MemberLocToStruct`, once we
486*12c85518Srobert // have a systematic way of soundly comparing this map.
487*12c85518Srobert if (DeclToLoc.size() != PrevEnv.DeclToLoc.size() ||
488*12c85518Srobert ExprToLoc.size() != PrevEnv.ExprToLoc.size() ||
489*12c85518Srobert LocToVal.size() != PrevEnv.LocToVal.size() ||
490*12c85518Srobert MemberLocToStruct.size() != PrevEnv.MemberLocToStruct.size())
491*12c85518Srobert Effect = LatticeJoinEffect::Changed;
492*12c85518Srobert
493*12c85518Srobert return Effect;
494*12c85518Srobert }
495*12c85518Srobert
join(const Environment & Other,Environment::ValueModel & Model)496*12c85518Srobert LatticeJoinEffect Environment::join(const Environment &Other,
497*12c85518Srobert Environment::ValueModel &Model) {
498*12c85518Srobert assert(DACtx == Other.DACtx);
499*12c85518Srobert assert(ReturnLoc == Other.ReturnLoc);
500*12c85518Srobert assert(ThisPointeeLoc == Other.ThisPointeeLoc);
501*12c85518Srobert assert(CallStack == Other.CallStack);
502*12c85518Srobert
503*12c85518Srobert auto Effect = LatticeJoinEffect::Unchanged;
504*12c85518Srobert
505*12c85518Srobert Environment JoinedEnv(*DACtx);
506*12c85518Srobert
507*12c85518Srobert JoinedEnv.CallStack = CallStack;
508*12c85518Srobert JoinedEnv.ReturnLoc = ReturnLoc;
509*12c85518Srobert JoinedEnv.ThisPointeeLoc = ThisPointeeLoc;
510*12c85518Srobert
511*12c85518Srobert JoinedEnv.DeclToLoc = intersectDenseMaps(DeclToLoc, Other.DeclToLoc);
512*12c85518Srobert if (DeclToLoc.size() != JoinedEnv.DeclToLoc.size())
513*12c85518Srobert Effect = LatticeJoinEffect::Changed;
514*12c85518Srobert
515*12c85518Srobert JoinedEnv.ExprToLoc = intersectDenseMaps(ExprToLoc, Other.ExprToLoc);
516*12c85518Srobert if (ExprToLoc.size() != JoinedEnv.ExprToLoc.size())
517*12c85518Srobert Effect = LatticeJoinEffect::Changed;
518*12c85518Srobert
519*12c85518Srobert JoinedEnv.MemberLocToStruct =
520*12c85518Srobert intersectDenseMaps(MemberLocToStruct, Other.MemberLocToStruct);
521*12c85518Srobert if (MemberLocToStruct.size() != JoinedEnv.MemberLocToStruct.size())
522*12c85518Srobert Effect = LatticeJoinEffect::Changed;
523*12c85518Srobert
524*12c85518Srobert // FIXME: set `Effect` as needed.
525*12c85518Srobert // FIXME: update join to detect backedges and simplify the flow condition
526*12c85518Srobert // accordingly.
527*12c85518Srobert JoinedEnv.FlowConditionToken = &DACtx->joinFlowConditions(
528*12c85518Srobert *FlowConditionToken, *Other.FlowConditionToken);
529*12c85518Srobert
530*12c85518Srobert for (auto &Entry : LocToVal) {
531*12c85518Srobert const StorageLocation *Loc = Entry.first;
532*12c85518Srobert assert(Loc != nullptr);
533*12c85518Srobert
534*12c85518Srobert Value *Val = Entry.second;
535*12c85518Srobert assert(Val != nullptr);
536*12c85518Srobert
537*12c85518Srobert auto It = Other.LocToVal.find(Loc);
538*12c85518Srobert if (It == Other.LocToVal.end())
539*12c85518Srobert continue;
540*12c85518Srobert assert(It->second != nullptr);
541*12c85518Srobert
542*12c85518Srobert if (areEquivalentValues(*Val, *It->second)) {
543*12c85518Srobert JoinedEnv.LocToVal.insert({Loc, Val});
544*12c85518Srobert continue;
545*12c85518Srobert }
546*12c85518Srobert
547*12c85518Srobert if (Value *MergedVal =
548*12c85518Srobert mergeDistinctValues(Loc->getType(), *Val, *this, *It->second, Other,
549*12c85518Srobert JoinedEnv, Model)) {
550*12c85518Srobert JoinedEnv.LocToVal.insert({Loc, MergedVal});
551*12c85518Srobert Effect = LatticeJoinEffect::Changed;
552*12c85518Srobert }
553*12c85518Srobert }
554*12c85518Srobert if (LocToVal.size() != JoinedEnv.LocToVal.size())
555*12c85518Srobert Effect = LatticeJoinEffect::Changed;
556*12c85518Srobert
557*12c85518Srobert *this = std::move(JoinedEnv);
558*12c85518Srobert
559*12c85518Srobert return Effect;
560*12c85518Srobert }
561*12c85518Srobert
createStorageLocation(QualType Type)562*12c85518Srobert StorageLocation &Environment::createStorageLocation(QualType Type) {
563*12c85518Srobert return DACtx->createStorageLocation(Type);
564*12c85518Srobert }
565*12c85518Srobert
createStorageLocation(const VarDecl & D)566*12c85518Srobert StorageLocation &Environment::createStorageLocation(const VarDecl &D) {
567*12c85518Srobert // Evaluated declarations are always assigned the same storage locations to
568*12c85518Srobert // ensure that the environment stabilizes across loop iterations. Storage
569*12c85518Srobert // locations for evaluated declarations are stored in the analysis context.
570*12c85518Srobert return DACtx->getStableStorageLocation(D);
571*12c85518Srobert }
572*12c85518Srobert
createStorageLocation(const Expr & E)573*12c85518Srobert StorageLocation &Environment::createStorageLocation(const Expr &E) {
574*12c85518Srobert // Evaluated expressions are always assigned the same storage locations to
575*12c85518Srobert // ensure that the environment stabilizes across loop iterations. Storage
576*12c85518Srobert // locations for evaluated expressions are stored in the analysis context.
577*12c85518Srobert return DACtx->getStableStorageLocation(E);
578*12c85518Srobert }
579*12c85518Srobert
setStorageLocation(const ValueDecl & D,StorageLocation & Loc)580*12c85518Srobert void Environment::setStorageLocation(const ValueDecl &D, StorageLocation &Loc) {
581*12c85518Srobert assert(DeclToLoc.find(&D) == DeclToLoc.end());
582*12c85518Srobert DeclToLoc[&D] = &Loc;
583*12c85518Srobert }
584*12c85518Srobert
getStorageLocation(const ValueDecl & D,SkipPast SP) const585*12c85518Srobert StorageLocation *Environment::getStorageLocation(const ValueDecl &D,
586*12c85518Srobert SkipPast SP) const {
587*12c85518Srobert auto It = DeclToLoc.find(&D);
588*12c85518Srobert return It == DeclToLoc.end() ? nullptr : &skip(*It->second, SP);
589*12c85518Srobert }
590*12c85518Srobert
setStorageLocation(const Expr & E,StorageLocation & Loc)591*12c85518Srobert void Environment::setStorageLocation(const Expr &E, StorageLocation &Loc) {
592*12c85518Srobert const Expr &CanonE = ignoreCFGOmittedNodes(E);
593*12c85518Srobert assert(ExprToLoc.find(&CanonE) == ExprToLoc.end());
594*12c85518Srobert ExprToLoc[&CanonE] = &Loc;
595*12c85518Srobert }
596*12c85518Srobert
getStorageLocation(const Expr & E,SkipPast SP) const597*12c85518Srobert StorageLocation *Environment::getStorageLocation(const Expr &E,
598*12c85518Srobert SkipPast SP) const {
599*12c85518Srobert // FIXME: Add a test with parens.
600*12c85518Srobert auto It = ExprToLoc.find(&ignoreCFGOmittedNodes(E));
601*12c85518Srobert return It == ExprToLoc.end() ? nullptr : &skip(*It->second, SP);
602*12c85518Srobert }
603*12c85518Srobert
getThisPointeeStorageLocation() const604*12c85518Srobert StorageLocation *Environment::getThisPointeeStorageLocation() const {
605*12c85518Srobert return ThisPointeeLoc;
606*12c85518Srobert }
607*12c85518Srobert
getReturnStorageLocation() const608*12c85518Srobert StorageLocation *Environment::getReturnStorageLocation() const {
609*12c85518Srobert return ReturnLoc;
610*12c85518Srobert }
611*12c85518Srobert
getOrCreateNullPointerValue(QualType PointeeType)612*12c85518Srobert PointerValue &Environment::getOrCreateNullPointerValue(QualType PointeeType) {
613*12c85518Srobert return DACtx->getOrCreateNullPointerValue(PointeeType);
614*12c85518Srobert }
615*12c85518Srobert
setValue(const StorageLocation & Loc,Value & Val)616*12c85518Srobert void Environment::setValue(const StorageLocation &Loc, Value &Val) {
617*12c85518Srobert LocToVal[&Loc] = &Val;
618*12c85518Srobert
619*12c85518Srobert if (auto *StructVal = dyn_cast<StructValue>(&Val)) {
620*12c85518Srobert auto &AggregateLoc = *cast<AggregateStorageLocation>(&Loc);
621*12c85518Srobert
622*12c85518Srobert const QualType Type = AggregateLoc.getType();
623*12c85518Srobert assert(Type->isStructureOrClassType() || Type->isUnionType());
624*12c85518Srobert
625*12c85518Srobert for (const FieldDecl *Field : DACtx->getReferencedFields(Type)) {
626*12c85518Srobert assert(Field != nullptr);
627*12c85518Srobert StorageLocation &FieldLoc = AggregateLoc.getChild(*Field);
628*12c85518Srobert MemberLocToStruct[&FieldLoc] = std::make_pair(StructVal, Field);
629*12c85518Srobert if (auto *FieldVal = StructVal->getChild(*Field))
630*12c85518Srobert setValue(FieldLoc, *FieldVal);
631*12c85518Srobert }
632*12c85518Srobert }
633*12c85518Srobert
634*12c85518Srobert auto It = MemberLocToStruct.find(&Loc);
635*12c85518Srobert if (It != MemberLocToStruct.end()) {
636*12c85518Srobert // `Loc` is the location of a struct member so we need to also update the
637*12c85518Srobert // value of the member in the corresponding `StructValue`.
638*12c85518Srobert
639*12c85518Srobert assert(It->second.first != nullptr);
640*12c85518Srobert StructValue &StructVal = *It->second.first;
641*12c85518Srobert
642*12c85518Srobert assert(It->second.second != nullptr);
643*12c85518Srobert const ValueDecl &Member = *It->second.second;
644*12c85518Srobert
645*12c85518Srobert StructVal.setChild(Member, Val);
646*12c85518Srobert }
647*12c85518Srobert }
648*12c85518Srobert
getValue(const StorageLocation & Loc) const649*12c85518Srobert Value *Environment::getValue(const StorageLocation &Loc) const {
650*12c85518Srobert auto It = LocToVal.find(&Loc);
651*12c85518Srobert return It == LocToVal.end() ? nullptr : It->second;
652*12c85518Srobert }
653*12c85518Srobert
getValue(const ValueDecl & D,SkipPast SP) const654*12c85518Srobert Value *Environment::getValue(const ValueDecl &D, SkipPast SP) const {
655*12c85518Srobert auto *Loc = getStorageLocation(D, SP);
656*12c85518Srobert if (Loc == nullptr)
657*12c85518Srobert return nullptr;
658*12c85518Srobert return getValue(*Loc);
659*12c85518Srobert }
660*12c85518Srobert
getValue(const Expr & E,SkipPast SP) const661*12c85518Srobert Value *Environment::getValue(const Expr &E, SkipPast SP) const {
662*12c85518Srobert auto *Loc = getStorageLocation(E, SP);
663*12c85518Srobert if (Loc == nullptr)
664*12c85518Srobert return nullptr;
665*12c85518Srobert return getValue(*Loc);
666*12c85518Srobert }
667*12c85518Srobert
createValue(QualType Type)668*12c85518Srobert Value *Environment::createValue(QualType Type) {
669*12c85518Srobert llvm::DenseSet<QualType> Visited;
670*12c85518Srobert int CreatedValuesCount = 0;
671*12c85518Srobert Value *Val = createValueUnlessSelfReferential(Type, Visited, /*Depth=*/0,
672*12c85518Srobert CreatedValuesCount);
673*12c85518Srobert if (CreatedValuesCount > MaxCompositeValueSize) {
674*12c85518Srobert llvm::errs() << "Attempting to initialize a huge value of type: " << Type
675*12c85518Srobert << '\n';
676*12c85518Srobert }
677*12c85518Srobert return Val;
678*12c85518Srobert }
679*12c85518Srobert
createValueUnlessSelfReferential(QualType Type,llvm::DenseSet<QualType> & Visited,int Depth,int & CreatedValuesCount)680*12c85518Srobert Value *Environment::createValueUnlessSelfReferential(
681*12c85518Srobert QualType Type, llvm::DenseSet<QualType> &Visited, int Depth,
682*12c85518Srobert int &CreatedValuesCount) {
683*12c85518Srobert assert(!Type.isNull());
684*12c85518Srobert
685*12c85518Srobert // Allow unlimited fields at depth 1; only cap at deeper nesting levels.
686*12c85518Srobert if ((Depth > 1 && CreatedValuesCount > MaxCompositeValueSize) ||
687*12c85518Srobert Depth > MaxCompositeValueDepth)
688*12c85518Srobert return nullptr;
689*12c85518Srobert
690*12c85518Srobert if (Type->isBooleanType()) {
691*12c85518Srobert CreatedValuesCount++;
692*12c85518Srobert return &makeAtomicBoolValue();
693*12c85518Srobert }
694*12c85518Srobert
695*12c85518Srobert if (Type->isIntegerType()) {
696*12c85518Srobert // FIXME: consider instead `return nullptr`, given that we do nothing useful
697*12c85518Srobert // with integers, and so distinguishing them serves no purpose, but could
698*12c85518Srobert // prevent convergence.
699*12c85518Srobert CreatedValuesCount++;
700*12c85518Srobert return &takeOwnership(std::make_unique<IntegerValue>());
701*12c85518Srobert }
702*12c85518Srobert
703*12c85518Srobert if (Type->isReferenceType()) {
704*12c85518Srobert CreatedValuesCount++;
705*12c85518Srobert QualType PointeeType = Type->castAs<ReferenceType>()->getPointeeType();
706*12c85518Srobert auto &PointeeLoc = createStorageLocation(PointeeType);
707*12c85518Srobert
708*12c85518Srobert if (Visited.insert(PointeeType.getCanonicalType()).second) {
709*12c85518Srobert Value *PointeeVal = createValueUnlessSelfReferential(
710*12c85518Srobert PointeeType, Visited, Depth, CreatedValuesCount);
711*12c85518Srobert Visited.erase(PointeeType.getCanonicalType());
712*12c85518Srobert
713*12c85518Srobert if (PointeeVal != nullptr)
714*12c85518Srobert setValue(PointeeLoc, *PointeeVal);
715*12c85518Srobert }
716*12c85518Srobert
717*12c85518Srobert return &takeOwnership(std::make_unique<ReferenceValue>(PointeeLoc));
718*12c85518Srobert }
719*12c85518Srobert
720*12c85518Srobert if (Type->isPointerType()) {
721*12c85518Srobert CreatedValuesCount++;
722*12c85518Srobert QualType PointeeType = Type->castAs<PointerType>()->getPointeeType();
723*12c85518Srobert auto &PointeeLoc = createStorageLocation(PointeeType);
724*12c85518Srobert
725*12c85518Srobert if (Visited.insert(PointeeType.getCanonicalType()).second) {
726*12c85518Srobert Value *PointeeVal = createValueUnlessSelfReferential(
727*12c85518Srobert PointeeType, Visited, Depth, CreatedValuesCount);
728*12c85518Srobert Visited.erase(PointeeType.getCanonicalType());
729*12c85518Srobert
730*12c85518Srobert if (PointeeVal != nullptr)
731*12c85518Srobert setValue(PointeeLoc, *PointeeVal);
732*12c85518Srobert }
733*12c85518Srobert
734*12c85518Srobert return &takeOwnership(std::make_unique<PointerValue>(PointeeLoc));
735*12c85518Srobert }
736*12c85518Srobert
737*12c85518Srobert if (Type->isStructureOrClassType() || Type->isUnionType()) {
738*12c85518Srobert CreatedValuesCount++;
739*12c85518Srobert llvm::DenseMap<const ValueDecl *, Value *> FieldValues;
740*12c85518Srobert for (const FieldDecl *Field : DACtx->getReferencedFields(Type)) {
741*12c85518Srobert assert(Field != nullptr);
742*12c85518Srobert
743*12c85518Srobert QualType FieldType = Field->getType();
744*12c85518Srobert if (Visited.contains(FieldType.getCanonicalType()))
745*12c85518Srobert continue;
746*12c85518Srobert
747*12c85518Srobert Visited.insert(FieldType.getCanonicalType());
748*12c85518Srobert if (auto *FieldValue = createValueUnlessSelfReferential(
749*12c85518Srobert FieldType, Visited, Depth + 1, CreatedValuesCount))
750*12c85518Srobert FieldValues.insert({Field, FieldValue});
751*12c85518Srobert Visited.erase(FieldType.getCanonicalType());
752*12c85518Srobert }
753*12c85518Srobert
754*12c85518Srobert return &takeOwnership(
755*12c85518Srobert std::make_unique<StructValue>(std::move(FieldValues)));
756*12c85518Srobert }
757*12c85518Srobert
758*12c85518Srobert return nullptr;
759*12c85518Srobert }
760*12c85518Srobert
skip(StorageLocation & Loc,SkipPast SP) const761*12c85518Srobert StorageLocation &Environment::skip(StorageLocation &Loc, SkipPast SP) const {
762*12c85518Srobert switch (SP) {
763*12c85518Srobert case SkipPast::None:
764*12c85518Srobert return Loc;
765*12c85518Srobert case SkipPast::Reference:
766*12c85518Srobert // References cannot be chained so we only need to skip past one level of
767*12c85518Srobert // indirection.
768*12c85518Srobert if (auto *Val = dyn_cast_or_null<ReferenceValue>(getValue(Loc)))
769*12c85518Srobert return Val->getReferentLoc();
770*12c85518Srobert return Loc;
771*12c85518Srobert case SkipPast::ReferenceThenPointer:
772*12c85518Srobert StorageLocation &LocPastRef = skip(Loc, SkipPast::Reference);
773*12c85518Srobert if (auto *Val = dyn_cast_or_null<PointerValue>(getValue(LocPastRef)))
774*12c85518Srobert return Val->getPointeeLoc();
775*12c85518Srobert return LocPastRef;
776*12c85518Srobert }
777*12c85518Srobert llvm_unreachable("bad SkipPast kind");
778*12c85518Srobert }
779*12c85518Srobert
skip(const StorageLocation & Loc,SkipPast SP) const780*12c85518Srobert const StorageLocation &Environment::skip(const StorageLocation &Loc,
781*12c85518Srobert SkipPast SP) const {
782*12c85518Srobert return skip(*const_cast<StorageLocation *>(&Loc), SP);
783*12c85518Srobert }
784*12c85518Srobert
addToFlowCondition(BoolValue & Val)785*12c85518Srobert void Environment::addToFlowCondition(BoolValue &Val) {
786*12c85518Srobert DACtx->addFlowConditionConstraint(*FlowConditionToken, Val);
787*12c85518Srobert }
788*12c85518Srobert
flowConditionImplies(BoolValue & Val) const789*12c85518Srobert bool Environment::flowConditionImplies(BoolValue &Val) const {
790*12c85518Srobert return DACtx->flowConditionImplies(*FlowConditionToken, Val);
791*12c85518Srobert }
792*12c85518Srobert
dump(raw_ostream & OS) const793*12c85518Srobert void Environment::dump(raw_ostream &OS) const {
794*12c85518Srobert // FIXME: add printing for remaining fields and allow caller to decide what
795*12c85518Srobert // fields are printed.
796*12c85518Srobert OS << "DeclToLoc:\n";
797*12c85518Srobert for (auto [D, L] : DeclToLoc)
798*12c85518Srobert OS << " [" << D->getName() << ", " << L << "]\n";
799*12c85518Srobert
800*12c85518Srobert OS << "ExprToLoc:\n";
801*12c85518Srobert for (auto [E, L] : ExprToLoc)
802*12c85518Srobert OS << " [" << E << ", " << L << "]\n";
803*12c85518Srobert
804*12c85518Srobert OS << "LocToVal:\n";
805*12c85518Srobert for (auto [L, V] : LocToVal) {
806*12c85518Srobert OS << " [" << L << ", " << V << ": " << *V << "]\n";
807*12c85518Srobert }
808*12c85518Srobert
809*12c85518Srobert OS << "FlowConditionToken:\n";
810*12c85518Srobert DACtx->dumpFlowCondition(*FlowConditionToken);
811*12c85518Srobert }
812*12c85518Srobert
dump() const813*12c85518Srobert void Environment::dump() const {
814*12c85518Srobert dump(llvm::dbgs());
815*12c85518Srobert }
816*12c85518Srobert
817*12c85518Srobert } // namespace dataflow
818*12c85518Srobert } // namespace clang
819