clang  9.0.0svn
MemRegion.cpp
Go to the documentation of this file.
1 //===- MemRegion.cpp - Abstract memory regions for static analysis --------===//
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 MemRegion and its subclasses. MemRegion defines a
10 // partially-typed abstraction of memory useful for path-sensitive dataflow
11 // analyses.
12 //
13 //===----------------------------------------------------------------------===//
14 
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/Attr.h"
18 #include "clang/AST/CharUnits.h"
19 #include "clang/AST/Decl.h"
20 #include "clang/AST/DeclCXX.h"
21 #include "clang/AST/DeclObjC.h"
22 #include "clang/AST/Expr.h"
24 #include "clang/AST/RecordLayout.h"
25 #include "clang/AST/Type.h"
29 #include "clang/Basic/LLVM.h"
34 #include "llvm/ADT/APInt.h"
35 #include "llvm/ADT/FoldingSet.h"
36 #include "llvm/ADT/Optional.h"
37 #include "llvm/ADT/PointerUnion.h"
38 #include "llvm/ADT/SmallString.h"
39 #include "llvm/ADT/StringRef.h"
40 #include "llvm/ADT/Twine.h"
41 #include "llvm/Support/Allocator.h"
42 #include "llvm/Support/Casting.h"
43 #include "llvm/Support/CheckedArithmetic.h"
44 #include "llvm/Support/Compiler.h"
45 #include "llvm/Support/Debug.h"
46 #include "llvm/Support/ErrorHandling.h"
47 #include "llvm/Support/raw_ostream.h"
48 #include <cassert>
49 #include <cstdint>
50 #include <functional>
51 #include <iterator>
52 #include <string>
53 #include <tuple>
54 #include <utility>
55 
56 using namespace clang;
57 using namespace ento;
58 
59 #define DEBUG_TYPE "MemRegion"
60 
61 //===----------------------------------------------------------------------===//
62 // MemRegion Construction.
63 //===----------------------------------------------------------------------===//
64 
65 template <typename RegionTy, typename SuperTy, typename Arg1Ty>
66 RegionTy* MemRegionManager::getSubRegion(const Arg1Ty arg1,
67  const SuperTy *superRegion) {
68  llvm::FoldingSetNodeID ID;
69  RegionTy::ProfileRegion(ID, arg1, superRegion);
70  void *InsertPos;
71  auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos));
72 
73  if (!R) {
74  R = A.Allocate<RegionTy>();
75  new (R) RegionTy(arg1, superRegion);
76  Regions.InsertNode(R, InsertPos);
77  }
78 
79  return R;
80 }
81 
82 template <typename RegionTy, typename SuperTy, typename Arg1Ty, typename Arg2Ty>
83 RegionTy* MemRegionManager::getSubRegion(const Arg1Ty arg1, const Arg2Ty arg2,
84  const SuperTy *superRegion) {
85  llvm::FoldingSetNodeID ID;
86  RegionTy::ProfileRegion(ID, arg1, arg2, superRegion);
87  void *InsertPos;
88  auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos));
89 
90  if (!R) {
91  R = A.Allocate<RegionTy>();
92  new (R) RegionTy(arg1, arg2, superRegion);
93  Regions.InsertNode(R, InsertPos);
94  }
95 
96  return R;
97 }
98 
99 template <typename RegionTy, typename SuperTy,
100  typename Arg1Ty, typename Arg2Ty, typename Arg3Ty>
101 RegionTy* MemRegionManager::getSubRegion(const Arg1Ty arg1, const Arg2Ty arg2,
102  const Arg3Ty arg3,
103  const SuperTy *superRegion) {
104  llvm::FoldingSetNodeID ID;
105  RegionTy::ProfileRegion(ID, arg1, arg2, arg3, superRegion);
106  void *InsertPos;
107  auto *R = cast_or_null<RegionTy>(Regions.FindNodeOrInsertPos(ID, InsertPos));
108 
109  if (!R) {
110  R = A.Allocate<RegionTy>();
111  new (R) RegionTy(arg1, arg2, arg3, superRegion);
112  Regions.InsertNode(R, InsertPos);
113  }
114 
115  return R;
116 }
117 
118 //===----------------------------------------------------------------------===//
119 // Object destruction.
120 //===----------------------------------------------------------------------===//
121 
122 MemRegion::~MemRegion() = default;
123 
124 // All regions and their data are BumpPtrAllocated. No need to call their
125 // destructors.
127 
128 //===----------------------------------------------------------------------===//
129 // Basic methods.
130 //===----------------------------------------------------------------------===//
131 
132 bool SubRegion::isSubRegionOf(const MemRegion* R) const {
133  const MemRegion* r = this;
134  do {
135  if (r == R)
136  return true;
137  if (const auto *sr = dyn_cast<SubRegion>(r))
138  r = sr->getSuperRegion();
139  else
140  break;
141  } while (r != nullptr);
142  return false;
143 }
144 
146  const SubRegion* r = this;
147  do {
148  const MemRegion *superRegion = r->getSuperRegion();
149  if (const auto *sr = dyn_cast<SubRegion>(superRegion)) {
150  r = sr;
151  continue;
152  }
153  return superRegion->getMemRegionManager();
154  } while (true);
155 }
156 
158  const auto *SSR = dyn_cast<StackSpaceRegion>(getMemorySpace());
159  return SSR ? SSR->getStackFrame() : nullptr;
160 }
161 
162 //===----------------------------------------------------------------------===//
163 // Region extents.
164 //===----------------------------------------------------------------------===//
165 
167  ASTContext &Ctx = svalBuilder.getContext();
168  QualType T = getDesugaredValueType(Ctx);
169 
170  if (isa<VariableArrayType>(T))
171  return nonloc::SymbolVal(svalBuilder.getSymbolManager().getExtentSymbol(this));
172  if (T->isIncompleteType())
173  return UnknownVal();
174 
175  CharUnits size = Ctx.getTypeSizeInChars(T);
176  QualType sizeTy = svalBuilder.getArrayIndexType();
177  return svalBuilder.makeIntVal(size.getQuantity(), sizeTy);
178 }
179 
181  // Force callers to deal with bitfields explicitly.
182  if (getDecl()->isBitField())
183  return UnknownVal();
184 
185  DefinedOrUnknownSVal Extent = DeclRegion::getExtent(svalBuilder);
186 
187  // A zero-length array at the end of a struct often stands for dynamically-
188  // allocated extra memory.
189  if (Extent.isZeroConstant()) {
190  QualType T = getDesugaredValueType(svalBuilder.getContext());
191 
192  if (isa<ConstantArrayType>(T))
193  return UnknownVal();
194  }
195 
196  return Extent;
197 }
198 
200  return nonloc::SymbolVal(svalBuilder.getSymbolManager().getExtentSymbol(this));
201 }
202 
204  return nonloc::SymbolVal(svalBuilder.getSymbolManager().getExtentSymbol(this));
205 }
206 
208  return svalBuilder.makeIntVal(getStringLiteral()->getByteLength()+1,
209  svalBuilder.getArrayIndexType());
210 }
211 
212 ObjCIvarRegion::ObjCIvarRegion(const ObjCIvarDecl *ivd, const SubRegion *sReg)
213  : DeclRegion(ivd, sReg, ObjCIvarRegionKind) {}
214 
216  return cast<ObjCIvarDecl>(D);
217 }
218 
220  return getDecl()->getType();
221 }
222 
224  return QualType(getDecl()->getTypeForDecl(), 0);
225 }
226 
228  return QualType(getDecl()->getTypeForDecl(), 0);
229 }
230 
231 //===----------------------------------------------------------------------===//
232 // FoldingSet profiling.
233 //===----------------------------------------------------------------------===//
234 
235 void MemSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const {
236  ID.AddInteger(static_cast<unsigned>(getKind()));
237 }
238 
239 void StackSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const {
240  ID.AddInteger(static_cast<unsigned>(getKind()));
241  ID.AddPointer(getStackFrame());
242 }
243 
244 void StaticGlobalSpaceRegion::Profile(llvm::FoldingSetNodeID &ID) const {
245  ID.AddInteger(static_cast<unsigned>(getKind()));
246  ID.AddPointer(getCodeRegion());
247 }
248 
249 void StringRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
250  const StringLiteral *Str,
251  const MemRegion *superRegion) {
252  ID.AddInteger(static_cast<unsigned>(StringRegionKind));
253  ID.AddPointer(Str);
254  ID.AddPointer(superRegion);
255 }
256 
257 void ObjCStringRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
258  const ObjCStringLiteral *Str,
259  const MemRegion *superRegion) {
260  ID.AddInteger(static_cast<unsigned>(ObjCStringRegionKind));
261  ID.AddPointer(Str);
262  ID.AddPointer(superRegion);
263 }
264 
265 void AllocaRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
266  const Expr *Ex, unsigned cnt,
267  const MemRegion *superRegion) {
268  ID.AddInteger(static_cast<unsigned>(AllocaRegionKind));
269  ID.AddPointer(Ex);
270  ID.AddInteger(cnt);
271  ID.AddPointer(superRegion);
272 }
273 
274 void AllocaRegion::Profile(llvm::FoldingSetNodeID& ID) const {
275  ProfileRegion(ID, Ex, Cnt, superRegion);
276 }
277 
278 void CompoundLiteralRegion::Profile(llvm::FoldingSetNodeID& ID) const {
279  CompoundLiteralRegion::ProfileRegion(ID, CL, superRegion);
280 }
281 
282 void CompoundLiteralRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
283  const CompoundLiteralExpr *CL,
284  const MemRegion* superRegion) {
285  ID.AddInteger(static_cast<unsigned>(CompoundLiteralRegionKind));
286  ID.AddPointer(CL);
287  ID.AddPointer(superRegion);
288 }
289 
290 void CXXThisRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
291  const PointerType *PT,
292  const MemRegion *sRegion) {
293  ID.AddInteger(static_cast<unsigned>(CXXThisRegionKind));
294  ID.AddPointer(PT);
295  ID.AddPointer(sRegion);
296 }
297 
298 void CXXThisRegion::Profile(llvm::FoldingSetNodeID &ID) const {
299  CXXThisRegion::ProfileRegion(ID, ThisPointerTy, superRegion);
300 }
301 
302 void ObjCIvarRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
303  const ObjCIvarDecl *ivd,
304  const MemRegion* superRegion) {
305  DeclRegion::ProfileRegion(ID, ivd, superRegion, ObjCIvarRegionKind);
306 }
307 
308 void DeclRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, const Decl *D,
309  const MemRegion* superRegion, Kind k) {
310  ID.AddInteger(static_cast<unsigned>(k));
311  ID.AddPointer(D);
312  ID.AddPointer(superRegion);
313 }
314 
315 void DeclRegion::Profile(llvm::FoldingSetNodeID& ID) const {
316  DeclRegion::ProfileRegion(ID, D, superRegion, getKind());
317 }
318 
319 void VarRegion::Profile(llvm::FoldingSetNodeID &ID) const {
320  VarRegion::ProfileRegion(ID, getDecl(), superRegion);
321 }
322 
323 void SymbolicRegion::ProfileRegion(llvm::FoldingSetNodeID& ID, SymbolRef sym,
324  const MemRegion *sreg) {
325  ID.AddInteger(static_cast<unsigned>(MemRegion::SymbolicRegionKind));
326  ID.Add(sym);
327  ID.AddPointer(sreg);
328 }
329 
330 void SymbolicRegion::Profile(llvm::FoldingSetNodeID& ID) const {
331  SymbolicRegion::ProfileRegion(ID, sym, getSuperRegion());
332 }
333 
334 void ElementRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
335  QualType ElementType, SVal Idx,
336  const MemRegion* superRegion) {
337  ID.AddInteger(MemRegion::ElementRegionKind);
338  ID.Add(ElementType);
339  ID.AddPointer(superRegion);
340  Idx.Profile(ID);
341 }
342 
343 void ElementRegion::Profile(llvm::FoldingSetNodeID& ID) const {
344  ElementRegion::ProfileRegion(ID, ElementType, Index, superRegion);
345 }
346 
347 void FunctionCodeRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
348  const NamedDecl *FD,
349  const MemRegion*) {
350  ID.AddInteger(MemRegion::FunctionCodeRegionKind);
351  ID.AddPointer(FD);
352 }
353 
354 void FunctionCodeRegion::Profile(llvm::FoldingSetNodeID& ID) const {
355  FunctionCodeRegion::ProfileRegion(ID, FD, superRegion);
356 }
357 
358 void BlockCodeRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
359  const BlockDecl *BD, CanQualType,
360  const AnalysisDeclContext *AC,
361  const MemRegion*) {
362  ID.AddInteger(MemRegion::BlockCodeRegionKind);
363  ID.AddPointer(BD);
364 }
365 
366 void BlockCodeRegion::Profile(llvm::FoldingSetNodeID& ID) const {
367  BlockCodeRegion::ProfileRegion(ID, BD, locTy, AC, superRegion);
368 }
369 
370 void BlockDataRegion::ProfileRegion(llvm::FoldingSetNodeID& ID,
371  const BlockCodeRegion *BC,
372  const LocationContext *LC,
373  unsigned BlkCount,
374  const MemRegion *sReg) {
375  ID.AddInteger(MemRegion::BlockDataRegionKind);
376  ID.AddPointer(BC);
377  ID.AddPointer(LC);
378  ID.AddInteger(BlkCount);
379  ID.AddPointer(sReg);
380 }
381 
382 void BlockDataRegion::Profile(llvm::FoldingSetNodeID& ID) const {
383  BlockDataRegion::ProfileRegion(ID, BC, LC, BlockCount, getSuperRegion());
384 }
385 
386 void CXXTempObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
387  Expr const *Ex,
388  const MemRegion *sReg) {
389  ID.AddPointer(Ex);
390  ID.AddPointer(sReg);
391 }
392 
393 void CXXTempObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const {
394  ProfileRegion(ID, Ex, getSuperRegion());
395 }
396 
397 void CXXBaseObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
398  const CXXRecordDecl *RD,
399  bool IsVirtual,
400  const MemRegion *SReg) {
401  ID.AddPointer(RD);
402  ID.AddBoolean(IsVirtual);
403  ID.AddPointer(SReg);
404 }
405 
406 void CXXBaseObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const {
407  ProfileRegion(ID, getDecl(), isVirtual(), superRegion);
408 }
409 
410 void CXXDerivedObjectRegion::ProfileRegion(llvm::FoldingSetNodeID &ID,
411  const CXXRecordDecl *RD,
412  const MemRegion *SReg) {
413  ID.AddPointer(RD);
414  ID.AddPointer(SReg);
415 }
416 
417 void CXXDerivedObjectRegion::Profile(llvm::FoldingSetNodeID &ID) const {
418  ProfileRegion(ID, getDecl(), superRegion);
419 }
420 
421 //===----------------------------------------------------------------------===//
422 // Region anchors.
423 //===----------------------------------------------------------------------===//
424 
425 void GlobalsSpaceRegion::anchor() {}
426 
427 void NonStaticGlobalSpaceRegion::anchor() {}
428 
429 void StackSpaceRegion::anchor() {}
430 
431 void TypedRegion::anchor() {}
432 
433 void TypedValueRegion::anchor() {}
434 
435 void CodeTextRegion::anchor() {}
436 
437 void SubRegion::anchor() {}
438 
439 //===----------------------------------------------------------------------===//
440 // Region pretty-printing.
441 //===----------------------------------------------------------------------===//
442 
443 LLVM_DUMP_METHOD void MemRegion::dump() const {
444  dumpToStream(llvm::errs());
445 }
446 
447 std::string MemRegion::getString() const {
448  std::string s;
449  llvm::raw_string_ostream os(s);
450  dumpToStream(os);
451  return os.str();
452 }
453 
454 void MemRegion::dumpToStream(raw_ostream &os) const {
455  os << "<Unknown Region>";
456 }
457 
458 void AllocaRegion::dumpToStream(raw_ostream &os) const {
459  os << "alloca{S" << Ex->getID(getContext()) << ',' << Cnt << '}';
460 }
461 
462 void FunctionCodeRegion::dumpToStream(raw_ostream &os) const {
463  os << "code{" << getDecl()->getDeclName().getAsString() << '}';
464 }
465 
466 void BlockCodeRegion::dumpToStream(raw_ostream &os) const {
467  os << "block_code{" << static_cast<const void *>(this) << '}';
468 }
469 
470 void BlockDataRegion::dumpToStream(raw_ostream &os) const {
471  os << "block_data{" << BC;
472  os << "; ";
474  I = referenced_vars_begin(),
475  E = referenced_vars_end(); I != E; ++I)
476  os << "(" << I.getCapturedRegion() << "<-" <<
477  I.getOriginalRegion() << ") ";
478  os << '}';
479 }
480 
481 void CompoundLiteralRegion::dumpToStream(raw_ostream &os) const {
482  // FIXME: More elaborate pretty-printing.
483  os << "{ S" << CL->getID(getContext()) << " }";
484 }
485 
486 void CXXTempObjectRegion::dumpToStream(raw_ostream &os) const {
487  os << "temp_object{" << getValueType().getAsString() << ", "
488  << "S" << Ex->getID(getContext()) << '}';
489 }
490 
491 void CXXBaseObjectRegion::dumpToStream(raw_ostream &os) const {
492  os << "Base{" << superRegion << ',' << getDecl()->getName() << '}';
493 }
494 
495 void CXXDerivedObjectRegion::dumpToStream(raw_ostream &os) const {
496  os << "Derived{" << superRegion << ',' << getDecl()->getName() << '}';
497 }
498 
499 void CXXThisRegion::dumpToStream(raw_ostream &os) const {
500  os << "this";
501 }
502 
503 void ElementRegion::dumpToStream(raw_ostream &os) const {
504  os << "Element{" << superRegion << ','
505  << Index << ',' << getElementType().getAsString() << '}';
506 }
507 
508 void FieldRegion::dumpToStream(raw_ostream &os) const {
509  os << superRegion << "->" << *getDecl();
510 }
511 
512 void ObjCIvarRegion::dumpToStream(raw_ostream &os) const {
513  os << "Ivar{" << superRegion << ',' << *getDecl() << '}';
514 }
515 
516 void StringRegion::dumpToStream(raw_ostream &os) const {
517  assert(Str != nullptr && "Expecting non-null StringLiteral");
518  Str->printPretty(os, nullptr, PrintingPolicy(getContext().getLangOpts()));
519 }
520 
521 void ObjCStringRegion::dumpToStream(raw_ostream &os) const {
522  assert(Str != nullptr && "Expecting non-null ObjCStringLiteral");
523  Str->printPretty(os, nullptr, PrintingPolicy(getContext().getLangOpts()));
524 }
525 
526 void SymbolicRegion::dumpToStream(raw_ostream &os) const {
527  if (isa<HeapSpaceRegion>(getSuperRegion()))
528  os << "Heap";
529  os << "SymRegion{" << sym << '}';
530 }
531 
532 void VarRegion::dumpToStream(raw_ostream &os) const {
533  const auto *VD = cast<VarDecl>(D);
534  if (const IdentifierInfo *ID = VD->getIdentifier())
535  os << ID->getName();
536  else
537  os << "VarRegion{D" << VD->getID() << '}';
538 }
539 
540 LLVM_DUMP_METHOD void RegionRawOffset::dump() const {
541  dumpToStream(llvm::errs());
542 }
543 
544 void RegionRawOffset::dumpToStream(raw_ostream &os) const {
545  os << "raw_offset{" << getRegion() << ',' << getOffset().getQuantity() << '}';
546 }
547 
548 void CodeSpaceRegion::dumpToStream(raw_ostream &os) const {
549  os << "CodeSpaceRegion";
550 }
551 
552 void StaticGlobalSpaceRegion::dumpToStream(raw_ostream &os) const {
553  os << "StaticGlobalsMemSpace{" << CR << '}';
554 }
555 
556 void GlobalInternalSpaceRegion::dumpToStream(raw_ostream &os) const {
557  os << "GlobalInternalSpaceRegion";
558 }
559 
560 void GlobalSystemSpaceRegion::dumpToStream(raw_ostream &os) const {
561  os << "GlobalSystemSpaceRegion";
562 }
563 
564 void GlobalImmutableSpaceRegion::dumpToStream(raw_ostream &os) const {
565  os << "GlobalImmutableSpaceRegion";
566 }
567 
568 void HeapSpaceRegion::dumpToStream(raw_ostream &os) const {
569  os << "HeapSpaceRegion";
570 }
571 
572 void UnknownSpaceRegion::dumpToStream(raw_ostream &os) const {
573  os << "UnknownSpaceRegion";
574 }
575 
576 void StackArgumentsSpaceRegion::dumpToStream(raw_ostream &os) const {
577  os << "StackArgumentsSpaceRegion";
578 }
579 
580 void StackLocalsSpaceRegion::dumpToStream(raw_ostream &os) const {
581  os << "StackLocalsSpaceRegion";
582 }
583 
585  return canPrintPrettyAsExpr();
586 }
587 
589  return false;
590 }
591 
592 void MemRegion::printPretty(raw_ostream &os) const {
593  assert(canPrintPretty() && "This region cannot be printed pretty.");
594  os << "'";
595  printPrettyAsExpr(os);
596  os << "'";
597 }
598 
599 void MemRegion::printPrettyAsExpr(raw_ostream &) const {
600  llvm_unreachable("This region cannot be printed pretty.");
601 }
602 
604  return true;
605 }
606 
607 void VarRegion::printPrettyAsExpr(raw_ostream &os) const {
608  os << getDecl()->getName();
609 }
610 
612  return true;
613 }
614 
615 void ObjCIvarRegion::printPrettyAsExpr(raw_ostream &os) const {
616  os << getDecl()->getName();
617 }
618 
620  return true;
621 }
622 
624  return superRegion->canPrintPrettyAsExpr();
625 }
626 
627 void FieldRegion::printPrettyAsExpr(raw_ostream &os) const {
628  assert(canPrintPrettyAsExpr());
629  superRegion->printPrettyAsExpr(os);
630  os << "." << getDecl()->getName();
631 }
632 
633 void FieldRegion::printPretty(raw_ostream &os) const {
634  if (canPrintPrettyAsExpr()) {
635  os << "\'";
636  printPrettyAsExpr(os);
637  os << "'";
638  } else {
639  os << "field " << "\'" << getDecl()->getName() << "'";
640  }
641 }
642 
644  return superRegion->canPrintPrettyAsExpr();
645 }
646 
647 void CXXBaseObjectRegion::printPrettyAsExpr(raw_ostream &os) const {
648  superRegion->printPrettyAsExpr(os);
649 }
650 
652  return superRegion->canPrintPrettyAsExpr();
653 }
654 
655 void CXXDerivedObjectRegion::printPrettyAsExpr(raw_ostream &os) const {
656  superRegion->printPrettyAsExpr(os);
657 }
658 
659 std::string MemRegion::getDescriptiveName(bool UseQuotes) const {
660  std::string VariableName;
661  std::string ArrayIndices;
662  const MemRegion *R = this;
663  SmallString<50> buf;
664  llvm::raw_svector_ostream os(buf);
665 
666  // Obtain array indices to add them to the variable name.
667  const ElementRegion *ER = nullptr;
668  while ((ER = R->getAs<ElementRegion>())) {
669  // Index is a ConcreteInt.
670  if (auto CI = ER->getIndex().getAs<nonloc::ConcreteInt>()) {
672  CI->getValue().toString(Idx);
673  ArrayIndices = (llvm::Twine("[") + Idx.str() + "]" + ArrayIndices).str();
674  }
675  // If not a ConcreteInt, try to obtain the variable
676  // name by calling 'getDescriptiveName' recursively.
677  else {
678  std::string Idx = ER->getDescriptiveName(false);
679  if (!Idx.empty()) {
680  ArrayIndices = (llvm::Twine("[") + Idx + "]" + ArrayIndices).str();
681  }
682  }
683  R = ER->getSuperRegion();
684  }
685 
686  // Get variable name.
687  if (R && R->canPrintPrettyAsExpr()) {
688  R->printPrettyAsExpr(os);
689  if (UseQuotes)
690  return (llvm::Twine("'") + os.str() + ArrayIndices + "'").str();
691  else
692  return (llvm::Twine(os.str()) + ArrayIndices).str();
693  }
694 
695  return VariableName;
696 }
697 
699  const auto *const VR = dyn_cast<VarRegion>(this->getBaseRegion());
700  const auto *const FR = dyn_cast<FieldRegion>(this);
701 
702  // Check for more specific regions first.
703  // FieldRegion
704  if (FR) {
705  return FR->getDecl()->getSourceRange();
706  }
707  // VarRegion
708  else if (VR) {
709  return VR->getDecl()->getSourceRange();
710  }
711  // Return invalid source range (can be checked by client).
712  else
713  return {};
714 }
715 
716 //===----------------------------------------------------------------------===//
717 // MemRegionManager methods.
718 //===----------------------------------------------------------------------===//
719 
720 template <typename REG>
721 const REG *MemRegionManager::LazyAllocate(REG*& region) {
722  if (!region) {
723  region = A.Allocate<REG>();
724  new (region) REG(this);
725  }
726 
727  return region;
728 }
729 
730 template <typename REG, typename ARG>
731 const REG *MemRegionManager::LazyAllocate(REG*& region, ARG a) {
732  if (!region) {
733  region = A.Allocate<REG>();
734  new (region) REG(this, a);
735  }
736 
737  return region;
738 }
739 
742  assert(STC);
743  StackLocalsSpaceRegion *&R = StackLocalsSpaceRegions[STC];
744 
745  if (R)
746  return R;
747 
748  R = A.Allocate<StackLocalsSpaceRegion>();
749  new (R) StackLocalsSpaceRegion(this, STC);
750  return R;
751 }
752 
755  assert(STC);
756  StackArgumentsSpaceRegion *&R = StackArgumentsSpaceRegions[STC];
757 
758  if (R)
759  return R;
760 
761  R = A.Allocate<StackArgumentsSpaceRegion>();
762  new (R) StackArgumentsSpaceRegion(this, STC);
763  return R;
764 }
765 
766 const GlobalsSpaceRegion
768  const CodeTextRegion *CR) {
769  if (!CR) {
770  if (K == MemRegion::GlobalSystemSpaceRegionKind)
771  return LazyAllocate(SystemGlobals);
772  if (K == MemRegion::GlobalImmutableSpaceRegionKind)
773  return LazyAllocate(ImmutableGlobals);
774  assert(K == MemRegion::GlobalInternalSpaceRegionKind);
775  return LazyAllocate(InternalGlobals);
776  }
777 
778  assert(K == MemRegion::StaticGlobalSpaceRegionKind);
779  StaticGlobalSpaceRegion *&R = StaticsGlobalSpaceRegions[CR];
780  if (R)
781  return R;
782 
783  R = A.Allocate<StaticGlobalSpaceRegion>();
784  new (R) StaticGlobalSpaceRegion(this, CR);
785  return R;
786 }
787 
789  return LazyAllocate(heap);
790 }
791 
793  return LazyAllocate(unknown);
794 }
795 
797  return LazyAllocate(code);
798 }
799 
800 //===----------------------------------------------------------------------===//
801 // Constructing regions.
802 //===----------------------------------------------------------------------===//
803 
805  return getSubRegion<StringRegion>(
806  Str, cast<GlobalInternalSpaceRegion>(getGlobalsRegion()));
807 }
808 
809 const ObjCStringRegion *
811  return getSubRegion<ObjCStringRegion>(
812  Str, cast<GlobalInternalSpaceRegion>(getGlobalsRegion()));
813 }
814 
815 /// Look through a chain of LocationContexts to either find the
816 /// StackFrameContext that matches a DeclContext, or find a VarRegion
817 /// for a variable captured by a block.
818 static llvm::PointerUnion<const StackFrameContext *, const VarRegion *>
820  const DeclContext *DC,
821  const VarDecl *VD) {
822  while (LC) {
823  if (const auto *SFC = dyn_cast<StackFrameContext>(LC)) {
824  if (cast<DeclContext>(SFC->getDecl()) == DC)
825  return SFC;
826  }
827  if (const auto *BC = dyn_cast<BlockInvocationContext>(LC)) {
828  const auto *BR =
829  static_cast<const BlockDataRegion *>(BC->getContextData());
830  // FIXME: This can be made more efficient.
831  for (BlockDataRegion::referenced_vars_iterator
832  I = BR->referenced_vars_begin(),
833  E = BR->referenced_vars_end(); I != E; ++I) {
834  const VarRegion *VR = I.getOriginalRegion();
835  if (VR->getDecl() == VD)
836  return cast<VarRegion>(I.getCapturedRegion());
837  }
838  }
839 
840  LC = LC->getParent();
841  }
842  return (const StackFrameContext *)nullptr;
843 }
844 
846  const LocationContext *LC) {
847  const MemRegion *sReg = nullptr;
848 
849  if (D->hasGlobalStorage() && !D->isStaticLocal()) {
850 
851  // First handle the globals defined in system headers.
853  // Whitelist the system globals which often DO GET modified, assume the
854  // rest are immutable.
855  if (D->getName().find("errno") != StringRef::npos)
856  sReg = getGlobalsRegion(MemRegion::GlobalSystemSpaceRegionKind);
857  else
858  sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind);
859 
860  // Treat other globals as GlobalInternal unless they are constants.
861  } else {
862  QualType GQT = D->getType();
863  const Type *GT = GQT.getTypePtrOrNull();
864  // TODO: We could walk the complex types here and see if everything is
865  // constified.
866  if (GT && GQT.isConstQualified() && GT->isArithmeticType())
867  sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind);
868  else
869  sReg = getGlobalsRegion();
870  }
871 
872  // Finally handle static locals.
873  } else {
874  // FIXME: Once we implement scope handling, we will need to properly lookup
875  // 'D' to the proper LocationContext.
876  const DeclContext *DC = D->getDeclContext();
877  llvm::PointerUnion<const StackFrameContext *, const VarRegion *> V =
879 
880  if (V.is<const VarRegion*>())
881  return V.get<const VarRegion*>();
882 
883  const auto *STC = V.get<const StackFrameContext *>();
884 
885  if (!STC) {
886  // FIXME: Assign a more sensible memory space to static locals
887  // we see from within blocks that we analyze as top-level declarations.
888  sReg = getUnknownRegion();
889  } else {
890  if (D->hasLocalStorage()) {
891  sReg = isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)
892  ? static_cast<const MemRegion*>(getStackArgumentsRegion(STC))
893  : static_cast<const MemRegion*>(getStackLocalsRegion(STC));
894  }
895  else {
896  assert(D->isStaticLocal());
897  const Decl *STCD = STC->getDecl();
898  if (isa<FunctionDecl>(STCD) || isa<ObjCMethodDecl>(STCD))
899  sReg = getGlobalsRegion(MemRegion::StaticGlobalSpaceRegionKind,
900  getFunctionCodeRegion(cast<NamedDecl>(STCD)));
901  else if (const auto *BD = dyn_cast<BlockDecl>(STCD)) {
902  // FIXME: The fallback type here is totally bogus -- though it should
903  // never be queried, it will prevent uniquing with the real
904  // BlockCodeRegion. Ideally we'd fix the AST so that we always had a
905  // signature.
906  QualType T;
907  if (const TypeSourceInfo *TSI = BD->getSignatureAsWritten())
908  T = TSI->getType();
909  if (T.isNull())
910  T = getContext().VoidTy;
911  if (!T->getAs<FunctionType>())
914 
915  const BlockCodeRegion *BTR =
917  STC->getAnalysisDeclContext());
918  sReg = getGlobalsRegion(MemRegion::StaticGlobalSpaceRegionKind,
919  BTR);
920  }
921  else {
922  sReg = getGlobalsRegion();
923  }
924  }
925  }
926  }
927 
928  return getSubRegion<VarRegion>(D, sReg);
929 }
930 
932  const MemRegion *superR) {
933  return getSubRegion<VarRegion>(D, superR);
934 }
935 
936 const BlockDataRegion *
938  const LocationContext *LC,
939  unsigned blockCount) {
940  const MemSpaceRegion *sReg = nullptr;
941  const BlockDecl *BD = BC->getDecl();
942  if (!BD->hasCaptures()) {
943  // This handles 'static' blocks.
944  sReg = getGlobalsRegion(MemRegion::GlobalImmutableSpaceRegionKind);
945  }
946  else {
947  if (LC) {
948  // FIXME: Once we implement scope handling, we want the parent region
949  // to be the scope.
950  const StackFrameContext *STC = LC->getStackFrame();
951  assert(STC);
952  sReg = getStackLocalsRegion(STC);
953  }
954  else {
955  // We allow 'LC' to be NULL for cases where want BlockDataRegions
956  // without context-sensitivity.
957  sReg = getUnknownRegion();
958  }
959  }
960 
961  return getSubRegion<BlockDataRegion>(BC, LC, blockCount, sReg);
962 }
963 
964 const CXXTempObjectRegion *
966  return getSubRegion<CXXTempObjectRegion>(
967  Ex, getGlobalsRegion(MemRegion::GlobalInternalSpaceRegionKind, nullptr));
968 }
969 
972  const LocationContext *LC) {
973  const MemSpaceRegion *sReg = nullptr;
974 
975  if (CL->isFileScope())
976  sReg = getGlobalsRegion();
977  else {
978  const StackFrameContext *STC = LC->getStackFrame();
979  assert(STC);
980  sReg = getStackLocalsRegion(STC);
981  }
982 
983  return getSubRegion<CompoundLiteralRegion>(CL, sReg);
984 }
985 
986 const ElementRegion*
988  const SubRegion* superRegion,
989  ASTContext &Ctx){
990  QualType T = Ctx.getCanonicalType(elementType).getUnqualifiedType();
991 
992  llvm::FoldingSetNodeID ID;
993  ElementRegion::ProfileRegion(ID, T, Idx, superRegion);
994 
995  void *InsertPos;
996  MemRegion* data = Regions.FindNodeOrInsertPos(ID, InsertPos);
997  auto *R = cast_or_null<ElementRegion>(data);
998 
999  if (!R) {
1000  R = A.Allocate<ElementRegion>();
1001  new (R) ElementRegion(T, Idx, superRegion);
1002  Regions.InsertNode(R, InsertPos);
1003  }
1004 
1005  return R;
1006 }
1007 
1008 const FunctionCodeRegion *
1010  return getSubRegion<FunctionCodeRegion>(FD, getCodeRegion());
1011 }
1012 
1013 const BlockCodeRegion *
1015  AnalysisDeclContext *AC) {
1016  return getSubRegion<BlockCodeRegion>(BD, locTy, AC, getCodeRegion());
1017 }
1018 
1019 /// getSymbolicRegion - Retrieve or create a "symbolic" memory region.
1021  return getSubRegion<SymbolicRegion>(sym, getUnknownRegion());
1022 }
1023 
1025  return getSubRegion<SymbolicRegion>(Sym, getHeapRegion());
1026 }
1027 
1028 const FieldRegion*
1030  const SubRegion* superRegion){
1031  return getSubRegion<FieldRegion>(d, superRegion);
1032 }
1033 
1034 const ObjCIvarRegion*
1036  const SubRegion* superRegion) {
1037  return getSubRegion<ObjCIvarRegion>(d, superRegion);
1038 }
1039 
1040 const CXXTempObjectRegion*
1042  LocationContext const *LC) {
1043  const StackFrameContext *SFC = LC->getStackFrame();
1044  assert(SFC);
1045  return getSubRegion<CXXTempObjectRegion>(E, getStackLocalsRegion(SFC));
1046 }
1047 
1048 /// Checks whether \p BaseClass is a valid virtual or direct non-virtual base
1049 /// class of the type of \p Super.
1050 static bool isValidBaseClass(const CXXRecordDecl *BaseClass,
1051  const TypedValueRegion *Super,
1052  bool IsVirtual) {
1053  BaseClass = BaseClass->getCanonicalDecl();
1054 
1055  const CXXRecordDecl *Class = Super->getValueType()->getAsCXXRecordDecl();
1056  if (!Class)
1057  return true;
1058 
1059  if (IsVirtual)
1060  return Class->isVirtuallyDerivedFrom(BaseClass);
1061 
1062  for (const auto &I : Class->bases()) {
1063  if (I.getType()->getAsCXXRecordDecl()->getCanonicalDecl() == BaseClass)
1064  return true;
1065  }
1066 
1067  return false;
1068 }
1069 
1070 const CXXBaseObjectRegion *
1072  const SubRegion *Super,
1073  bool IsVirtual) {
1074  if (isa<TypedValueRegion>(Super)) {
1075  assert(isValidBaseClass(RD, dyn_cast<TypedValueRegion>(Super), IsVirtual));
1076  (void)&isValidBaseClass;
1077 
1078  if (IsVirtual) {
1079  // Virtual base regions should not be layered, since the layout rules
1080  // are different.
1081  while (const auto *Base = dyn_cast<CXXBaseObjectRegion>(Super))
1082  Super = cast<SubRegion>(Base->getSuperRegion());
1083  assert(Super && !isa<MemSpaceRegion>(Super));
1084  }
1085  }
1086 
1087  return getSubRegion<CXXBaseObjectRegion>(RD, IsVirtual, Super);
1088 }
1089 
1090 const CXXDerivedObjectRegion *
1092  const SubRegion *Super) {
1093  return getSubRegion<CXXDerivedObjectRegion>(RD, Super);
1094 }
1095 
1096 const CXXThisRegion*
1098  const LocationContext *LC) {
1099  const auto *PT = thisPointerTy->getAs<PointerType>();
1100  assert(PT);
1101  // Inside the body of the operator() of a lambda a this expr might refer to an
1102  // object in one of the parent location contexts.
1103  const auto *D = dyn_cast<CXXMethodDecl>(LC->getDecl());
1104  // FIXME: when operator() of lambda is analyzed as a top level function and
1105  // 'this' refers to a this to the enclosing scope, there is no right region to
1106  // return.
1107  while (!LC->inTopFrame() && (!D || D->isStatic() ||
1108  PT != D->getThisType()->getAs<PointerType>())) {
1109  LC = LC->getParent();
1110  D = dyn_cast<CXXMethodDecl>(LC->getDecl());
1111  }
1112  const StackFrameContext *STC = LC->getStackFrame();
1113  assert(STC);
1114  return getSubRegion<CXXThisRegion>(PT, getStackArgumentsRegion(STC));
1115 }
1116 
1117 const AllocaRegion*
1119  const LocationContext *LC) {
1120  const StackFrameContext *STC = LC->getStackFrame();
1121  assert(STC);
1122  return getSubRegion<AllocaRegion>(E, cnt, getStackLocalsRegion(STC));
1123 }
1124 
1126  const MemRegion *R = this;
1127  const auto *SR = dyn_cast<SubRegion>(this);
1128 
1129  while (SR) {
1130  R = SR->getSuperRegion();
1131  SR = dyn_cast<SubRegion>(R);
1132  }
1133 
1134  return dyn_cast<MemSpaceRegion>(R);
1135 }
1136 
1138  return isa<StackSpaceRegion>(getMemorySpace());
1139 }
1140 
1142  return isa<StackLocalsSpaceRegion>(getMemorySpace());
1143 }
1144 
1146  return isa<StackArgumentsSpaceRegion>(getMemorySpace());
1147 }
1148 
1150  const MemSpaceRegion *MS = getMemorySpace();
1151  return isa<StackArgumentsSpaceRegion>(MS) ||
1152  isa<GlobalsSpaceRegion>(MS);
1153 }
1154 
1155 // getBaseRegion strips away all elements and fields, and get the base region
1156 // of them.
1158  const MemRegion *R = this;
1159  while (true) {
1160  switch (R->getKind()) {
1161  case MemRegion::ElementRegionKind:
1162  case MemRegion::FieldRegionKind:
1163  case MemRegion::ObjCIvarRegionKind:
1164  case MemRegion::CXXBaseObjectRegionKind:
1165  case MemRegion::CXXDerivedObjectRegionKind:
1166  R = cast<SubRegion>(R)->getSuperRegion();
1167  continue;
1168  default:
1169  break;
1170  }
1171  break;
1172  }
1173  return R;
1174 }
1175 
1176 // getgetMostDerivedObjectRegion gets the region of the root class of a C++
1177 // class hierarchy.
1179  const MemRegion *R = this;
1180  while (const auto *BR = dyn_cast<CXXBaseObjectRegion>(R))
1181  R = BR->getSuperRegion();
1182  return R;
1183 }
1184 
1186  return false;
1187 }
1188 
1189 //===----------------------------------------------------------------------===//
1190 // View handling.
1191 //===----------------------------------------------------------------------===//
1192 
1193 const MemRegion *MemRegion::StripCasts(bool StripBaseAndDerivedCasts) const {
1194  const MemRegion *R = this;
1195  while (true) {
1196  switch (R->getKind()) {
1197  case ElementRegionKind: {
1198  const auto *ER = cast<ElementRegion>(R);
1199  if (!ER->getIndex().isZeroConstant())
1200  return R;
1201  R = ER->getSuperRegion();
1202  break;
1203  }
1204  case CXXBaseObjectRegionKind:
1205  case CXXDerivedObjectRegionKind:
1206  if (!StripBaseAndDerivedCasts)
1207  return R;
1208  R = cast<TypedValueRegion>(R)->getSuperRegion();
1209  break;
1210  default:
1211  return R;
1212  }
1213  }
1214 }
1215 
1217  const auto *SubR = dyn_cast<SubRegion>(this);
1218 
1219  while (SubR) {
1220  if (const auto *SymR = dyn_cast<SymbolicRegion>(SubR))
1221  return SymR;
1222  SubR = dyn_cast<SubRegion>(SubR->getSuperRegion());
1223  }
1224  return nullptr;
1225 }
1226 
1228  int64_t offset = 0;
1229  const ElementRegion *ER = this;
1230  const MemRegion *superR = nullptr;
1231  ASTContext &C = getContext();
1232 
1233  // FIXME: Handle multi-dimensional arrays.
1234 
1235  while (ER) {
1236  superR = ER->getSuperRegion();
1237 
1238  // FIXME: generalize to symbolic offsets.
1239  SVal index = ER->getIndex();
1240  if (auto CI = index.getAs<nonloc::ConcreteInt>()) {
1241  // Update the offset.
1242  int64_t i = CI->getValue().getSExtValue();
1243 
1244  if (i != 0) {
1245  QualType elemType = ER->getElementType();
1246 
1247  // If we are pointing to an incomplete type, go no further.
1248  if (elemType->isIncompleteType()) {
1249  superR = ER;
1250  break;
1251  }
1252 
1253  int64_t size = C.getTypeSizeInChars(elemType).getQuantity();
1254  if (auto NewOffset = llvm::checkedMulAdd(i, size, offset)) {
1255  offset = *NewOffset;
1256  } else {
1257  LLVM_DEBUG(llvm::dbgs() << "MemRegion::getAsArrayOffset: "
1258  << "offset overflowing, returning unknown\n");
1259 
1260  return nullptr;
1261  }
1262  }
1263 
1264  // Go to the next ElementRegion (if any).
1265  ER = dyn_cast<ElementRegion>(superR);
1266  continue;
1267  }
1268 
1269  return nullptr;
1270  }
1271 
1272  assert(superR && "super region cannot be NULL");
1273  return RegionRawOffset(superR, CharUnits::fromQuantity(offset));
1274 }
1275 
1276 /// Returns true if \p Base is an immediate base class of \p Child
1277 static bool isImmediateBase(const CXXRecordDecl *Child,
1278  const CXXRecordDecl *Base) {
1279  assert(Child && "Child must not be null");
1280  // Note that we do NOT canonicalize the base class here, because
1281  // ASTRecordLayout doesn't either. If that leads us down the wrong path,
1282  // so be it; at least we won't crash.
1283  for (const auto &I : Child->bases()) {
1284  if (I.getType()->getAsCXXRecordDecl() == Base)
1285  return true;
1286  }
1287 
1288  return false;
1289 }
1290 
1291 static RegionOffset calculateOffset(const MemRegion *R) {
1292  const MemRegion *SymbolicOffsetBase = nullptr;
1293  int64_t Offset = 0;
1294 
1295  while (true) {
1296  switch (R->getKind()) {
1297  case MemRegion::CodeSpaceRegionKind:
1298  case MemRegion::StackLocalsSpaceRegionKind:
1299  case MemRegion::StackArgumentsSpaceRegionKind:
1300  case MemRegion::HeapSpaceRegionKind:
1301  case MemRegion::UnknownSpaceRegionKind:
1302  case MemRegion::StaticGlobalSpaceRegionKind:
1303  case MemRegion::GlobalInternalSpaceRegionKind:
1304  case MemRegion::GlobalSystemSpaceRegionKind:
1305  case MemRegion::GlobalImmutableSpaceRegionKind:
1306  // Stores can bind directly to a region space to set a default value.
1307  assert(Offset == 0 && !SymbolicOffsetBase);
1308  goto Finish;
1309 
1310  case MemRegion::FunctionCodeRegionKind:
1311  case MemRegion::BlockCodeRegionKind:
1312  case MemRegion::BlockDataRegionKind:
1313  // These will never have bindings, but may end up having values requested
1314  // if the user does some strange casting.
1315  if (Offset != 0)
1316  SymbolicOffsetBase = R;
1317  goto Finish;
1318 
1319  case MemRegion::SymbolicRegionKind:
1320  case MemRegion::AllocaRegionKind:
1321  case MemRegion::CompoundLiteralRegionKind:
1322  case MemRegion::CXXThisRegionKind:
1323  case MemRegion::StringRegionKind:
1324  case MemRegion::ObjCStringRegionKind:
1325  case MemRegion::VarRegionKind:
1326  case MemRegion::CXXTempObjectRegionKind:
1327  // Usual base regions.
1328  goto Finish;
1329 
1330  case MemRegion::ObjCIvarRegionKind:
1331  // This is a little strange, but it's a compromise between
1332  // ObjCIvarRegions having unknown compile-time offsets (when using the
1333  // non-fragile runtime) and yet still being distinct, non-overlapping
1334  // regions. Thus we treat them as "like" base regions for the purposes
1335  // of computing offsets.
1336  goto Finish;
1337 
1338  case MemRegion::CXXBaseObjectRegionKind: {
1339  const auto *BOR = cast<CXXBaseObjectRegion>(R);
1340  R = BOR->getSuperRegion();
1341 
1342  QualType Ty;
1343  bool RootIsSymbolic = false;
1344  if (const auto *TVR = dyn_cast<TypedValueRegion>(R)) {
1345  Ty = TVR->getDesugaredValueType(R->getContext());
1346  } else if (const auto *SR = dyn_cast<SymbolicRegion>(R)) {
1347  // If our base region is symbolic, we don't know what type it really is.
1348  // Pretend the type of the symbol is the true dynamic type.
1349  // (This will at least be self-consistent for the life of the symbol.)
1350  Ty = SR->getSymbol()->getType()->getPointeeType();
1351  RootIsSymbolic = true;
1352  }
1353 
1354  const CXXRecordDecl *Child = Ty->getAsCXXRecordDecl();
1355  if (!Child) {
1356  // We cannot compute the offset of the base class.
1357  SymbolicOffsetBase = R;
1358  } else {
1359  if (RootIsSymbolic) {
1360  // Base layers on symbolic regions may not be type-correct.
1361  // Double-check the inheritance here, and revert to a symbolic offset
1362  // if it's invalid (e.g. due to a reinterpret_cast).
1363  if (BOR->isVirtual()) {
1364  if (!Child->isVirtuallyDerivedFrom(BOR->getDecl()))
1365  SymbolicOffsetBase = R;
1366  } else {
1367  if (!isImmediateBase(Child, BOR->getDecl()))
1368  SymbolicOffsetBase = R;
1369  }
1370  }
1371  }
1372 
1373  // Don't bother calculating precise offsets if we already have a
1374  // symbolic offset somewhere in the chain.
1375  if (SymbolicOffsetBase)
1376  continue;
1377 
1378  CharUnits BaseOffset;
1379  const ASTRecordLayout &Layout = R->getContext().getASTRecordLayout(Child);
1380  if (BOR->isVirtual())
1381  BaseOffset = Layout.getVBaseClassOffset(BOR->getDecl());
1382  else
1383  BaseOffset = Layout.getBaseClassOffset(BOR->getDecl());
1384 
1385  // The base offset is in chars, not in bits.
1386  Offset += BaseOffset.getQuantity() * R->getContext().getCharWidth();
1387  break;
1388  }
1389 
1390  case MemRegion::CXXDerivedObjectRegionKind: {
1391  // TODO: Store the base type in the CXXDerivedObjectRegion and use it.
1392  goto Finish;
1393  }
1394 
1395  case MemRegion::ElementRegionKind: {
1396  const auto *ER = cast<ElementRegion>(R);
1397  R = ER->getSuperRegion();
1398 
1399  QualType EleTy = ER->getValueType();
1400  if (EleTy->isIncompleteType()) {
1401  // We cannot compute the offset of the base class.
1402  SymbolicOffsetBase = R;
1403  continue;
1404  }
1405 
1406  SVal Index = ER->getIndex();
1408  Index.getAs<nonloc::ConcreteInt>()) {
1409  // Don't bother calculating precise offsets if we already have a
1410  // symbolic offset somewhere in the chain.
1411  if (SymbolicOffsetBase)
1412  continue;
1413 
1414  int64_t i = CI->getValue().getSExtValue();
1415  // This type size is in bits.
1416  Offset += i * R->getContext().getTypeSize(EleTy);
1417  } else {
1418  // We cannot compute offset for non-concrete index.
1419  SymbolicOffsetBase = R;
1420  }
1421  break;
1422  }
1423  case MemRegion::FieldRegionKind: {
1424  const auto *FR = cast<FieldRegion>(R);
1425  R = FR->getSuperRegion();
1426 
1427  const RecordDecl *RD = FR->getDecl()->getParent();
1428  if (RD->isUnion() || !RD->isCompleteDefinition()) {
1429  // We cannot compute offset for incomplete type.
1430  // For unions, we could treat everything as offset 0, but we'd rather
1431  // treat each field as a symbolic offset so they aren't stored on top
1432  // of each other, since we depend on things in typed regions actually
1433  // matching their types.
1434  SymbolicOffsetBase = R;
1435  }
1436 
1437  // Don't bother calculating precise offsets if we already have a
1438  // symbolic offset somewhere in the chain.
1439  if (SymbolicOffsetBase)
1440  continue;
1441 
1442  // Get the field number.
1443  unsigned idx = 0;
1444  for (RecordDecl::field_iterator FI = RD->field_begin(),
1445  FE = RD->field_end(); FI != FE; ++FI, ++idx) {
1446  if (FR->getDecl() == *FI)
1447  break;
1448  }
1449  const ASTRecordLayout &Layout = R->getContext().getASTRecordLayout(RD);
1450  // This is offset in bits.
1451  Offset += Layout.getFieldOffset(idx);
1452  break;
1453  }
1454  }
1455  }
1456 
1457  Finish:
1458  if (SymbolicOffsetBase)
1459  return RegionOffset(SymbolicOffsetBase, RegionOffset::Symbolic);
1460  return RegionOffset(R, Offset);
1461 }
1462 
1464  if (!cachedOffset)
1465  cachedOffset = calculateOffset(this);
1466  return *cachedOffset;
1467 }
1468 
1469 //===----------------------------------------------------------------------===//
1470 // BlockDataRegion
1471 //===----------------------------------------------------------------------===//
1472 
1473 std::pair<const VarRegion *, const VarRegion *>
1474 BlockDataRegion::getCaptureRegions(const VarDecl *VD) {
1475  MemRegionManager &MemMgr = *getMemRegionManager();
1476  const VarRegion *VR = nullptr;
1477  const VarRegion *OriginalVR = nullptr;
1478 
1479  if (!VD->hasAttr<BlocksAttr>() && VD->hasLocalStorage()) {
1480  VR = MemMgr.getVarRegion(VD, this);
1481  OriginalVR = MemMgr.getVarRegion(VD, LC);
1482  }
1483  else {
1484  if (LC) {
1485  VR = MemMgr.getVarRegion(VD, LC);
1486  OriginalVR = VR;
1487  }
1488  else {
1489  VR = MemMgr.getVarRegion(VD, MemMgr.getUnknownRegion());
1490  OriginalVR = MemMgr.getVarRegion(VD, LC);
1491  }
1492  }
1493  return std::make_pair(VR, OriginalVR);
1494 }
1495 
1496 void BlockDataRegion::LazyInitializeReferencedVars() {
1497  if (ReferencedVars)
1498  return;
1499 
1500  AnalysisDeclContext *AC = getCodeRegion()->getAnalysisDeclContext();
1501  const auto &ReferencedBlockVars = AC->getReferencedBlockVars(BC->getDecl());
1502  auto NumBlockVars =
1503  std::distance(ReferencedBlockVars.begin(), ReferencedBlockVars.end());
1504 
1505  if (NumBlockVars == 0) {
1506  ReferencedVars = (void*) 0x1;
1507  return;
1508  }
1509 
1510  MemRegionManager &MemMgr = *getMemRegionManager();
1511  llvm::BumpPtrAllocator &A = MemMgr.getAllocator();
1512  BumpVectorContext BC(A);
1513 
1514  using VarVec = BumpVector<const MemRegion *>;
1515 
1516  auto *BV = A.Allocate<VarVec>();
1517  new (BV) VarVec(BC, NumBlockVars);
1518  auto *BVOriginal = A.Allocate<VarVec>();
1519  new (BVOriginal) VarVec(BC, NumBlockVars);
1520 
1521  for (const auto *VD : ReferencedBlockVars) {
1522  const VarRegion *VR = nullptr;
1523  const VarRegion *OriginalVR = nullptr;
1524  std::tie(VR, OriginalVR) = getCaptureRegions(VD);
1525  assert(VR);
1526  assert(OriginalVR);
1527  BV->push_back(VR, BC);
1528  BVOriginal->push_back(OriginalVR, BC);
1529  }
1530 
1531  ReferencedVars = BV;
1532  OriginalVars = BVOriginal;
1533 }
1534 
1537  const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars();
1538 
1539  auto *Vec = static_cast<BumpVector<const MemRegion *> *>(ReferencedVars);
1540 
1541  if (Vec == (void*) 0x1)
1542  return BlockDataRegion::referenced_vars_iterator(nullptr, nullptr);
1543 
1544  auto *VecOriginal =
1545  static_cast<BumpVector<const MemRegion *> *>(OriginalVars);
1546 
1547  return BlockDataRegion::referenced_vars_iterator(Vec->begin(),
1548  VecOriginal->begin());
1549 }
1550 
1553  const_cast<BlockDataRegion*>(this)->LazyInitializeReferencedVars();
1554 
1555  auto *Vec = static_cast<BumpVector<const MemRegion *> *>(ReferencedVars);
1556 
1557  if (Vec == (void*) 0x1)
1558  return BlockDataRegion::referenced_vars_iterator(nullptr, nullptr);
1559 
1560  auto *VecOriginal =
1561  static_cast<BumpVector<const MemRegion *> *>(OriginalVars);
1562 
1564  VecOriginal->end());
1565 }
1566 
1568  for (referenced_vars_iterator I = referenced_vars_begin(),
1569  E = referenced_vars_end();
1570  I != E; ++I) {
1571  if (I.getCapturedRegion() == R)
1572  return I.getOriginalRegion();
1573  }
1574  return nullptr;
1575 }
1576 
1577 //===----------------------------------------------------------------------===//
1578 // RegionAndSymbolInvalidationTraits
1579 //===----------------------------------------------------------------------===//
1580 
1582  InvalidationKinds IK) {
1583  SymTraitsMap[Sym] |= IK;
1584 }
1585 
1587  InvalidationKinds IK) {
1588  assert(MR);
1589  if (const auto *SR = dyn_cast<SymbolicRegion>(MR))
1590  setTrait(SR->getSymbol(), IK);
1591  else
1592  MRTraitsMap[MR] |= IK;
1593 }
1594 
1596  InvalidationKinds IK) const {
1597  const_symbol_iterator I = SymTraitsMap.find(Sym);
1598  if (I != SymTraitsMap.end())
1599  return I->second & IK;
1600 
1601  return false;
1602 }
1603 
1605  InvalidationKinds IK) const {
1606  if (!MR)
1607  return false;
1608 
1609  if (const auto *SR = dyn_cast<SymbolicRegion>(MR))
1610  return hasTrait(SR->getSymbol(), IK);
1611 
1612  const_region_iterator I = MRTraitsMap.find(MR);
1613  if (I != MRTraitsMap.end())
1614  return I->second & IK;
1615 
1616  return false;
1617 }
Defines the clang::ASTContext interface.
void dumpToStream(raw_ostream &os) const override
Definition: MemRegion.cpp:526
nonloc::ConcreteInt makeIntVal(const IntegerLiteral *integer)
Definition: SValBuilder.h:278
CompoundLiteralRegion - A memory region representing a compound literal.
Definition: MemRegion.h:874
void dumpToStream(raw_ostream &os) const override
Definition: MemRegion.cpp:495
const ObjCIvarDecl * getDecl() const
Definition: MemRegion.cpp:215
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition: Type.h:2539
QualType getArrayIndexType() const
Definition: SValBuilder.h:164
SymbolManager & getSymbolManager()
Definition: SValBuilder.h:171
A (possibly-)qualified type.
Definition: Type.h:637
MemRegion - The root abstract class for all memory regions.
Definition: MemRegion.h:94
base_class_range bases()
Definition: DeclCXX.h:822
bool hasTrait(SymbolRef Sym, InvalidationKinds IK) const
Definition: MemRegion.cpp:1595
bool hasCaptures() const
True if this block (or its nested blocks) captures anything of local storage from its enclosing scope...
Definition: Decl.h:3976
std::string getString() const
Get a string representation of a region for debug use.
Definition: MemRegion.cpp:447
bool isArithmeticType() const
Definition: Type.cpp:1951
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition: Type.h:3357
NonLoc getIndex() const
Definition: MemRegion.h:1105
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee...
Definition: Type.cpp:504
CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const
getBaseClassOffset - Get the offset, in chars, for the given base class.
Definition: RecordLayout.h:232
C Language Family Type Representation.
Defines the SourceManager interface.
bool canPrintPrettyAsExpr() const override
Returns true if this region&#39;s textual representation can be used as part of a larger expression...
Definition: MemRegion.cpp:611
virtual void dumpToStream(raw_ostream &os) const
Definition: MemRegion.cpp:454
BlockCodeRegion - A region that represents code texts of blocks (closures).
Definition: MemRegion.h:627
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
void dumpToStream(raw_ostream &os) const override
Definition: MemRegion.cpp:521
const StringRegion * getStringRegion(const StringLiteral *Str)
Definition: MemRegion.cpp:804
void printPrettyAsExpr(raw_ostream &os) const override
Print the region as expression.
Definition: MemRegion.cpp:655
const GlobalsSpaceRegion * getGlobalsRegion(MemRegion::Kind K=MemRegion::GlobalInternalSpaceRegionKind, const CodeTextRegion *R=nullptr)
getGlobalsRegion - Retrieve the memory region associated with global variables.
Definition: MemRegion.cpp:767
QualType getBlockPointerType(QualType T) const
Return the uniqued reference to the type for a block of the specified type.
The base class of the type hierarchy.
Definition: Type.h:1406
CanQual< T > getUnqualifiedType() const
Retrieve the unqualified form of this type.
MemSpaceRegion - A memory region that represents a "memory space"; for example, the set of global var...
Definition: MemRegion.h:198
A container of type source information.
Definition: Decl.h:86
Value representing integer constant.
Definition: SVals.h:376
const ObjCIvarRegion * getObjCIvarRegion(const ObjCIvarDecl *ivd, const SubRegion *superRegion)
getObjCIvarRegion - Retrieve or create the memory region associated with a specified Objective-c inst...
Definition: MemRegion.cpp:1035
float __ovld __cnfn distance(float p0, float p1)
Returns the distance between p0 and p1.
AllocaRegion - A region that represents an untyped blob of bytes created by a call to &#39;alloca&#39;...
Definition: MemRegion.h:471
bool isCompleteDefinition() const
Return true if this decl has its body fully specified.
Definition: Decl.h:3168
CodeSpaceRegion - The memory space that holds the executable code of functions and blocks...
Definition: MemRegion.h:222
const StackArgumentsSpaceRegion * getStackArgumentsRegion(const StackFrameContext *STC)
getStackArgumentsRegion - Retrieve the memory region associated with function/method arguments of the...
Definition: MemRegion.cpp:754
Represents a variable declaration or definition.
Definition: Decl.h:812
void printPretty(raw_ostream &os) const override
Print the region for use in diagnostics.
Definition: MemRegion.cpp:633
CompoundLiteralExpr - [C99 6.5.2.5].
Definition: Expr.h:2931
void Profile(llvm::FoldingSetNodeID &ID) const override
Definition: MemRegion.cpp:330
const T * getAs() const
Member-template getAs<specific type>&#39;.
Definition: Type.h:6754
void setTrait(SymbolRef Sym, InvalidationKinds IK)
Definition: MemRegion.cpp:1581
QualType getElementType() const
Definition: MemRegion.h:1109
void Profile(llvm::FoldingSetNodeID &ID) const override
Definition: MemRegion.cpp:366
const ObjCStringRegion * getObjCStringRegion(const ObjCStringLiteral *Str)
Definition: MemRegion.cpp:810
static bool isValidBaseClass(const CXXRecordDecl *BaseClass, const TypedValueRegion *Super, bool IsVirtual)
Checks whether BaseClass is a valid virtual or direct non-virtual base class of the type of Super...
Definition: MemRegion.cpp:1050
Describes how types, statements, expressions, and declarations should be printed. ...
Definition: PrettyPrinter.h:37
QualType getValueType() const override
Definition: MemRegion.cpp:227
Symbolic value.
Definition: SymExpr.h:29
CXXThisRegion - Represents the region for the implicit &#39;this&#39; parameter in a call to a C++ method...
Definition: MemRegion.h:975
const MemRegion * getSuperRegion() const
Definition: MemRegion.h:447
Represents a struct/union/class.
Definition: Decl.h:3592
const SymbolicRegion * getSymbolicBase() const
If this is a symbolic region, returns the region.
Definition: MemRegion.cpp:1216
One of these records is kept for each identifier that is lexed.
DefinedOrUnknownSVal getExtent(SValBuilder &svalBuilder) const override
getExtent - Returns the size of the region in bytes.
Definition: MemRegion.cpp:180
const HeapSpaceRegion * getHeapRegion()
getHeapRegion - Retrieve the memory region associated with the generic "heap".
Definition: MemRegion.cpp:788
The region associated with an ObjCStringLiteral.
Definition: MemRegion.h:838
void dumpToStream(raw_ostream &os) const
Definition: MemRegion.cpp:544
void Profile(llvm::FoldingSetNodeID &ID) const override
Definition: MemRegion.cpp:274
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:154
bool isFileScope() const
Definition: Expr.h:2961
const FieldDecl * getDecl() const
Definition: MemRegion.h:1019
Represents a member of a struct/union/class.
Definition: Decl.h:2578
AnalysisDeclContext contains the context data for the function or method under analysis.
const StackLocalsSpaceRegion * getStackLocalsRegion(const StackFrameContext *STC)
getStackLocalsRegion - Retrieve the memory region associated with the specified stack frame...
Definition: MemRegion.cpp:741
void Profile(llvm::FoldingSetNodeID &ID) const override
Definition: MemRegion.cpp:354
void printPrettyAsExpr(raw_ostream &os) const override
Print the region as expression.
Definition: MemRegion.cpp:615
CharUnits getVBaseClassOffset(const CXXRecordDecl *VBase) const
getVBaseClassOffset - Get the offset, in chars, for the given base class.
Definition: RecordLayout.h:240
TypeSourceInfo * getSignatureAsWritten() const
Definition: Decl.h:3940
const SymbolicRegion * getSymbolicRegion(SymbolRef Sym)
Retrieve or create a "symbolic" memory region.
Definition: MemRegion.cpp:1020
void Profile(llvm::FoldingSetNodeID &ID) const override
Definition: MemRegion.cpp:235
BlockDataRegion - A region that represents a block instance.
Definition: MemRegion.h:672
const AllocaRegion * getAllocaRegion(const Expr *Ex, unsigned Cnt, const LocationContext *LC)
getAllocaRegion - Retrieve a region associated with a call to alloca().
Definition: MemRegion.cpp:1118
const UnknownSpaceRegion * getUnknownRegion()
getUnknownRegion - Retrieve the memory region associated with unknown memory space.
Definition: MemRegion.cpp:792
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:37
void dumpToStream(raw_ostream &os) const override
Definition: MemRegion.cpp:508
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
QualType getValueType() const override
Definition: MemRegion.cpp:219
bool canPrintPrettyAsExpr() const override
Returns true if this region&#39;s textual representation can be used as part of a larger expression...
Definition: MemRegion.cpp:651
virtual bool inTopFrame() const
Return true if the current LocationContext has no caller context.
const LocationContext * getParent() const
bool hasStackParametersStorage() const
Definition: MemRegion.cpp:1145
void Profile(llvm::FoldingSetNodeID &ID) const override
Definition: MemRegion.cpp:343
void Profile(llvm::FoldingSetNodeID &ID) const override
Definition: MemRegion.cpp:382
void dumpToStream(raw_ostream &os) const override
Definition: MemRegion.cpp:491
ObjCStringLiteral, used for Objective-C string literals i.e.
Definition: ExprObjC.h:50
void Profile(llvm::FoldingSetNodeID &ID) const override
Definition: MemRegion.cpp:244
CXXRecordDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
Definition: DeclCXX.h:727
field_iterator field_begin() const
Definition: Decl.cpp:4144
void dumpToStream(raw_ostream &os) const override
Definition: MemRegion.cpp:556
void dumpToStream(raw_ostream &os) const override
Definition: MemRegion.cpp:568
referenced_vars_iterator referenced_vars_end() const
Definition: MemRegion.cpp:1552
void dumpToStream(raw_ostream &os) const override
Definition: MemRegion.cpp:532
Represent a region&#39;s offset within the top level base region.
Definition: MemRegion.h:62
const MemSpaceRegion * getMemorySpace() const
Definition: MemRegion.cpp:1125
void Profile(llvm::FoldingSetNodeID &ID) const override
Definition: MemRegion.cpp:278
static void ProfileRegion(llvm::FoldingSetNodeID &ID, const Decl *D, const MemRegion *superRegion, Kind k)
Definition: MemRegion.cpp:308
bool canPrintPretty() const override
Returns true if this region can be printed in a user-friendly way.
Definition: MemRegion.cpp:619
const BlockCodeRegion * getBlockCodeRegion(const BlockDecl *BD, CanQualType locTy, AnalysisDeclContext *AC)
Definition: MemRegion.cpp:1014
void dumpToStream(raw_ostream &os) const override
Definition: MemRegion.cpp:516
const CodeSpaceRegion * getCodeRegion()
Definition: MemRegion.cpp:796
bool hasAttr() const
Definition: DeclBase.h:530
return Out str()
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1612
DefinedOrUnknownSVal getExtent(SValBuilder &svalBuilder) const override
getExtent - Returns the size of the region in bytes.
Definition: MemRegion.cpp:166
SourceRange sourceRange() const
Retrieve source range from memory region.
Definition: MemRegion.cpp:698
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:178
DefinedOrUnknownSVal getExtent(SValBuilder &svalBuilder) const override
getExtent - Returns the size of the region in bytes.
Definition: MemRegion.cpp:199
const RegionTy * getAs() const
Definition: MemRegion.h:1230
SymbolicRegion - A special, "non-concrete" region.
Definition: MemRegion.h:763
unsigned Offset
Definition: Format.cpp:1630
ASTRecordLayout - This class contains layout information for one RecordDecl, which is a struct/union/...
Definition: RecordLayout.h:38
bool canPrintPrettyAsExpr() const override
Returns true if this region&#39;s textual representation can be used as part of a larger expression...
Definition: MemRegion.cpp:643
Pepresents a block literal declaration, which is like an unnamed FunctionDecl.
Definition: Decl.h:3857
This represents one expression.
Definition: Expr.h:106
void Profile(llvm::FoldingSetNodeID &ID) const override
Definition: MemRegion.cpp:417
void printPrettyAsExpr(raw_ostream &os) const override
Print the region as expression.
Definition: MemRegion.cpp:627
bool isInSystemHeader(SourceLocation Loc) const
Returns if a SourceLocation is in a system header.
bool hasLocalStorage() const
Returns true if a variable with function scope is a non-static local variable.
Definition: Decl.h:1035
bool canPrintPrettyAsExpr() const override
Returns true if this region&#39;s textual representation can be used as part of a larger expression...
Definition: MemRegion.cpp:623
DefinedOrUnknownSVal getExtent(SValBuilder &svalBuilder) const override
getExtent - Returns the size of the region in bytes.
Definition: MemRegion.cpp:203
void dumpToStream(raw_ostream &os) const override
Definition: MemRegion.cpp:576
field_iterator field_end() const
Definition: Decl.h:3786
static const int64_t Symbolic
Definition: MemRegion.h:72
DeclContext * getDeclContext()
Definition: DeclBase.h:426
llvm::BumpPtrAllocator & getAllocator()
Definition: MemRegion.h:1267
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition: CharUnits.h:62
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition: DeclBase.h:1751
void Profile(llvm::FoldingSetNodeID &ID) const override
Definition: MemRegion.cpp:406
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
Definition: Decl.cpp:3816
bool isNull() const
Return true if this QualType doesn&#39;t point to a type yet.
Definition: Type.h:702
const StackFrameContext * getStackFrame() const
Definition: MemRegion.h:394
The region of the static variables within the current CodeTextRegion scope.
Definition: MemRegion.h:256
const VarDecl * getDecl() const
Definition: MemRegion.h:952
Optional< T > getAs() const
Convert to the specified SVal type, returning None if this SVal is not of the desired type...
Definition: SVals.h:111
bool isConstQualified() const
Determine whether this type is const-qualified.
Definition: Type.h:6133
void dumpToStream(raw_ostream &os) const override
Definition: MemRegion.cpp:564
uint64_t getFieldOffset(unsigned FieldNo) const
getFieldOffset - Get the offset of the given field index, in bits.
Definition: RecordLayout.h:190
void Profile(llvm::FoldingSetNodeID &ID) const
Definition: SVals.h:123
Kind getKind() const
Definition: MemRegion.h:169
const BlockDecl * getDecl() const
Definition: MemRegion.h:651
const MemRegion * getMostDerivedObjectRegion() const
Recursively retrieve the region of the most derived class instance of regions of C++ base class insta...
Definition: MemRegion.cpp:1178
FunctionCodeRegion - A region that represents code texts of function.
Definition: MemRegion.h:580
virtual bool canPrintPretty() const
Returns true if this region can be printed in a user-friendly way.
Definition: MemRegion.cpp:584
static bool isImmediateBase(const CXXRecordDecl *Child, const CXXRecordDecl *Base)
Returns true if Base is an immediate base class of Child.
Definition: MemRegion.cpp:1277
void dumpToStream(raw_ostream &os) const override
Definition: MemRegion.cpp:548
const CXXTempObjectRegion * getCXXTempObjectRegion(Expr const *Ex, LocationContext const *LC)
Definition: MemRegion.cpp:1041
bool isSubRegionOf(const MemRegion *R) const override
Check if the region is a subregion of the given region.
Definition: MemRegion.cpp:132
void dumpToStream(raw_ostream &os) const override
Definition: MemRegion.cpp:470
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2040
ASTContext & getContext()
Definition: SValBuilder.h:155
void dumpToStream(raw_ostream &os) const override
Definition: MemRegion.cpp:503
bool hasGlobalStorage() const
Returns true for all variables that do not have local storage.
Definition: Decl.h:1077
void printPrettyAsExpr(raw_ostream &os) const override
Print the region as expression.
Definition: MemRegion.cpp:647
SVal - This represents a symbolic expression, which can be either an L-value or an R-value...
Definition: SVals.h:75
int64_t getID(const ASTContext &Context) const
Definition: Stmt.cpp:312
void dumpToStream(raw_ostream &os) const override
Definition: MemRegion.cpp:466
CanQualType VoidTy
Definition: ASTContext.h:1015
const StackFrameContext * getStackFrame() const
Definition: MemRegion.cpp:157
const CXXDerivedObjectRegion * getCXXDerivedObjectRegion(const CXXRecordDecl *BaseClass, const SubRegion *Super)
Create a CXXDerivedObjectRegion with the given derived class for region Super.
Definition: MemRegion.cpp:1091
DefinedOrUnknownSVal getExtent(SValBuilder &svalBuilder) const override
getExtent - Returns the size of the region in bytes.
Definition: MemRegion.cpp:207
virtual void printPrettyAsExpr(raw_ostream &os) const
Print the region as expression.
Definition: MemRegion.cpp:599
const CompoundLiteralRegion * getCompoundLiteralRegion(const CompoundLiteralExpr *CL, const LocationContext *LC)
getCompoundLiteralRegion - Retrieve the region associated with a given CompoundLiteral.
Definition: MemRegion.cpp:971
void Profile(llvm::FoldingSetNodeID &ID) const override
Definition: MemRegion.cpp:239
referenced_vars_iterator referenced_vars_begin() const
Definition: MemRegion.cpp:1536
void dumpToStream(raw_ostream &os) const override
Definition: MemRegion.cpp:481
std::string getDescriptiveName(bool UseQuotes=true) const
Get descriptive name for memory region.
Definition: MemRegion.cpp:659
const CXXThisRegion * getCXXThisRegion(QualType thisPointerTy, const LocationContext *LC)
getCXXThisRegion - Retrieve the [artificial] region associated with the parameter &#39;this&#39;...
Definition: MemRegion.cpp:1097
RegionRawOffset getAsArrayOffset() const
Compute the offset within the array. The array might also be a subobject.
Definition: MemRegion.cpp:1227
virtual void printPretty(raw_ostream &os) const
Print the region for use in diagnostics.
Definition: MemRegion.cpp:592
const CXXBaseObjectRegion * getCXXBaseObjectRegion(const CXXRecordDecl *BaseClass, const SubRegion *Super, bool IsVirtual)
Create a CXXBaseObjectRegion with the given base class for region Super.
Definition: MemRegion.cpp:1071
bool isVirtuallyDerivedFrom(const CXXRecordDecl *Base) const
Determine whether this class is virtually derived from the class Base.
const SymbolicRegion * getSymbolicHeapRegion(SymbolRef sym)
Return a unique symbolic region belonging to heap memory space.
Definition: MemRegion.cpp:1024
Dataflow Directional Tag Classes.
QualType getValueType() const override
Definition: MemRegion.cpp:223
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1261
bool isZeroConstant() const
Definition: SVals.cpp:233
llvm::iterator_range< referenced_decls_iterator > getReferencedBlockVars(const BlockDecl *BD)
const VarRegion * getVarRegion(const VarDecl *D, const LocationContext *LC)
getVarRegion - Retrieve or create the memory region associated with a specified VarDecl and LocationC...
Definition: MemRegion.cpp:845
InvalidationKinds
Describes different invalidation traits.
Definition: MemRegion.h:1455
const ElementRegion * getElementRegion(QualType elementType, NonLoc Idx, const SubRegion *superRegion, ASTContext &Ctx)
getElementRegion - Retrieve the memory region associated with the associated element type...
Definition: MemRegion.cpp:987
QualType getFunctionNoProtoType(QualType ResultTy, const FunctionType::ExtInfo &Info) const
Return a K&R style C function type like &#39;int()&#39;.
const MemRegion * StripCasts(bool StripBaseAndDerivedCasts=true) const
Definition: MemRegion.cpp:1193
Represents symbolic expression that isn&#39;t a location.
Definition: SVals.h:346
const VarRegion * getOriginalRegion(const VarRegion *VR) const
Return the original region for a captured region, if one exists.
Definition: MemRegion.cpp:1567
void Profile(llvm::FoldingSetNodeID &ID) const override
Definition: MemRegion.cpp:315
static llvm::PointerUnion< const StackFrameContext *, const VarRegion * > getStackOrCaptureRegionForDeclContext(const LocationContext *LC, const DeclContext *DC, const VarDecl *VD)
Look through a chain of LocationContexts to either find the StackFrameContext that matches a DeclCont...
Definition: MemRegion.cpp:819
void dumpToStream(raw_ostream &os) const override
Definition: MemRegion.cpp:462
const FunctionCodeRegion * getFunctionCodeRegion(const NamedDecl *FD)
Definition: MemRegion.cpp:1009
virtual bool canPrintPrettyAsExpr() const
Returns true if this region&#39;s textual representation can be used as part of a larger expression...
Definition: MemRegion.cpp:588
specific_decl_iterator - Iterates over a subrange of declarations stored in a DeclContext, providing only those that are of type SpecificDecl (or a class derived from it).
Definition: DeclBase.h:2016
const Decl * getDecl() const
const CXXTempObjectRegion * getCXXStaticTempObjectRegion(const Expr *Ex)
Create a CXXTempObjectRegion for temporaries which are lifetime-extended by static references...
Definition: MemRegion.cpp:965
void dumpToStream(raw_ostream &os) const override
Definition: MemRegion.cpp:572
virtual MemRegionManager * getMemRegionManager() const =0
void dumpToStream(raw_ostream &os) const override
Definition: MemRegion.cpp:458
SubRegion - A region that subsets another larger region.
Definition: MemRegion.h:435
bool isStaticLocal() const
Returns true if a variable with function scope is a static local variable.
Definition: Decl.h:1059
RegionOffset getAsOffset() const
Compute the offset within the top level memory object.
Definition: MemRegion.cpp:1463
bool hasStackStorage() const
Definition: MemRegion.cpp:1137
const StackFrameContext * getStackFrame() const
void Profile(llvm::FoldingSetNodeID &ID) const override
Definition: MemRegion.cpp:298
const Type * getTypePtrOrNull() const
Definition: Type.h:6078
SourceManager & getSourceManager()
Definition: ASTContext.h:661
bool isIncompleteType(NamedDecl **Def=nullptr) const
Types are partitioned into 3 broad categories (C99 6.2.5p1): object types, function types...
Definition: Type.cpp:2030
const BlockDataRegion * getBlockDataRegion(const BlockCodeRegion *bc, const LocationContext *lc, unsigned blockCount)
getBlockDataRegion - Get the memory region associated with an instance of a block.
Definition: MemRegion.cpp:937
CanQualType getCanonicalType(QualType T) const
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
Definition: ASTContext.h:2255
MemRegionManager * getMemRegionManager() const override
Definition: MemRegion.cpp:145
void Profile(llvm::FoldingSetNodeID &ID) const override
Definition: MemRegion.cpp:393
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
bool canPrintPrettyAsExpr() const override
Returns true if this region&#39;s textual representation can be used as part of a larger expression...
Definition: MemRegion.cpp:603
void printPretty(raw_ostream &OS, PrinterHelper *Helper, const PrintingPolicy &Policy, unsigned Indentation=0, StringRef NewlineSymbol="\, const ASTContext *Context=nullptr) const
const SymbolExtent * getExtentSymbol(const SubRegion *R)
static void ProfileRegion(llvm::FoldingSetNodeID &ID, SymbolRef sym, const MemRegion *superRegion)
Definition: MemRegion.cpp:323
Represents a C++ struct/union/class.
Definition: DeclCXX.h:299
void dumpToStream(raw_ostream &os) const override
Definition: MemRegion.cpp:580
static RegionOffset calculateOffset(const MemRegion *R)
Definition: MemRegion.cpp:1291
virtual bool isSubRegionOf(const MemRegion *R) const
Check if the region is a subregion of the given region.
Definition: MemRegion.cpp:1185
void printPrettyAsExpr(raw_ostream &os) const override
Print the region as expression.
Definition: MemRegion.cpp:607
ObjCIvarDecl - Represents an ObjC instance variable.
Definition: DeclObjC.h:1944
const MemRegion * getBaseRegion() const
Definition: MemRegion.cpp:1157
StringLiteral - This represents a string literal expression, e.g.
Definition: Expr.h:1572
void dumpToStream(raw_ostream &os) const override
Definition: MemRegion.cpp:486
StringRegion - Region associated with a StringLiteral.
Definition: MemRegion.h:803
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition: Decl.h:275
void Profile(llvm::FoldingSetNodeID &ID) const override
Definition: MemRegion.cpp:319
ElementRegion is used to represent both array elements and casts.
Definition: MemRegion.h:1085
static Decl::Kind getKind(const Decl *D)
Definition: DeclBase.cpp:953
bool isUnion() const
Definition: Decl.h:3251
void dumpToStream(raw_ostream &os) const override
Definition: MemRegion.cpp:560
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
QualType getType() const
Definition: Decl.h:647
bool hasStackNonParametersStorage() const
Definition: MemRegion.cpp:1141
A trivial tuple used to represent a source range.
This represents a decl that may have a name.
Definition: Decl.h:248
bool hasGlobalsOrParametersStorage() const
Definition: MemRegion.cpp:1149
void dumpToStream(raw_ostream &os) const override
Definition: MemRegion.cpp:552
const FieldRegion * getFieldRegion(const FieldDecl *fd, const SubRegion *superRegion)
getFieldRegion - Retrieve or create the memory region associated with a specified FieldDecl...
Definition: MemRegion.cpp:1029
void dumpToStream(raw_ostream &os) const override
Definition: MemRegion.cpp:499
SourceLocation getLocation() const
Definition: DeclBase.h:417
void dumpToStream(raw_ostream &os) const override
Definition: MemRegion.cpp:512