ExprEngine.cpp 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140
  1. //=-- ExprEngine.cpp - Path-Sensitive Expression-Level Dataflow ---*- C++ -*-=
  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 a meta-engine for path-sensitive dataflow analysis that
  11. // is built on GREngine, but provides the boilerplate to execute transfer
  12. // functions and build the ExplodedGraph at the expression level.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #define DEBUG_TYPE "ExprEngine"
  16. #include "clang/StaticAnalyzer/Core/CheckerManager.h"
  17. #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
  18. #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
  19. #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
  20. #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
  21. #include "clang/AST/CharUnits.h"
  22. #include "clang/AST/ParentMap.h"
  23. #include "clang/AST/StmtObjC.h"
  24. #include "clang/AST/StmtCXX.h"
  25. #include "clang/Basic/Builtins.h"
  26. #include "clang/Basic/SourceManager.h"
  27. #include "clang/Basic/PrettyStackTrace.h"
  28. #include "llvm/Support/raw_ostream.h"
  29. #include "llvm/ADT/ImmutableList.h"
  30. #include "llvm/ADT/Statistic.h"
  31. #ifndef NDEBUG
  32. #include "llvm/Support/GraphWriter.h"
  33. #endif
  34. using namespace clang;
  35. using namespace ento;
  36. using llvm::APSInt;
  37. STATISTIC(NumRemoveDeadBindings,
  38. "The # of times RemoveDeadBindings is called");
  39. STATISTIC(NumMaxBlockCountReached,
  40. "The # of aborted paths due to reaching the maximum block count in "
  41. "a top level function");
  42. STATISTIC(NumMaxBlockCountReachedInInlined,
  43. "The # of aborted paths due to reaching the maximum block count in "
  44. "an inlined function");
  45. STATISTIC(NumTimesRetriedWithoutInlining,
  46. "The # of times we re-evaluated a call without inlining");
  47. //===----------------------------------------------------------------------===//
  48. // Engine construction and deletion.
  49. //===----------------------------------------------------------------------===//
  50. ExprEngine::ExprEngine(AnalysisManager &mgr, bool gcEnabled,
  51. SetOfConstDecls *VisitedCalleesIn,
  52. FunctionSummariesTy *FS)
  53. : AMgr(mgr),
  54. AnalysisDeclContexts(mgr.getAnalysisDeclContextManager()),
  55. Engine(*this, FS),
  56. G(Engine.getGraph()),
  57. StateMgr(getContext(), mgr.getStoreManagerCreator(),
  58. mgr.getConstraintManagerCreator(), G.getAllocator(),
  59. this),
  60. SymMgr(StateMgr.getSymbolManager()),
  61. svalBuilder(StateMgr.getSValBuilder()),
  62. EntryNode(NULL),
  63. currStmt(NULL), currStmtIdx(0), currBldrCtx(0),
  64. ObjCNoRet(mgr.getASTContext()),
  65. ObjCGCEnabled(gcEnabled), BR(mgr, *this),
  66. VisitedCallees(VisitedCalleesIn)
  67. {
  68. unsigned TrimInterval = mgr.options.getGraphTrimInterval();
  69. if (TrimInterval != 0) {
  70. // Enable eager node reclaimation when constructing the ExplodedGraph.
  71. G.enableNodeReclamation(TrimInterval);
  72. }
  73. }
  74. ExprEngine::~ExprEngine() {
  75. BR.FlushReports();
  76. }
  77. //===----------------------------------------------------------------------===//
  78. // Utility methods.
  79. //===----------------------------------------------------------------------===//
  80. ProgramStateRef ExprEngine::getInitialState(const LocationContext *InitLoc) {
  81. ProgramStateRef state = StateMgr.getInitialState(InitLoc);
  82. const Decl *D = InitLoc->getDecl();
  83. // Preconditions.
  84. // FIXME: It would be nice if we had a more general mechanism to add
  85. // such preconditions. Some day.
  86. do {
  87. if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
  88. // Precondition: the first argument of 'main' is an integer guaranteed
  89. // to be > 0.
  90. const IdentifierInfo *II = FD->getIdentifier();
  91. if (!II || !(II->getName() == "main" && FD->getNumParams() > 0))
  92. break;
  93. const ParmVarDecl *PD = FD->getParamDecl(0);
  94. QualType T = PD->getType();
  95. if (!T->isIntegerType())
  96. break;
  97. const MemRegion *R = state->getRegion(PD, InitLoc);
  98. if (!R)
  99. break;
  100. SVal V = state->getSVal(loc::MemRegionVal(R));
  101. SVal Constraint_untested = evalBinOp(state, BO_GT, V,
  102. svalBuilder.makeZeroVal(T),
  103. getContext().IntTy);
  104. DefinedOrUnknownSVal *Constraint =
  105. dyn_cast<DefinedOrUnknownSVal>(&Constraint_untested);
  106. if (!Constraint)
  107. break;
  108. if (ProgramStateRef newState = state->assume(*Constraint, true))
  109. state = newState;
  110. }
  111. break;
  112. }
  113. while (0);
  114. if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
  115. // Precondition: 'self' is always non-null upon entry to an Objective-C
  116. // method.
  117. const ImplicitParamDecl *SelfD = MD->getSelfDecl();
  118. const MemRegion *R = state->getRegion(SelfD, InitLoc);
  119. SVal V = state->getSVal(loc::MemRegionVal(R));
  120. if (const Loc *LV = dyn_cast<Loc>(&V)) {
  121. // Assume that the pointer value in 'self' is non-null.
  122. state = state->assume(*LV, true);
  123. assert(state && "'self' cannot be null");
  124. }
  125. }
  126. if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
  127. if (!MD->isStatic()) {
  128. // Precondition: 'this' is always non-null upon entry to the
  129. // top-level function. This is our starting assumption for
  130. // analyzing an "open" program.
  131. const StackFrameContext *SFC = InitLoc->getCurrentStackFrame();
  132. if (SFC->getParent() == 0) {
  133. loc::MemRegionVal L = svalBuilder.getCXXThis(MD, SFC);
  134. SVal V = state->getSVal(L);
  135. if (const Loc *LV = dyn_cast<Loc>(&V)) {
  136. state = state->assume(*LV, true);
  137. assert(state && "'this' cannot be null");
  138. }
  139. }
  140. }
  141. }
  142. return state;
  143. }
  144. /// If the value of the given expression is a NonLoc, copy it into a new
  145. /// temporary region, and replace the value of the expression with that.
  146. static ProgramStateRef createTemporaryRegionIfNeeded(ProgramStateRef State,
  147. const LocationContext *LC,
  148. const Expr *E) {
  149. SVal V = State->getSVal(E, LC);
  150. if (isa<NonLoc>(V)) {
  151. MemRegionManager &MRMgr = State->getStateManager().getRegionManager();
  152. const MemRegion *R = MRMgr.getCXXTempObjectRegion(E, LC);
  153. State = State->bindLoc(loc::MemRegionVal(R), V);
  154. State = State->BindExpr(E, LC, loc::MemRegionVal(R));
  155. }
  156. return State;
  157. }
  158. //===----------------------------------------------------------------------===//
  159. // Top-level transfer function logic (Dispatcher).
  160. //===----------------------------------------------------------------------===//
  161. /// evalAssume - Called by ConstraintManager. Used to call checker-specific
  162. /// logic for handling assumptions on symbolic values.
  163. ProgramStateRef ExprEngine::processAssume(ProgramStateRef state,
  164. SVal cond, bool assumption) {
  165. return getCheckerManager().runCheckersForEvalAssume(state, cond, assumption);
  166. }
  167. bool ExprEngine::wantsRegionChangeUpdate(ProgramStateRef state) {
  168. return getCheckerManager().wantsRegionChangeUpdate(state);
  169. }
  170. ProgramStateRef
  171. ExprEngine::processRegionChanges(ProgramStateRef state,
  172. const StoreManager::InvalidatedSymbols *invalidated,
  173. ArrayRef<const MemRegion *> Explicits,
  174. ArrayRef<const MemRegion *> Regions,
  175. const CallEvent *Call) {
  176. return getCheckerManager().runCheckersForRegionChanges(state, invalidated,
  177. Explicits, Regions, Call);
  178. }
  179. void ExprEngine::printState(raw_ostream &Out, ProgramStateRef State,
  180. const char *NL, const char *Sep) {
  181. getCheckerManager().runCheckersForPrintState(Out, State, NL, Sep);
  182. }
  183. void ExprEngine::processEndWorklist(bool hasWorkRemaining) {
  184. getCheckerManager().runCheckersForEndAnalysis(G, BR, *this);
  185. }
  186. void ExprEngine::processCFGElement(const CFGElement E, ExplodedNode *Pred,
  187. unsigned StmtIdx, NodeBuilderContext *Ctx) {
  188. currStmtIdx = StmtIdx;
  189. currBldrCtx = Ctx;
  190. switch (E.getKind()) {
  191. case CFGElement::Invalid:
  192. llvm_unreachable("Unexpected CFGElement kind.");
  193. case CFGElement::Statement:
  194. ProcessStmt(const_cast<Stmt*>(E.getAs<CFGStmt>()->getStmt()), Pred);
  195. return;
  196. case CFGElement::Initializer:
  197. ProcessInitializer(E.getAs<CFGInitializer>()->getInitializer(), Pred);
  198. return;
  199. case CFGElement::AutomaticObjectDtor:
  200. case CFGElement::BaseDtor:
  201. case CFGElement::MemberDtor:
  202. case CFGElement::TemporaryDtor:
  203. ProcessImplicitDtor(*E.getAs<CFGImplicitDtor>(), Pred);
  204. return;
  205. }
  206. currBldrCtx = 0;
  207. }
  208. static bool shouldRemoveDeadBindings(AnalysisManager &AMgr,
  209. const CFGStmt S,
  210. const ExplodedNode *Pred,
  211. const LocationContext *LC) {
  212. // Are we never purging state values?
  213. if (AMgr.options.AnalysisPurgeOpt == PurgeNone)
  214. return false;
  215. // Is this the beginning of a basic block?
  216. if (isa<BlockEntrance>(Pred->getLocation()))
  217. return true;
  218. // Is this on a non-expression?
  219. if (!isa<Expr>(S.getStmt()))
  220. return true;
  221. // Run before processing a call.
  222. if (CallEvent::isCallStmt(S.getStmt()))
  223. return true;
  224. // Is this an expression that is consumed by another expression? If so,
  225. // postpone cleaning out the state.
  226. ParentMap &PM = LC->getAnalysisDeclContext()->getParentMap();
  227. return !PM.isConsumedExpr(cast<Expr>(S.getStmt()));
  228. }
  229. void ExprEngine::removeDead(ExplodedNode *Pred, ExplodedNodeSet &Out,
  230. const Stmt *ReferenceStmt,
  231. const LocationContext *LC,
  232. const Stmt *DiagnosticStmt,
  233. ProgramPoint::Kind K) {
  234. assert((K == ProgramPoint::PreStmtPurgeDeadSymbolsKind ||
  235. ReferenceStmt == 0) && "PreStmt is not generally supported by "
  236. "the SymbolReaper yet");
  237. NumRemoveDeadBindings++;
  238. CleanedState = Pred->getState();
  239. SymbolReaper SymReaper(LC, ReferenceStmt, SymMgr, getStoreManager());
  240. getCheckerManager().runCheckersForLiveSymbols(CleanedState, SymReaper);
  241. // Create a state in which dead bindings are removed from the environment
  242. // and the store. TODO: The function should just return new env and store,
  243. // not a new state.
  244. const StackFrameContext *SFC = LC->getCurrentStackFrame();
  245. CleanedState = StateMgr.removeDeadBindings(CleanedState, SFC, SymReaper);
  246. // Process any special transfer function for dead symbols.
  247. // A tag to track convenience transitions, which can be removed at cleanup.
  248. static SimpleProgramPointTag cleanupTag("ExprEngine : Clean Node");
  249. if (!SymReaper.hasDeadSymbols()) {
  250. // Generate a CleanedNode that has the environment and store cleaned
  251. // up. Since no symbols are dead, we can optimize and not clean out
  252. // the constraint manager.
  253. StmtNodeBuilder Bldr(Pred, Out, *currBldrCtx);
  254. Bldr.generateNode(DiagnosticStmt, Pred, CleanedState, &cleanupTag, K);
  255. } else {
  256. // Call checkers with the non-cleaned state so that they could query the
  257. // values of the soon to be dead symbols.
  258. ExplodedNodeSet CheckedSet;
  259. getCheckerManager().runCheckersForDeadSymbols(CheckedSet, Pred, SymReaper,
  260. DiagnosticStmt, *this, K);
  261. // For each node in CheckedSet, generate CleanedNodes that have the
  262. // environment, the store, and the constraints cleaned up but have the
  263. // user-supplied states as the predecessors.
  264. StmtNodeBuilder Bldr(CheckedSet, Out, *currBldrCtx);
  265. for (ExplodedNodeSet::const_iterator
  266. I = CheckedSet.begin(), E = CheckedSet.end(); I != E; ++I) {
  267. ProgramStateRef CheckerState = (*I)->getState();
  268. // The constraint manager has not been cleaned up yet, so clean up now.
  269. CheckerState = getConstraintManager().removeDeadBindings(CheckerState,
  270. SymReaper);
  271. assert(StateMgr.haveEqualEnvironments(CheckerState, Pred->getState()) &&
  272. "Checkers are not allowed to modify the Environment as a part of "
  273. "checkDeadSymbols processing.");
  274. assert(StateMgr.haveEqualStores(CheckerState, Pred->getState()) &&
  275. "Checkers are not allowed to modify the Store as a part of "
  276. "checkDeadSymbols processing.");
  277. // Create a state based on CleanedState with CheckerState GDM and
  278. // generate a transition to that state.
  279. ProgramStateRef CleanedCheckerSt =
  280. StateMgr.getPersistentStateWithGDM(CleanedState, CheckerState);
  281. Bldr.generateNode(DiagnosticStmt, *I, CleanedCheckerSt, &cleanupTag, K);
  282. }
  283. }
  284. }
  285. void ExprEngine::ProcessStmt(const CFGStmt S,
  286. ExplodedNode *Pred) {
  287. // Reclaim any unnecessary nodes in the ExplodedGraph.
  288. G.reclaimRecentlyAllocatedNodes();
  289. currStmt = S.getStmt();
  290. PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
  291. currStmt->getLocStart(),
  292. "Error evaluating statement");
  293. // Remove dead bindings and symbols.
  294. EntryNode = Pred;
  295. ExplodedNodeSet CleanedStates;
  296. if (shouldRemoveDeadBindings(AMgr, S, Pred, EntryNode->getLocationContext())){
  297. removeDead(EntryNode, CleanedStates, currStmt,
  298. Pred->getLocationContext(), currStmt);
  299. } else
  300. CleanedStates.Add(EntryNode);
  301. // Visit the statement.
  302. ExplodedNodeSet Dst;
  303. for (ExplodedNodeSet::iterator I = CleanedStates.begin(),
  304. E = CleanedStates.end(); I != E; ++I) {
  305. ExplodedNodeSet DstI;
  306. // Visit the statement.
  307. Visit(currStmt, *I, DstI);
  308. Dst.insert(DstI);
  309. }
  310. // Enqueue the new nodes onto the work list.
  311. Engine.enqueue(Dst, currBldrCtx->getBlock(), currStmtIdx);
  312. // NULL out these variables to cleanup.
  313. CleanedState = NULL;
  314. EntryNode = NULL;
  315. currStmt = 0;
  316. }
  317. void ExprEngine::ProcessInitializer(const CFGInitializer Init,
  318. ExplodedNode *Pred) {
  319. const CXXCtorInitializer *BMI = Init.getInitializer();
  320. PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
  321. BMI->getSourceLocation(),
  322. "Error evaluating initializer");
  323. // We don't set EntryNode and currStmt. And we don't clean up state.
  324. const StackFrameContext *stackFrame =
  325. cast<StackFrameContext>(Pred->getLocationContext());
  326. const CXXConstructorDecl *decl =
  327. cast<CXXConstructorDecl>(stackFrame->getDecl());
  328. ProgramStateRef State = Pred->getState();
  329. SVal thisVal = State->getSVal(svalBuilder.getCXXThis(decl, stackFrame));
  330. PostInitializer PP(BMI, stackFrame);
  331. ExplodedNodeSet Tmp(Pred);
  332. // Evaluate the initializer, if necessary
  333. if (BMI->isAnyMemberInitializer()) {
  334. // Constructors build the object directly in the field,
  335. // but non-objects must be copied in from the initializer.
  336. const Expr *Init = BMI->getInit();
  337. if (!isa<CXXConstructExpr>(Init)) {
  338. SVal FieldLoc;
  339. if (BMI->isIndirectMemberInitializer())
  340. FieldLoc = State->getLValue(BMI->getIndirectMember(), thisVal);
  341. else
  342. FieldLoc = State->getLValue(BMI->getMember(), thisVal);
  343. SVal InitVal = State->getSVal(BMI->getInit(), stackFrame);
  344. Tmp.clear();
  345. evalBind(Tmp, Init, Pred, FieldLoc, InitVal, /*isInit=*/true, &PP);
  346. }
  347. } else {
  348. assert(BMI->isBaseInitializer() || BMI->isDelegatingInitializer());
  349. // We already did all the work when visiting the CXXConstructExpr.
  350. }
  351. // Construct PostInitializer nodes whether the state changed or not,
  352. // so that the diagnostics don't get confused.
  353. ExplodedNodeSet Dst;
  354. NodeBuilder Bldr(Tmp, Dst, *currBldrCtx);
  355. for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; ++I) {
  356. ExplodedNode *N = *I;
  357. Bldr.generateNode(PP, N->getState(), N);
  358. }
  359. // Enqueue the new nodes onto the work list.
  360. Engine.enqueue(Dst, currBldrCtx->getBlock(), currStmtIdx);
  361. }
  362. void ExprEngine::ProcessImplicitDtor(const CFGImplicitDtor D,
  363. ExplodedNode *Pred) {
  364. ExplodedNodeSet Dst;
  365. switch (D.getKind()) {
  366. case CFGElement::AutomaticObjectDtor:
  367. ProcessAutomaticObjDtor(cast<CFGAutomaticObjDtor>(D), Pred, Dst);
  368. break;
  369. case CFGElement::BaseDtor:
  370. ProcessBaseDtor(cast<CFGBaseDtor>(D), Pred, Dst);
  371. break;
  372. case CFGElement::MemberDtor:
  373. ProcessMemberDtor(cast<CFGMemberDtor>(D), Pred, Dst);
  374. break;
  375. case CFGElement::TemporaryDtor:
  376. ProcessTemporaryDtor(cast<CFGTemporaryDtor>(D), Pred, Dst);
  377. break;
  378. default:
  379. llvm_unreachable("Unexpected dtor kind.");
  380. }
  381. // Enqueue the new nodes onto the work list.
  382. Engine.enqueue(Dst, currBldrCtx->getBlock(), currStmtIdx);
  383. }
  384. void ExprEngine::ProcessAutomaticObjDtor(const CFGAutomaticObjDtor Dtor,
  385. ExplodedNode *Pred,
  386. ExplodedNodeSet &Dst) {
  387. ProgramStateRef state = Pred->getState();
  388. const VarDecl *varDecl = Dtor.getVarDecl();
  389. QualType varType = varDecl->getType();
  390. if (const ReferenceType *refType = varType->getAs<ReferenceType>())
  391. varType = refType->getPointeeType();
  392. Loc dest = state->getLValue(varDecl, Pred->getLocationContext());
  393. VisitCXXDestructor(varType, cast<loc::MemRegionVal>(dest).getRegion(),
  394. Dtor.getTriggerStmt(), /*IsBase=*/false, Pred, Dst);
  395. }
  396. void ExprEngine::ProcessBaseDtor(const CFGBaseDtor D,
  397. ExplodedNode *Pred, ExplodedNodeSet &Dst) {
  398. const LocationContext *LCtx = Pred->getLocationContext();
  399. ProgramStateRef State = Pred->getState();
  400. const CXXDestructorDecl *CurDtor = cast<CXXDestructorDecl>(LCtx->getDecl());
  401. Loc ThisPtr = getSValBuilder().getCXXThis(CurDtor,
  402. LCtx->getCurrentStackFrame());
  403. SVal ThisVal = Pred->getState()->getSVal(ThisPtr);
  404. // Create the base object region.
  405. QualType BaseTy = D.getBaseSpecifier()->getType();
  406. SVal BaseVal = getStoreManager().evalDerivedToBase(ThisVal, BaseTy);
  407. VisitCXXDestructor(BaseTy, cast<loc::MemRegionVal>(BaseVal).getRegion(),
  408. CurDtor->getBody(), /*IsBase=*/true, Pred, Dst);
  409. }
  410. void ExprEngine::ProcessMemberDtor(const CFGMemberDtor D,
  411. ExplodedNode *Pred, ExplodedNodeSet &Dst) {
  412. const FieldDecl *Member = D.getFieldDecl();
  413. ProgramStateRef State = Pred->getState();
  414. const LocationContext *LCtx = Pred->getLocationContext();
  415. const CXXDestructorDecl *CurDtor = cast<CXXDestructorDecl>(LCtx->getDecl());
  416. Loc ThisVal = getSValBuilder().getCXXThis(CurDtor,
  417. LCtx->getCurrentStackFrame());
  418. SVal FieldVal = State->getLValue(Member, cast<Loc>(State->getSVal(ThisVal)));
  419. VisitCXXDestructor(Member->getType(),
  420. cast<loc::MemRegionVal>(FieldVal).getRegion(),
  421. CurDtor->getBody(), /*IsBase=*/false, Pred, Dst);
  422. }
  423. void ExprEngine::ProcessTemporaryDtor(const CFGTemporaryDtor D,
  424. ExplodedNode *Pred,
  425. ExplodedNodeSet &Dst) {}
  426. void ExprEngine::Visit(const Stmt *S, ExplodedNode *Pred,
  427. ExplodedNodeSet &DstTop) {
  428. PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
  429. S->getLocStart(),
  430. "Error evaluating statement");
  431. ExplodedNodeSet Dst;
  432. StmtNodeBuilder Bldr(Pred, DstTop, *currBldrCtx);
  433. // Expressions to ignore.
  434. if (const Expr *Ex = dyn_cast<Expr>(S))
  435. S = Ex->IgnoreParens();
  436. // FIXME: add metadata to the CFG so that we can disable
  437. // this check when we KNOW that there is no block-level subexpression.
  438. // The motivation is that this check requires a hashtable lookup.
  439. if (S != currStmt && Pred->getLocationContext()->getCFG()->isBlkExpr(S))
  440. return;
  441. switch (S->getStmtClass()) {
  442. // C++ and ARC stuff we don't support yet.
  443. case Expr::ObjCIndirectCopyRestoreExprClass:
  444. case Stmt::CXXDependentScopeMemberExprClass:
  445. case Stmt::CXXPseudoDestructorExprClass:
  446. case Stmt::CXXTryStmtClass:
  447. case Stmt::CXXTypeidExprClass:
  448. case Stmt::CXXUuidofExprClass:
  449. case Stmt::CXXUnresolvedConstructExprClass:
  450. case Stmt::DependentScopeDeclRefExprClass:
  451. case Stmt::UnaryTypeTraitExprClass:
  452. case Stmt::BinaryTypeTraitExprClass:
  453. case Stmt::TypeTraitExprClass:
  454. case Stmt::ArrayTypeTraitExprClass:
  455. case Stmt::ExpressionTraitExprClass:
  456. case Stmt::UnresolvedLookupExprClass:
  457. case Stmt::UnresolvedMemberExprClass:
  458. case Stmt::CXXNoexceptExprClass:
  459. case Stmt::PackExpansionExprClass:
  460. case Stmt::SubstNonTypeTemplateParmPackExprClass:
  461. case Stmt::FunctionParmPackExprClass:
  462. case Stmt::SEHTryStmtClass:
  463. case Stmt::SEHExceptStmtClass:
  464. case Stmt::LambdaExprClass:
  465. case Stmt::SEHFinallyStmtClass: {
  466. const ExplodedNode *node = Bldr.generateSink(S, Pred, Pred->getState());
  467. Engine.addAbortedBlock(node, currBldrCtx->getBlock());
  468. break;
  469. }
  470. case Stmt::ParenExprClass:
  471. llvm_unreachable("ParenExprs already handled.");
  472. case Stmt::GenericSelectionExprClass:
  473. llvm_unreachable("GenericSelectionExprs already handled.");
  474. // Cases that should never be evaluated simply because they shouldn't
  475. // appear in the CFG.
  476. case Stmt::BreakStmtClass:
  477. case Stmt::CaseStmtClass:
  478. case Stmt::CompoundStmtClass:
  479. case Stmt::ContinueStmtClass:
  480. case Stmt::CXXForRangeStmtClass:
  481. case Stmt::DefaultStmtClass:
  482. case Stmt::DoStmtClass:
  483. case Stmt::ForStmtClass:
  484. case Stmt::GotoStmtClass:
  485. case Stmt::IfStmtClass:
  486. case Stmt::IndirectGotoStmtClass:
  487. case Stmt::LabelStmtClass:
  488. case Stmt::AttributedStmtClass:
  489. case Stmt::NoStmtClass:
  490. case Stmt::NullStmtClass:
  491. case Stmt::SwitchStmtClass:
  492. case Stmt::WhileStmtClass:
  493. case Expr::MSDependentExistsStmtClass:
  494. llvm_unreachable("Stmt should not be in analyzer evaluation loop");
  495. case Stmt::ObjCSubscriptRefExprClass:
  496. case Stmt::ObjCPropertyRefExprClass:
  497. llvm_unreachable("These are handled by PseudoObjectExpr");
  498. case Stmt::GNUNullExprClass: {
  499. // GNU __null is a pointer-width integer, not an actual pointer.
  500. ProgramStateRef state = Pred->getState();
  501. state = state->BindExpr(S, Pred->getLocationContext(),
  502. svalBuilder.makeIntValWithPtrWidth(0, false));
  503. Bldr.generateNode(S, Pred, state);
  504. break;
  505. }
  506. case Stmt::ObjCAtSynchronizedStmtClass:
  507. Bldr.takeNodes(Pred);
  508. VisitObjCAtSynchronizedStmt(cast<ObjCAtSynchronizedStmt>(S), Pred, Dst);
  509. Bldr.addNodes(Dst);
  510. break;
  511. case Stmt::ExprWithCleanupsClass:
  512. // Handled due to fully linearised CFG.
  513. break;
  514. // Cases not handled yet; but will handle some day.
  515. case Stmt::DesignatedInitExprClass:
  516. case Stmt::ExtVectorElementExprClass:
  517. case Stmt::ImaginaryLiteralClass:
  518. case Stmt::ObjCAtCatchStmtClass:
  519. case Stmt::ObjCAtFinallyStmtClass:
  520. case Stmt::ObjCAtTryStmtClass:
  521. case Stmt::ObjCAutoreleasePoolStmtClass:
  522. case Stmt::ObjCEncodeExprClass:
  523. case Stmt::ObjCIsaExprClass:
  524. case Stmt::ObjCProtocolExprClass:
  525. case Stmt::ObjCSelectorExprClass:
  526. case Stmt::ParenListExprClass:
  527. case Stmt::PredefinedExprClass:
  528. case Stmt::ShuffleVectorExprClass:
  529. case Stmt::VAArgExprClass:
  530. case Stmt::CUDAKernelCallExprClass:
  531. case Stmt::OpaqueValueExprClass:
  532. case Stmt::AsTypeExprClass:
  533. case Stmt::AtomicExprClass:
  534. // Fall through.
  535. // Cases we intentionally don't evaluate, since they don't need
  536. // to be explicitly evaluated.
  537. case Stmt::AddrLabelExprClass:
  538. case Stmt::IntegerLiteralClass:
  539. case Stmt::CharacterLiteralClass:
  540. case Stmt::ImplicitValueInitExprClass:
  541. case Stmt::CXXScalarValueInitExprClass:
  542. case Stmt::CXXBoolLiteralExprClass:
  543. case Stmt::ObjCBoolLiteralExprClass:
  544. case Stmt::FloatingLiteralClass:
  545. case Stmt::SizeOfPackExprClass:
  546. case Stmt::StringLiteralClass:
  547. case Stmt::ObjCStringLiteralClass:
  548. case Stmt::CXXBindTemporaryExprClass:
  549. case Stmt::CXXDefaultArgExprClass:
  550. case Stmt::SubstNonTypeTemplateParmExprClass:
  551. case Stmt::CXXNullPtrLiteralExprClass: {
  552. Bldr.takeNodes(Pred);
  553. ExplodedNodeSet preVisit;
  554. getCheckerManager().runCheckersForPreStmt(preVisit, Pred, S, *this);
  555. getCheckerManager().runCheckersForPostStmt(Dst, preVisit, S, *this);
  556. Bldr.addNodes(Dst);
  557. break;
  558. }
  559. case Expr::ObjCArrayLiteralClass:
  560. case Expr::ObjCDictionaryLiteralClass:
  561. // FIXME: explicitly model with a region and the actual contents
  562. // of the container. For now, conjure a symbol.
  563. case Expr::ObjCBoxedExprClass: {
  564. Bldr.takeNodes(Pred);
  565. ExplodedNodeSet preVisit;
  566. getCheckerManager().runCheckersForPreStmt(preVisit, Pred, S, *this);
  567. ExplodedNodeSet Tmp;
  568. StmtNodeBuilder Bldr2(preVisit, Tmp, *currBldrCtx);
  569. const Expr *Ex = cast<Expr>(S);
  570. QualType resultType = Ex->getType();
  571. for (ExplodedNodeSet::iterator it = preVisit.begin(), et = preVisit.end();
  572. it != et; ++it) {
  573. ExplodedNode *N = *it;
  574. const LocationContext *LCtx = N->getLocationContext();
  575. SVal result = svalBuilder.conjureSymbolVal(0, Ex, LCtx, resultType,
  576. currBldrCtx->blockCount());
  577. ProgramStateRef state = N->getState()->BindExpr(Ex, LCtx, result);
  578. Bldr2.generateNode(S, N, state);
  579. }
  580. getCheckerManager().runCheckersForPostStmt(Dst, Tmp, S, *this);
  581. Bldr.addNodes(Dst);
  582. break;
  583. }
  584. case Stmt::ArraySubscriptExprClass:
  585. Bldr.takeNodes(Pred);
  586. VisitLvalArraySubscriptExpr(cast<ArraySubscriptExpr>(S), Pred, Dst);
  587. Bldr.addNodes(Dst);
  588. break;
  589. case Stmt::GCCAsmStmtClass:
  590. Bldr.takeNodes(Pred);
  591. VisitGCCAsmStmt(cast<GCCAsmStmt>(S), Pred, Dst);
  592. Bldr.addNodes(Dst);
  593. break;
  594. case Stmt::MSAsmStmtClass:
  595. Bldr.takeNodes(Pred);
  596. VisitMSAsmStmt(cast<MSAsmStmt>(S), Pred, Dst);
  597. Bldr.addNodes(Dst);
  598. break;
  599. case Stmt::BlockExprClass:
  600. Bldr.takeNodes(Pred);
  601. VisitBlockExpr(cast<BlockExpr>(S), Pred, Dst);
  602. Bldr.addNodes(Dst);
  603. break;
  604. case Stmt::BinaryOperatorClass: {
  605. const BinaryOperator* B = cast<BinaryOperator>(S);
  606. if (B->isLogicalOp()) {
  607. Bldr.takeNodes(Pred);
  608. VisitLogicalExpr(B, Pred, Dst);
  609. Bldr.addNodes(Dst);
  610. break;
  611. }
  612. else if (B->getOpcode() == BO_Comma) {
  613. ProgramStateRef state = Pred->getState();
  614. Bldr.generateNode(B, Pred,
  615. state->BindExpr(B, Pred->getLocationContext(),
  616. state->getSVal(B->getRHS(),
  617. Pred->getLocationContext())));
  618. break;
  619. }
  620. Bldr.takeNodes(Pred);
  621. if (AMgr.options.eagerlyAssumeBinOpBifurcation &&
  622. (B->isRelationalOp() || B->isEqualityOp())) {
  623. ExplodedNodeSet Tmp;
  624. VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Tmp);
  625. evalEagerlyAssumeBinOpBifurcation(Dst, Tmp, cast<Expr>(S));
  626. }
  627. else
  628. VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
  629. Bldr.addNodes(Dst);
  630. break;
  631. }
  632. case Stmt::CXXOperatorCallExprClass: {
  633. const CXXOperatorCallExpr *OCE = cast<CXXOperatorCallExpr>(S);
  634. // For instance method operators, make sure the 'this' argument has a
  635. // valid region.
  636. const Decl *Callee = OCE->getCalleeDecl();
  637. if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(Callee)) {
  638. if (MD->isInstance()) {
  639. ProgramStateRef State = Pred->getState();
  640. const LocationContext *LCtx = Pred->getLocationContext();
  641. ProgramStateRef NewState =
  642. createTemporaryRegionIfNeeded(State, LCtx, OCE->getArg(0));
  643. if (NewState != State)
  644. Pred = Bldr.generateNode(OCE, Pred, NewState, /*Tag=*/0,
  645. ProgramPoint::PreStmtKind);
  646. }
  647. }
  648. // FALLTHROUGH
  649. }
  650. case Stmt::CallExprClass:
  651. case Stmt::CXXMemberCallExprClass:
  652. case Stmt::UserDefinedLiteralClass: {
  653. Bldr.takeNodes(Pred);
  654. VisitCallExpr(cast<CallExpr>(S), Pred, Dst);
  655. Bldr.addNodes(Dst);
  656. break;
  657. }
  658. case Stmt::CXXCatchStmtClass: {
  659. Bldr.takeNodes(Pred);
  660. VisitCXXCatchStmt(cast<CXXCatchStmt>(S), Pred, Dst);
  661. Bldr.addNodes(Dst);
  662. break;
  663. }
  664. case Stmt::CXXTemporaryObjectExprClass:
  665. case Stmt::CXXConstructExprClass: {
  666. Bldr.takeNodes(Pred);
  667. VisitCXXConstructExpr(cast<CXXConstructExpr>(S), Pred, Dst);
  668. Bldr.addNodes(Dst);
  669. break;
  670. }
  671. case Stmt::CXXNewExprClass: {
  672. Bldr.takeNodes(Pred);
  673. const CXXNewExpr *NE = cast<CXXNewExpr>(S);
  674. VisitCXXNewExpr(NE, Pred, Dst);
  675. Bldr.addNodes(Dst);
  676. break;
  677. }
  678. case Stmt::CXXDeleteExprClass: {
  679. Bldr.takeNodes(Pred);
  680. const CXXDeleteExpr *CDE = cast<CXXDeleteExpr>(S);
  681. VisitCXXDeleteExpr(CDE, Pred, Dst);
  682. Bldr.addNodes(Dst);
  683. break;
  684. }
  685. // FIXME: ChooseExpr is really a constant. We need to fix
  686. // the CFG do not model them as explicit control-flow.
  687. case Stmt::ChooseExprClass: { // __builtin_choose_expr
  688. Bldr.takeNodes(Pred);
  689. const ChooseExpr *C = cast<ChooseExpr>(S);
  690. VisitGuardedExpr(C, C->getLHS(), C->getRHS(), Pred, Dst);
  691. Bldr.addNodes(Dst);
  692. break;
  693. }
  694. case Stmt::CompoundAssignOperatorClass:
  695. Bldr.takeNodes(Pred);
  696. VisitBinaryOperator(cast<BinaryOperator>(S), Pred, Dst);
  697. Bldr.addNodes(Dst);
  698. break;
  699. case Stmt::CompoundLiteralExprClass:
  700. Bldr.takeNodes(Pred);
  701. VisitCompoundLiteralExpr(cast<CompoundLiteralExpr>(S), Pred, Dst);
  702. Bldr.addNodes(Dst);
  703. break;
  704. case Stmt::BinaryConditionalOperatorClass:
  705. case Stmt::ConditionalOperatorClass: { // '?' operator
  706. Bldr.takeNodes(Pred);
  707. const AbstractConditionalOperator *C
  708. = cast<AbstractConditionalOperator>(S);
  709. VisitGuardedExpr(C, C->getTrueExpr(), C->getFalseExpr(), Pred, Dst);
  710. Bldr.addNodes(Dst);
  711. break;
  712. }
  713. case Stmt::CXXThisExprClass:
  714. Bldr.takeNodes(Pred);
  715. VisitCXXThisExpr(cast<CXXThisExpr>(S), Pred, Dst);
  716. Bldr.addNodes(Dst);
  717. break;
  718. case Stmt::DeclRefExprClass: {
  719. Bldr.takeNodes(Pred);
  720. const DeclRefExpr *DE = cast<DeclRefExpr>(S);
  721. VisitCommonDeclRefExpr(DE, DE->getDecl(), Pred, Dst);
  722. Bldr.addNodes(Dst);
  723. break;
  724. }
  725. case Stmt::DeclStmtClass:
  726. Bldr.takeNodes(Pred);
  727. VisitDeclStmt(cast<DeclStmt>(S), Pred, Dst);
  728. Bldr.addNodes(Dst);
  729. break;
  730. case Stmt::ImplicitCastExprClass:
  731. case Stmt::CStyleCastExprClass:
  732. case Stmt::CXXStaticCastExprClass:
  733. case Stmt::CXXDynamicCastExprClass:
  734. case Stmt::CXXReinterpretCastExprClass:
  735. case Stmt::CXXConstCastExprClass:
  736. case Stmt::CXXFunctionalCastExprClass:
  737. case Stmt::ObjCBridgedCastExprClass: {
  738. Bldr.takeNodes(Pred);
  739. const CastExpr *C = cast<CastExpr>(S);
  740. // Handle the previsit checks.
  741. ExplodedNodeSet dstPrevisit;
  742. getCheckerManager().runCheckersForPreStmt(dstPrevisit, Pred, C, *this);
  743. // Handle the expression itself.
  744. ExplodedNodeSet dstExpr;
  745. for (ExplodedNodeSet::iterator i = dstPrevisit.begin(),
  746. e = dstPrevisit.end(); i != e ; ++i) {
  747. VisitCast(C, C->getSubExpr(), *i, dstExpr);
  748. }
  749. // Handle the postvisit checks.
  750. getCheckerManager().runCheckersForPostStmt(Dst, dstExpr, C, *this);
  751. Bldr.addNodes(Dst);
  752. break;
  753. }
  754. case Expr::MaterializeTemporaryExprClass: {
  755. Bldr.takeNodes(Pred);
  756. const MaterializeTemporaryExpr *MTE = cast<MaterializeTemporaryExpr>(S);
  757. CreateCXXTemporaryObject(MTE, Pred, Dst);
  758. Bldr.addNodes(Dst);
  759. break;
  760. }
  761. case Stmt::InitListExprClass:
  762. Bldr.takeNodes(Pred);
  763. VisitInitListExpr(cast<InitListExpr>(S), Pred, Dst);
  764. Bldr.addNodes(Dst);
  765. break;
  766. case Stmt::MemberExprClass:
  767. Bldr.takeNodes(Pred);
  768. VisitMemberExpr(cast<MemberExpr>(S), Pred, Dst);
  769. Bldr.addNodes(Dst);
  770. break;
  771. case Stmt::ObjCIvarRefExprClass:
  772. Bldr.takeNodes(Pred);
  773. VisitLvalObjCIvarRefExpr(cast<ObjCIvarRefExpr>(S), Pred, Dst);
  774. Bldr.addNodes(Dst);
  775. break;
  776. case Stmt::ObjCForCollectionStmtClass:
  777. Bldr.takeNodes(Pred);
  778. VisitObjCForCollectionStmt(cast<ObjCForCollectionStmt>(S), Pred, Dst);
  779. Bldr.addNodes(Dst);
  780. break;
  781. case Stmt::ObjCMessageExprClass:
  782. Bldr.takeNodes(Pred);
  783. VisitObjCMessage(cast<ObjCMessageExpr>(S), Pred, Dst);
  784. Bldr.addNodes(Dst);
  785. break;
  786. case Stmt::ObjCAtThrowStmtClass:
  787. case Stmt::CXXThrowExprClass:
  788. // FIXME: This is not complete. We basically treat @throw as
  789. // an abort.
  790. Bldr.generateSink(S, Pred, Pred->getState());
  791. break;
  792. case Stmt::ReturnStmtClass:
  793. Bldr.takeNodes(Pred);
  794. VisitReturnStmt(cast<ReturnStmt>(S), Pred, Dst);
  795. Bldr.addNodes(Dst);
  796. break;
  797. case Stmt::OffsetOfExprClass:
  798. Bldr.takeNodes(Pred);
  799. VisitOffsetOfExpr(cast<OffsetOfExpr>(S), Pred, Dst);
  800. Bldr.addNodes(Dst);
  801. break;
  802. case Stmt::UnaryExprOrTypeTraitExprClass:
  803. Bldr.takeNodes(Pred);
  804. VisitUnaryExprOrTypeTraitExpr(cast<UnaryExprOrTypeTraitExpr>(S),
  805. Pred, Dst);
  806. Bldr.addNodes(Dst);
  807. break;
  808. case Stmt::StmtExprClass: {
  809. const StmtExpr *SE = cast<StmtExpr>(S);
  810. if (SE->getSubStmt()->body_empty()) {
  811. // Empty statement expression.
  812. assert(SE->getType() == getContext().VoidTy
  813. && "Empty statement expression must have void type.");
  814. break;
  815. }
  816. if (Expr *LastExpr = dyn_cast<Expr>(*SE->getSubStmt()->body_rbegin())) {
  817. ProgramStateRef state = Pred->getState();
  818. Bldr.generateNode(SE, Pred,
  819. state->BindExpr(SE, Pred->getLocationContext(),
  820. state->getSVal(LastExpr,
  821. Pred->getLocationContext())));
  822. }
  823. break;
  824. }
  825. case Stmt::UnaryOperatorClass: {
  826. Bldr.takeNodes(Pred);
  827. const UnaryOperator *U = cast<UnaryOperator>(S);
  828. if (AMgr.options.eagerlyAssumeBinOpBifurcation && (U->getOpcode() == UO_LNot)) {
  829. ExplodedNodeSet Tmp;
  830. VisitUnaryOperator(U, Pred, Tmp);
  831. evalEagerlyAssumeBinOpBifurcation(Dst, Tmp, U);
  832. }
  833. else
  834. VisitUnaryOperator(U, Pred, Dst);
  835. Bldr.addNodes(Dst);
  836. break;
  837. }
  838. case Stmt::PseudoObjectExprClass: {
  839. Bldr.takeNodes(Pred);
  840. ProgramStateRef state = Pred->getState();
  841. const PseudoObjectExpr *PE = cast<PseudoObjectExpr>(S);
  842. if (const Expr *Result = PE->getResultExpr()) {
  843. SVal V = state->getSVal(Result, Pred->getLocationContext());
  844. Bldr.generateNode(S, Pred,
  845. state->BindExpr(S, Pred->getLocationContext(), V));
  846. }
  847. else
  848. Bldr.generateNode(S, Pred,
  849. state->BindExpr(S, Pred->getLocationContext(),
  850. UnknownVal()));
  851. Bldr.addNodes(Dst);
  852. break;
  853. }
  854. }
  855. }
  856. bool ExprEngine::replayWithoutInlining(ExplodedNode *N,
  857. const LocationContext *CalleeLC) {
  858. const StackFrameContext *CalleeSF = CalleeLC->getCurrentStackFrame();
  859. const StackFrameContext *CallerSF = CalleeSF->getParent()->getCurrentStackFrame();
  860. assert(CalleeSF && CallerSF);
  861. ExplodedNode *BeforeProcessingCall = 0;
  862. const Stmt *CE = CalleeSF->getCallSite();
  863. // Find the first node before we started processing the call expression.
  864. while (N) {
  865. ProgramPoint L = N->getLocation();
  866. BeforeProcessingCall = N;
  867. N = N->pred_empty() ? NULL : *(N->pred_begin());
  868. // Skip the nodes corresponding to the inlined code.
  869. if (L.getLocationContext()->getCurrentStackFrame() != CallerSF)
  870. continue;
  871. // We reached the caller. Find the node right before we started
  872. // processing the call.
  873. if (L.isPurgeKind())
  874. continue;
  875. if (isa<PreImplicitCall>(&L))
  876. continue;
  877. if (isa<CallEnter>(&L))
  878. continue;
  879. if (const StmtPoint *SP = dyn_cast<StmtPoint>(&L))
  880. if (SP->getStmt() == CE)
  881. continue;
  882. break;
  883. }
  884. if (!BeforeProcessingCall)
  885. return false;
  886. // TODO: Clean up the unneeded nodes.
  887. // Build an Epsilon node from which we will restart the analyzes.
  888. // Note that CE is permitted to be NULL!
  889. ProgramPoint NewNodeLoc =
  890. EpsilonPoint(BeforeProcessingCall->getLocationContext(), CE);
  891. // Add the special flag to GDM to signal retrying with no inlining.
  892. // Note, changing the state ensures that we are not going to cache out.
  893. ProgramStateRef NewNodeState = BeforeProcessingCall->getState();
  894. NewNodeState = NewNodeState->set<ReplayWithoutInlining>((void*)CE);
  895. // Make the new node a successor of BeforeProcessingCall.
  896. bool IsNew = false;
  897. ExplodedNode *NewNode = G.getNode(NewNodeLoc, NewNodeState, false, &IsNew);
  898. // We cached out at this point. Caching out is common due to us backtracking
  899. // from the inlined function, which might spawn several paths.
  900. if (!IsNew)
  901. return true;
  902. NewNode->addPredecessor(BeforeProcessingCall, G);
  903. // Add the new node to the work list.
  904. Engine.enqueueStmtNode(NewNode, CalleeSF->getCallSiteBlock(),
  905. CalleeSF->getIndex());
  906. NumTimesRetriedWithoutInlining++;
  907. return true;
  908. }
  909. /// Block entrance. (Update counters).
  910. void ExprEngine::processCFGBlockEntrance(const BlockEdge &L,
  911. NodeBuilderWithSinks &nodeBuilder,
  912. ExplodedNode *Pred) {
  913. // FIXME: Refactor this into a checker.
  914. if (nodeBuilder.getContext().blockCount() >= AMgr.options.maxBlockVisitOnPath) {
  915. static SimpleProgramPointTag tag("ExprEngine : Block count exceeded");
  916. const ExplodedNode *Sink =
  917. nodeBuilder.generateSink(Pred->getState(), Pred, &tag);
  918. // Check if we stopped at the top level function or not.
  919. // Root node should have the location context of the top most function.
  920. const LocationContext *CalleeLC = Pred->getLocation().getLocationContext();
  921. const LocationContext *CalleeSF = CalleeLC->getCurrentStackFrame();
  922. const LocationContext *RootLC =
  923. (*G.roots_begin())->getLocation().getLocationContext();
  924. if (RootLC->getCurrentStackFrame() != CalleeSF) {
  925. Engine.FunctionSummaries->markReachedMaxBlockCount(CalleeSF->getDecl());
  926. // Re-run the call evaluation without inlining it, by storing the
  927. // no-inlining policy in the state and enqueuing the new work item on
  928. // the list. Replay should almost never fail. Use the stats to catch it
  929. // if it does.
  930. if ((!AMgr.options.NoRetryExhausted &&
  931. replayWithoutInlining(Pred, CalleeLC)))
  932. return;
  933. NumMaxBlockCountReachedInInlined++;
  934. } else
  935. NumMaxBlockCountReached++;
  936. // Make sink nodes as exhausted(for stats) only if retry failed.
  937. Engine.blocksExhausted.push_back(std::make_pair(L, Sink));
  938. }
  939. }
  940. //===----------------------------------------------------------------------===//
  941. // Branch processing.
  942. //===----------------------------------------------------------------------===//
  943. /// RecoverCastedSymbol - A helper function for ProcessBranch that is used
  944. /// to try to recover some path-sensitivity for casts of symbolic
  945. /// integers that promote their values (which are currently not tracked well).
  946. /// This function returns the SVal bound to Condition->IgnoreCasts if all the
  947. // cast(s) did was sign-extend the original value.
  948. static SVal RecoverCastedSymbol(ProgramStateManager& StateMgr,
  949. ProgramStateRef state,
  950. const Stmt *Condition,
  951. const LocationContext *LCtx,
  952. ASTContext &Ctx) {
  953. const Expr *Ex = dyn_cast<Expr>(Condition);
  954. if (!Ex)
  955. return UnknownVal();
  956. uint64_t bits = 0;
  957. bool bitsInit = false;
  958. while (const CastExpr *CE = dyn_cast<CastExpr>(Ex)) {
  959. QualType T = CE->getType();
  960. if (!T->isIntegerType())
  961. return UnknownVal();
  962. uint64_t newBits = Ctx.getTypeSize(T);
  963. if (!bitsInit || newBits < bits) {
  964. bitsInit = true;
  965. bits = newBits;
  966. }
  967. Ex = CE->getSubExpr();
  968. }
  969. // We reached a non-cast. Is it a symbolic value?
  970. QualType T = Ex->getType();
  971. if (!bitsInit || !T->isIntegerType() || Ctx.getTypeSize(T) > bits)
  972. return UnknownVal();
  973. return state->getSVal(Ex, LCtx);
  974. }
  975. static const Stmt *ResolveCondition(const Stmt *Condition,
  976. const CFGBlock *B) {
  977. if (const Expr *Ex = dyn_cast<Expr>(Condition))
  978. Condition = Ex->IgnoreParens();
  979. const BinaryOperator *BO = dyn_cast<BinaryOperator>(Condition);
  980. if (!BO || !BO->isLogicalOp())
  981. return Condition;
  982. // For logical operations, we still have the case where some branches
  983. // use the traditional "merge" approach and others sink the branch
  984. // directly into the basic blocks representing the logical operation.
  985. // We need to distinguish between those two cases here.
  986. // The invariants are still shifting, but it is possible that the
  987. // last element in a CFGBlock is not a CFGStmt. Look for the last
  988. // CFGStmt as the value of the condition.
  989. CFGBlock::const_reverse_iterator I = B->rbegin(), E = B->rend();
  990. for (; I != E; ++I) {
  991. CFGElement Elem = *I;
  992. CFGStmt *CS = dyn_cast<CFGStmt>(&Elem);
  993. if (!CS)
  994. continue;
  995. if (CS->getStmt() != Condition)
  996. break;
  997. return Condition;
  998. }
  999. assert(I != E);
  1000. while (Condition) {
  1001. BO = dyn_cast<BinaryOperator>(Condition);
  1002. if (!BO || !BO->isLogicalOp())
  1003. return Condition;
  1004. Condition = BO->getRHS()->IgnoreParens();
  1005. }
  1006. llvm_unreachable("could not resolve condition");
  1007. }
  1008. void ExprEngine::processBranch(const Stmt *Condition, const Stmt *Term,
  1009. NodeBuilderContext& BldCtx,
  1010. ExplodedNode *Pred,
  1011. ExplodedNodeSet &Dst,
  1012. const CFGBlock *DstT,
  1013. const CFGBlock *DstF) {
  1014. currBldrCtx = &BldCtx;
  1015. // Check for NULL conditions; e.g. "for(;;)"
  1016. if (!Condition) {
  1017. BranchNodeBuilder NullCondBldr(Pred, Dst, BldCtx, DstT, DstF);
  1018. NullCondBldr.markInfeasible(false);
  1019. NullCondBldr.generateNode(Pred->getState(), true, Pred);
  1020. return;
  1021. }
  1022. // Resolve the condition in the precense of nested '||' and '&&'.
  1023. if (const Expr *Ex = dyn_cast<Expr>(Condition))
  1024. Condition = Ex->IgnoreParens();
  1025. Condition = ResolveCondition(Condition, BldCtx.getBlock());
  1026. PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
  1027. Condition->getLocStart(),
  1028. "Error evaluating branch");
  1029. ExplodedNodeSet CheckersOutSet;
  1030. getCheckerManager().runCheckersForBranchCondition(Condition, CheckersOutSet,
  1031. Pred, *this);
  1032. // We generated only sinks.
  1033. if (CheckersOutSet.empty())
  1034. return;
  1035. BranchNodeBuilder builder(CheckersOutSet, Dst, BldCtx, DstT, DstF);
  1036. for (NodeBuilder::iterator I = CheckersOutSet.begin(),
  1037. E = CheckersOutSet.end(); E != I; ++I) {
  1038. ExplodedNode *PredI = *I;
  1039. if (PredI->isSink())
  1040. continue;
  1041. ProgramStateRef PrevState = Pred->getState();
  1042. SVal X = PrevState->getSVal(Condition, Pred->getLocationContext());
  1043. if (X.isUnknownOrUndef()) {
  1044. // Give it a chance to recover from unknown.
  1045. if (const Expr *Ex = dyn_cast<Expr>(Condition)) {
  1046. if (Ex->getType()->isIntegerType()) {
  1047. // Try to recover some path-sensitivity. Right now casts of symbolic
  1048. // integers that promote their values are currently not tracked well.
  1049. // If 'Condition' is such an expression, try and recover the
  1050. // underlying value and use that instead.
  1051. SVal recovered = RecoverCastedSymbol(getStateManager(),
  1052. PrevState, Condition,
  1053. Pred->getLocationContext(),
  1054. getContext());
  1055. if (!recovered.isUnknown()) {
  1056. X = recovered;
  1057. }
  1058. }
  1059. }
  1060. }
  1061. // If the condition is still unknown, give up.
  1062. if (X.isUnknownOrUndef()) {
  1063. builder.generateNode(PrevState, true, PredI);
  1064. builder.generateNode(PrevState, false, PredI);
  1065. continue;
  1066. }
  1067. DefinedSVal V = cast<DefinedSVal>(X);
  1068. // Process the true branch.
  1069. if (builder.isFeasible(true)) {
  1070. if (ProgramStateRef state = PrevState->assume(V, true))
  1071. builder.generateNode(state, true, PredI);
  1072. else
  1073. builder.markInfeasible(true);
  1074. }
  1075. // Process the false branch.
  1076. if (builder.isFeasible(false)) {
  1077. if (ProgramStateRef state = PrevState->assume(V, false))
  1078. builder.generateNode(state, false, PredI);
  1079. else
  1080. builder.markInfeasible(false);
  1081. }
  1082. }
  1083. currBldrCtx = 0;
  1084. }
  1085. /// processIndirectGoto - Called by CoreEngine. Used to generate successor
  1086. /// nodes by processing the 'effects' of a computed goto jump.
  1087. void ExprEngine::processIndirectGoto(IndirectGotoNodeBuilder &builder) {
  1088. ProgramStateRef state = builder.getState();
  1089. SVal V = state->getSVal(builder.getTarget(), builder.getLocationContext());
  1090. // Three possibilities:
  1091. //
  1092. // (1) We know the computed label.
  1093. // (2) The label is NULL (or some other constant), or Undefined.
  1094. // (3) We have no clue about the label. Dispatch to all targets.
  1095. //
  1096. typedef IndirectGotoNodeBuilder::iterator iterator;
  1097. if (isa<loc::GotoLabel>(V)) {
  1098. const LabelDecl *L = cast<loc::GotoLabel>(V).getLabel();
  1099. for (iterator I = builder.begin(), E = builder.end(); I != E; ++I) {
  1100. if (I.getLabel() == L) {
  1101. builder.generateNode(I, state);
  1102. return;
  1103. }
  1104. }
  1105. llvm_unreachable("No block with label.");
  1106. }
  1107. if (isa<loc::ConcreteInt>(V) || isa<UndefinedVal>(V)) {
  1108. // Dispatch to the first target and mark it as a sink.
  1109. //ExplodedNode* N = builder.generateNode(builder.begin(), state, true);
  1110. // FIXME: add checker visit.
  1111. // UndefBranches.insert(N);
  1112. return;
  1113. }
  1114. // This is really a catch-all. We don't support symbolics yet.
  1115. // FIXME: Implement dispatch for symbolic pointers.
  1116. for (iterator I=builder.begin(), E=builder.end(); I != E; ++I)
  1117. builder.generateNode(I, state);
  1118. }
  1119. /// ProcessEndPath - Called by CoreEngine. Used to generate end-of-path
  1120. /// nodes when the control reaches the end of a function.
  1121. void ExprEngine::processEndOfFunction(NodeBuilderContext& BC,
  1122. ExplodedNode *Pred) {
  1123. StateMgr.EndPath(Pred->getState());
  1124. ExplodedNodeSet Dst;
  1125. getCheckerManager().runCheckersForEndPath(BC, Dst, Pred, *this);
  1126. Engine.enqueueEndOfFunction(Dst);
  1127. }
  1128. /// ProcessSwitch - Called by CoreEngine. Used to generate successor
  1129. /// nodes by processing the 'effects' of a switch statement.
  1130. void ExprEngine::processSwitch(SwitchNodeBuilder& builder) {
  1131. typedef SwitchNodeBuilder::iterator iterator;
  1132. ProgramStateRef state = builder.getState();
  1133. const Expr *CondE = builder.getCondition();
  1134. SVal CondV_untested = state->getSVal(CondE, builder.getLocationContext());
  1135. if (CondV_untested.isUndef()) {
  1136. //ExplodedNode* N = builder.generateDefaultCaseNode(state, true);
  1137. // FIXME: add checker
  1138. //UndefBranches.insert(N);
  1139. return;
  1140. }
  1141. DefinedOrUnknownSVal CondV = cast<DefinedOrUnknownSVal>(CondV_untested);
  1142. ProgramStateRef DefaultSt = state;
  1143. iterator I = builder.begin(), EI = builder.end();
  1144. bool defaultIsFeasible = I == EI;
  1145. for ( ; I != EI; ++I) {
  1146. // Successor may be pruned out during CFG construction.
  1147. if (!I.getBlock())
  1148. continue;
  1149. const CaseStmt *Case = I.getCase();
  1150. // Evaluate the LHS of the case value.
  1151. llvm::APSInt V1 = Case->getLHS()->EvaluateKnownConstInt(getContext());
  1152. assert(V1.getBitWidth() == getContext().getTypeSize(CondE->getType()));
  1153. // Get the RHS of the case, if it exists.
  1154. llvm::APSInt V2;
  1155. if (const Expr *E = Case->getRHS())
  1156. V2 = E->EvaluateKnownConstInt(getContext());
  1157. else
  1158. V2 = V1;
  1159. // FIXME: Eventually we should replace the logic below with a range
  1160. // comparison, rather than concretize the values within the range.
  1161. // This should be easy once we have "ranges" for NonLVals.
  1162. do {
  1163. nonloc::ConcreteInt CaseVal(getBasicVals().getValue(V1));
  1164. DefinedOrUnknownSVal Res = svalBuilder.evalEQ(DefaultSt ? DefaultSt : state,
  1165. CondV, CaseVal);
  1166. // Now "assume" that the case matches.
  1167. if (ProgramStateRef stateNew = state->assume(Res, true)) {
  1168. builder.generateCaseStmtNode(I, stateNew);
  1169. // If CondV evaluates to a constant, then we know that this
  1170. // is the *only* case that we can take, so stop evaluating the
  1171. // others.
  1172. if (isa<nonloc::ConcreteInt>(CondV))
  1173. return;
  1174. }
  1175. // Now "assume" that the case doesn't match. Add this state
  1176. // to the default state (if it is feasible).
  1177. if (DefaultSt) {
  1178. if (ProgramStateRef stateNew = DefaultSt->assume(Res, false)) {
  1179. defaultIsFeasible = true;
  1180. DefaultSt = stateNew;
  1181. }
  1182. else {
  1183. defaultIsFeasible = false;
  1184. DefaultSt = NULL;
  1185. }
  1186. }
  1187. // Concretize the next value in the range.
  1188. if (V1 == V2)
  1189. break;
  1190. ++V1;
  1191. assert (V1 <= V2);
  1192. } while (true);
  1193. }
  1194. if (!defaultIsFeasible)
  1195. return;
  1196. // If we have switch(enum value), the default branch is not
  1197. // feasible if all of the enum constants not covered by 'case:' statements
  1198. // are not feasible values for the switch condition.
  1199. //
  1200. // Note that this isn't as accurate as it could be. Even if there isn't
  1201. // a case for a particular enum value as long as that enum value isn't
  1202. // feasible then it shouldn't be considered for making 'default:' reachable.
  1203. const SwitchStmt *SS = builder.getSwitch();
  1204. const Expr *CondExpr = SS->getCond()->IgnoreParenImpCasts();
  1205. if (CondExpr->getType()->getAs<EnumType>()) {
  1206. if (SS->isAllEnumCasesCovered())
  1207. return;
  1208. }
  1209. builder.generateDefaultCaseNode(DefaultSt);
  1210. }
  1211. //===----------------------------------------------------------------------===//
  1212. // Transfer functions: Loads and stores.
  1213. //===----------------------------------------------------------------------===//
  1214. void ExprEngine::VisitCommonDeclRefExpr(const Expr *Ex, const NamedDecl *D,
  1215. ExplodedNode *Pred,
  1216. ExplodedNodeSet &Dst) {
  1217. StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
  1218. ProgramStateRef state = Pred->getState();
  1219. const LocationContext *LCtx = Pred->getLocationContext();
  1220. if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
  1221. assert(Ex->isGLValue());
  1222. SVal V = state->getLValue(VD, Pred->getLocationContext());
  1223. // For references, the 'lvalue' is the pointer address stored in the
  1224. // reference region.
  1225. if (VD->getType()->isReferenceType()) {
  1226. if (const MemRegion *R = V.getAsRegion())
  1227. V = state->getSVal(R);
  1228. else
  1229. V = UnknownVal();
  1230. }
  1231. Bldr.generateNode(Ex, Pred, state->BindExpr(Ex, LCtx, V), 0,
  1232. ProgramPoint::PostLValueKind);
  1233. return;
  1234. }
  1235. if (const EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(D)) {
  1236. assert(!Ex->isGLValue());
  1237. SVal V = svalBuilder.makeIntVal(ED->getInitVal());
  1238. Bldr.generateNode(Ex, Pred, state->BindExpr(Ex, LCtx, V));
  1239. return;
  1240. }
  1241. if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
  1242. SVal V = svalBuilder.getFunctionPointer(FD);
  1243. Bldr.generateNode(Ex, Pred, state->BindExpr(Ex, LCtx, V), 0,
  1244. ProgramPoint::PostLValueKind);
  1245. return;
  1246. }
  1247. if (isa<FieldDecl>(D)) {
  1248. // FIXME: Compute lvalue of field pointers-to-member.
  1249. // Right now we just use a non-null void pointer, so that it gives proper
  1250. // results in boolean contexts.
  1251. SVal V = svalBuilder.conjureSymbolVal(Ex, LCtx, getContext().VoidPtrTy,
  1252. currBldrCtx->blockCount());
  1253. state = state->assume(cast<DefinedOrUnknownSVal>(V), true);
  1254. Bldr.generateNode(Ex, Pred, state->BindExpr(Ex, LCtx, V), 0,
  1255. ProgramPoint::PostLValueKind);
  1256. return;
  1257. }
  1258. llvm_unreachable("Support for this Decl not implemented.");
  1259. }
  1260. /// VisitArraySubscriptExpr - Transfer function for array accesses
  1261. void ExprEngine::VisitLvalArraySubscriptExpr(const ArraySubscriptExpr *A,
  1262. ExplodedNode *Pred,
  1263. ExplodedNodeSet &Dst){
  1264. const Expr *Base = A->getBase()->IgnoreParens();
  1265. const Expr *Idx = A->getIdx()->IgnoreParens();
  1266. ExplodedNodeSet checkerPreStmt;
  1267. getCheckerManager().runCheckersForPreStmt(checkerPreStmt, Pred, A, *this);
  1268. StmtNodeBuilder Bldr(checkerPreStmt, Dst, *currBldrCtx);
  1269. for (ExplodedNodeSet::iterator it = checkerPreStmt.begin(),
  1270. ei = checkerPreStmt.end(); it != ei; ++it) {
  1271. const LocationContext *LCtx = (*it)->getLocationContext();
  1272. ProgramStateRef state = (*it)->getState();
  1273. SVal V = state->getLValue(A->getType(),
  1274. state->getSVal(Idx, LCtx),
  1275. state->getSVal(Base, LCtx));
  1276. assert(A->isGLValue());
  1277. Bldr.generateNode(A, *it, state->BindExpr(A, LCtx, V), 0,
  1278. ProgramPoint::PostLValueKind);
  1279. }
  1280. }
  1281. /// VisitMemberExpr - Transfer function for member expressions.
  1282. void ExprEngine::VisitMemberExpr(const MemberExpr *M, ExplodedNode *Pred,
  1283. ExplodedNodeSet &TopDst) {
  1284. StmtNodeBuilder Bldr(Pred, TopDst, *currBldrCtx);
  1285. ExplodedNodeSet Dst;
  1286. ValueDecl *Member = M->getMemberDecl();
  1287. // Handle static member variables and enum constants accessed via
  1288. // member syntax.
  1289. if (isa<VarDecl>(Member) || isa<EnumConstantDecl>(Member)) {
  1290. Bldr.takeNodes(Pred);
  1291. VisitCommonDeclRefExpr(M, Member, Pred, Dst);
  1292. Bldr.addNodes(Dst);
  1293. return;
  1294. }
  1295. ProgramStateRef state = Pred->getState();
  1296. const LocationContext *LCtx = Pred->getLocationContext();
  1297. Expr *BaseExpr = M->getBase();
  1298. // Handle C++ method calls.
  1299. if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Member)) {
  1300. if (MD->isInstance())
  1301. state = createTemporaryRegionIfNeeded(state, LCtx, BaseExpr);
  1302. SVal MDVal = svalBuilder.getFunctionPointer(MD);
  1303. state = state->BindExpr(M, LCtx, MDVal);
  1304. Bldr.generateNode(M, Pred, state);
  1305. return;
  1306. }
  1307. // Handle regular struct fields / member variables.
  1308. state = createTemporaryRegionIfNeeded(state, LCtx, BaseExpr);
  1309. SVal baseExprVal = state->getSVal(BaseExpr, LCtx);
  1310. FieldDecl *field = cast<FieldDecl>(Member);
  1311. SVal L = state->getLValue(field, baseExprVal);
  1312. if (M->isGLValue()) {
  1313. if (field->getType()->isReferenceType()) {
  1314. if (const MemRegion *R = L.getAsRegion())
  1315. L = state->getSVal(R);
  1316. else
  1317. L = UnknownVal();
  1318. }
  1319. Bldr.generateNode(M, Pred, state->BindExpr(M, LCtx, L), 0,
  1320. ProgramPoint::PostLValueKind);
  1321. } else {
  1322. Bldr.takeNodes(Pred);
  1323. evalLoad(Dst, M, M, Pred, state, L);
  1324. Bldr.addNodes(Dst);
  1325. }
  1326. }
  1327. /// evalBind - Handle the semantics of binding a value to a specific location.
  1328. /// This method is used by evalStore and (soon) VisitDeclStmt, and others.
  1329. void ExprEngine::evalBind(ExplodedNodeSet &Dst, const Stmt *StoreE,
  1330. ExplodedNode *Pred,
  1331. SVal location, SVal Val,
  1332. bool atDeclInit, const ProgramPoint *PP) {
  1333. const LocationContext *LC = Pred->getLocationContext();
  1334. PostStmt PS(StoreE, LC);
  1335. if (!PP)
  1336. PP = &PS;
  1337. // Do a previsit of the bind.
  1338. ExplodedNodeSet CheckedSet;
  1339. getCheckerManager().runCheckersForBind(CheckedSet, Pred, location, Val,
  1340. StoreE, *this, *PP);
  1341. // If the location is not a 'Loc', it will already be handled by
  1342. // the checkers. There is nothing left to do.
  1343. if (!isa<Loc>(location)) {
  1344. Dst = CheckedSet;
  1345. return;
  1346. }
  1347. ExplodedNodeSet TmpDst;
  1348. StmtNodeBuilder Bldr(CheckedSet, TmpDst, *currBldrCtx);
  1349. for (ExplodedNodeSet::iterator I = CheckedSet.begin(), E = CheckedSet.end();
  1350. I!=E; ++I) {
  1351. ExplodedNode *PredI = *I;
  1352. ProgramStateRef state = PredI->getState();
  1353. // When binding the value, pass on the hint that this is a initialization.
  1354. // For initializations, we do not need to inform clients of region
  1355. // changes.
  1356. state = state->bindLoc(cast<Loc>(location),
  1357. Val, /* notifyChanges = */ !atDeclInit);
  1358. const MemRegion *LocReg = 0;
  1359. if (loc::MemRegionVal *LocRegVal = dyn_cast<loc::MemRegionVal>(&location)) {
  1360. LocReg = LocRegVal->getRegion();
  1361. }
  1362. const ProgramPoint L = PostStore(StoreE, LC, LocReg, 0);
  1363. Bldr.generateNode(L, state, PredI);
  1364. }
  1365. Dst.insert(TmpDst);
  1366. }
  1367. /// evalStore - Handle the semantics of a store via an assignment.
  1368. /// @param Dst The node set to store generated state nodes
  1369. /// @param AssignE The assignment expression if the store happens in an
  1370. /// assignment.
  1371. /// @param LocationE The location expression that is stored to.
  1372. /// @param state The current simulation state
  1373. /// @param location The location to store the value
  1374. /// @param Val The value to be stored
  1375. void ExprEngine::evalStore(ExplodedNodeSet &Dst, const Expr *AssignE,
  1376. const Expr *LocationE,
  1377. ExplodedNode *Pred,
  1378. ProgramStateRef state, SVal location, SVal Val,
  1379. const ProgramPointTag *tag) {
  1380. // Proceed with the store. We use AssignE as the anchor for the PostStore
  1381. // ProgramPoint if it is non-NULL, and LocationE otherwise.
  1382. const Expr *StoreE = AssignE ? AssignE : LocationE;
  1383. // Evaluate the location (checks for bad dereferences).
  1384. ExplodedNodeSet Tmp;
  1385. evalLocation(Tmp, AssignE, LocationE, Pred, state, location, tag, false);
  1386. if (Tmp.empty())
  1387. return;
  1388. if (location.isUndef())
  1389. return;
  1390. for (ExplodedNodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI)
  1391. evalBind(Dst, StoreE, *NI, location, Val, false);
  1392. }
  1393. void ExprEngine::evalLoad(ExplodedNodeSet &Dst,
  1394. const Expr *NodeEx,
  1395. const Expr *BoundEx,
  1396. ExplodedNode *Pred,
  1397. ProgramStateRef state,
  1398. SVal location,
  1399. const ProgramPointTag *tag,
  1400. QualType LoadTy)
  1401. {
  1402. assert(!isa<NonLoc>(location) && "location cannot be a NonLoc.");
  1403. // Are we loading from a region? This actually results in two loads; one
  1404. // to fetch the address of the referenced value and one to fetch the
  1405. // referenced value.
  1406. if (const TypedValueRegion *TR =
  1407. dyn_cast_or_null<TypedValueRegion>(location.getAsRegion())) {
  1408. QualType ValTy = TR->getValueType();
  1409. if (const ReferenceType *RT = ValTy->getAs<ReferenceType>()) {
  1410. static SimpleProgramPointTag
  1411. loadReferenceTag("ExprEngine : Load Reference");
  1412. ExplodedNodeSet Tmp;
  1413. evalLoadCommon(Tmp, NodeEx, BoundEx, Pred, state,
  1414. location, &loadReferenceTag,
  1415. getContext().getPointerType(RT->getPointeeType()));
  1416. // Perform the load from the referenced value.
  1417. for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end() ; I!=E; ++I) {
  1418. state = (*I)->getState();
  1419. location = state->getSVal(BoundEx, (*I)->getLocationContext());
  1420. evalLoadCommon(Dst, NodeEx, BoundEx, *I, state, location, tag, LoadTy);
  1421. }
  1422. return;
  1423. }
  1424. }
  1425. evalLoadCommon(Dst, NodeEx, BoundEx, Pred, state, location, tag, LoadTy);
  1426. }
  1427. void ExprEngine::evalLoadCommon(ExplodedNodeSet &Dst,
  1428. const Expr *NodeEx,
  1429. const Expr *BoundEx,
  1430. ExplodedNode *Pred,
  1431. ProgramStateRef state,
  1432. SVal location,
  1433. const ProgramPointTag *tag,
  1434. QualType LoadTy) {
  1435. assert(NodeEx);
  1436. assert(BoundEx);
  1437. // Evaluate the location (checks for bad dereferences).
  1438. ExplodedNodeSet Tmp;
  1439. evalLocation(Tmp, NodeEx, BoundEx, Pred, state, location, tag, true);
  1440. if (Tmp.empty())
  1441. return;
  1442. StmtNodeBuilder Bldr(Tmp, Dst, *currBldrCtx);
  1443. if (location.isUndef())
  1444. return;
  1445. // Proceed with the load.
  1446. for (ExplodedNodeSet::iterator NI=Tmp.begin(), NE=Tmp.end(); NI!=NE; ++NI) {
  1447. state = (*NI)->getState();
  1448. const LocationContext *LCtx = (*NI)->getLocationContext();
  1449. if (location.isUnknown()) {
  1450. // This is important. We must nuke the old binding.
  1451. Bldr.generateNode(NodeEx, *NI,
  1452. state->BindExpr(BoundEx, LCtx, UnknownVal()),
  1453. tag, ProgramPoint::PostLoadKind);
  1454. }
  1455. else {
  1456. if (LoadTy.isNull())
  1457. LoadTy = BoundEx->getType();
  1458. SVal V = state->getSVal(cast<Loc>(location), LoadTy);
  1459. Bldr.generateNode(NodeEx, *NI,
  1460. state->bindExprAndLocation(BoundEx, LCtx, location, V),
  1461. tag, ProgramPoint::PostLoadKind);
  1462. }
  1463. }
  1464. }
  1465. void ExprEngine::evalLocation(ExplodedNodeSet &Dst,
  1466. const Stmt *NodeEx,
  1467. const Stmt *BoundEx,
  1468. ExplodedNode *Pred,
  1469. ProgramStateRef state,
  1470. SVal location,
  1471. const ProgramPointTag *tag,
  1472. bool isLoad) {
  1473. StmtNodeBuilder BldrTop(Pred, Dst, *currBldrCtx);
  1474. // Early checks for performance reason.
  1475. if (location.isUnknown()) {
  1476. return;
  1477. }
  1478. ExplodedNodeSet Src;
  1479. BldrTop.takeNodes(Pred);
  1480. StmtNodeBuilder Bldr(Pred, Src, *currBldrCtx);
  1481. if (Pred->getState() != state) {
  1482. // Associate this new state with an ExplodedNode.
  1483. // FIXME: If I pass null tag, the graph is incorrect, e.g for
  1484. // int *p;
  1485. // p = 0;
  1486. // *p = 0xDEADBEEF;
  1487. // "p = 0" is not noted as "Null pointer value stored to 'p'" but
  1488. // instead "int *p" is noted as
  1489. // "Variable 'p' initialized to a null pointer value"
  1490. static SimpleProgramPointTag tag("ExprEngine: Location");
  1491. Bldr.generateNode(NodeEx, Pred, state, &tag);
  1492. }
  1493. ExplodedNodeSet Tmp;
  1494. getCheckerManager().runCheckersForLocation(Tmp, Src, location, isLoad,
  1495. NodeEx, BoundEx, *this);
  1496. BldrTop.addNodes(Tmp);
  1497. }
  1498. std::pair<const ProgramPointTag *, const ProgramPointTag*>
  1499. ExprEngine::geteagerlyAssumeBinOpBifurcationTags() {
  1500. static SimpleProgramPointTag
  1501. eagerlyAssumeBinOpBifurcationTrue("ExprEngine : Eagerly Assume True"),
  1502. eagerlyAssumeBinOpBifurcationFalse("ExprEngine : Eagerly Assume False");
  1503. return std::make_pair(&eagerlyAssumeBinOpBifurcationTrue,
  1504. &eagerlyAssumeBinOpBifurcationFalse);
  1505. }
  1506. void ExprEngine::evalEagerlyAssumeBinOpBifurcation(ExplodedNodeSet &Dst,
  1507. ExplodedNodeSet &Src,
  1508. const Expr *Ex) {
  1509. StmtNodeBuilder Bldr(Src, Dst, *currBldrCtx);
  1510. for (ExplodedNodeSet::iterator I=Src.begin(), E=Src.end(); I!=E; ++I) {
  1511. ExplodedNode *Pred = *I;
  1512. // Test if the previous node was as the same expression. This can happen
  1513. // when the expression fails to evaluate to anything meaningful and
  1514. // (as an optimization) we don't generate a node.
  1515. ProgramPoint P = Pred->getLocation();
  1516. if (!isa<PostStmt>(P) || cast<PostStmt>(P).getStmt() != Ex) {
  1517. continue;
  1518. }
  1519. ProgramStateRef state = Pred->getState();
  1520. SVal V = state->getSVal(Ex, Pred->getLocationContext());
  1521. nonloc::SymbolVal *SEV = dyn_cast<nonloc::SymbolVal>(&V);
  1522. if (SEV && SEV->isExpression()) {
  1523. const std::pair<const ProgramPointTag *, const ProgramPointTag*> &tags =
  1524. geteagerlyAssumeBinOpBifurcationTags();
  1525. // First assume that the condition is true.
  1526. if (ProgramStateRef StateTrue = state->assume(*SEV, true)) {
  1527. SVal Val = svalBuilder.makeIntVal(1U, Ex->getType());
  1528. StateTrue = StateTrue->BindExpr(Ex, Pred->getLocationContext(), Val);
  1529. Bldr.generateNode(Ex, Pred, StateTrue, tags.first);
  1530. }
  1531. // Next, assume that the condition is false.
  1532. if (ProgramStateRef StateFalse = state->assume(*SEV, false)) {
  1533. SVal Val = svalBuilder.makeIntVal(0U, Ex->getType());
  1534. StateFalse = StateFalse->BindExpr(Ex, Pred->getLocationContext(), Val);
  1535. Bldr.generateNode(Ex, Pred, StateFalse, tags.second);
  1536. }
  1537. }
  1538. }
  1539. }
  1540. void ExprEngine::VisitGCCAsmStmt(const GCCAsmStmt *A, ExplodedNode *Pred,
  1541. ExplodedNodeSet &Dst) {
  1542. StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
  1543. // We have processed both the inputs and the outputs. All of the outputs
  1544. // should evaluate to Locs. Nuke all of their values.
  1545. // FIXME: Some day in the future it would be nice to allow a "plug-in"
  1546. // which interprets the inline asm and stores proper results in the
  1547. // outputs.
  1548. ProgramStateRef state = Pred->getState();
  1549. for (GCCAsmStmt::const_outputs_iterator OI = A->begin_outputs(),
  1550. OE = A->end_outputs(); OI != OE; ++OI) {
  1551. SVal X = state->getSVal(*OI, Pred->getLocationContext());
  1552. assert (!isa<NonLoc>(X)); // Should be an Lval, or unknown, undef.
  1553. if (isa<Loc>(X))
  1554. state = state->bindLoc(cast<Loc>(X), UnknownVal());
  1555. }
  1556. Bldr.generateNode(A, Pred, state);
  1557. }
  1558. void ExprEngine::VisitMSAsmStmt(const MSAsmStmt *A, ExplodedNode *Pred,
  1559. ExplodedNodeSet &Dst) {
  1560. StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
  1561. Bldr.generateNode(A, Pred, Pred->getState());
  1562. }
  1563. //===----------------------------------------------------------------------===//
  1564. // Visualization.
  1565. //===----------------------------------------------------------------------===//
  1566. #ifndef NDEBUG
  1567. static ExprEngine* GraphPrintCheckerState;
  1568. static SourceManager* GraphPrintSourceManager;
  1569. namespace llvm {
  1570. template<>
  1571. struct DOTGraphTraits<ExplodedNode*> :
  1572. public DefaultDOTGraphTraits {
  1573. DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
  1574. // FIXME: Since we do not cache error nodes in ExprEngine now, this does not
  1575. // work.
  1576. static std::string getNodeAttributes(const ExplodedNode *N, void*) {
  1577. #if 0
  1578. // FIXME: Replace with a general scheme to tell if the node is
  1579. // an error node.
  1580. if (GraphPrintCheckerState->isImplicitNullDeref(N) ||
  1581. GraphPrintCheckerState->isExplicitNullDeref(N) ||
  1582. GraphPrintCheckerState->isUndefDeref(N) ||
  1583. GraphPrintCheckerState->isUndefStore(N) ||
  1584. GraphPrintCheckerState->isUndefControlFlow(N) ||
  1585. GraphPrintCheckerState->isUndefResult(N) ||
  1586. GraphPrintCheckerState->isBadCall(N) ||
  1587. GraphPrintCheckerState->isUndefArg(N))
  1588. return "color=\"red\",style=\"filled\"";
  1589. if (GraphPrintCheckerState->isNoReturnCall(N))
  1590. return "color=\"blue\",style=\"filled\"";
  1591. #endif
  1592. return "";
  1593. }
  1594. static void printLocation(llvm::raw_ostream &Out, SourceLocation SLoc) {
  1595. if (SLoc.isFileID()) {
  1596. Out << "\\lline="
  1597. << GraphPrintSourceManager->getExpansionLineNumber(SLoc)
  1598. << " col="
  1599. << GraphPrintSourceManager->getExpansionColumnNumber(SLoc)
  1600. << "\\l";
  1601. }
  1602. }
  1603. static std::string getNodeLabel(const ExplodedNode *N, void*){
  1604. std::string sbuf;
  1605. llvm::raw_string_ostream Out(sbuf);
  1606. // Program Location.
  1607. ProgramPoint Loc = N->getLocation();
  1608. switch (Loc.getKind()) {
  1609. case ProgramPoint::BlockEntranceKind: {
  1610. Out << "Block Entrance: B"
  1611. << cast<BlockEntrance>(Loc).getBlock()->getBlockID();
  1612. if (const NamedDecl *ND =
  1613. dyn_cast<NamedDecl>(Loc.getLocationContext()->getDecl())) {
  1614. Out << " (";
  1615. ND->printName(Out);
  1616. Out << ")";
  1617. }
  1618. break;
  1619. }
  1620. case ProgramPoint::BlockExitKind:
  1621. assert (false);
  1622. break;
  1623. case ProgramPoint::CallEnterKind:
  1624. Out << "CallEnter";
  1625. break;
  1626. case ProgramPoint::CallExitBeginKind:
  1627. Out << "CallExitBegin";
  1628. break;
  1629. case ProgramPoint::CallExitEndKind:
  1630. Out << "CallExitEnd";
  1631. break;
  1632. case ProgramPoint::PostStmtPurgeDeadSymbolsKind:
  1633. Out << "PostStmtPurgeDeadSymbols";
  1634. break;
  1635. case ProgramPoint::PreStmtPurgeDeadSymbolsKind:
  1636. Out << "PreStmtPurgeDeadSymbols";
  1637. break;
  1638. case ProgramPoint::EpsilonKind:
  1639. Out << "Epsilon Point";
  1640. break;
  1641. case ProgramPoint::PreImplicitCallKind: {
  1642. ImplicitCallPoint *PC = cast<ImplicitCallPoint>(&Loc);
  1643. Out << "PreCall: ";
  1644. // FIXME: Get proper printing options.
  1645. PC->getDecl()->print(Out, LangOptions());
  1646. printLocation(Out, PC->getLocation());
  1647. break;
  1648. }
  1649. case ProgramPoint::PostImplicitCallKind: {
  1650. ImplicitCallPoint *PC = cast<ImplicitCallPoint>(&Loc);
  1651. Out << "PostCall: ";
  1652. // FIXME: Get proper printing options.
  1653. PC->getDecl()->print(Out, LangOptions());
  1654. printLocation(Out, PC->getLocation());
  1655. break;
  1656. }
  1657. default: {
  1658. if (StmtPoint *L = dyn_cast<StmtPoint>(&Loc)) {
  1659. const Stmt *S = L->getStmt();
  1660. Out << S->getStmtClassName() << ' ' << (const void*) S << ' ';
  1661. LangOptions LO; // FIXME.
  1662. S->printPretty(Out, 0, PrintingPolicy(LO));
  1663. printLocation(Out, S->getLocStart());
  1664. if (isa<PreStmt>(Loc))
  1665. Out << "\\lPreStmt\\l;";
  1666. else if (isa<PostLoad>(Loc))
  1667. Out << "\\lPostLoad\\l;";
  1668. else if (isa<PostStore>(Loc))
  1669. Out << "\\lPostStore\\l";
  1670. else if (isa<PostLValue>(Loc))
  1671. Out << "\\lPostLValue\\l";
  1672. #if 0
  1673. // FIXME: Replace with a general scheme to determine
  1674. // the name of the check.
  1675. if (GraphPrintCheckerState->isImplicitNullDeref(N))
  1676. Out << "\\|Implicit-Null Dereference.\\l";
  1677. else if (GraphPrintCheckerState->isExplicitNullDeref(N))
  1678. Out << "\\|Explicit-Null Dereference.\\l";
  1679. else if (GraphPrintCheckerState->isUndefDeref(N))
  1680. Out << "\\|Dereference of undefialied value.\\l";
  1681. else if (GraphPrintCheckerState->isUndefStore(N))
  1682. Out << "\\|Store to Undefined Loc.";
  1683. else if (GraphPrintCheckerState->isUndefResult(N))
  1684. Out << "\\|Result of operation is undefined.";
  1685. else if (GraphPrintCheckerState->isNoReturnCall(N))
  1686. Out << "\\|Call to function marked \"noreturn\".";
  1687. else if (GraphPrintCheckerState->isBadCall(N))
  1688. Out << "\\|Call to NULL/Undefined.";
  1689. else if (GraphPrintCheckerState->isUndefArg(N))
  1690. Out << "\\|Argument in call is undefined";
  1691. #endif
  1692. break;
  1693. }
  1694. const BlockEdge &E = cast<BlockEdge>(Loc);
  1695. Out << "Edge: (B" << E.getSrc()->getBlockID() << ", B"
  1696. << E.getDst()->getBlockID() << ')';
  1697. if (const Stmt *T = E.getSrc()->getTerminator()) {
  1698. SourceLocation SLoc = T->getLocStart();
  1699. Out << "\\|Terminator: ";
  1700. LangOptions LO; // FIXME.
  1701. E.getSrc()->printTerminator(Out, LO);
  1702. if (SLoc.isFileID()) {
  1703. Out << "\\lline="
  1704. << GraphPrintSourceManager->getExpansionLineNumber(SLoc)
  1705. << " col="
  1706. << GraphPrintSourceManager->getExpansionColumnNumber(SLoc);
  1707. }
  1708. if (isa<SwitchStmt>(T)) {
  1709. const Stmt *Label = E.getDst()->getLabel();
  1710. if (Label) {
  1711. if (const CaseStmt *C = dyn_cast<CaseStmt>(Label)) {
  1712. Out << "\\lcase ";
  1713. LangOptions LO; // FIXME.
  1714. C->getLHS()->printPretty(Out, 0, PrintingPolicy(LO));
  1715. if (const Stmt *RHS = C->getRHS()) {
  1716. Out << " .. ";
  1717. RHS->printPretty(Out, 0, PrintingPolicy(LO));
  1718. }
  1719. Out << ":";
  1720. }
  1721. else {
  1722. assert (isa<DefaultStmt>(Label));
  1723. Out << "\\ldefault:";
  1724. }
  1725. }
  1726. else
  1727. Out << "\\l(implicit) default:";
  1728. }
  1729. else if (isa<IndirectGotoStmt>(T)) {
  1730. // FIXME
  1731. }
  1732. else {
  1733. Out << "\\lCondition: ";
  1734. if (*E.getSrc()->succ_begin() == E.getDst())
  1735. Out << "true";
  1736. else
  1737. Out << "false";
  1738. }
  1739. Out << "\\l";
  1740. }
  1741. #if 0
  1742. // FIXME: Replace with a general scheme to determine
  1743. // the name of the check.
  1744. if (GraphPrintCheckerState->isUndefControlFlow(N)) {
  1745. Out << "\\|Control-flow based on\\lUndefined value.\\l";
  1746. }
  1747. #endif
  1748. }
  1749. }
  1750. ProgramStateRef state = N->getState();
  1751. Out << "\\|StateID: " << (const void*) state.getPtr()
  1752. << " NodeID: " << (const void*) N << "\\|";
  1753. state->printDOT(Out);
  1754. Out << "\\l";
  1755. if (const ProgramPointTag *tag = Loc.getTag()) {
  1756. Out << "\\|Tag: " << tag->getTagDescription();
  1757. Out << "\\l";
  1758. }
  1759. return Out.str();
  1760. }
  1761. };
  1762. } // end llvm namespace
  1763. #endif
  1764. #ifndef NDEBUG
  1765. template <typename ITERATOR>
  1766. ExplodedNode *GetGraphNode(ITERATOR I) { return *I; }
  1767. template <> ExplodedNode*
  1768. GetGraphNode<llvm::DenseMap<ExplodedNode*, Expr*>::iterator>
  1769. (llvm::DenseMap<ExplodedNode*, Expr*>::iterator I) {
  1770. return I->first;
  1771. }
  1772. #endif
  1773. void ExprEngine::ViewGraph(bool trim) {
  1774. #ifndef NDEBUG
  1775. if (trim) {
  1776. std::vector<ExplodedNode*> Src;
  1777. // Flush any outstanding reports to make sure we cover all the nodes.
  1778. // This does not cause them to get displayed.
  1779. for (BugReporter::iterator I=BR.begin(), E=BR.end(); I!=E; ++I)
  1780. const_cast<BugType*>(*I)->FlushReports(BR);
  1781. // Iterate through the reports and get their nodes.
  1782. for (BugReporter::EQClasses_iterator
  1783. EI = BR.EQClasses_begin(), EE = BR.EQClasses_end(); EI != EE; ++EI) {
  1784. ExplodedNode *N = const_cast<ExplodedNode*>(EI->begin()->getErrorNode());
  1785. if (N) Src.push_back(N);
  1786. }
  1787. ViewGraph(&Src[0], &Src[0]+Src.size());
  1788. }
  1789. else {
  1790. GraphPrintCheckerState = this;
  1791. GraphPrintSourceManager = &getContext().getSourceManager();
  1792. llvm::ViewGraph(*G.roots_begin(), "ExprEngine");
  1793. GraphPrintCheckerState = NULL;
  1794. GraphPrintSourceManager = NULL;
  1795. }
  1796. #endif
  1797. }
  1798. void ExprEngine::ViewGraph(ExplodedNode** Beg, ExplodedNode** End) {
  1799. #ifndef NDEBUG
  1800. GraphPrintCheckerState = this;
  1801. GraphPrintSourceManager = &getContext().getSourceManager();
  1802. std::auto_ptr<ExplodedGraph> TrimmedG(G.Trim(Beg, End).first);
  1803. if (!TrimmedG.get())
  1804. llvm::errs() << "warning: Trimmed ExplodedGraph is empty.\n";
  1805. else
  1806. llvm::ViewGraph(*TrimmedG->roots_begin(), "TrimmedExprEngine");
  1807. GraphPrintCheckerState = NULL;
  1808. GraphPrintSourceManager = NULL;
  1809. #endif
  1810. }