1 //===--- CheckerManager.h - Static Analyzer Checker Manager -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Defines the Static Analyzer Checker Manager.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_SA_CORE_CHECKERMANAGER_H
15 #define LLVM_CLANG_SA_CORE_CHECKERMANAGER_H
17 #include "clang/Analysis/ProgramPoint.h"
18 #include "clang/Basic/LangOptions.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/Store.h"
20 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/SmallVector.h"
33 class AnalysisManager;
40 class ExplodedNodeSet;
44 struct NodeBuilderContext;
48 template <typename T> class CheckerFn;
50 template <typename RET, typename P1, typename P2, typename P3, typename P4,
52 class CheckerFn<RET(P1, P2, P3, P4, P5)> {
53 typedef RET (*Func)(void *, P1, P2, P3, P4, P5);
57 CheckerFn(CheckerBase *checker, Func fn) : Fn(fn), Checker(checker) { }
58 RET operator()(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) const {
59 return Fn(Checker, p1, p2, p3, p4, p5);
63 template <typename RET, typename P1, typename P2, typename P3, typename P4>
64 class CheckerFn<RET(P1, P2, P3, P4)> {
65 typedef RET (*Func)(void *, P1, P2, P3, P4);
69 CheckerFn(CheckerBase *checker, Func fn) : Fn(fn), Checker(checker) { }
70 RET operator()(P1 p1, P2 p2, P3 p3, P4 p4) const {
71 return Fn(Checker, p1, p2, p3, p4);
75 template <typename RET, typename P1, typename P2, typename P3>
76 class CheckerFn<RET(P1, P2, P3)> {
77 typedef RET (*Func)(void *, P1, P2, P3);
81 CheckerFn(CheckerBase *checker, Func fn) : Fn(fn), Checker(checker) { }
82 RET operator()(P1 p1, P2 p2, P3 p3) const { return Fn(Checker, p1, p2, p3); }
85 template <typename RET, typename P1, typename P2>
86 class CheckerFn<RET(P1, P2)> {
87 typedef RET (*Func)(void *, P1, P2);
91 CheckerFn(CheckerBase *checker, Func fn) : Fn(fn), Checker(checker) { }
92 RET operator()(P1 p1, P2 p2) const { return Fn(Checker, p1, p2); }
95 template <typename RET, typename P1>
96 class CheckerFn<RET(P1)> {
97 typedef RET (*Func)(void *, P1);
100 CheckerBase *Checker;
101 CheckerFn(CheckerBase *checker, Func fn) : Fn(fn), Checker(checker) { }
102 RET operator()(P1 p1) const { return Fn(Checker, p1); }
105 template <typename RET>
106 class CheckerFn<RET()> {
107 typedef RET (*Func)(void *);
110 CheckerBase *Checker;
111 CheckerFn(CheckerBase *checker, Func fn) : Fn(fn), Checker(checker) { }
112 RET operator()() const { return Fn(Checker); }
115 /// \brief Describes the different reasons a pointer escapes
117 enum PointerEscapeKind {
118 /// A pointer escapes due to binding its value to a location
119 /// that the analyzer cannot track.
122 /// The pointer has been passed to a function call directly.
123 PSK_DirectEscapeOnCall,
125 /// The pointer has been passed to a function indirectly.
126 /// For example, the pointer is accessible through an
127 /// argument to a function.
128 PSK_IndirectEscapeOnCall,
130 /// The reason for pointer escape is unknown. For example,
131 /// a region containing this pointer is invalidated.
135 class CheckerManager {
136 const LangOptions LangOpts;
137 AnalyzerOptionsRef AOptions;
139 CheckerManager(const LangOptions &langOpts,
140 AnalyzerOptionsRef AOptions)
141 : LangOpts(langOpts),
142 AOptions(AOptions) {}
146 bool hasPathSensitiveCheckers() const;
148 void finishedCheckerRegistration();
150 const LangOptions &getLangOpts() const { return LangOpts; }
151 AnalyzerOptions &getAnalyzerOptions() { return *AOptions; }
153 typedef CheckerBase *CheckerRef;
154 typedef const void *CheckerTag;
155 typedef CheckerFn<void ()> CheckerDtor;
157 //===----------------------------------------------------------------------===//
159 //===----------------------------------------------------------------------===//
161 /// \brief Used to register checkers.
163 /// \returns a pointer to the checker object.
164 template <typename CHECKER>
165 CHECKER *registerChecker() {
166 CheckerTag tag = getTag<CHECKER>();
167 CheckerRef &ref = CheckerTags[tag];
169 return static_cast<CHECKER *>(ref); // already registered.
171 CHECKER *checker = new CHECKER();
172 CheckerDtors.push_back(CheckerDtor(checker, destruct<CHECKER>));
173 CHECKER::_register(checker, *this);
178 template <typename CHECKER>
179 CHECKER *registerChecker(AnalyzerOptions &AOpts) {
180 CheckerTag tag = getTag<CHECKER>();
181 CheckerRef &ref = CheckerTags[tag];
183 return static_cast<CHECKER *>(ref); // already registered.
185 CHECKER *checker = new CHECKER(AOpts);
186 CheckerDtors.push_back(CheckerDtor(checker, destruct<CHECKER>));
187 CHECKER::_register(checker, *this);
192 //===----------------------------------------------------------------------===//
193 // Functions for running checkers for AST traversing..
194 //===----------------------------------------------------------------------===//
196 /// \brief Run checkers handling Decls.
197 void runCheckersOnASTDecl(const Decl *D, AnalysisManager& mgr,
200 /// \brief Run checkers handling Decls containing a Stmt body.
201 void runCheckersOnASTBody(const Decl *D, AnalysisManager& mgr,
204 //===----------------------------------------------------------------------===//
205 // Functions for running checkers for path-sensitive checking.
206 //===----------------------------------------------------------------------===//
208 /// \brief Run checkers for pre-visiting Stmts.
210 /// The notification is performed for every explored CFGElement, which does
211 /// not include the control flow statements such as IfStmt.
213 /// \sa runCheckersForBranchCondition, runCheckersForPostStmt
214 void runCheckersForPreStmt(ExplodedNodeSet &Dst,
215 const ExplodedNodeSet &Src,
218 runCheckersForStmt(/*isPreVisit=*/true, Dst, Src, S, Eng);
221 /// \brief Run checkers for post-visiting Stmts.
223 /// The notification is performed for every explored CFGElement, which does
224 /// not include the control flow statements such as IfStmt.
226 /// \sa runCheckersForBranchCondition, runCheckersForPreStmt
227 void runCheckersForPostStmt(ExplodedNodeSet &Dst,
228 const ExplodedNodeSet &Src,
231 bool wasInlined = false) {
232 runCheckersForStmt(/*isPreVisit=*/false, Dst, Src, S, Eng, wasInlined);
235 /// \brief Run checkers for visiting Stmts.
236 void runCheckersForStmt(bool isPreVisit,
237 ExplodedNodeSet &Dst, const ExplodedNodeSet &Src,
238 const Stmt *S, ExprEngine &Eng,
239 bool wasInlined = false);
241 /// \brief Run checkers for pre-visiting obj-c messages.
242 void runCheckersForPreObjCMessage(ExplodedNodeSet &Dst,
243 const ExplodedNodeSet &Src,
244 const ObjCMethodCall &msg,
246 runCheckersForObjCMessage(/*isPreVisit=*/true, Dst, Src, msg, Eng);
249 /// \brief Run checkers for post-visiting obj-c messages.
250 void runCheckersForPostObjCMessage(ExplodedNodeSet &Dst,
251 const ExplodedNodeSet &Src,
252 const ObjCMethodCall &msg,
254 bool wasInlined = false) {
255 runCheckersForObjCMessage(/*isPreVisit=*/false, Dst, Src, msg, Eng,
259 /// \brief Run checkers for visiting obj-c messages.
260 void runCheckersForObjCMessage(bool isPreVisit,
261 ExplodedNodeSet &Dst,
262 const ExplodedNodeSet &Src,
263 const ObjCMethodCall &msg, ExprEngine &Eng,
264 bool wasInlined = false);
266 /// \brief Run checkers for pre-visiting obj-c messages.
267 void runCheckersForPreCall(ExplodedNodeSet &Dst, const ExplodedNodeSet &Src,
268 const CallEvent &Call, ExprEngine &Eng) {
269 runCheckersForCallEvent(/*isPreVisit=*/true, Dst, Src, Call, Eng);
272 /// \brief Run checkers for post-visiting obj-c messages.
273 void runCheckersForPostCall(ExplodedNodeSet &Dst, const ExplodedNodeSet &Src,
274 const CallEvent &Call, ExprEngine &Eng,
275 bool wasInlined = false) {
276 runCheckersForCallEvent(/*isPreVisit=*/false, Dst, Src, Call, Eng,
280 /// \brief Run checkers for visiting obj-c messages.
281 void runCheckersForCallEvent(bool isPreVisit, ExplodedNodeSet &Dst,
282 const ExplodedNodeSet &Src,
283 const CallEvent &Call, ExprEngine &Eng,
284 bool wasInlined = false);
286 /// \brief Run checkers for load/store of a location.
287 void runCheckersForLocation(ExplodedNodeSet &Dst,
288 const ExplodedNodeSet &Src,
295 /// \brief Run checkers for binding of a value to a location.
296 void runCheckersForBind(ExplodedNodeSet &Dst,
297 const ExplodedNodeSet &Src,
298 SVal location, SVal val,
299 const Stmt *S, ExprEngine &Eng,
300 const ProgramPoint &PP);
302 /// \brief Run checkers for end of analysis.
303 void runCheckersForEndAnalysis(ExplodedGraph &G, BugReporter &BR,
306 /// \brief Run checkers on end of function.
307 void runCheckersForEndFunction(NodeBuilderContext &BC,
308 ExplodedNodeSet &Dst,
312 /// \brief Run checkers for branch condition.
313 void runCheckersForBranchCondition(const Stmt *condition,
314 ExplodedNodeSet &Dst, ExplodedNode *Pred,
317 /// \brief Run checkers for live symbols.
319 /// Allows modifying SymbolReaper object. For example, checkers can explicitly
320 /// register symbols of interest as live. These symbols will not be marked
321 /// dead and removed.
322 void runCheckersForLiveSymbols(ProgramStateRef state,
323 SymbolReaper &SymReaper);
325 /// \brief Run checkers for dead symbols.
327 /// Notifies checkers when symbols become dead. For example, this allows
328 /// checkers to aggressively clean up/reduce the checker state and produce
329 /// precise diagnostics.
330 void runCheckersForDeadSymbols(ExplodedNodeSet &Dst,
331 const ExplodedNodeSet &Src,
332 SymbolReaper &SymReaper, const Stmt *S,
334 ProgramPoint::Kind K);
336 /// \brief True if at least one checker wants to check region changes.
337 bool wantsRegionChangeUpdate(ProgramStateRef state);
339 /// \brief Run checkers for region changes.
341 /// This corresponds to the check::RegionChanges callback.
342 /// \param state The current program state.
343 /// \param invalidated A set of all symbols potentially touched by the change.
344 /// \param ExplicitRegions The regions explicitly requested for invalidation.
345 /// For example, in the case of a function call, these would be arguments.
346 /// \param Regions The transitive closure of accessible regions,
347 /// i.e. all regions that may have been touched by this change.
348 /// \param Call The call expression wrapper if the regions are invalidated
351 runCheckersForRegionChanges(ProgramStateRef state,
352 const InvalidatedSymbols *invalidated,
353 ArrayRef<const MemRegion *> ExplicitRegions,
354 ArrayRef<const MemRegion *> Regions,
355 const CallEvent *Call);
357 /// \brief Run checkers when pointers escape.
359 /// This notifies the checkers about pointer escape, which occurs whenever
360 /// the analyzer cannot track the symbol any more. For example, as a
361 /// result of assigning a pointer into a global or when it's passed to a
362 /// function call the analyzer cannot model.
364 /// \param State The state at the point of escape.
365 /// \param Escaped The list of escaped symbols.
366 /// \param Call The corresponding CallEvent, if the symbols escape as
367 /// parameters to the given call.
368 /// \param Kind The reason of pointer escape.
369 /// \param ITraits Information about invalidation for a particular
371 /// \returns Checkers can modify the state by returning a new one.
373 runCheckersForPointerEscape(ProgramStateRef State,
374 const InvalidatedSymbols &Escaped,
375 const CallEvent *Call,
376 PointerEscapeKind Kind,
377 RegionAndSymbolInvalidationTraits *ITraits);
379 /// \brief Run checkers for handling assumptions on symbolic values.
380 ProgramStateRef runCheckersForEvalAssume(ProgramStateRef state,
381 SVal Cond, bool Assumption);
383 /// \brief Run checkers for evaluating a call.
385 /// Warning: Currently, the CallEvent MUST come from a CallExpr!
386 void runCheckersForEvalCall(ExplodedNodeSet &Dst,
387 const ExplodedNodeSet &Src,
388 const CallEvent &CE, ExprEngine &Eng);
390 /// \brief Run checkers for the entire Translation Unit.
391 void runCheckersOnEndOfTranslationUnit(const TranslationUnitDecl *TU,
392 AnalysisManager &mgr,
395 /// \brief Run checkers for debug-printing a ProgramState.
397 /// Unlike most other callbacks, any checker can simply implement the virtual
398 /// method CheckerBase::printState if it has custom data to print.
399 /// \param Out The output stream
400 /// \param State The state being printed
401 /// \param NL The preferred representation of a newline.
402 /// \param Sep The preferred separator between different kinds of data.
403 void runCheckersForPrintState(raw_ostream &Out, ProgramStateRef State,
404 const char *NL, const char *Sep);
406 //===----------------------------------------------------------------------===//
407 // Internal registration functions for AST traversing.
408 //===----------------------------------------------------------------------===//
410 // Functions used by the registration mechanism, checkers should not touch
413 typedef CheckerFn<void (const Decl *, AnalysisManager&, BugReporter &)>
416 typedef bool (*HandlesDeclFunc)(const Decl *D);
417 void _registerForDecl(CheckDeclFunc checkfn, HandlesDeclFunc isForDeclFn);
419 void _registerForBody(CheckDeclFunc checkfn);
421 //===----------------------------------------------------------------------===//
422 // Internal registration functions for path-sensitive checking.
423 //===----------------------------------------------------------------------===//
425 typedef CheckerFn<void (const Stmt *, CheckerContext &)> CheckStmtFunc;
427 typedef CheckerFn<void (const ObjCMethodCall &, CheckerContext &)>
428 CheckObjCMessageFunc;
430 typedef CheckerFn<void (const CallEvent &, CheckerContext &)>
433 typedef CheckerFn<void (const SVal &location, bool isLoad,
438 typedef CheckerFn<void (const SVal &location, const SVal &val,
439 const Stmt *S, CheckerContext &)>
442 typedef CheckerFn<void (ExplodedGraph &, BugReporter &, ExprEngine &)>
443 CheckEndAnalysisFunc;
445 typedef CheckerFn<void (CheckerContext &)>
446 CheckEndFunctionFunc;
448 typedef CheckerFn<void (const Stmt *, CheckerContext &)>
449 CheckBranchConditionFunc;
451 typedef CheckerFn<void (SymbolReaper &, CheckerContext &)>
452 CheckDeadSymbolsFunc;
454 typedef CheckerFn<void (ProgramStateRef,SymbolReaper &)> CheckLiveSymbolsFunc;
456 typedef CheckerFn<ProgramStateRef (ProgramStateRef,
457 const InvalidatedSymbols *symbols,
458 ArrayRef<const MemRegion *> ExplicitRegions,
459 ArrayRef<const MemRegion *> Regions,
460 const CallEvent *Call)>
461 CheckRegionChangesFunc;
463 typedef CheckerFn<bool (ProgramStateRef)> WantsRegionChangeUpdateFunc;
465 typedef CheckerFn<ProgramStateRef (ProgramStateRef,
466 const InvalidatedSymbols &Escaped,
467 const CallEvent *Call,
468 PointerEscapeKind Kind,
469 RegionAndSymbolInvalidationTraits *ITraits)>
470 CheckPointerEscapeFunc;
472 typedef CheckerFn<ProgramStateRef (ProgramStateRef,
473 const SVal &cond, bool assumption)>
476 typedef CheckerFn<bool (const CallExpr *, CheckerContext &)>
479 typedef CheckerFn<void (const TranslationUnitDecl *,
480 AnalysisManager&, BugReporter &)>
481 CheckEndOfTranslationUnit;
483 typedef bool (*HandlesStmtFunc)(const Stmt *D);
484 void _registerForPreStmt(CheckStmtFunc checkfn,
485 HandlesStmtFunc isForStmtFn);
486 void _registerForPostStmt(CheckStmtFunc checkfn,
487 HandlesStmtFunc isForStmtFn);
489 void _registerForPreObjCMessage(CheckObjCMessageFunc checkfn);
490 void _registerForPostObjCMessage(CheckObjCMessageFunc checkfn);
492 void _registerForPreCall(CheckCallFunc checkfn);
493 void _registerForPostCall(CheckCallFunc checkfn);
495 void _registerForLocation(CheckLocationFunc checkfn);
497 void _registerForBind(CheckBindFunc checkfn);
499 void _registerForEndAnalysis(CheckEndAnalysisFunc checkfn);
501 void _registerForEndFunction(CheckEndFunctionFunc checkfn);
503 void _registerForBranchCondition(CheckBranchConditionFunc checkfn);
505 void _registerForLiveSymbols(CheckLiveSymbolsFunc checkfn);
507 void _registerForDeadSymbols(CheckDeadSymbolsFunc checkfn);
509 void _registerForRegionChanges(CheckRegionChangesFunc checkfn,
510 WantsRegionChangeUpdateFunc wantUpdateFn);
512 void _registerForPointerEscape(CheckPointerEscapeFunc checkfn);
514 void _registerForConstPointerEscape(CheckPointerEscapeFunc checkfn);
516 void _registerForEvalAssume(EvalAssumeFunc checkfn);
518 void _registerForEvalCall(EvalCallFunc checkfn);
520 void _registerForEndOfTranslationUnit(CheckEndOfTranslationUnit checkfn);
522 //===----------------------------------------------------------------------===//
523 // Internal registration functions for events.
524 //===----------------------------------------------------------------------===//
526 typedef void *EventTag;
527 typedef CheckerFn<void (const void *event)> CheckEventFunc;
529 template <typename EVENT>
530 void _registerListenerForEvent(CheckEventFunc checkfn) {
531 EventInfo &info = Events[getTag<EVENT>()];
532 info.Checkers.push_back(checkfn);
535 template <typename EVENT>
536 void _registerDispatcherForEvent() {
537 EventInfo &info = Events[getTag<EVENT>()];
538 info.HasDispatcher = true;
541 template <typename EVENT>
542 void _dispatchEvent(const EVENT &event) const {
543 EventsTy::const_iterator I = Events.find(getTag<EVENT>());
544 if (I == Events.end())
546 const EventInfo &info = I->second;
547 for (unsigned i = 0, e = info.Checkers.size(); i != e; ++i)
548 info.Checkers[i](&event);
551 //===----------------------------------------------------------------------===//
552 // Implementation details.
553 //===----------------------------------------------------------------------===//
556 template <typename CHECKER>
557 static void destruct(void *obj) { delete static_cast<CHECKER *>(obj); }
559 template <typename T>
560 static void *getTag() { static int tag; return &tag; }
562 llvm::DenseMap<CheckerTag, CheckerRef> CheckerTags;
564 std::vector<CheckerDtor> CheckerDtors;
566 struct DeclCheckerInfo {
567 CheckDeclFunc CheckFn;
568 HandlesDeclFunc IsForDeclFn;
570 std::vector<DeclCheckerInfo> DeclCheckers;
572 std::vector<CheckDeclFunc> BodyCheckers;
574 typedef SmallVector<CheckDeclFunc, 4> CachedDeclCheckers;
575 typedef llvm::DenseMap<unsigned, CachedDeclCheckers> CachedDeclCheckersMapTy;
576 CachedDeclCheckersMapTy CachedDeclCheckersMap;
578 struct StmtCheckerInfo {
579 CheckStmtFunc CheckFn;
580 HandlesStmtFunc IsForStmtFn;
583 std::vector<StmtCheckerInfo> StmtCheckers;
585 typedef SmallVector<CheckStmtFunc, 4> CachedStmtCheckers;
586 typedef llvm::DenseMap<unsigned, CachedStmtCheckers> CachedStmtCheckersMapTy;
587 CachedStmtCheckersMapTy CachedStmtCheckersMap;
589 const CachedStmtCheckers &getCachedStmtCheckersFor(const Stmt *S,
592 std::vector<CheckObjCMessageFunc> PreObjCMessageCheckers;
593 std::vector<CheckObjCMessageFunc> PostObjCMessageCheckers;
595 std::vector<CheckCallFunc> PreCallCheckers;
596 std::vector<CheckCallFunc> PostCallCheckers;
598 std::vector<CheckLocationFunc> LocationCheckers;
600 std::vector<CheckBindFunc> BindCheckers;
602 std::vector<CheckEndAnalysisFunc> EndAnalysisCheckers;
604 std::vector<CheckEndFunctionFunc> EndFunctionCheckers;
606 std::vector<CheckBranchConditionFunc> BranchConditionCheckers;
608 std::vector<CheckLiveSymbolsFunc> LiveSymbolsCheckers;
610 std::vector<CheckDeadSymbolsFunc> DeadSymbolsCheckers;
612 struct RegionChangesCheckerInfo {
613 CheckRegionChangesFunc CheckFn;
614 WantsRegionChangeUpdateFunc WantUpdateFn;
616 std::vector<RegionChangesCheckerInfo> RegionChangesCheckers;
618 std::vector<CheckPointerEscapeFunc> PointerEscapeCheckers;
620 std::vector<EvalAssumeFunc> EvalAssumeCheckers;
622 std::vector<EvalCallFunc> EvalCallCheckers;
624 std::vector<CheckEndOfTranslationUnit> EndOfTranslationUnitCheckers;
627 SmallVector<CheckEventFunc, 4> Checkers;
629 EventInfo() : HasDispatcher(false) { }
632 typedef llvm::DenseMap<EventTag, EventInfo> EventsTy;
636 } // end ento namespace
638 } // end clang namespace