1f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/Checker.h"
2f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
3f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/CheckerRegistry.h"
4f4a2713aSLionel Sambuc #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
5f4a2713aSLionel Sambuc
6f4a2713aSLionel Sambuc using namespace clang;
7f4a2713aSLionel Sambuc using namespace ento;
8f4a2713aSLionel Sambuc
9f4a2713aSLionel Sambuc namespace {
10f4a2713aSLionel Sambuc class MainCallChecker : public Checker < check::PreStmt<CallExpr> > {
11*0a6a1f1dSLionel Sambuc mutable std::unique_ptr<BugType> BT;
12f4a2713aSLionel Sambuc
13f4a2713aSLionel Sambuc public:
14f4a2713aSLionel Sambuc void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
15f4a2713aSLionel Sambuc };
16f4a2713aSLionel Sambuc } // end anonymous namespace
17f4a2713aSLionel Sambuc
checkPreStmt(const CallExpr * CE,CheckerContext & C) const18f4a2713aSLionel Sambuc void MainCallChecker::checkPreStmt(const CallExpr *CE, CheckerContext &C) const {
19f4a2713aSLionel Sambuc const ProgramStateRef state = C.getState();
20f4a2713aSLionel Sambuc const LocationContext *LC = C.getLocationContext();
21f4a2713aSLionel Sambuc const Expr *Callee = CE->getCallee();
22f4a2713aSLionel Sambuc const FunctionDecl *FD = state->getSVal(Callee, LC).getAsFunctionDecl();
23f4a2713aSLionel Sambuc
24f4a2713aSLionel Sambuc if (!FD)
25f4a2713aSLionel Sambuc return;
26f4a2713aSLionel Sambuc
27f4a2713aSLionel Sambuc // Get the name of the callee.
28f4a2713aSLionel Sambuc IdentifierInfo *II = FD->getIdentifier();
29f4a2713aSLionel Sambuc if (!II) // if no identifier, not a simple C function
30f4a2713aSLionel Sambuc return;
31f4a2713aSLionel Sambuc
32f4a2713aSLionel Sambuc if (II->isStr("main")) {
33f4a2713aSLionel Sambuc ExplodedNode *N = C.generateSink();
34f4a2713aSLionel Sambuc if (!N)
35f4a2713aSLionel Sambuc return;
36f4a2713aSLionel Sambuc
37f4a2713aSLionel Sambuc if (!BT)
38*0a6a1f1dSLionel Sambuc BT.reset(new BugType(this, "call to main", "example analyzer plugin"));
39f4a2713aSLionel Sambuc
40f4a2713aSLionel Sambuc BugReport *report = new BugReport(*BT, BT->getName(), N);
41f4a2713aSLionel Sambuc report->addRange(Callee->getSourceRange());
42f4a2713aSLionel Sambuc C.emitReport(report);
43f4a2713aSLionel Sambuc }
44f4a2713aSLionel Sambuc }
45f4a2713aSLionel Sambuc
46f4a2713aSLionel Sambuc // Register plugin!
47f4a2713aSLionel Sambuc extern "C"
clang_registerCheckers(CheckerRegistry & registry)48f4a2713aSLionel Sambuc void clang_registerCheckers (CheckerRegistry ®istry) {
49f4a2713aSLionel Sambuc registry.addChecker<MainCallChecker>("example.MainCallChecker", "Disallows calls to functions called main");
50f4a2713aSLionel Sambuc }
51f4a2713aSLionel Sambuc
52f4a2713aSLionel Sambuc extern "C"
53f4a2713aSLionel Sambuc const char clang_analyzerAPIVersionString[] = CLANG_ANALYZER_API_VERSION_STRING;
54