2011-08-17 09:30:59 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
2012-12-04 17:37:22 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerRegistry.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2011-08-17 09:30:59 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class MainCallChecker : public Checker < check::PreStmt<CallExpr> > {
|
2014-03-09 19:34:25 +08:00
|
|
|
mutable std::unique_ptr<BugType> BT;
|
2011-08-17 09:30:59 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
void MainCallChecker::checkPreStmt(const CallExpr *CE, CheckerContext &C) const {
|
2012-02-10 04:02:49 +08:00
|
|
|
const ProgramStateRef state = C.getState();
|
|
|
|
const LocationContext *LC = C.getLocationContext();
|
2011-08-17 09:30:59 +08:00
|
|
|
const Expr *Callee = CE->getCallee();
|
2012-02-10 04:02:49 +08:00
|
|
|
const FunctionDecl *FD = state->getSVal(Callee, LC).getAsFunctionDecl();
|
2011-08-17 09:30:59 +08:00
|
|
|
|
|
|
|
if (!FD)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get the name of the callee.
|
|
|
|
IdentifierInfo *II = FD->getIdentifier();
|
|
|
|
if (!II) // if no identifier, not a simple C function
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (II->isStr("main")) {
|
|
|
|
ExplodedNode *N = C.generateSink();
|
|
|
|
if (!N)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!BT)
|
2014-02-12 05:49:21 +08:00
|
|
|
BT.reset(new BugType(this, "call to main", "example analyzer plugin"));
|
2011-08-17 09:30:59 +08:00
|
|
|
|
2011-08-18 07:00:25 +08:00
|
|
|
BugReport *report = new BugReport(*BT, BT->getName(), N);
|
2011-08-17 09:30:59 +08:00
|
|
|
report->addRange(Callee->getSourceRange());
|
2012-11-02 10:04:01 +08:00
|
|
|
C.emitReport(report);
|
2011-08-17 09:30:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register plugin!
|
|
|
|
extern "C"
|
|
|
|
void clang_registerCheckers (CheckerRegistry ®istry) {
|
|
|
|
registry.addChecker<MainCallChecker>("example.MainCallChecker", "Disallows calls to functions called main");
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
const char clang_analyzerAPIVersionString[] = CLANG_ANALYZER_API_VERSION_STRING;
|