blob: a720bb964707840a9e79defffd31e4befa34886c [file] [log] [blame]
Jordy Rose77a33a72011-08-17 01:30:59 +00001#include "clang/StaticAnalyzer/Core/Checker.h"
2#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
3#include "clang/StaticAnalyzer/Core/CheckerRegistry.h"
4#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
5
6using namespace clang;
7using namespace ento;
8
9namespace {
10class MainCallChecker : public Checker < check::PreStmt<CallExpr> > {
11 mutable llvm::OwningPtr<BugType> BT;
12
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 {
19 const ProgramState *state = C.getState();
20 const Expr *Callee = CE->getCallee();
21 const FunctionDecl *FD = state->getSVal(Callee).getAsFunctionDecl();
22
23 if (!FD)
24 return;
25
26 // Get the name of the callee.
27 IdentifierInfo *II = FD->getIdentifier();
28 if (!II) // if no identifier, not a simple C function
29 return;
30
31 if (II->isStr("main")) {
32 ExplodedNode *N = C.generateSink();
33 if (!N)
34 return;
35
36 if (!BT)
Jordy Rose26fb4cb2011-08-17 03:23:51 +000037 BT.reset(new BugType("call to main", "example analyzer plugin"));
Jordy Rose77a33a72011-08-17 01:30:59 +000038
39 RangedBugReport *report = new RangedBugReport(*BT, BT->getName(), N);
40 report->addRange(Callee->getSourceRange());
41 C.EmitReport(report);
42 }
43}
44
45// Register plugin!
46extern "C"
47void clang_registerCheckers (CheckerRegistry &registry) {
48 registry.addChecker<MainCallChecker>("example.MainCallChecker", "Disallows calls to functions called main");
49}
50
51extern "C"
52const char clang_analyzerAPIVersionString[] = CLANG_ANALYZER_API_VERSION_STRING;