blob: 8801f9a158fa85706dda3f2a6a416e3cf1d035d9 [file] [log] [blame]
Jordy Rose77a33a72011-08-17 01:30:59 +00001#include "clang/StaticAnalyzer/Core/Checker.h"
Jordy Rose77a33a72011-08-17 01:30:59 +00002#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Chandler Carruthe0c6e932012-12-04 09:37:22 +00003#include "clang/StaticAnalyzer/Core/CheckerRegistry.h"
4#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Jordy Rose77a33a72011-08-17 01:30:59 +00005
6using namespace clang;
7using namespace ento;
8
9namespace {
10class MainCallChecker : public Checker < check::PreStmt<CallExpr> > {
Dylan Noblesmith1e4c01b2012-02-13 12:32:21 +000011 mutable OwningPtr<BugType> BT;
Jordy Rose77a33a72011-08-17 01:30:59 +000012
13public:
14 void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
15};
16} // end anonymous namespace
17
18void MainCallChecker::checkPreStmt(const CallExpr *CE, CheckerContext &C) const {
Dylan Noblesmith8f79e3f2012-02-09 20:02:49 +000019 const ProgramStateRef state = C.getState();
20 const LocationContext *LC = C.getLocationContext();
Jordy Rose77a33a72011-08-17 01:30:59 +000021 const Expr *Callee = CE->getCallee();
Dylan Noblesmith8f79e3f2012-02-09 20:02:49 +000022 const FunctionDecl *FD = state->getSVal(Callee, LC).getAsFunctionDecl();
Jordy Rose77a33a72011-08-17 01:30:59 +000023
24 if (!FD)
25 return;
26
27 // Get the name of the callee.
28 IdentifierInfo *II = FD->getIdentifier();
29 if (!II) // if no identifier, not a simple C function
30 return;
31
32 if (II->isStr("main")) {
33 ExplodedNode *N = C.generateSink();
34 if (!N)
35 return;
36
37 if (!BT)
Jordy Rose26fb4cb2011-08-17 03:23:51 +000038 BT.reset(new BugType("call to main", "example analyzer plugin"));
Jordy Rose77a33a72011-08-17 01:30:59 +000039
Anna Zakse172e8b2011-08-17 23:00:25 +000040 BugReport *report = new BugReport(*BT, BT->getName(), N);
Jordy Rose77a33a72011-08-17 01:30:59 +000041 report->addRange(Callee->getSourceRange());
NAKAMURA Takumi5b4379f2012-11-02 02:04:01 +000042 C.emitReport(report);
Jordy Rose77a33a72011-08-17 01:30:59 +000043 }
44}
45
46// Register plugin!
47extern "C"
48void clang_registerCheckers (CheckerRegistry &registry) {
49 registry.addChecker<MainCallChecker>("example.MainCallChecker", "Disallows calls to functions called main");
50}
51
52extern "C"
53const char clang_analyzerAPIVersionString[] = CLANG_ANALYZER_API_VERSION_STRING;