xref: /llvm-project/clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp (revision 2cb51449f0d9ed06de87b4a47b5074eb6eec2e23)
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), DeclCtx(Other.DeclCtx), ReturnLoc(Other.ReturnLoc),
158       ThisPointeeLoc(Other.ThisPointeeLoc), DeclToLoc(Other.DeclToLoc),
159       ExprToLoc(Other.ExprToLoc), LocToVal(Other.LocToVal),
160       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 &DeclCtxArg)
172     : Environment(DACtx) {
173   setDeclCtx(&DeclCtxArg);
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 Environment Environment::pushCall(const CallExpr *Call) const {
209   Environment Env(*this);
210   // FIXME: Support references here.
211   Env.ReturnLoc = Env.getStorageLocation(*Call, SkipPast::Reference);
212 
213   const auto *FuncDecl = Call->getDirectCallee();
214   assert(FuncDecl != nullptr);
215 
216   Env.setDeclCtx(FuncDecl);
217 
218   // FIXME: In order to allow the callee to reference globals, we probably need
219   // to call `initGlobalVars` here in some way.
220 
221   if (const auto *MethodCall = dyn_cast<CXXMemberCallExpr>(Call)) {
222     if (const Expr *Arg = MethodCall->getImplicitObjectArgument()) {
223       Env.ThisPointeeLoc = Env.getStorageLocation(*Arg, SkipPast::Reference);
224     }
225   }
226 
227   auto ParamIt = FuncDecl->param_begin();
228   auto ArgIt = Call->arg_begin();
229   auto ArgEnd = Call->arg_end();
230 
231   // FIXME: Parameters don't always map to arguments 1:1; examples include
232   // overloaded operators implemented as member functions, and parameter packs.
233   for (; ArgIt != ArgEnd; ++ParamIt, ++ArgIt) {
234     assert(ParamIt != FuncDecl->param_end());
235 
236     const Expr *Arg = *ArgIt;
237     auto *ArgLoc = Env.getStorageLocation(*Arg, SkipPast::Reference);
238     assert(ArgLoc != nullptr);
239 
240     const VarDecl *Param = *ParamIt;
241     auto &Loc = Env.createStorageLocation(*Param);
242     Env.setStorageLocation(*Param, Loc);
243 
244     QualType ParamType = Param->getType();
245     if (ParamType->isReferenceType()) {
246       auto &Val = Env.takeOwnership(std::make_unique<ReferenceValue>(*ArgLoc));
247       Env.setValue(Loc, Val);
248     } else if (auto *ArgVal = Env.getValue(*ArgLoc)) {
249       Env.setValue(Loc, *ArgVal);
250     } else if (Value *Val = Env.createValue(ParamType)) {
251       Env.setValue(Loc, *Val);
252     }
253   }
254 
255   return Env;
256 }
257 
258 void Environment::popCall(const Environment &CalleeEnv) {
259   // We ignore `DACtx` because it's already the same in both. We don't want the
260   // callee's `DeclCtx`, `ReturnLoc` or `ThisPointeeLoc`. We don't bring back
261   // `DeclToLoc` and `ExprToLoc` because we want to be able to later analyze the
262   // same callee in a different context, and `setStorageLocation` requires there
263   // to not already be a storage location assigned. Conceptually, these maps
264   // capture information from the local scope, so when popping that scope, we do
265   // not propagate the maps.
266   this->LocToVal = std::move(CalleeEnv.LocToVal);
267   this->MemberLocToStruct = std::move(CalleeEnv.MemberLocToStruct);
268   this->FlowConditionToken = std::move(CalleeEnv.FlowConditionToken);
269 }
270 
271 bool Environment::equivalentTo(const Environment &Other,
272                                Environment::ValueModel &Model) const {
273   assert(DACtx == Other.DACtx);
274 
275   if (ReturnLoc != Other.ReturnLoc)
276     return false;
277 
278   if (ThisPointeeLoc != Other.ThisPointeeLoc)
279     return false;
280 
281   if (DeclToLoc != Other.DeclToLoc)
282     return false;
283 
284   if (ExprToLoc != Other.ExprToLoc)
285     return false;
286 
287   // Compare the contents for the intersection of their domains.
288   for (auto &Entry : LocToVal) {
289     const StorageLocation *Loc = Entry.first;
290     assert(Loc != nullptr);
291 
292     Value *Val = Entry.second;
293     assert(Val != nullptr);
294 
295     auto It = Other.LocToVal.find(Loc);
296     if (It == Other.LocToVal.end())
297       continue;
298     assert(It->second != nullptr);
299 
300     if (!equivalentValues(Loc->getType(), Val, *this, It->second, Other, Model))
301       return false;
302   }
303 
304   return true;
305 }
306 
307 LatticeJoinEffect Environment::join(const Environment &Other,
308                                     Environment::ValueModel &Model) {
309   assert(DACtx == Other.DACtx);
310   assert(ReturnLoc == Other.ReturnLoc);
311   assert(ThisPointeeLoc == Other.ThisPointeeLoc);
312   assert(DeclCtx == Other.DeclCtx);
313 
314   auto Effect = LatticeJoinEffect::Unchanged;
315 
316   Environment JoinedEnv(*DACtx);
317 
318   JoinedEnv.setDeclCtx(DeclCtx);
319   JoinedEnv.ReturnLoc = ReturnLoc;
320   JoinedEnv.ThisPointeeLoc = ThisPointeeLoc;
321 
322   JoinedEnv.DeclToLoc = intersectDenseMaps(DeclToLoc, Other.DeclToLoc);
323   if (DeclToLoc.size() != JoinedEnv.DeclToLoc.size())
324     Effect = LatticeJoinEffect::Changed;
325 
326   JoinedEnv.ExprToLoc = intersectDenseMaps(ExprToLoc, Other.ExprToLoc);
327   if (ExprToLoc.size() != JoinedEnv.ExprToLoc.size())
328     Effect = LatticeJoinEffect::Changed;
329 
330   JoinedEnv.MemberLocToStruct =
331       intersectDenseMaps(MemberLocToStruct, Other.MemberLocToStruct);
332   if (MemberLocToStruct.size() != JoinedEnv.MemberLocToStruct.size())
333     Effect = LatticeJoinEffect::Changed;
334 
335   // FIXME: set `Effect` as needed.
336   JoinedEnv.FlowConditionToken = &DACtx->joinFlowConditions(
337       *FlowConditionToken, *Other.FlowConditionToken);
338 
339   for (auto &Entry : LocToVal) {
340     const StorageLocation *Loc = Entry.first;
341     assert(Loc != nullptr);
342 
343     Value *Val = Entry.second;
344     assert(Val != nullptr);
345 
346     auto It = Other.LocToVal.find(Loc);
347     if (It == Other.LocToVal.end())
348       continue;
349     assert(It->second != nullptr);
350 
351     if (Val == It->second) {
352       JoinedEnv.LocToVal.insert({Loc, Val});
353       continue;
354     }
355 
356     if (Value *MergedVal = mergeDistinctValues(
357             Loc->getType(), Val, *this, It->second, Other, JoinedEnv, Model))
358       JoinedEnv.LocToVal.insert({Loc, MergedVal});
359   }
360   if (LocToVal.size() != JoinedEnv.LocToVal.size())
361     Effect = LatticeJoinEffect::Changed;
362 
363   *this = std::move(JoinedEnv);
364 
365   return Effect;
366 }
367 
368 StorageLocation &Environment::createStorageLocation(QualType Type) {
369   return DACtx->createStorageLocation(Type);
370 }
371 
372 StorageLocation &Environment::createStorageLocation(const VarDecl &D) {
373   // Evaluated declarations are always assigned the same storage locations to
374   // ensure that the environment stabilizes across loop iterations. Storage
375   // locations for evaluated declarations are stored in the analysis context.
376   return DACtx->getStableStorageLocation(D);
377 }
378 
379 StorageLocation &Environment::createStorageLocation(const Expr &E) {
380   // Evaluated expressions are always assigned the same storage locations to
381   // ensure that the environment stabilizes across loop iterations. Storage
382   // locations for evaluated expressions are stored in the analysis context.
383   return DACtx->getStableStorageLocation(E);
384 }
385 
386 void Environment::setStorageLocation(const ValueDecl &D, StorageLocation &Loc) {
387   assert(DeclToLoc.find(&D) == DeclToLoc.end());
388   DeclToLoc[&D] = &Loc;
389 }
390 
391 StorageLocation *Environment::getStorageLocation(const ValueDecl &D,
392                                                  SkipPast SP) const {
393   auto It = DeclToLoc.find(&D);
394   return It == DeclToLoc.end() ? nullptr : &skip(*It->second, SP);
395 }
396 
397 void Environment::setStorageLocation(const Expr &E, StorageLocation &Loc) {
398   const Expr &CanonE = ignoreCFGOmittedNodes(E);
399   assert(ExprToLoc.find(&CanonE) == ExprToLoc.end());
400   ExprToLoc[&CanonE] = &Loc;
401 }
402 
403 StorageLocation *Environment::getStorageLocation(const Expr &E,
404                                                  SkipPast SP) const {
405   // FIXME: Add a test with parens.
406   auto It = ExprToLoc.find(&ignoreCFGOmittedNodes(E));
407   return It == ExprToLoc.end() ? nullptr : &skip(*It->second, SP);
408 }
409 
410 StorageLocation *Environment::getThisPointeeStorageLocation() const {
411   return ThisPointeeLoc;
412 }
413 
414 StorageLocation *Environment::getReturnStorageLocation() const {
415   return ReturnLoc;
416 }
417 
418 PointerValue &Environment::getOrCreateNullPointerValue(QualType PointeeType) {
419   return DACtx->getOrCreateNullPointerValue(PointeeType);
420 }
421 
422 void Environment::setValue(const StorageLocation &Loc, Value &Val) {
423   LocToVal[&Loc] = &Val;
424 
425   if (auto *StructVal = dyn_cast<StructValue>(&Val)) {
426     auto &AggregateLoc = *cast<AggregateStorageLocation>(&Loc);
427 
428     const QualType Type = AggregateLoc.getType();
429     assert(Type->isStructureOrClassType());
430 
431     for (const FieldDecl *Field : getObjectFields(Type)) {
432       assert(Field != nullptr);
433       StorageLocation &FieldLoc = AggregateLoc.getChild(*Field);
434       MemberLocToStruct[&FieldLoc] = std::make_pair(StructVal, Field);
435       if (auto *FieldVal = StructVal->getChild(*Field))
436         setValue(FieldLoc, *FieldVal);
437     }
438   }
439 
440   auto It = MemberLocToStruct.find(&Loc);
441   if (It != MemberLocToStruct.end()) {
442     // `Loc` is the location of a struct member so we need to also update the
443     // value of the member in the corresponding `StructValue`.
444 
445     assert(It->second.first != nullptr);
446     StructValue &StructVal = *It->second.first;
447 
448     assert(It->second.second != nullptr);
449     const ValueDecl &Member = *It->second.second;
450 
451     StructVal.setChild(Member, Val);
452   }
453 }
454 
455 Value *Environment::getValue(const StorageLocation &Loc) const {
456   auto It = LocToVal.find(&Loc);
457   return It == LocToVal.end() ? nullptr : It->second;
458 }
459 
460 Value *Environment::getValue(const ValueDecl &D, SkipPast SP) const {
461   auto *Loc = getStorageLocation(D, SP);
462   if (Loc == nullptr)
463     return nullptr;
464   return getValue(*Loc);
465 }
466 
467 Value *Environment::getValue(const Expr &E, SkipPast SP) const {
468   auto *Loc = getStorageLocation(E, SP);
469   if (Loc == nullptr)
470     return nullptr;
471   return getValue(*Loc);
472 }
473 
474 Value *Environment::createValue(QualType Type) {
475   llvm::DenseSet<QualType> Visited;
476   int CreatedValuesCount = 0;
477   Value *Val = createValueUnlessSelfReferential(Type, Visited, /*Depth=*/0,
478                                                 CreatedValuesCount);
479   if (CreatedValuesCount > MaxCompositeValueSize) {
480     llvm::errs() << "Attempting to initialize a huge value of type: " << Type
481                  << '\n';
482   }
483   return Val;
484 }
485 
486 Value *Environment::createValueUnlessSelfReferential(
487     QualType Type, llvm::DenseSet<QualType> &Visited, int Depth,
488     int &CreatedValuesCount) {
489   assert(!Type.isNull());
490 
491   // Allow unlimited fields at depth 1; only cap at deeper nesting levels.
492   if ((Depth > 1 && CreatedValuesCount > MaxCompositeValueSize) ||
493       Depth > MaxCompositeValueDepth)
494     return nullptr;
495 
496   if (Type->isBooleanType()) {
497     CreatedValuesCount++;
498     return &makeAtomicBoolValue();
499   }
500 
501   if (Type->isIntegerType()) {
502     CreatedValuesCount++;
503     return &takeOwnership(std::make_unique<IntegerValue>());
504   }
505 
506   if (Type->isReferenceType()) {
507     CreatedValuesCount++;
508     QualType PointeeType = Type->castAs<ReferenceType>()->getPointeeType();
509     auto &PointeeLoc = createStorageLocation(PointeeType);
510 
511     if (Visited.insert(PointeeType.getCanonicalType()).second) {
512       Value *PointeeVal = createValueUnlessSelfReferential(
513           PointeeType, Visited, Depth, CreatedValuesCount);
514       Visited.erase(PointeeType.getCanonicalType());
515 
516       if (PointeeVal != nullptr)
517         setValue(PointeeLoc, *PointeeVal);
518     }
519 
520     return &takeOwnership(std::make_unique<ReferenceValue>(PointeeLoc));
521   }
522 
523   if (Type->isPointerType()) {
524     CreatedValuesCount++;
525     QualType PointeeType = Type->castAs<PointerType>()->getPointeeType();
526     auto &PointeeLoc = createStorageLocation(PointeeType);
527 
528     if (Visited.insert(PointeeType.getCanonicalType()).second) {
529       Value *PointeeVal = createValueUnlessSelfReferential(
530           PointeeType, Visited, Depth, CreatedValuesCount);
531       Visited.erase(PointeeType.getCanonicalType());
532 
533       if (PointeeVal != nullptr)
534         setValue(PointeeLoc, *PointeeVal);
535     }
536 
537     return &takeOwnership(std::make_unique<PointerValue>(PointeeLoc));
538   }
539 
540   if (Type->isStructureOrClassType()) {
541     CreatedValuesCount++;
542     // FIXME: Initialize only fields that are accessed in the context that is
543     // being analyzed.
544     llvm::DenseMap<const ValueDecl *, Value *> FieldValues;
545     for (const FieldDecl *Field : getObjectFields(Type)) {
546       assert(Field != nullptr);
547 
548       QualType FieldType = Field->getType();
549       if (Visited.contains(FieldType.getCanonicalType()))
550         continue;
551 
552       Visited.insert(FieldType.getCanonicalType());
553       if (auto *FieldValue = createValueUnlessSelfReferential(
554               FieldType, Visited, Depth + 1, CreatedValuesCount))
555         FieldValues.insert({Field, FieldValue});
556       Visited.erase(FieldType.getCanonicalType());
557     }
558 
559     return &takeOwnership(
560         std::make_unique<StructValue>(std::move(FieldValues)));
561   }
562 
563   return nullptr;
564 }
565 
566 StorageLocation &Environment::skip(StorageLocation &Loc, SkipPast SP) const {
567   switch (SP) {
568   case SkipPast::None:
569     return Loc;
570   case SkipPast::Reference:
571     // References cannot be chained so we only need to skip past one level of
572     // indirection.
573     if (auto *Val = dyn_cast_or_null<ReferenceValue>(getValue(Loc)))
574       return Val->getReferentLoc();
575     return Loc;
576   case SkipPast::ReferenceThenPointer:
577     StorageLocation &LocPastRef = skip(Loc, SkipPast::Reference);
578     if (auto *Val = dyn_cast_or_null<PointerValue>(getValue(LocPastRef)))
579       return Val->getPointeeLoc();
580     return LocPastRef;
581   }
582   llvm_unreachable("bad SkipPast kind");
583 }
584 
585 const StorageLocation &Environment::skip(const StorageLocation &Loc,
586                                          SkipPast SP) const {
587   return skip(*const_cast<StorageLocation *>(&Loc), SP);
588 }
589 
590 void Environment::addToFlowCondition(BoolValue &Val) {
591   DACtx->addFlowConditionConstraint(*FlowConditionToken, Val);
592 }
593 
594 bool Environment::flowConditionImplies(BoolValue &Val) const {
595   return DACtx->flowConditionImplies(*FlowConditionToken, Val);
596 }
597 
598 void Environment::dump() const {
599   DACtx->dumpFlowCondition(*FlowConditionToken);
600 }
601 
602 } // namespace dataflow
603 } // namespace clang
604