Path: blob/main/contrib/llvm-project/clang/lib/Analysis/plugins/SampleAnalyzer/MainCallChecker.cpp
35293 views
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"1#include "clang/StaticAnalyzer/Core/Checker.h"2#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"3#include "clang/StaticAnalyzer/Frontend/CheckerRegistry.h"45using namespace clang;6using namespace ento;78namespace {9class MainCallChecker : public Checker<check::PreStmt<CallExpr>> {10mutable std::unique_ptr<BugType> BT;1112public:13void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;14};15} // end anonymous namespace1617void MainCallChecker::checkPreStmt(const CallExpr *CE,18CheckerContext &C) const {19const Expr *Callee = CE->getCallee();20const FunctionDecl *FD = C.getSVal(Callee).getAsFunctionDecl();2122if (!FD)23return;2425// Get the name of the callee.26IdentifierInfo *II = FD->getIdentifier();27if (!II) // if no identifier, not a simple C function28return;2930if (II->isStr("main")) {31ExplodedNode *N = C.generateErrorNode();32if (!N)33return;3435if (!BT)36BT.reset(new BugType(this, "call to main", "example analyzer plugin"));3738auto report =39std::make_unique<PathSensitiveBugReport>(*BT, BT->getDescription(), N);40report->addRange(Callee->getSourceRange());41C.emitReport(std::move(report));42}43}4445// Register plugin!46extern "C" void clang_registerCheckers(CheckerRegistry ®istry) {47registry.addChecker<MainCallChecker>(48"example.MainCallChecker", "Disallows calls to functions called main",49"");50}5152extern "C" const char clang_analyzerAPIVersionString[] =53CLANG_ANALYZER_API_VERSION_STRING;545556