MemRegion.cpp 56 KB

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