xref: /llvm-project/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp (revision eda2eaabf2949c08ba94c92b9aad6fccb3c8eaa2)
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 initGlobalVar(const VarDecl &D, Environment &Env) {
155   if (!D.hasGlobalStorage() ||
156       Env.getStorageLocation(D, SkipPast::None) != nullptr)
157     return;
158 
159   auto &Loc = Env.createStorageLocation(D);
160   Env.setStorageLocation(D, Loc);
161   if (auto *Val = Env.createValue(D.getType()))
162     Env.setValue(Loc, *Val);
163 }
164 
165 /// Initializes a global storage value.
166 static void initGlobalVar(const Decl &D, Environment &Env) {
167   if (auto *V = dyn_cast<VarDecl>(&D))
168     initGlobalVar(*V, Env);
169 }
170 
171 /// Initializes global storage values that are declared or referenced from
172 /// sub-statements of `S`.
173 // FIXME: Add support for resetting globals after function calls to enable
174 // the implementation of sound analyses.
175 static void initGlobalVars(const Stmt &S, Environment &Env) {
176   for (auto *Child : S.children()) {
177     if (Child != nullptr)
178       initGlobalVars(*Child, Env);
179   }
180 
181   if (auto *DS = dyn_cast<DeclStmt>(&S)) {
182     if (DS->isSingleDecl()) {
183       initGlobalVar(*DS->getSingleDecl(), Env);
184     } else {
185       for (auto *D : DS->getDeclGroup())
186         initGlobalVar(*D, Env);
187     }
188   } else if (auto *E = dyn_cast<DeclRefExpr>(&S)) {
189     initGlobalVar(*E->getDecl(), Env);
190   } else if (auto *E = dyn_cast<MemberExpr>(&S)) {
191     initGlobalVar(*E->getMemberDecl(), Env);
192   }
193 }
194 
195 Environment::Environment(DataflowAnalysisContext &DACtx)
196     : DACtx(&DACtx), FlowConditionToken(&DACtx.makeFlowConditionToken()) {}
197 
198 Environment::Environment(const Environment &Other)
199     : DACtx(Other.DACtx), CallStack(Other.CallStack),
200       ReturnLoc(Other.ReturnLoc), ThisPointeeLoc(Other.ThisPointeeLoc),
201       DeclToLoc(Other.DeclToLoc), ExprToLoc(Other.ExprToLoc),
202       LocToVal(Other.LocToVal), MemberLocToStruct(Other.MemberLocToStruct),
203       FlowConditionToken(&DACtx->forkFlowCondition(*Other.FlowConditionToken)) {
204 }
205 
206 Environment &Environment::operator=(const Environment &Other) {
207   Environment Copy(Other);
208   *this = std::move(Copy);
209   return *this;
210 }
211 
212 Environment::Environment(DataflowAnalysisContext &DACtx,
213                          const DeclContext &DeclCtx)
214     : Environment(DACtx) {
215   CallStack.push_back(&DeclCtx);
216 
217   if (const auto *FuncDecl = dyn_cast<FunctionDecl>(&DeclCtx)) {
218     assert(FuncDecl->getBody() != nullptr);
219     initGlobalVars(*FuncDecl->getBody(), *this);
220     for (const auto *ParamDecl : FuncDecl->parameters()) {
221       assert(ParamDecl != nullptr);
222       auto &ParamLoc = createStorageLocation(*ParamDecl);
223       setStorageLocation(*ParamDecl, ParamLoc);
224       if (Value *ParamVal = createValue(ParamDecl->getType()))
225         setValue(ParamLoc, *ParamVal);
226     }
227 
228     QualType ReturnType = FuncDecl->getReturnType();
229     ReturnLoc = &createStorageLocation(ReturnType);
230   }
231 
232   if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(&DeclCtx)) {
233     auto *Parent = MethodDecl->getParent();
234     assert(Parent != nullptr);
235     if (Parent->isLambda())
236       MethodDecl = dyn_cast<CXXMethodDecl>(Parent->getDeclContext());
237 
238     if (MethodDecl && !MethodDecl->isStatic()) {
239       QualType ThisPointeeType = MethodDecl->getThisObjectType();
240       // FIXME: Add support for union types.
241       if (!ThisPointeeType->isUnionType()) {
242         ThisPointeeLoc = &createStorageLocation(ThisPointeeType);
243         if (Value *ThisPointeeVal = createValue(ThisPointeeType))
244           setValue(*ThisPointeeLoc, *ThisPointeeVal);
245       }
246     }
247   }
248 
249   // Look for global variable references in the constructor-initializers.
250   if (const auto *CtorDecl = dyn_cast<CXXConstructorDecl>(&DeclCtx)) {
251     for (const auto *Init : CtorDecl->inits()) {
252       const Expr *E = Init->getInit();
253       assert(E != nullptr);
254       initGlobalVars(*E, *this);
255     }
256   }
257 }
258 
259 bool Environment::canDescend(unsigned MaxDepth,
260                              const DeclContext *Callee) const {
261   return CallStack.size() <= MaxDepth && !llvm::is_contained(CallStack, Callee);
262 }
263 
264 Environment Environment::pushCall(const CallExpr *Call) const {
265   Environment Env(*this);
266 
267   // FIXME: Support references here.
268   Env.ReturnLoc = getStorageLocation(*Call, SkipPast::Reference);
269 
270   if (const auto *MethodCall = dyn_cast<CXXMemberCallExpr>(Call)) {
271     if (const Expr *Arg = MethodCall->getImplicitObjectArgument()) {
272       if (!isa<CXXThisExpr>(Arg))
273         Env.ThisPointeeLoc = getStorageLocation(*Arg, SkipPast::Reference);
274       // Otherwise (when the argument is `this`), retain the current
275       // environment's `ThisPointeeLoc`.
276     }
277   }
278 
279   Env.pushCallInternal(Call->getDirectCallee(),
280                        llvm::makeArrayRef(Call->getArgs(), Call->getNumArgs()));
281 
282   return Env;
283 }
284 
285 Environment Environment::pushCall(const CXXConstructExpr *Call) const {
286   Environment Env(*this);
287 
288   // FIXME: Support references here.
289   Env.ReturnLoc = getStorageLocation(*Call, SkipPast::Reference);
290 
291   Env.ThisPointeeLoc = Env.ReturnLoc;
292 
293   Env.pushCallInternal(Call->getConstructor(),
294                        llvm::makeArrayRef(Call->getArgs(), Call->getNumArgs()));
295 
296   return Env;
297 }
298 
299 void Environment::pushCallInternal(const FunctionDecl *FuncDecl,
300                                    ArrayRef<const Expr *> Args) {
301   CallStack.push_back(FuncDecl);
302 
303   // FIXME: In order to allow the callee to reference globals, we probably need
304   // to call `initGlobalVars` here in some way.
305 
306   auto ParamIt = FuncDecl->param_begin();
307 
308   // FIXME: Parameters don't always map to arguments 1:1; examples include
309   // overloaded operators implemented as member functions, and parameter packs.
310   for (unsigned ArgIndex = 0; ArgIndex < Args.size(); ++ParamIt, ++ArgIndex) {
311     assert(ParamIt != FuncDecl->param_end());
312 
313     const Expr *Arg = Args[ArgIndex];
314     auto *ArgLoc = getStorageLocation(*Arg, SkipPast::Reference);
315     if (ArgLoc == nullptr)
316       continue;
317 
318     const VarDecl *Param = *ParamIt;
319     auto &Loc = createStorageLocation(*Param);
320     setStorageLocation(*Param, Loc);
321 
322     QualType ParamType = Param->getType();
323     if (ParamType->isReferenceType()) {
324       auto &Val = takeOwnership(std::make_unique<ReferenceValue>(*ArgLoc));
325       setValue(Loc, Val);
326     } else if (auto *ArgVal = getValue(*ArgLoc)) {
327       setValue(Loc, *ArgVal);
328     } else if (Value *Val = createValue(ParamType)) {
329       setValue(Loc, *Val);
330     }
331   }
332 }
333 
334 void Environment::popCall(const Environment &CalleeEnv) {
335   // We ignore `DACtx` because it's already the same in both. We don't want the
336   // callee's `DeclCtx`, `ReturnLoc` or `ThisPointeeLoc`. We don't bring back
337   // `DeclToLoc` and `ExprToLoc` because we want to be able to later analyze the
338   // same callee in a different context, and `setStorageLocation` requires there
339   // to not already be a storage location assigned. Conceptually, these maps
340   // capture information from the local scope, so when popping that scope, we do
341   // not propagate the maps.
342   this->LocToVal = std::move(CalleeEnv.LocToVal);
343   this->MemberLocToStruct = std::move(CalleeEnv.MemberLocToStruct);
344   this->FlowConditionToken = std::move(CalleeEnv.FlowConditionToken);
345 }
346 
347 bool Environment::equivalentTo(const Environment &Other,
348                                Environment::ValueModel &Model) const {
349   assert(DACtx == Other.DACtx);
350 
351   if (ReturnLoc != Other.ReturnLoc)
352     return false;
353 
354   if (ThisPointeeLoc != Other.ThisPointeeLoc)
355     return false;
356 
357   if (DeclToLoc != Other.DeclToLoc)
358     return false;
359 
360   if (ExprToLoc != Other.ExprToLoc)
361     return false;
362 
363   // Compare the contents for the intersection of their domains.
364   for (auto &Entry : LocToVal) {
365     const StorageLocation *Loc = Entry.first;
366     assert(Loc != nullptr);
367 
368     Value *Val = Entry.second;
369     assert(Val != nullptr);
370 
371     auto It = Other.LocToVal.find(Loc);
372     if (It == Other.LocToVal.end())
373       continue;
374     assert(It->second != nullptr);
375 
376     if (!areEquivalentValues(*Val, *It->second) &&
377         !compareDistinctValues(Loc->getType(), *Val, *this, *It->second, Other,
378                                Model))
379       return false;
380   }
381 
382   return true;
383 }
384 
385 LatticeJoinEffect Environment::widen(const Environment &PrevEnv,
386                                      Environment::ValueModel &Model) {
387   assert(DACtx == PrevEnv.DACtx);
388   assert(ReturnLoc == PrevEnv.ReturnLoc);
389   assert(ThisPointeeLoc == PrevEnv.ThisPointeeLoc);
390   assert(CallStack == PrevEnv.CallStack);
391 
392   auto Effect = LatticeJoinEffect::Unchanged;
393 
394   // By the API, `PrevEnv` is a previous version of the environment for the same
395   // block, so we have some guarantees about its shape. In particular, it will
396   // be the result of a join or widen operation on previous values for this
397   // block. For `DeclToLoc` and `ExprToLoc`, join guarantees that these maps are
398   // subsets of the maps in `PrevEnv`. So, as long as we maintain this property
399   // here, we don't need change their current values to widen.
400   //
401   // FIXME: `MemberLocToStruct` does not share the above property, because
402   // `join` can cause the map size to increase (when we add fresh data in places
403   // of conflict). Once this issue with join is resolved, re-enable the
404   // assertion below or replace with something that captures the desired
405   // invariant.
406   assert(DeclToLoc.size() <= PrevEnv.DeclToLoc.size());
407   assert(ExprToLoc.size() <= PrevEnv.ExprToLoc.size());
408   // assert(MemberLocToStruct.size() <= PrevEnv.MemberLocToStruct.size());
409 
410   llvm::DenseMap<const StorageLocation *, Value *> WidenedLocToVal;
411   for (auto &Entry : LocToVal) {
412     const StorageLocation *Loc = Entry.first;
413     assert(Loc != nullptr);
414 
415     Value *Val = Entry.second;
416     assert(Val != nullptr);
417 
418     auto PrevIt = PrevEnv.LocToVal.find(Loc);
419     if (PrevIt == PrevEnv.LocToVal.end())
420       continue;
421     assert(PrevIt->second != nullptr);
422 
423     if (areEquivalentValues(*Val, *PrevIt->second)) {
424       WidenedLocToVal.insert({Loc, Val});
425       continue;
426     }
427 
428     Value &WidenedVal = widenDistinctValues(Loc->getType(), *PrevIt->second,
429                                             PrevEnv, *Val, *this, Model);
430     WidenedLocToVal.insert({Loc, &WidenedVal});
431     if (&WidenedVal != PrevIt->second)
432       Effect = LatticeJoinEffect::Changed;
433   }
434   LocToVal = std::move(WidenedLocToVal);
435   // FIXME: update the equivalence calculation for `MemberLocToStruct`, once we
436   // have a systematic way of soundly comparing this map.
437   if (DeclToLoc.size() != PrevEnv.DeclToLoc.size() ||
438       ExprToLoc.size() != PrevEnv.ExprToLoc.size() ||
439       LocToVal.size() != PrevEnv.LocToVal.size() ||
440       MemberLocToStruct.size() != PrevEnv.MemberLocToStruct.size())
441     Effect = LatticeJoinEffect::Changed;
442 
443   return Effect;
444 }
445 
446 LatticeJoinEffect Environment::join(const Environment &Other,
447                                     Environment::ValueModel &Model) {
448   assert(DACtx == Other.DACtx);
449   assert(ReturnLoc == Other.ReturnLoc);
450   assert(ThisPointeeLoc == Other.ThisPointeeLoc);
451   assert(CallStack == Other.CallStack);
452 
453   auto Effect = LatticeJoinEffect::Unchanged;
454 
455   Environment JoinedEnv(*DACtx);
456 
457   JoinedEnv.CallStack = CallStack;
458   JoinedEnv.ReturnLoc = ReturnLoc;
459   JoinedEnv.ThisPointeeLoc = ThisPointeeLoc;
460 
461   JoinedEnv.DeclToLoc = intersectDenseMaps(DeclToLoc, Other.DeclToLoc);
462   if (DeclToLoc.size() != JoinedEnv.DeclToLoc.size())
463     Effect = LatticeJoinEffect::Changed;
464 
465   JoinedEnv.ExprToLoc = intersectDenseMaps(ExprToLoc, Other.ExprToLoc);
466   if (ExprToLoc.size() != JoinedEnv.ExprToLoc.size())
467     Effect = LatticeJoinEffect::Changed;
468 
469   JoinedEnv.MemberLocToStruct =
470       intersectDenseMaps(MemberLocToStruct, Other.MemberLocToStruct);
471   if (MemberLocToStruct.size() != JoinedEnv.MemberLocToStruct.size())
472     Effect = LatticeJoinEffect::Changed;
473 
474   // FIXME: set `Effect` as needed.
475   // FIXME: update join to detect backedges and simplify the flow condition
476   // accordingly.
477   JoinedEnv.FlowConditionToken = &DACtx->joinFlowConditions(
478       *FlowConditionToken, *Other.FlowConditionToken);
479 
480   for (auto &Entry : LocToVal) {
481     const StorageLocation *Loc = Entry.first;
482     assert(Loc != nullptr);
483 
484     Value *Val = Entry.second;
485     assert(Val != nullptr);
486 
487     auto It = Other.LocToVal.find(Loc);
488     if (It == Other.LocToVal.end())
489       continue;
490     assert(It->second != nullptr);
491 
492     if (areEquivalentValues(*Val, *It->second)) {
493       JoinedEnv.LocToVal.insert({Loc, Val});
494       continue;
495     }
496 
497     if (Value *MergedVal =
498             mergeDistinctValues(Loc->getType(), *Val, *this, *It->second, Other,
499                                 JoinedEnv, Model)) {
500       JoinedEnv.LocToVal.insert({Loc, MergedVal});
501       Effect = LatticeJoinEffect::Changed;
502     }
503   }
504   if (LocToVal.size() != JoinedEnv.LocToVal.size())
505     Effect = LatticeJoinEffect::Changed;
506 
507   *this = std::move(JoinedEnv);
508 
509   return Effect;
510 }
511 
512 StorageLocation &Environment::createStorageLocation(QualType Type) {
513   return DACtx->createStorageLocation(Type);
514 }
515 
516 StorageLocation &Environment::createStorageLocation(const VarDecl &D) {
517   // Evaluated declarations are always assigned the same storage locations to
518   // ensure that the environment stabilizes across loop iterations. Storage
519   // locations for evaluated declarations are stored in the analysis context.
520   return DACtx->getStableStorageLocation(D);
521 }
522 
523 StorageLocation &Environment::createStorageLocation(const Expr &E) {
524   // Evaluated expressions are always assigned the same storage locations to
525   // ensure that the environment stabilizes across loop iterations. Storage
526   // locations for evaluated expressions are stored in the analysis context.
527   return DACtx->getStableStorageLocation(E);
528 }
529 
530 void Environment::setStorageLocation(const ValueDecl &D, StorageLocation &Loc) {
531   assert(DeclToLoc.find(&D) == DeclToLoc.end());
532   DeclToLoc[&D] = &Loc;
533 }
534 
535 StorageLocation *Environment::getStorageLocation(const ValueDecl &D,
536                                                  SkipPast SP) const {
537   auto It = DeclToLoc.find(&D);
538   return It == DeclToLoc.end() ? nullptr : &skip(*It->second, SP);
539 }
540 
541 void Environment::setStorageLocation(const Expr &E, StorageLocation &Loc) {
542   const Expr &CanonE = ignoreCFGOmittedNodes(E);
543   assert(ExprToLoc.find(&CanonE) == ExprToLoc.end());
544   ExprToLoc[&CanonE] = &Loc;
545 }
546 
547 StorageLocation *Environment::getStorageLocation(const Expr &E,
548                                                  SkipPast SP) const {
549   // FIXME: Add a test with parens.
550   auto It = ExprToLoc.find(&ignoreCFGOmittedNodes(E));
551   return It == ExprToLoc.end() ? nullptr : &skip(*It->second, SP);
552 }
553 
554 StorageLocation *Environment::getThisPointeeStorageLocation() const {
555   return ThisPointeeLoc;
556 }
557 
558 StorageLocation *Environment::getReturnStorageLocation() const {
559   return ReturnLoc;
560 }
561 
562 PointerValue &Environment::getOrCreateNullPointerValue(QualType PointeeType) {
563   return DACtx->getOrCreateNullPointerValue(PointeeType);
564 }
565 
566 void Environment::setValue(const StorageLocation &Loc, Value &Val) {
567   LocToVal[&Loc] = &Val;
568 
569   if (auto *StructVal = dyn_cast<StructValue>(&Val)) {
570     auto &AggregateLoc = *cast<AggregateStorageLocation>(&Loc);
571 
572     const QualType Type = AggregateLoc.getType();
573     assert(Type->isStructureOrClassType());
574 
575     for (const FieldDecl *Field : getObjectFields(Type)) {
576       assert(Field != nullptr);
577       StorageLocation &FieldLoc = AggregateLoc.getChild(*Field);
578       MemberLocToStruct[&FieldLoc] = std::make_pair(StructVal, Field);
579       if (auto *FieldVal = StructVal->getChild(*Field))
580         setValue(FieldLoc, *FieldVal);
581     }
582   }
583 
584   auto It = MemberLocToStruct.find(&Loc);
585   if (It != MemberLocToStruct.end()) {
586     // `Loc` is the location of a struct member so we need to also update the
587     // value of the member in the corresponding `StructValue`.
588 
589     assert(It->second.first != nullptr);
590     StructValue &StructVal = *It->second.first;
591 
592     assert(It->second.second != nullptr);
593     const ValueDecl &Member = *It->second.second;
594 
595     StructVal.setChild(Member, Val);
596   }
597 }
598 
599 Value *Environment::getValue(const StorageLocation &Loc) const {
600   auto It = LocToVal.find(&Loc);
601   return It == LocToVal.end() ? nullptr : It->second;
602 }
603 
604 Value *Environment::getValue(const ValueDecl &D, SkipPast SP) const {
605   auto *Loc = getStorageLocation(D, SP);
606   if (Loc == nullptr)
607     return nullptr;
608   return getValue(*Loc);
609 }
610 
611 Value *Environment::getValue(const Expr &E, SkipPast SP) const {
612   auto *Loc = getStorageLocation(E, SP);
613   if (Loc == nullptr)
614     return nullptr;
615   return getValue(*Loc);
616 }
617 
618 Value *Environment::createValue(QualType Type) {
619   llvm::DenseSet<QualType> Visited;
620   int CreatedValuesCount = 0;
621   Value *Val = createValueUnlessSelfReferential(Type, Visited, /*Depth=*/0,
622                                                 CreatedValuesCount);
623   if (CreatedValuesCount > MaxCompositeValueSize) {
624     llvm::errs() << "Attempting to initialize a huge value of type: " << Type
625                  << '\n';
626   }
627   return Val;
628 }
629 
630 Value *Environment::createValueUnlessSelfReferential(
631     QualType Type, llvm::DenseSet<QualType> &Visited, int Depth,
632     int &CreatedValuesCount) {
633   assert(!Type.isNull());
634 
635   // Allow unlimited fields at depth 1; only cap at deeper nesting levels.
636   if ((Depth > 1 && CreatedValuesCount > MaxCompositeValueSize) ||
637       Depth > MaxCompositeValueDepth)
638     return nullptr;
639 
640   if (Type->isBooleanType()) {
641     CreatedValuesCount++;
642     return &makeAtomicBoolValue();
643   }
644 
645   if (Type->isIntegerType()) {
646     // FIXME: consider instead `return nullptr`, given that we do nothing useful
647     // with integers, and so distinguishing them serves no purpose, but could
648     // prevent convergence.
649     CreatedValuesCount++;
650     return &takeOwnership(std::make_unique<IntegerValue>());
651   }
652 
653   if (Type->isReferenceType()) {
654     CreatedValuesCount++;
655     QualType PointeeType = Type->castAs<ReferenceType>()->getPointeeType();
656     auto &PointeeLoc = createStorageLocation(PointeeType);
657 
658     if (Visited.insert(PointeeType.getCanonicalType()).second) {
659       Value *PointeeVal = createValueUnlessSelfReferential(
660           PointeeType, Visited, Depth, CreatedValuesCount);
661       Visited.erase(PointeeType.getCanonicalType());
662 
663       if (PointeeVal != nullptr)
664         setValue(PointeeLoc, *PointeeVal);
665     }
666 
667     return &takeOwnership(std::make_unique<ReferenceValue>(PointeeLoc));
668   }
669 
670   if (Type->isPointerType()) {
671     CreatedValuesCount++;
672     QualType PointeeType = Type->castAs<PointerType>()->getPointeeType();
673     auto &PointeeLoc = createStorageLocation(PointeeType);
674 
675     if (Visited.insert(PointeeType.getCanonicalType()).second) {
676       Value *PointeeVal = createValueUnlessSelfReferential(
677           PointeeType, Visited, Depth, CreatedValuesCount);
678       Visited.erase(PointeeType.getCanonicalType());
679 
680       if (PointeeVal != nullptr)
681         setValue(PointeeLoc, *PointeeVal);
682     }
683 
684     return &takeOwnership(std::make_unique<PointerValue>(PointeeLoc));
685   }
686 
687   if (Type->isStructureOrClassType()) {
688     CreatedValuesCount++;
689     // FIXME: Initialize only fields that are accessed in the context that is
690     // being analyzed.
691     llvm::DenseMap<const ValueDecl *, Value *> FieldValues;
692     for (const FieldDecl *Field : getObjectFields(Type)) {
693       assert(Field != nullptr);
694 
695       QualType FieldType = Field->getType();
696       if (Visited.contains(FieldType.getCanonicalType()))
697         continue;
698 
699       Visited.insert(FieldType.getCanonicalType());
700       if (auto *FieldValue = createValueUnlessSelfReferential(
701               FieldType, Visited, Depth + 1, CreatedValuesCount))
702         FieldValues.insert({Field, FieldValue});
703       Visited.erase(FieldType.getCanonicalType());
704     }
705 
706     return &takeOwnership(
707         std::make_unique<StructValue>(std::move(FieldValues)));
708   }
709 
710   return nullptr;
711 }
712 
713 StorageLocation &Environment::skip(StorageLocation &Loc, SkipPast SP) const {
714   switch (SP) {
715   case SkipPast::None:
716     return Loc;
717   case SkipPast::Reference:
718     // References cannot be chained so we only need to skip past one level of
719     // indirection.
720     if (auto *Val = dyn_cast_or_null<ReferenceValue>(getValue(Loc)))
721       return Val->getReferentLoc();
722     return Loc;
723   case SkipPast::ReferenceThenPointer:
724     StorageLocation &LocPastRef = skip(Loc, SkipPast::Reference);
725     if (auto *Val = dyn_cast_or_null<PointerValue>(getValue(LocPastRef)))
726       return Val->getPointeeLoc();
727     return LocPastRef;
728   }
729   llvm_unreachable("bad SkipPast kind");
730 }
731 
732 const StorageLocation &Environment::skip(const StorageLocation &Loc,
733                                          SkipPast SP) const {
734   return skip(*const_cast<StorageLocation *>(&Loc), SP);
735 }
736 
737 void Environment::addToFlowCondition(BoolValue &Val) {
738   DACtx->addFlowConditionConstraint(*FlowConditionToken, Val);
739 }
740 
741 bool Environment::flowConditionImplies(BoolValue &Val) const {
742   return DACtx->flowConditionImplies(*FlowConditionToken, Val);
743 }
744 
745 void Environment::dump() const {
746   DACtx->dumpFlowCondition(*FlowConditionToken);
747 }
748 
749 } // namespace dataflow
750 } // namespace clang
751