xref: /llvm-project/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp (revision 2efc8f8d6561421c3f47de2f27a365ddfb734425)
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/Support/Casting.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include <cassert>
26 #include <memory>
27 #include <utility>
28 
29 namespace clang {
30 namespace dataflow {
31 
32 // FIXME: convert these to parameters of the analysis or environment. Current
33 // settings have been experimentaly validated, but only for a particular
34 // analysis.
35 static constexpr int MaxCompositeValueDepth = 3;
36 static constexpr int MaxCompositeValueSize = 1000;
37 
38 /// Returns a map consisting of key-value entries that are present in both maps.
39 template <typename K, typename V>
40 llvm::DenseMap<K, V> intersectDenseMaps(const llvm::DenseMap<K, V> &Map1,
41                                         const llvm::DenseMap<K, V> &Map2) {
42   llvm::DenseMap<K, V> Result;
43   for (auto &Entry : Map1) {
44     auto It = Map2.find(Entry.first);
45     if (It != Map2.end() && Entry.second == It->second)
46       Result.insert({Entry.first, Entry.second});
47   }
48   return Result;
49 }
50 
51 static bool areEquivalentIndirectionValues(Value *Val1, Value *Val2) {
52   if (auto *IndVal1 = dyn_cast<ReferenceValue>(Val1)) {
53     auto *IndVal2 = cast<ReferenceValue>(Val2);
54     return &IndVal1->getReferentLoc() == &IndVal2->getReferentLoc();
55   }
56   if (auto *IndVal1 = dyn_cast<PointerValue>(Val1)) {
57     auto *IndVal2 = cast<PointerValue>(Val2);
58     return &IndVal1->getPointeeLoc() == &IndVal2->getPointeeLoc();
59   }
60   return false;
61 }
62 
63 /// Returns true if and only if `Val1` is equivalent to `Val2`.
64 static bool equivalentValues(QualType Type, Value *Val1,
65                              const Environment &Env1, Value *Val2,
66                              const Environment &Env2,
67                              Environment::ValueModel &Model) {
68   return Val1 == Val2 || areEquivalentIndirectionValues(Val1, Val2) ||
69          Model.compareEquivalent(Type, *Val1, Env1, *Val2, Env2);
70 }
71 
72 /// Attempts to merge distinct values `Val1` and `Val2` in `Env1` and `Env2`,
73 /// respectively, of the same type `Type`. Merging generally produces a single
74 /// value that (soundly) approximates the two inputs, although the actual
75 /// meaning depends on `Model`.
76 static Value *mergeDistinctValues(QualType Type, Value *Val1,
77                                   const Environment &Env1, Value *Val2,
78                                   const Environment &Env2,
79                                   Environment &MergedEnv,
80                                   Environment::ValueModel &Model) {
81   // Join distinct boolean values preserving information about the constraints
82   // in the respective path conditions.
83   //
84   // FIXME: Does not work for backedges, since the two (or more) paths will not
85   // have mutually exclusive conditions.
86   if (auto *Expr1 = dyn_cast<BoolValue>(Val1)) {
87     auto *Expr2 = cast<BoolValue>(Val2);
88     auto &MergedVal = MergedEnv.makeAtomicBoolValue();
89     MergedEnv.addToFlowCondition(MergedEnv.makeOr(
90         MergedEnv.makeAnd(Env1.getFlowConditionToken(),
91                           MergedEnv.makeIff(MergedVal, *Expr1)),
92         MergedEnv.makeAnd(Env2.getFlowConditionToken(),
93                           MergedEnv.makeIff(MergedVal, *Expr2))));
94     return &MergedVal;
95   }
96 
97   // FIXME: add unit tests that cover this statement.
98   if (areEquivalentIndirectionValues(Val1, Val2)) {
99     return Val1;
100   }
101 
102   // FIXME: Consider destroying `MergedValue` immediately if `ValueModel::merge`
103   // returns false to avoid storing unneeded values in `DACtx`.
104   if (Value *MergedVal = MergedEnv.createValue(Type))
105     if (Model.merge(Type, *Val1, Env1, *Val2, Env2, *MergedVal, MergedEnv))
106       return MergedVal;
107 
108   return nullptr;
109 }
110 
111 /// Initializes a global storage value.
112 static void initGlobalVar(const VarDecl &D, Environment &Env) {
113   if (!D.hasGlobalStorage() ||
114       Env.getStorageLocation(D, SkipPast::None) != nullptr)
115     return;
116 
117   auto &Loc = Env.createStorageLocation(D);
118   Env.setStorageLocation(D, Loc);
119   if (auto *Val = Env.createValue(D.getType()))
120     Env.setValue(Loc, *Val);
121 }
122 
123 /// Initializes a global storage value.
124 static void initGlobalVar(const Decl &D, Environment &Env) {
125   if (auto *V = dyn_cast<VarDecl>(&D))
126     initGlobalVar(*V, Env);
127 }
128 
129 /// Initializes global storage values that are declared or referenced from
130 /// sub-statements of `S`.
131 // FIXME: Add support for resetting globals after function calls to enable
132 // the implementation of sound analyses.
133 static void initGlobalVars(const Stmt &S, Environment &Env) {
134   for (auto *Child : S.children()) {
135     if (Child != nullptr)
136       initGlobalVars(*Child, Env);
137   }
138 
139   if (auto *DS = dyn_cast<DeclStmt>(&S)) {
140     if (DS->isSingleDecl()) {
141       initGlobalVar(*DS->getSingleDecl(), Env);
142     } else {
143       for (auto *D : DS->getDeclGroup())
144         initGlobalVar(*D, Env);
145     }
146   } else if (auto *E = dyn_cast<DeclRefExpr>(&S)) {
147     initGlobalVar(*E->getDecl(), Env);
148   } else if (auto *E = dyn_cast<MemberExpr>(&S)) {
149     initGlobalVar(*E->getMemberDecl(), Env);
150   }
151 }
152 
153 Environment::Environment(DataflowAnalysisContext &DACtx)
154     : DACtx(&DACtx), FlowConditionToken(&DACtx.makeFlowConditionToken()) {}
155 
156 Environment::Environment(const Environment &Other)
157     : DACtx(Other.DACtx), CallStack(Other.CallStack),
158       ReturnLoc(Other.ReturnLoc), ThisPointeeLoc(Other.ThisPointeeLoc),
159       DeclToLoc(Other.DeclToLoc), ExprToLoc(Other.ExprToLoc),
160       LocToVal(Other.LocToVal), MemberLocToStruct(Other.MemberLocToStruct),
161       FlowConditionToken(&DACtx->forkFlowCondition(*Other.FlowConditionToken)) {
162 }
163 
164 Environment &Environment::operator=(const Environment &Other) {
165   Environment Copy(Other);
166   *this = std::move(Copy);
167   return *this;
168 }
169 
170 Environment::Environment(DataflowAnalysisContext &DACtx,
171                          const DeclContext &DeclCtx)
172     : Environment(DACtx) {
173   CallStack.push_back(&DeclCtx);
174 
175   if (const auto *FuncDecl = dyn_cast<FunctionDecl>(&DeclCtx)) {
176     assert(FuncDecl->getBody() != nullptr);
177     initGlobalVars(*FuncDecl->getBody(), *this);
178     for (const auto *ParamDecl : FuncDecl->parameters()) {
179       assert(ParamDecl != nullptr);
180       auto &ParamLoc = createStorageLocation(*ParamDecl);
181       setStorageLocation(*ParamDecl, ParamLoc);
182       if (Value *ParamVal = createValue(ParamDecl->getType()))
183         setValue(ParamLoc, *ParamVal);
184     }
185 
186     QualType ReturnType = FuncDecl->getReturnType();
187     ReturnLoc = &createStorageLocation(ReturnType);
188   }
189 
190   if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(&DeclCtx)) {
191     auto *Parent = MethodDecl->getParent();
192     assert(Parent != nullptr);
193     if (Parent->isLambda())
194       MethodDecl = dyn_cast<CXXMethodDecl>(Parent->getDeclContext());
195 
196     if (MethodDecl && !MethodDecl->isStatic()) {
197       QualType ThisPointeeType = MethodDecl->getThisObjectType();
198       // FIXME: Add support for union types.
199       if (!ThisPointeeType->isUnionType()) {
200         ThisPointeeLoc = &createStorageLocation(ThisPointeeType);
201         if (Value *ThisPointeeVal = createValue(ThisPointeeType))
202           setValue(*ThisPointeeLoc, *ThisPointeeVal);
203       }
204     }
205   }
206 }
207 
208 bool Environment::canDescend(unsigned MaxDepth,
209                              const DeclContext *Callee) const {
210   return CallStack.size() <= MaxDepth &&
211          std::find(CallStack.begin(), CallStack.end(), Callee) ==
212              CallStack.end();
213 }
214 
215 Environment Environment::pushCall(const CallExpr *Call) const {
216   Environment Env(*this);
217 
218   // FIXME: Support references here.
219   Env.ReturnLoc = getStorageLocation(*Call, SkipPast::Reference);
220 
221   if (const auto *MethodCall = dyn_cast<CXXMemberCallExpr>(Call)) {
222     if (const Expr *Arg = MethodCall->getImplicitObjectArgument()) {
223       Env.ThisPointeeLoc = getStorageLocation(*Arg, SkipPast::Reference);
224     }
225   }
226 
227   Env.pushCallInternal(Call->getDirectCallee(),
228                        llvm::makeArrayRef(Call->getArgs(), Call->getNumArgs()));
229 
230   return Env;
231 }
232 
233 Environment Environment::pushCall(const CXXConstructExpr *Call) const {
234   Environment Env(*this);
235 
236   // FIXME: Support references here.
237   Env.ReturnLoc = getStorageLocation(*Call, SkipPast::Reference);
238 
239   Env.ThisPointeeLoc = Env.ReturnLoc;
240 
241   Env.pushCallInternal(Call->getConstructor(),
242                        llvm::makeArrayRef(Call->getArgs(), Call->getNumArgs()));
243 
244   return Env;
245 }
246 
247 void Environment::pushCallInternal(const FunctionDecl *FuncDecl,
248                                    ArrayRef<const Expr *> Args) {
249   CallStack.push_back(FuncDecl);
250 
251   // FIXME: In order to allow the callee to reference globals, we probably need
252   // to call `initGlobalVars` here in some way.
253 
254   auto ParamIt = FuncDecl->param_begin();
255 
256   // FIXME: Parameters don't always map to arguments 1:1; examples include
257   // overloaded operators implemented as member functions, and parameter packs.
258   for (unsigned ArgIndex = 0; ArgIndex < Args.size(); ++ParamIt, ++ArgIndex) {
259     assert(ParamIt != FuncDecl->param_end());
260 
261     const Expr *Arg = Args[ArgIndex];
262     auto *ArgLoc = getStorageLocation(*Arg, SkipPast::Reference);
263     if (ArgLoc == nullptr)
264       continue;
265 
266     const VarDecl *Param = *ParamIt;
267     auto &Loc = createStorageLocation(*Param);
268     setStorageLocation(*Param, Loc);
269 
270     QualType ParamType = Param->getType();
271     if (ParamType->isReferenceType()) {
272       auto &Val = takeOwnership(std::make_unique<ReferenceValue>(*ArgLoc));
273       setValue(Loc, Val);
274     } else if (auto *ArgVal = getValue(*ArgLoc)) {
275       setValue(Loc, *ArgVal);
276     } else if (Value *Val = createValue(ParamType)) {
277       setValue(Loc, *Val);
278     }
279   }
280 }
281 
282 void Environment::popCall(const Environment &CalleeEnv) {
283   // We ignore `DACtx` because it's already the same in both. We don't want the
284   // callee's `DeclCtx`, `ReturnLoc` or `ThisPointeeLoc`. We don't bring back
285   // `DeclToLoc` and `ExprToLoc` because we want to be able to later analyze the
286   // same callee in a different context, and `setStorageLocation` requires there
287   // to not already be a storage location assigned. Conceptually, these maps
288   // capture information from the local scope, so when popping that scope, we do
289   // not propagate the maps.
290   this->LocToVal = std::move(CalleeEnv.LocToVal);
291   this->MemberLocToStruct = std::move(CalleeEnv.MemberLocToStruct);
292   this->FlowConditionToken = std::move(CalleeEnv.FlowConditionToken);
293 }
294 
295 bool Environment::equivalentTo(const Environment &Other,
296                                Environment::ValueModel &Model) const {
297   assert(DACtx == Other.DACtx);
298 
299   if (ReturnLoc != Other.ReturnLoc)
300     return false;
301 
302   if (ThisPointeeLoc != Other.ThisPointeeLoc)
303     return false;
304 
305   if (DeclToLoc != Other.DeclToLoc)
306     return false;
307 
308   if (ExprToLoc != Other.ExprToLoc)
309     return false;
310 
311   // Compare the contents for the intersection of their domains.
312   for (auto &Entry : LocToVal) {
313     const StorageLocation *Loc = Entry.first;
314     assert(Loc != nullptr);
315 
316     Value *Val = Entry.second;
317     assert(Val != nullptr);
318 
319     auto It = Other.LocToVal.find(Loc);
320     if (It == Other.LocToVal.end())
321       continue;
322     assert(It->second != nullptr);
323 
324     if (!equivalentValues(Loc->getType(), Val, *this, It->second, Other, Model))
325       return false;
326   }
327 
328   return true;
329 }
330 
331 LatticeJoinEffect Environment::join(const Environment &Other,
332                                     Environment::ValueModel &Model) {
333   assert(DACtx == Other.DACtx);
334   assert(ReturnLoc == Other.ReturnLoc);
335   assert(ThisPointeeLoc == Other.ThisPointeeLoc);
336   assert(CallStack == Other.CallStack);
337 
338   auto Effect = LatticeJoinEffect::Unchanged;
339 
340   Environment JoinedEnv(*DACtx);
341 
342   JoinedEnv.CallStack = CallStack;
343   JoinedEnv.ReturnLoc = ReturnLoc;
344   JoinedEnv.ThisPointeeLoc = ThisPointeeLoc;
345 
346   JoinedEnv.DeclToLoc = intersectDenseMaps(DeclToLoc, Other.DeclToLoc);
347   if (DeclToLoc.size() != JoinedEnv.DeclToLoc.size())
348     Effect = LatticeJoinEffect::Changed;
349 
350   JoinedEnv.ExprToLoc = intersectDenseMaps(ExprToLoc, Other.ExprToLoc);
351   if (ExprToLoc.size() != JoinedEnv.ExprToLoc.size())
352     Effect = LatticeJoinEffect::Changed;
353 
354   JoinedEnv.MemberLocToStruct =
355       intersectDenseMaps(MemberLocToStruct, Other.MemberLocToStruct);
356   if (MemberLocToStruct.size() != JoinedEnv.MemberLocToStruct.size())
357     Effect = LatticeJoinEffect::Changed;
358 
359   // FIXME: set `Effect` as needed.
360   JoinedEnv.FlowConditionToken = &DACtx->joinFlowConditions(
361       *FlowConditionToken, *Other.FlowConditionToken);
362 
363   for (auto &Entry : LocToVal) {
364     const StorageLocation *Loc = Entry.first;
365     assert(Loc != nullptr);
366 
367     Value *Val = Entry.second;
368     assert(Val != nullptr);
369 
370     auto It = Other.LocToVal.find(Loc);
371     if (It == Other.LocToVal.end())
372       continue;
373     assert(It->second != nullptr);
374 
375     if (Val == It->second) {
376       JoinedEnv.LocToVal.insert({Loc, Val});
377       continue;
378     }
379 
380     if (Value *MergedVal = mergeDistinctValues(
381             Loc->getType(), Val, *this, It->second, Other, JoinedEnv, Model))
382       JoinedEnv.LocToVal.insert({Loc, MergedVal});
383   }
384   if (LocToVal.size() != JoinedEnv.LocToVal.size())
385     Effect = LatticeJoinEffect::Changed;
386 
387   *this = std::move(JoinedEnv);
388 
389   return Effect;
390 }
391 
392 StorageLocation &Environment::createStorageLocation(QualType Type) {
393   return DACtx->createStorageLocation(Type);
394 }
395 
396 StorageLocation &Environment::createStorageLocation(const VarDecl &D) {
397   // Evaluated declarations are always assigned the same storage locations to
398   // ensure that the environment stabilizes across loop iterations. Storage
399   // locations for evaluated declarations are stored in the analysis context.
400   return DACtx->getStableStorageLocation(D);
401 }
402 
403 StorageLocation &Environment::createStorageLocation(const Expr &E) {
404   // Evaluated expressions are always assigned the same storage locations to
405   // ensure that the environment stabilizes across loop iterations. Storage
406   // locations for evaluated expressions are stored in the analysis context.
407   return DACtx->getStableStorageLocation(E);
408 }
409 
410 void Environment::setStorageLocation(const ValueDecl &D, StorageLocation &Loc) {
411   assert(DeclToLoc.find(&D) == DeclToLoc.end());
412   DeclToLoc[&D] = &Loc;
413 }
414 
415 StorageLocation *Environment::getStorageLocation(const ValueDecl &D,
416                                                  SkipPast SP) const {
417   auto It = DeclToLoc.find(&D);
418   return It == DeclToLoc.end() ? nullptr : &skip(*It->second, SP);
419 }
420 
421 void Environment::setStorageLocation(const Expr &E, StorageLocation &Loc) {
422   const Expr &CanonE = ignoreCFGOmittedNodes(E);
423   assert(ExprToLoc.find(&CanonE) == ExprToLoc.end());
424   ExprToLoc[&CanonE] = &Loc;
425 }
426 
427 StorageLocation *Environment::getStorageLocation(const Expr &E,
428                                                  SkipPast SP) const {
429   // FIXME: Add a test with parens.
430   auto It = ExprToLoc.find(&ignoreCFGOmittedNodes(E));
431   return It == ExprToLoc.end() ? nullptr : &skip(*It->second, SP);
432 }
433 
434 StorageLocation *Environment::getThisPointeeStorageLocation() const {
435   return ThisPointeeLoc;
436 }
437 
438 StorageLocation *Environment::getReturnStorageLocation() const {
439   return ReturnLoc;
440 }
441 
442 PointerValue &Environment::getOrCreateNullPointerValue(QualType PointeeType) {
443   return DACtx->getOrCreateNullPointerValue(PointeeType);
444 }
445 
446 void Environment::setValue(const StorageLocation &Loc, Value &Val) {
447   LocToVal[&Loc] = &Val;
448 
449   if (auto *StructVal = dyn_cast<StructValue>(&Val)) {
450     auto &AggregateLoc = *cast<AggregateStorageLocation>(&Loc);
451 
452     const QualType Type = AggregateLoc.getType();
453     assert(Type->isStructureOrClassType());
454 
455     for (const FieldDecl *Field : getObjectFields(Type)) {
456       assert(Field != nullptr);
457       StorageLocation &FieldLoc = AggregateLoc.getChild(*Field);
458       MemberLocToStruct[&FieldLoc] = std::make_pair(StructVal, Field);
459       if (auto *FieldVal = StructVal->getChild(*Field))
460         setValue(FieldLoc, *FieldVal);
461     }
462   }
463 
464   auto It = MemberLocToStruct.find(&Loc);
465   if (It != MemberLocToStruct.end()) {
466     // `Loc` is the location of a struct member so we need to also update the
467     // value of the member in the corresponding `StructValue`.
468 
469     assert(It->second.first != nullptr);
470     StructValue &StructVal = *It->second.first;
471 
472     assert(It->second.second != nullptr);
473     const ValueDecl &Member = *It->second.second;
474 
475     StructVal.setChild(Member, Val);
476   }
477 }
478 
479 Value *Environment::getValue(const StorageLocation &Loc) const {
480   auto It = LocToVal.find(&Loc);
481   return It == LocToVal.end() ? nullptr : It->second;
482 }
483 
484 Value *Environment::getValue(const ValueDecl &D, SkipPast SP) const {
485   auto *Loc = getStorageLocation(D, SP);
486   if (Loc == nullptr)
487     return nullptr;
488   return getValue(*Loc);
489 }
490 
491 Value *Environment::getValue(const Expr &E, SkipPast SP) const {
492   auto *Loc = getStorageLocation(E, SP);
493   if (Loc == nullptr)
494     return nullptr;
495   return getValue(*Loc);
496 }
497 
498 Value *Environment::createValue(QualType Type) {
499   llvm::DenseSet<QualType> Visited;
500   int CreatedValuesCount = 0;
501   Value *Val = createValueUnlessSelfReferential(Type, Visited, /*Depth=*/0,
502                                                 CreatedValuesCount);
503   if (CreatedValuesCount > MaxCompositeValueSize) {
504     llvm::errs() << "Attempting to initialize a huge value of type: " << Type
505                  << '\n';
506   }
507   return Val;
508 }
509 
510 Value *Environment::createValueUnlessSelfReferential(
511     QualType Type, llvm::DenseSet<QualType> &Visited, int Depth,
512     int &CreatedValuesCount) {
513   assert(!Type.isNull());
514 
515   // Allow unlimited fields at depth 1; only cap at deeper nesting levels.
516   if ((Depth > 1 && CreatedValuesCount > MaxCompositeValueSize) ||
517       Depth > MaxCompositeValueDepth)
518     return nullptr;
519 
520   if (Type->isBooleanType()) {
521     CreatedValuesCount++;
522     return &makeAtomicBoolValue();
523   }
524 
525   if (Type->isIntegerType()) {
526     CreatedValuesCount++;
527     return &takeOwnership(std::make_unique<IntegerValue>());
528   }
529 
530   if (Type->isReferenceType()) {
531     CreatedValuesCount++;
532     QualType PointeeType = Type->castAs<ReferenceType>()->getPointeeType();
533     auto &PointeeLoc = createStorageLocation(PointeeType);
534 
535     if (Visited.insert(PointeeType.getCanonicalType()).second) {
536       Value *PointeeVal = createValueUnlessSelfReferential(
537           PointeeType, Visited, Depth, CreatedValuesCount);
538       Visited.erase(PointeeType.getCanonicalType());
539 
540       if (PointeeVal != nullptr)
541         setValue(PointeeLoc, *PointeeVal);
542     }
543 
544     return &takeOwnership(std::make_unique<ReferenceValue>(PointeeLoc));
545   }
546 
547   if (Type->isPointerType()) {
548     CreatedValuesCount++;
549     QualType PointeeType = Type->castAs<PointerType>()->getPointeeType();
550     auto &PointeeLoc = createStorageLocation(PointeeType);
551 
552     if (Visited.insert(PointeeType.getCanonicalType()).second) {
553       Value *PointeeVal = createValueUnlessSelfReferential(
554           PointeeType, Visited, Depth, CreatedValuesCount);
555       Visited.erase(PointeeType.getCanonicalType());
556 
557       if (PointeeVal != nullptr)
558         setValue(PointeeLoc, *PointeeVal);
559     }
560 
561     return &takeOwnership(std::make_unique<PointerValue>(PointeeLoc));
562   }
563 
564   if (Type->isStructureOrClassType()) {
565     CreatedValuesCount++;
566     // FIXME: Initialize only fields that are accessed in the context that is
567     // being analyzed.
568     llvm::DenseMap<const ValueDecl *, Value *> FieldValues;
569     for (const FieldDecl *Field : getObjectFields(Type)) {
570       assert(Field != nullptr);
571 
572       QualType FieldType = Field->getType();
573       if (Visited.contains(FieldType.getCanonicalType()))
574         continue;
575 
576       Visited.insert(FieldType.getCanonicalType());
577       if (auto *FieldValue = createValueUnlessSelfReferential(
578               FieldType, Visited, Depth + 1, CreatedValuesCount))
579         FieldValues.insert({Field, FieldValue});
580       Visited.erase(FieldType.getCanonicalType());
581     }
582 
583     return &takeOwnership(
584         std::make_unique<StructValue>(std::move(FieldValues)));
585   }
586 
587   return nullptr;
588 }
589 
590 StorageLocation &Environment::skip(StorageLocation &Loc, SkipPast SP) const {
591   switch (SP) {
592   case SkipPast::None:
593     return Loc;
594   case SkipPast::Reference:
595     // References cannot be chained so we only need to skip past one level of
596     // indirection.
597     if (auto *Val = dyn_cast_or_null<ReferenceValue>(getValue(Loc)))
598       return Val->getReferentLoc();
599     return Loc;
600   case SkipPast::ReferenceThenPointer:
601     StorageLocation &LocPastRef = skip(Loc, SkipPast::Reference);
602     if (auto *Val = dyn_cast_or_null<PointerValue>(getValue(LocPastRef)))
603       return Val->getPointeeLoc();
604     return LocPastRef;
605   }
606   llvm_unreachable("bad SkipPast kind");
607 }
608 
609 const StorageLocation &Environment::skip(const StorageLocation &Loc,
610                                          SkipPast SP) const {
611   return skip(*const_cast<StorageLocation *>(&Loc), SP);
612 }
613 
614 void Environment::addToFlowCondition(BoolValue &Val) {
615   DACtx->addFlowConditionConstraint(*FlowConditionToken, Val);
616 }
617 
618 bool Environment::flowConditionImplies(BoolValue &Val) const {
619   return DACtx->flowConditionImplies(*FlowConditionToken, Val);
620 }
621 
622 void Environment::dump() const {
623   DACtx->dumpFlowCondition(*FlowConditionToken);
624 }
625 
626 } // namespace dataflow
627 } // namespace clang
628