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