clang  8.0.0svn
CheckerManager.cpp
Go to the documentation of this file.
1 //===- CheckerManager.cpp - Static Analyzer Checker Manager ---------------===//
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 // Defines the Static Analyzer Checker Manager.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "clang/AST/DeclBase.h"
16 #include "clang/AST/Stmt.h"
18 #include "clang/Basic/LLVM.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/Support/Casting.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include <cassert>
29 #include <vector>
30 
31 using namespace clang;
32 using namespace ento;
33 
35  return !StmtCheckers.empty() ||
36  !PreObjCMessageCheckers.empty() ||
37  !PostObjCMessageCheckers.empty() ||
38  !PreCallCheckers.empty() ||
39  !PostCallCheckers.empty() ||
40  !LocationCheckers.empty() ||
41  !BindCheckers.empty() ||
42  !EndAnalysisCheckers.empty() ||
43  !EndFunctionCheckers.empty() ||
44  !BranchConditionCheckers.empty() ||
45  !LiveSymbolsCheckers.empty() ||
46  !DeadSymbolsCheckers.empty() ||
47  !RegionChangesCheckers.empty() ||
48  !EvalAssumeCheckers.empty() ||
49  !EvalCallCheckers.empty();
50 }
51 
53 #ifndef NDEBUG
54  // Make sure that for every event that has listeners, there is at least
55  // one dispatcher registered for it.
56  for (const auto &Event : Events)
57  assert(Event.second.HasDispatcher &&
58  "No dispatcher registered for an event");
59 #endif
60 }
61 
62 //===----------------------------------------------------------------------===//
63 // Functions for running checkers for AST traversing..
64 //===----------------------------------------------------------------------===//
65 
67  BugReporter &BR) {
68  assert(D);
69 
70  unsigned DeclKind = D->getKind();
71  CachedDeclCheckers *checkers = nullptr;
72  CachedDeclCheckersMapTy::iterator CCI = CachedDeclCheckersMap.find(DeclKind);
73  if (CCI != CachedDeclCheckersMap.end()) {
74  checkers = &(CCI->second);
75  } else {
76  // Find the checkers that should run for this Decl and cache them.
77  checkers = &CachedDeclCheckersMap[DeclKind];
78  for (const auto &info : DeclCheckers)
79  if (info.IsForDeclFn(D))
80  checkers->push_back(info.CheckFn);
81  }
82 
83  assert(checkers);
84  for (const auto checker : *checkers)
85  checker(D, mgr, BR);
86 }
87 
89  BugReporter &BR) {
90  assert(D && D->hasBody());
91 
92  for (const auto BodyChecker : BodyCheckers)
93  BodyChecker(D, mgr, BR);
94 }
95 
96 //===----------------------------------------------------------------------===//
97 // Functions for running checkers for path-sensitive checking.
98 //===----------------------------------------------------------------------===//
99 
100 template <typename CHECK_CTX>
101 static void expandGraphWithCheckers(CHECK_CTX checkCtx,
102  ExplodedNodeSet &Dst,
103  const ExplodedNodeSet &Src) {
104  const NodeBuilderContext &BldrCtx = checkCtx.Eng.getBuilderContext();
105  if (Src.empty())
106  return;
107 
108  typename CHECK_CTX::CheckersTy::const_iterator
109  I = checkCtx.checkers_begin(), E = checkCtx.checkers_end();
110  if (I == E) {
111  Dst.insert(Src);
112  return;
113  }
114 
115  ExplodedNodeSet Tmp1, Tmp2;
116  const ExplodedNodeSet *PrevSet = &Src;
117 
118  for (; I != E; ++I) {
119  ExplodedNodeSet *CurrSet = nullptr;
120  if (I+1 == E)
121  CurrSet = &Dst;
122  else {
123  CurrSet = (PrevSet == &Tmp1) ? &Tmp2 : &Tmp1;
124  CurrSet->clear();
125  }
126 
127  NodeBuilder B(*PrevSet, *CurrSet, BldrCtx);
128  for (const auto &NI : *PrevSet)
129  checkCtx.runChecker(*I, B, NI);
130 
131  // If all the produced transitions are sinks, stop.
132  if (CurrSet->empty())
133  return;
134 
135  // Update which NodeSet is the current one.
136  PrevSet = CurrSet;
137  }
138 }
139 
140 namespace {
141 
142  struct CheckStmtContext {
144 
145  bool IsPreVisit;
146  const CheckersTy &Checkers;
147  const Stmt *S;
148  ExprEngine &Eng;
149  bool WasInlined;
150 
151  CheckStmtContext(bool isPreVisit, const CheckersTy &checkers,
152  const Stmt *s, ExprEngine &eng, bool wasInlined = false)
153  : IsPreVisit(isPreVisit), Checkers(checkers), S(s), Eng(eng),
154  WasInlined(wasInlined) {}
155 
156  CheckersTy::const_iterator checkers_begin() { return Checkers.begin(); }
157  CheckersTy::const_iterator checkers_end() { return Checkers.end(); }
158 
159  void runChecker(CheckerManager::CheckStmtFunc checkFn,
160  NodeBuilder &Bldr, ExplodedNode *Pred) {
161  // FIXME: Remove respondsToCallback from CheckerContext;
165  Pred->getLocationContext(), checkFn.Checker);
166  CheckerContext C(Bldr, Eng, Pred, L, WasInlined);
167  checkFn(S, C);
168  }
169  };
170 
171 } // namespace
172 
173 /// Run checkers for visiting Stmts.
175  ExplodedNodeSet &Dst,
176  const ExplodedNodeSet &Src,
177  const Stmt *S,
178  ExprEngine &Eng,
179  bool WasInlined) {
180  CheckStmtContext C(isPreVisit, getCachedStmtCheckersFor(S, isPreVisit),
181  S, Eng, WasInlined);
182  expandGraphWithCheckers(C, Dst, Src);
183 }
184 
185 namespace {
186 
187  struct CheckObjCMessageContext {
188  using CheckersTy = std::vector<CheckerManager::CheckObjCMessageFunc>;
189 
191  bool WasInlined;
192  const CheckersTy &Checkers;
193  const ObjCMethodCall &Msg;
194  ExprEngine &Eng;
195 
196  CheckObjCMessageContext(ObjCMessageVisitKind visitKind,
197  const CheckersTy &checkers,
198  const ObjCMethodCall &msg, ExprEngine &eng,
199  bool wasInlined)
200  : Kind(visitKind), WasInlined(wasInlined), Checkers(checkers), Msg(msg),
201  Eng(eng) {}
202 
203  CheckersTy::const_iterator checkers_begin() { return Checkers.begin(); }
204  CheckersTy::const_iterator checkers_end() { return Checkers.end(); }
205 
206  void runChecker(CheckerManager::CheckObjCMessageFunc checkFn,
207  NodeBuilder &Bldr, ExplodedNode *Pred) {
208  bool IsPreVisit;
209 
210  switch (Kind) {
212  IsPreVisit = true;
213  break;
216  IsPreVisit = false;
217  break;
218  }
219 
220  const ProgramPoint &L = Msg.getProgramPoint(IsPreVisit,checkFn.Checker);
221  CheckerContext C(Bldr, Eng, Pred, L, WasInlined);
222 
223  checkFn(*Msg.cloneWithState<ObjCMethodCall>(Pred->getState()), C);
224  }
225  };
226 
227 } // namespace
228 
229 /// Run checkers for visiting obj-c messages.
231  ExplodedNodeSet &Dst,
232  const ExplodedNodeSet &Src,
233  const ObjCMethodCall &msg,
234  ExprEngine &Eng,
235  bool WasInlined) {
236  auto &checkers = getObjCMessageCheckers(visitKind);
237  CheckObjCMessageContext C(visitKind, checkers, msg, Eng, WasInlined);
238  expandGraphWithCheckers(C, Dst, Src);
239 }
240 
241 const std::vector<CheckerManager::CheckObjCMessageFunc> &
242 CheckerManager::getObjCMessageCheckers(ObjCMessageVisitKind Kind) {
243  switch (Kind) {
245  return PreObjCMessageCheckers;
246  break;
248  return PostObjCMessageCheckers;
250  return ObjCMessageNilCheckers;
251  }
252  llvm_unreachable("Unknown Kind");
253 }
254 
255 namespace {
256 
257  // FIXME: This has all the same signatures as CheckObjCMessageContext.
258  // Is there a way we can merge the two?
259  struct CheckCallContext {
260  using CheckersTy = std::vector<CheckerManager::CheckCallFunc>;
261 
262  bool IsPreVisit, WasInlined;
263  const CheckersTy &Checkers;
264  const CallEvent &Call;
265  ExprEngine &Eng;
266 
267  CheckCallContext(bool isPreVisit, const CheckersTy &checkers,
268  const CallEvent &call, ExprEngine &eng,
269  bool wasInlined)
270  : IsPreVisit(isPreVisit), WasInlined(wasInlined), Checkers(checkers),
271  Call(call), Eng(eng) {}
272 
273  CheckersTy::const_iterator checkers_begin() { return Checkers.begin(); }
274  CheckersTy::const_iterator checkers_end() { return Checkers.end(); }
275 
276  void runChecker(CheckerManager::CheckCallFunc checkFn,
277  NodeBuilder &Bldr, ExplodedNode *Pred) {
278  const ProgramPoint &L = Call.getProgramPoint(IsPreVisit,checkFn.Checker);
279  CheckerContext C(Bldr, Eng, Pred, L, WasInlined);
280 
281  checkFn(*Call.cloneWithState(Pred->getState()), C);
282  }
283  };
284 
285 } // namespace
286 
287 /// Run checkers for visiting an abstract call event.
289  ExplodedNodeSet &Dst,
290  const ExplodedNodeSet &Src,
291  const CallEvent &Call,
292  ExprEngine &Eng,
293  bool WasInlined) {
294  CheckCallContext C(isPreVisit,
295  isPreVisit ? PreCallCheckers
296  : PostCallCheckers,
297  Call, Eng, WasInlined);
298  expandGraphWithCheckers(C, Dst, Src);
299 }
300 
301 namespace {
302 
303  struct CheckLocationContext {
304  using CheckersTy = std::vector<CheckerManager::CheckLocationFunc>;
305 
306  const CheckersTy &Checkers;
307  SVal Loc;
308  bool IsLoad;
309  const Stmt *NodeEx; /* Will become a CFGStmt */
310  const Stmt *BoundEx;
311  ExprEngine &Eng;
312 
313  CheckLocationContext(const CheckersTy &checkers,
314  SVal loc, bool isLoad, const Stmt *NodeEx,
315  const Stmt *BoundEx,
316  ExprEngine &eng)
317  : Checkers(checkers), Loc(loc), IsLoad(isLoad), NodeEx(NodeEx),
318  BoundEx(BoundEx), Eng(eng) {}
319 
320  CheckersTy::const_iterator checkers_begin() { return Checkers.begin(); }
321  CheckersTy::const_iterator checkers_end() { return Checkers.end(); }
322 
323  void runChecker(CheckerManager::CheckLocationFunc checkFn,
324  NodeBuilder &Bldr, ExplodedNode *Pred) {
327  const ProgramPoint &L =
329  Pred->getLocationContext(),
330  checkFn.Checker);
331  CheckerContext C(Bldr, Eng, Pred, L);
332  checkFn(Loc, IsLoad, BoundEx, C);
333  }
334  };
335 
336 } // namespace
337 
338 /// Run checkers for load/store of a location.
339 
341  const ExplodedNodeSet &Src,
342  SVal location, bool isLoad,
343  const Stmt *NodeEx,
344  const Stmt *BoundEx,
345  ExprEngine &Eng) {
346  CheckLocationContext C(LocationCheckers, location, isLoad, NodeEx,
347  BoundEx, Eng);
348  expandGraphWithCheckers(C, Dst, Src);
349 }
350 
351 namespace {
352 
353  struct CheckBindContext {
354  using CheckersTy = std::vector<CheckerManager::CheckBindFunc>;
355 
356  const CheckersTy &Checkers;
357  SVal Loc;
358  SVal Val;
359  const Stmt *S;
360  ExprEngine &Eng;
361  const ProgramPoint &PP;
362 
363  CheckBindContext(const CheckersTy &checkers,
364  SVal loc, SVal val, const Stmt *s, ExprEngine &eng,
365  const ProgramPoint &pp)
366  : Checkers(checkers), Loc(loc), Val(val), S(s), Eng(eng), PP(pp) {}
367 
368  CheckersTy::const_iterator checkers_begin() { return Checkers.begin(); }
369  CheckersTy::const_iterator checkers_end() { return Checkers.end(); }
370 
371  void runChecker(CheckerManager::CheckBindFunc checkFn,
372  NodeBuilder &Bldr, ExplodedNode *Pred) {
373  const ProgramPoint &L = PP.withTag(checkFn.Checker);
374  CheckerContext C(Bldr, Eng, Pred, L);
375 
376  checkFn(Loc, Val, S, C);
377  }
378  };
379 
380 } // namespace
381 
382 /// Run checkers for binding of a value to a location.
384  const ExplodedNodeSet &Src,
385  SVal location, SVal val,
386  const Stmt *S, ExprEngine &Eng,
387  const ProgramPoint &PP) {
388  CheckBindContext C(BindCheckers, location, val, S, Eng, PP);
389  expandGraphWithCheckers(C, Dst, Src);
390 }
391 
393  BugReporter &BR,
394  ExprEngine &Eng) {
395  for (const auto EndAnalysisChecker : EndAnalysisCheckers)
396  EndAnalysisChecker(G, BR, Eng);
397 }
398 
399 namespace {
400 
401 struct CheckBeginFunctionContext {
402  using CheckersTy = std::vector<CheckerManager::CheckBeginFunctionFunc>;
403 
404  const CheckersTy &Checkers;
405  ExprEngine &Eng;
406  const ProgramPoint &PP;
407 
408  CheckBeginFunctionContext(const CheckersTy &Checkers, ExprEngine &Eng,
409  const ProgramPoint &PP)
410  : Checkers(Checkers), Eng(Eng), PP(PP) {}
411 
412  CheckersTy::const_iterator checkers_begin() { return Checkers.begin(); }
413  CheckersTy::const_iterator checkers_end() { return Checkers.end(); }
414 
415  void runChecker(CheckerManager::CheckBeginFunctionFunc checkFn,
416  NodeBuilder &Bldr, ExplodedNode *Pred) {
417  const ProgramPoint &L = PP.withTag(checkFn.Checker);
418  CheckerContext C(Bldr, Eng, Pred, L);
419 
420  checkFn(C);
421  }
422 };
423 
424 } // namespace
425 
427  const BlockEdge &L,
428  ExplodedNode *Pred,
429  ExprEngine &Eng) {
430  ExplodedNodeSet Src;
431  Src.insert(Pred);
432  CheckBeginFunctionContext C(BeginFunctionCheckers, Eng, L);
433  expandGraphWithCheckers(C, Dst, Src);
434 }
435 
436 /// Run checkers for end of path.
437 // Note, We do not chain the checker output (like in expandGraphWithCheckers)
438 // for this callback since end of path nodes are expected to be final.
440  ExplodedNodeSet &Dst,
441  ExplodedNode *Pred,
442  ExprEngine &Eng,
443  const ReturnStmt *RS) {
444  // We define the builder outside of the loop bacause if at least one checkers
445  // creates a sucsessor for Pred, we do not need to generate an
446  // autotransition for it.
447  NodeBuilder Bldr(Pred, Dst, BC);
448  for (const auto checkFn : EndFunctionCheckers) {
449  const ProgramPoint &L = BlockEntrance(BC.Block,
450  Pred->getLocationContext(),
451  checkFn.Checker);
452  CheckerContext C(Bldr, Eng, Pred, L);
453  checkFn(RS, C);
454  }
455 }
456 
457 namespace {
458 
459  struct CheckBranchConditionContext {
460  using CheckersTy = std::vector<CheckerManager::CheckBranchConditionFunc>;
461 
462  const CheckersTy &Checkers;
463  const Stmt *Condition;
464  ExprEngine &Eng;
465 
466  CheckBranchConditionContext(const CheckersTy &checkers,
467  const Stmt *Cond, ExprEngine &eng)
468  : Checkers(checkers), Condition(Cond), Eng(eng) {}
469 
470  CheckersTy::const_iterator checkers_begin() { return Checkers.begin(); }
471  CheckersTy::const_iterator checkers_end() { return Checkers.end(); }
472 
473  void runChecker(CheckerManager::CheckBranchConditionFunc checkFn,
474  NodeBuilder &Bldr, ExplodedNode *Pred) {
475  ProgramPoint L = PostCondition(Condition, Pred->getLocationContext(),
476  checkFn.Checker);
477  CheckerContext C(Bldr, Eng, Pred, L);
478  checkFn(Condition, C);
479  }
480  };
481 
482 } // namespace
483 
484 /// Run checkers for branch condition.
486  ExplodedNodeSet &Dst,
487  ExplodedNode *Pred,
488  ExprEngine &Eng) {
489  ExplodedNodeSet Src;
490  Src.insert(Pred);
491  CheckBranchConditionContext C(BranchConditionCheckers, Condition, Eng);
492  expandGraphWithCheckers(C, Dst, Src);
493 }
494 
495 namespace {
496 
497  struct CheckNewAllocatorContext {
498  using CheckersTy = std::vector<CheckerManager::CheckNewAllocatorFunc>;
499 
500  const CheckersTy &Checkers;
501  const CXXNewExpr *NE;
502  SVal Target;
503  bool WasInlined;
504  ExprEngine &Eng;
505 
506  CheckNewAllocatorContext(const CheckersTy &Checkers, const CXXNewExpr *NE,
507  SVal Target, bool WasInlined, ExprEngine &Eng)
508  : Checkers(Checkers), NE(NE), Target(Target), WasInlined(WasInlined),
509  Eng(Eng) {}
510 
511  CheckersTy::const_iterator checkers_begin() { return Checkers.begin(); }
512  CheckersTy::const_iterator checkers_end() { return Checkers.end(); }
513 
514  void runChecker(CheckerManager::CheckNewAllocatorFunc checkFn,
515  NodeBuilder &Bldr, ExplodedNode *Pred) {
517  CheckerContext C(Bldr, Eng, Pred, L, WasInlined);
518  checkFn(NE, Target, C);
519  }
520  };
521 
522 } // namespace
523 
525  const CXXNewExpr *NE, SVal Target, ExplodedNodeSet &Dst, ExplodedNode *Pred,
526  ExprEngine &Eng, bool WasInlined) {
527  ExplodedNodeSet Src;
528  Src.insert(Pred);
529  CheckNewAllocatorContext C(NewAllocatorCheckers, NE, Target, WasInlined, Eng);
530  expandGraphWithCheckers(C, Dst, Src);
531 }
532 
533 /// Run checkers for live symbols.
535  SymbolReaper &SymReaper) {
536  for (const auto LiveSymbolsChecker : LiveSymbolsCheckers)
537  LiveSymbolsChecker(state, SymReaper);
538 }
539 
540 namespace {
541 
542  struct CheckDeadSymbolsContext {
543  using CheckersTy = std::vector<CheckerManager::CheckDeadSymbolsFunc>;
544 
545  const CheckersTy &Checkers;
546  SymbolReaper &SR;
547  const Stmt *S;
548  ExprEngine &Eng;
549  ProgramPoint::Kind ProgarmPointKind;
550 
551  CheckDeadSymbolsContext(const CheckersTy &checkers, SymbolReaper &sr,
552  const Stmt *s, ExprEngine &eng,
554  : Checkers(checkers), SR(sr), S(s), Eng(eng), ProgarmPointKind(K) {}
555 
556  CheckersTy::const_iterator checkers_begin() { return Checkers.begin(); }
557  CheckersTy::const_iterator checkers_end() { return Checkers.end(); }
558 
559  void runChecker(CheckerManager::CheckDeadSymbolsFunc checkFn,
560  NodeBuilder &Bldr, ExplodedNode *Pred) {
561  const ProgramPoint &L = ProgramPoint::getProgramPoint(S, ProgarmPointKind,
562  Pred->getLocationContext(), checkFn.Checker);
563  CheckerContext C(Bldr, Eng, Pred, L);
564 
565  // Note, do not pass the statement to the checkers without letting them
566  // differentiate if we ran remove dead bindings before or after the
567  // statement.
568  checkFn(SR, C);
569  }
570  };
571 
572 } // namespace
573 
574 /// Run checkers for dead symbols.
576  const ExplodedNodeSet &Src,
577  SymbolReaper &SymReaper,
578  const Stmt *S,
579  ExprEngine &Eng,
580  ProgramPoint::Kind K) {
581  CheckDeadSymbolsContext C(DeadSymbolsCheckers, SymReaper, S, Eng, K);
582  expandGraphWithCheckers(C, Dst, Src);
583 }
584 
585 /// Run checkers for region changes.
588  const InvalidatedSymbols *invalidated,
589  ArrayRef<const MemRegion *> ExplicitRegions,
591  const LocationContext *LCtx,
592  const CallEvent *Call) {
593  for (const auto RegionChangesChecker : RegionChangesCheckers) {
594  // If any checker declares the state infeasible (or if it starts that way),
595  // bail out.
596  if (!state)
597  return nullptr;
598  state = RegionChangesChecker(state, invalidated, ExplicitRegions, Regions,
599  LCtx, Call);
600  }
601  return state;
602 }
603 
604 /// Run checkers to process symbol escape event.
607  const InvalidatedSymbols &Escaped,
608  const CallEvent *Call,
611  assert((Call != nullptr ||
612  (Kind != PSK_DirectEscapeOnCall &&
613  Kind != PSK_IndirectEscapeOnCall)) &&
614  "Call must not be NULL when escaping on call");
615  for (const auto PointerEscapeChecker : PointerEscapeCheckers) {
616  // If any checker declares the state infeasible (or if it starts that
617  // way), bail out.
618  if (!State)
619  return nullptr;
620  State = PointerEscapeChecker(State, Escaped, Call, Kind, ETraits);
621  }
622  return State;
623 }
624 
625 /// Run checkers for handling assumptions on symbolic values.
628  SVal Cond, bool Assumption) {
629  for (const auto EvalAssumeChecker : EvalAssumeCheckers) {
630  // If any checker declares the state infeasible (or if it starts that way),
631  // bail out.
632  if (!state)
633  return nullptr;
634  state = EvalAssumeChecker(state, Cond, Assumption);
635  }
636  return state;
637 }
638 
639 /// Run checkers for evaluating a call.
640 /// Only one checker will evaluate the call.
642  const ExplodedNodeSet &Src,
643  const CallEvent &Call,
644  ExprEngine &Eng) {
645  const CallExpr *CE = cast<CallExpr>(Call.getOriginExpr());
646  for (const auto Pred : Src) {
647  bool anyEvaluated = false;
648 
649  ExplodedNodeSet checkDst;
650  NodeBuilder B(Pred, checkDst, Eng.getBuilderContext());
651 
652  // Check if any of the EvalCall callbacks can evaluate the call.
653  for (const auto EvalCallChecker : EvalCallCheckers) {
655  const ProgramPoint &L =
656  ProgramPoint::getProgramPoint(CE, K, Pred->getLocationContext(),
657  EvalCallChecker.Checker);
658  bool evaluated = false;
659  { // CheckerContext generates transitions(populates checkDest) on
660  // destruction, so introduce the scope to make sure it gets properly
661  // populated.
662  CheckerContext C(B, Eng, Pred, L);
663  evaluated = EvalCallChecker(CE, C);
664  }
665  assert(!(evaluated && anyEvaluated)
666  && "There are more than one checkers evaluating the call");
667  if (evaluated) {
668  anyEvaluated = true;
669  Dst.insert(checkDst);
670 #ifdef NDEBUG
671  break; // on release don't check that no other checker also evals.
672 #endif
673  }
674  }
675 
676  // If none of the checkers evaluated the call, ask ExprEngine to handle it.
677  if (!anyEvaluated) {
678  NodeBuilder B(Pred, Dst, Eng.getBuilderContext());
679  Eng.defaultEvalCall(B, Pred, Call);
680  }
681  }
682 }
683 
684 /// Run checkers for the entire Translation Unit.
686  const TranslationUnitDecl *TU,
687  AnalysisManager &mgr,
688  BugReporter &BR) {
689  for (const auto EndOfTranslationUnitChecker : EndOfTranslationUnitCheckers)
690  EndOfTranslationUnitChecker(TU, mgr, BR);
691 }
692 
695  const char *NL, const char *Sep) {
696  for (const auto &CheckerTag : CheckerTags)
697  CheckerTag.second->printState(Out, State, NL, Sep);
698 }
699 
700 //===----------------------------------------------------------------------===//
701 // Internal registration functions for AST traversing.
702 //===----------------------------------------------------------------------===//
703 
705  HandlesDeclFunc isForDeclFn) {
706  DeclCheckerInfo info = { checkfn, isForDeclFn };
707  DeclCheckers.push_back(info);
708 }
709 
711  BodyCheckers.push_back(checkfn);
712 }
713 
714 //===----------------------------------------------------------------------===//
715 // Internal registration functions for path-sensitive checking.
716 //===----------------------------------------------------------------------===//
717 
719  HandlesStmtFunc isForStmtFn) {
720  StmtCheckerInfo info = { checkfn, isForStmtFn, /*IsPreVisit*/true };
721  StmtCheckers.push_back(info);
722 }
723 
725  HandlesStmtFunc isForStmtFn) {
726  StmtCheckerInfo info = { checkfn, isForStmtFn, /*IsPreVisit*/false };
727  StmtCheckers.push_back(info);
728 }
729 
731  PreObjCMessageCheckers.push_back(checkfn);
732 }
733 
735  ObjCMessageNilCheckers.push_back(checkfn);
736 }
737 
739  PostObjCMessageCheckers.push_back(checkfn);
740 }
741 
743  PreCallCheckers.push_back(checkfn);
744 }
746  PostCallCheckers.push_back(checkfn);
747 }
748 
750  LocationCheckers.push_back(checkfn);
751 }
752 
754  BindCheckers.push_back(checkfn);
755 }
756 
758  EndAnalysisCheckers.push_back(checkfn);
759 }
760 
762  BeginFunctionCheckers.push_back(checkfn);
763 }
764 
766  EndFunctionCheckers.push_back(checkfn);
767 }
768 
770  CheckBranchConditionFunc checkfn) {
771  BranchConditionCheckers.push_back(checkfn);
772 }
773 
775  NewAllocatorCheckers.push_back(checkfn);
776 }
777 
779  LiveSymbolsCheckers.push_back(checkfn);
780 }
781 
783  DeadSymbolsCheckers.push_back(checkfn);
784 }
785 
787  RegionChangesCheckers.push_back(checkfn);
788 }
789 
791  PointerEscapeCheckers.push_back(checkfn);
792 }
793 
795  CheckPointerEscapeFunc checkfn) {
796  PointerEscapeCheckers.push_back(checkfn);
797 }
798 
800  EvalAssumeCheckers.push_back(checkfn);
801 }
802 
804  EvalCallCheckers.push_back(checkfn);
805 }
806 
808  CheckEndOfTranslationUnit checkfn) {
809  EndOfTranslationUnitCheckers.push_back(checkfn);
810 }
811 
812 //===----------------------------------------------------------------------===//
813 // Implementation details.
814 //===----------------------------------------------------------------------===//
815 
817 CheckerManager::getCachedStmtCheckersFor(const Stmt *S, bool isPreVisit) {
818  assert(S);
819 
820  unsigned Key = (S->getStmtClass() << 1) | unsigned(isPreVisit);
821  CachedStmtCheckersMapTy::iterator CCI = CachedStmtCheckersMap.find(Key);
822  if (CCI != CachedStmtCheckersMap.end())
823  return CCI->second;
824 
825  // Find the checkers that should run for this Stmt and cache them.
826  CachedStmtCheckers &Checkers = CachedStmtCheckersMap[Key];
827  for (const auto &Info : StmtCheckers)
828  if (Info.IsPreVisit == isPreVisit && Info.IsForStmtFn(S))
829  Checkers.push_back(Info.CheckFn);
830  return Checkers;
831 }
832 
834  for (const auto CheckerDtor : CheckerDtors)
835  CheckerDtor();
836 }
CallEventRef< T > cloneWithState(ProgramStateRef NewState) const
Returns a copy of this CallEvent, but using the given state.
void _registerForDeadSymbols(CheckDeadSymbolsFunc checkfn)
Stmt - This represents one statement.
Definition: Stmt.h:66
Information about invalidation for a particular region/symbol.
Definition: MemRegion.h:1384
ProgramPoint getProgramPoint(bool IsPreVisit=false, const ProgramPointTag *Tag=nullptr) const
Returns an appropriate ProgramPoint for this call.
Definition: CallEvent.cpp:339
static void expandGraphWithCheckers(CHECK_CTX checkCtx, ExplodedNodeSet &Dst, const ExplodedNodeSet &Src)
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
ProgramPoint withTag(const ProgramPointTag *tag) const
Create a new ProgramPoint object that is the same as the original except for using the specified tag ...
Definition: ProgramPoint.h:133
The pointer has been passed to a function indirectly.
const ProgramStateRef & getState() const
const Expr * getOriginExpr() const
Returns the expression whose value will be the result of this call.
Definition: CallEvent.h:249
void _registerForRegionChanges(CheckRegionChangesFunc checkfn)
void _registerForObjCMessageNil(CheckObjCMessageFunc checkfn)
const NodeBuilderContext & getBuilderContext()
Definition: ExprEngine.h:199
void runCheckersForLocation(ExplodedNodeSet &Dst, const ExplodedNodeSet &Src, SVal location, bool isLoad, const Stmt *NodeEx, const Stmt *BoundEx, ExprEngine &Eng)
Run checkers for load/store of a location.
static ProgramPoint getProgramPoint(const Stmt *S, ProgramPoint::Kind K, const LocationContext *LC, const ProgramPointTag *tag)
ProgramStateRef runCheckersForPointerEscape(ProgramStateRef State, const InvalidatedSymbols &Escaped, const CallEvent *Call, PointerEscapeKind Kind, RegionAndSymbolInvalidationTraits *ITraits)
Run checkers when pointers escape.
void _registerForPreCall(CheckCallFunc checkfn)
LineState State
void runCheckersForLiveSymbols(ProgramStateRef state, SymbolReaper &SymReaper)
Run checkers for live symbols.
i32 captured_struct **param SharedsTy A type which contains references the shared variables *param Shareds Context with the list of shared variables from the p *TaskFunction *param Data Additional data for task generation like final * state
void _registerForEndOfTranslationUnit(CheckEndOfTranslationUnit checkfn)
Represents any expression that calls an Objective-C method.
Definition: CallEvent.h:949
bool(*)(const Stmt *D) HandlesStmtFunc
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
const LocationContext * getLocationContext() const
void _registerForLiveSymbols(CheckLiveSymbolsFunc checkfn)
void runCheckersOnASTDecl(const Decl *D, AnalysisManager &mgr, BugReporter &BR)
Run checkers handling Decls.
void runCheckersForEndFunction(NodeBuilderContext &BC, ExplodedNodeSet &Dst, ExplodedNode *Pred, ExprEngine &Eng, const ReturnStmt *RS)
Run checkers on end of function.
void runCheckersForPrintState(raw_ostream &Out, ProgramStateRef State, const char *NL, const char *Sep)
Run checkers for debug-printing a ProgramState.
void runCheckersForBind(ExplodedNodeSet &Dst, const ExplodedNodeSet &Src, SVal location, SVal val, const Stmt *S, ExprEngine &Eng, const ProgramPoint &PP)
Run checkers for binding of a value to a location.
void _registerForBody(CheckDeclFunc checkfn)
for(unsigned I=0, E=TL.getNumArgs();I !=E;++I)
This is the simplest builder which generates nodes in the ExplodedGraph.
Definition: CoreEngine.h:228
void _registerForEndAnalysis(CheckEndAnalysisFunc checkfn)
void _registerForLocation(CheckLocationFunc checkfn)
The pointer has been passed to a function call directly.
void _registerForPointerEscape(CheckPointerEscapeFunc checkfn)
void runCheckersForEvalCall(ExplodedNodeSet &Dst, const ExplodedNodeSet &Src, const CallEvent &CE, ExprEngine &Eng)
Run checkers for evaluating a call.
ReturnStmt - This represents a return, optionally of an expression: return; return 4;...
Definition: Stmt.h:1588
void _registerForPostCall(CheckCallFunc checkfn)
virtual bool hasBody() const
Returns true if this Decl represents a declaration for a body of code, such as a function or method d...
Definition: DeclBase.h:998
BugReporter is a utility class for generating PathDiagnostics for analysis.
Definition: BugReporter.h:412
void runCheckersForNewAllocator(const CXXNewExpr *NE, SVal Target, ExplodedNodeSet &Dst, ExplodedNode *Pred, ExprEngine &Eng, bool wasInlined=false)
Run checkers between C++ operator new and constructor calls.
void _registerForPreObjCMessage(CheckObjCMessageFunc checkfn)
Kind
void runCheckersForBranchCondition(const Stmt *condition, ExplodedNodeSet &Dst, ExplodedNode *Pred, ExprEngine &Eng)
Run checkers for branch condition.
Represents a new-expression for memory allocation and constructor calls, e.g: "new CXXNewExpr(foo)"...
Definition: ExprCXX.h:2041
SVal - This represents a symbolic expression, which can be either an L-value or an R-value...
Definition: SVals.h:76
void _registerForBranchCondition(CheckBranchConditionFunc checkfn)
ProgramStateRef runCheckersForEvalAssume(ProgramStateRef state, SVal Cond, bool Assumption)
Run checkers for handling assumptions on symbolic values.
void runCheckersForObjCMessage(ObjCMessageVisitKind visitKind, ExplodedNodeSet &Dst, const ExplodedNodeSet &Src, const ObjCMethodCall &msg, ExprEngine &Eng, bool wasInlined=false)
Run checkers for visiting obj-c messages.
A class responsible for cleaning up unused symbols.
void runCheckersOnASTBody(const Decl *D, AnalysisManager &mgr, BugReporter &BR)
Run checkers handling Decls containing a Stmt body.
void insert(const ExplodedNodeSet &S)
void runCheckersForDeadSymbols(ExplodedNodeSet &Dst, const ExplodedNodeSet &Src, SymbolReaper &SymReaper, const Stmt *S, ExprEngine &Eng, ProgramPoint::Kind K)
Run checkers for dead symbols.
Dataflow Directional Tag Classes.
StmtClass getStmtClass() const
Definition: Stmt.h:391
void runCheckersForStmt(bool isPreVisit, ExplodedNodeSet &Dst, const ExplodedNodeSet &Src, const Stmt *S, ExprEngine &Eng, bool wasInlined=false)
Run checkers for visiting Stmts.
Kind getKind() const
Definition: DeclBase.h:428
void _registerForDecl(CheckDeclFunc checkfn, HandlesDeclFunc isForDeclFn)
void _registerForPostObjCMessage(CheckObjCMessageFunc checkfn)
Represents an abstract call to a function or method along a particular path.
Definition: CallEvent.h:165
void _registerForEndFunction(CheckEndFunctionFunc checkfn)
void _registerForEvalAssume(EvalAssumeFunc checkfn)
void _registerForPostStmt(CheckStmtFunc checkfn, HandlesStmtFunc isForStmtFn)
PointerEscapeKind
Describes the different reasons a pointer escapes during analysis.
void defaultEvalCall(NodeBuilder &B, ExplodedNode *Pred, const CallEvent &Call, const EvalCallOptions &CallOpts={})
Default implementation of call evaluation.
void _registerForNewAllocator(CheckNewAllocatorFunc checkfn)
void runCheckersForCallEvent(bool isPreVisit, ExplodedNodeSet &Dst, const ExplodedNodeSet &Src, const CallEvent &Call, ExprEngine &Eng, bool wasInlined=false)
Run checkers for visiting obj-c messages.
void runCheckersForEndAnalysis(ExplodedGraph &G, BugReporter &BR, ExprEngine &Eng)
Run checkers for end of analysis.
void _registerForBeginFunction(CheckBeginFunctionFunc checkfn)
void _registerForEvalCall(EvalCallFunc checkfn)
CheckerFn< void()> CheckerDtor
bool(*)(const Decl *D) HandlesDeclFunc
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2406
ProgramStateRef runCheckersForRegionChanges(ProgramStateRef state, const InvalidatedSymbols *invalidated, ArrayRef< const MemRegion *> ExplicitRegions, ArrayRef< const MemRegion *> Regions, const LocationContext *LCtx, const CallEvent *Call)
Run checkers for region changes.
void runCheckersForBeginFunction(ExplodedNodeSet &Dst, const BlockEdge &L, ExplodedNode *Pred, ExprEngine &Eng)
Run checkers on beginning of function.
The top declaration context.
Definition: Decl.h:107
void _registerForConstPointerEscape(CheckPointerEscapeFunc checkfn)
void runCheckersOnEndOfTranslationUnit(const TranslationUnitDecl *TU, AnalysisManager &mgr, BugReporter &BR)
Run checkers for the entire Translation Unit.
void _registerForPreStmt(CheckStmtFunc checkfn, HandlesStmtFunc isForStmtFn)
void _registerForBind(CheckBindFunc checkfn)