25 #include "llvm/ADT/SmallVector.h" 26 #include "llvm/Support/Casting.h" 27 #include "llvm/Support/ErrorHandling.h" 31 using namespace clang;
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();
56 for (
const auto &Event : Events)
57 assert(Event.second.HasDispatcher &&
58 "No dispatcher registered for an event");
70 unsigned DeclKind = D->
getKind();
72 CachedDeclCheckersMapTy::iterator CCI = CachedDeclCheckersMap.find(DeclKind);
73 if (CCI != CachedDeclCheckersMap.end()) {
74 checkers = &(CCI->second);
77 checkers = &CachedDeclCheckersMap[DeclKind];
78 for (
const auto &info : DeclCheckers)
79 if (info.IsForDeclFn(D))
80 checkers->push_back(info.CheckFn);
84 for (
const auto checker : *checkers)
92 for (
const auto BodyChecker : BodyCheckers)
93 BodyChecker(D, mgr, BR);
100 template <
typename CHECK_CTX>
108 typename CHECK_CTX::CheckersTy::const_iterator
109 I = checkCtx.checkers_begin(), E = checkCtx.checkers_end();
118 for (; I != E; ++I) {
123 CurrSet = (PrevSet == &Tmp1) ? &Tmp2 : &Tmp1;
128 for (
const auto &NI : *PrevSet)
129 checkCtx.runChecker(*I, B, NI);
132 if (CurrSet->
empty())
142 struct CheckStmtContext {
146 const CheckersTy &Checkers;
151 CheckStmtContext(
bool isPreVisit,
const CheckersTy &checkers,
153 : IsPreVisit(isPreVisit), Checkers(checkers), S(s), Eng(eng),
154 WasInlined(wasInlined) {}
156 CheckersTy::const_iterator checkers_begin() {
return Checkers.begin(); }
157 CheckersTy::const_iterator checkers_end() {
return Checkers.end(); }
180 CheckStmtContext C(isPreVisit, getCachedStmtCheckersFor(S, isPreVisit),
187 struct CheckObjCMessageContext {
188 using CheckersTy = std::vector<CheckerManager::CheckObjCMessageFunc>;
192 const CheckersTy &Checkers;
197 const CheckersTy &checkers,
200 :
Kind(visitKind), WasInlined(wasInlined), Checkers(checkers), Msg(msg),
203 CheckersTy::const_iterator checkers_begin() {
return Checkers.begin(); }
204 CheckersTy::const_iterator checkers_end() {
return Checkers.end(); }
220 const ProgramPoint &L = Msg.getProgramPoint(IsPreVisit,checkFn.Checker);
236 auto &checkers = getObjCMessageCheckers(visitKind);
237 CheckObjCMessageContext C(visitKind, checkers, msg, Eng, WasInlined);
241 const std::vector<CheckerManager::CheckObjCMessageFunc> &
245 return PreObjCMessageCheckers;
248 return PostObjCMessageCheckers;
250 return ObjCMessageNilCheckers;
252 llvm_unreachable(
"Unknown Kind");
259 struct CheckCallContext {
260 using CheckersTy = std::vector<CheckerManager::CheckCallFunc>;
262 bool IsPreVisit, WasInlined;
263 const CheckersTy &Checkers;
267 CheckCallContext(
bool isPreVisit,
const CheckersTy &checkers,
270 : IsPreVisit(isPreVisit), WasInlined(wasInlined), Checkers(checkers),
271 Call(call), Eng(eng) {}
273 CheckersTy::const_iterator checkers_begin() {
return Checkers.begin(); }
274 CheckersTy::const_iterator checkers_end() {
return Checkers.end(); }
294 CheckCallContext C(isPreVisit,
295 isPreVisit ? PreCallCheckers
297 Call, Eng, WasInlined);
303 struct CheckLocationContext {
304 using CheckersTy = std::vector<CheckerManager::CheckLocationFunc>;
306 const CheckersTy &Checkers;
313 CheckLocationContext(
const CheckersTy &checkers,
314 SVal loc,
bool isLoad,
const Stmt *NodeEx,
317 : Checkers(checkers), Loc(loc), IsLoad(isLoad), NodeEx(NodeEx),
318 BoundEx(BoundEx), Eng(eng) {}
320 CheckersTy::const_iterator checkers_begin() {
return Checkers.begin(); }
321 CheckersTy::const_iterator checkers_end() {
return Checkers.end(); }
332 checkFn(Loc, IsLoad, BoundEx, C);
342 SVal location,
bool isLoad,
346 CheckLocationContext C(LocationCheckers, location, isLoad, NodeEx,
353 struct CheckBindContext {
354 using CheckersTy = std::vector<CheckerManager::CheckBindFunc>;
356 const CheckersTy &Checkers;
363 CheckBindContext(
const CheckersTy &checkers,
366 : Checkers(checkers), Loc(loc), Val(val), S(s), Eng(eng), PP(pp) {}
368 CheckersTy::const_iterator checkers_begin() {
return Checkers.begin(); }
369 CheckersTy::const_iterator checkers_end() {
return Checkers.end(); }
376 checkFn(Loc, Val, S, C);
388 CheckBindContext C(BindCheckers, location, val, S, Eng, PP);
395 for (
const auto EndAnalysisChecker : EndAnalysisCheckers)
396 EndAnalysisChecker(G, BR, Eng);
401 struct CheckBeginFunctionContext {
402 using CheckersTy = std::vector<CheckerManager::CheckBeginFunctionFunc>;
404 const CheckersTy &Checkers;
408 CheckBeginFunctionContext(
const CheckersTy &Checkers,
ExprEngine &Eng,
410 : Checkers(Checkers), Eng(Eng), PP(PP) {}
412 CheckersTy::const_iterator checkers_begin() {
return Checkers.begin(); }
413 CheckersTy::const_iterator checkers_end() {
return Checkers.end(); }
432 CheckBeginFunctionContext C(BeginFunctionCheckers, Eng, L);
448 for (
const auto checkFn : EndFunctionCheckers) {
459 struct CheckBranchConditionContext {
460 using CheckersTy = std::vector<CheckerManager::CheckBranchConditionFunc>;
462 const CheckersTy &Checkers;
463 const Stmt *Condition;
466 CheckBranchConditionContext(
const CheckersTy &checkers,
468 : Checkers(checkers), Condition(Cond), Eng(eng) {}
470 CheckersTy::const_iterator checkers_begin() {
return Checkers.begin(); }
471 CheckersTy::const_iterator checkers_end() {
return Checkers.end(); }
478 checkFn(Condition, C);
491 CheckBranchConditionContext C(BranchConditionCheckers, Condition, Eng);
497 struct CheckNewAllocatorContext {
498 using CheckersTy = std::vector<CheckerManager::CheckNewAllocatorFunc>;
500 const CheckersTy &Checkers;
506 CheckNewAllocatorContext(
const CheckersTy &Checkers,
const CXXNewExpr *NE,
508 : Checkers(Checkers), NE(NE),
Target(Target), WasInlined(WasInlined),
511 CheckersTy::const_iterator checkers_begin() {
return Checkers.begin(); }
512 CheckersTy::const_iterator checkers_end() {
return Checkers.end(); }
518 checkFn(NE, Target, C);
529 CheckNewAllocatorContext C(NewAllocatorCheckers, NE, Target, WasInlined, Eng);
536 for (
const auto LiveSymbolsChecker : LiveSymbolsCheckers)
537 LiveSymbolsChecker(state, SymReaper);
542 struct CheckDeadSymbolsContext {
543 using CheckersTy = std::vector<CheckerManager::CheckDeadSymbolsFunc>;
545 const CheckersTy &Checkers;
551 CheckDeadSymbolsContext(
const CheckersTy &checkers,
SymbolReaper &sr,
554 : Checkers(checkers), SR(sr), S(s), Eng(eng), ProgarmPointKind(K) {}
556 CheckersTy::const_iterator checkers_begin() {
return Checkers.begin(); }
557 CheckersTy::const_iterator checkers_end() {
return Checkers.end(); }
581 CheckDeadSymbolsContext C(DeadSymbolsCheckers, SymReaper, S, Eng, K);
593 for (
const auto RegionChangesChecker : RegionChangesCheckers) {
598 state = RegionChangesChecker(state, invalidated, ExplicitRegions, Regions,
611 assert((Call !=
nullptr ||
614 "Call must not be NULL when escaping on call");
615 for (
const auto PointerEscapeChecker : PointerEscapeCheckers) {
620 State = PointerEscapeChecker(State, Escaped, Call, Kind, ETraits);
628 SVal Cond,
bool Assumption) {
629 for (
const auto EvalAssumeChecker : EvalAssumeCheckers) {
634 state = EvalAssumeChecker(state, Cond, Assumption);
646 for (
const auto Pred : Src) {
647 bool anyEvaluated =
false;
653 for (
const auto EvalCallChecker : EvalCallCheckers) {
657 EvalCallChecker.Checker);
658 bool evaluated =
false;
663 evaluated = EvalCallChecker(CE, C);
665 assert(!(evaluated && anyEvaluated)
666 &&
"There are more than one checkers evaluating the call");
689 for (
const auto EndOfTranslationUnitChecker : EndOfTranslationUnitCheckers)
690 EndOfTranslationUnitChecker(TU, mgr, BR);
695 const char *NL,
const char *Sep) {
697 CheckerTag.second->printState(Out, State, NL, Sep);
706 DeclCheckerInfo info = { checkfn, isForDeclFn };
707 DeclCheckers.push_back(info);
711 BodyCheckers.push_back(checkfn);
720 StmtCheckerInfo info = { checkfn, isForStmtFn,
true };
721 StmtCheckers.push_back(info);
726 StmtCheckerInfo info = { checkfn, isForStmtFn,
false };
727 StmtCheckers.push_back(info);
731 PreObjCMessageCheckers.push_back(checkfn);
735 ObjCMessageNilCheckers.push_back(checkfn);
739 PostObjCMessageCheckers.push_back(checkfn);
743 PreCallCheckers.push_back(checkfn);
746 PostCallCheckers.push_back(checkfn);
750 LocationCheckers.push_back(checkfn);
754 BindCheckers.push_back(checkfn);
758 EndAnalysisCheckers.push_back(checkfn);
762 BeginFunctionCheckers.push_back(checkfn);
766 EndFunctionCheckers.push_back(checkfn);
771 BranchConditionCheckers.push_back(checkfn);
775 NewAllocatorCheckers.push_back(checkfn);
779 LiveSymbolsCheckers.push_back(checkfn);
783 DeadSymbolsCheckers.push_back(checkfn);
787 RegionChangesCheckers.push_back(checkfn);
791 PointerEscapeCheckers.push_back(checkfn);
796 PointerEscapeCheckers.push_back(checkfn);
800 EvalAssumeCheckers.push_back(checkfn);
804 EvalCallCheckers.push_back(checkfn);
809 EndOfTranslationUnitCheckers.push_back(checkfn);
817 CheckerManager::getCachedStmtCheckersFor(
const Stmt *S,
bool isPreVisit) {
820 unsigned Key = (S->
getStmtClass() << 1) |
unsigned(isPreVisit);
821 CachedStmtCheckersMapTy::iterator CCI = CachedStmtCheckersMap.find(Key);
822 if (CCI != CachedStmtCheckersMap.end())
827 for (
const auto &Info : StmtCheckers)
828 if (Info.IsPreVisit == isPreVisit && Info.IsForStmtFn(S))
829 Checkers.push_back(Info.CheckFn);
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.
Information about invalidation for a particular region/symbol.
ProgramPoint getProgramPoint(bool IsPreVisit=false, const ProgramPointTag *Tag=nullptr) const
Returns an appropriate ProgramPoint for this call.
static void expandGraphWithCheckers(CHECK_CTX checkCtx, ExplodedNodeSet &Dst, const ExplodedNodeSet &Src)
Decl - This represents one declaration (or definition), e.g.
ProgramPoint withTag(const ProgramPointTag *tag) const
Create a new ProgramPoint object that is the same as the original except for using the specified tag ...
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.
void _registerForRegionChanges(CheckRegionChangesFunc checkfn)
void _registerForObjCMessageNil(CheckObjCMessageFunc checkfn)
void finishedCheckerRegistration()
const NodeBuilderContext & getBuilderContext()
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)
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.
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.
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;...
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...
BugReporter is a utility class for generating PathDiagnostics for analysis.
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)
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)"...
SVal - This represents a symbolic expression, which can be either an L-value or an R-value...
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
void runCheckersForStmt(bool isPreVisit, ExplodedNodeSet &Dst, const ExplodedNodeSet &Src, const Stmt *S, ExprEngine &Eng, bool wasInlined=false)
Run checkers for visiting Stmts.
void _registerForDecl(CheckDeclFunc checkfn, HandlesDeclFunc isForDeclFn)
void _registerForPostObjCMessage(CheckObjCMessageFunc checkfn)
Represents an abstract call to a function or method along a particular path.
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
bool hasPathSensitiveCheckers() const
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
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.
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)