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