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