clang 22.0.0git
MainCallChecker.cpp
Go to the documentation of this file.
5
6// This simple plugin is used by clang/test/Analysis/checker-plugins.c
7// to test the use of a checker that is defined in a plugin.
8
9using namespace clang;
10using namespace ento;
11
12namespace {
13class MainCallChecker : public Checker<check::PreStmt<CallExpr>> {
14
15 const BugType BT{this, "call to main", "example analyzer plugin"};
16
17public:
18 void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
19};
20} // end anonymous namespace
21
22void MainCallChecker::checkPreStmt(const CallExpr *CE,
23 CheckerContext &C) const {
24 const Expr *Callee = CE->getCallee();
25 const FunctionDecl *FD = C.getSVal(Callee).getAsFunctionDecl();
26
27 if (!FD)
28 return;
29
30 // Get the name of the callee.
31 IdentifierInfo *II = FD->getIdentifier();
32 if (!II) // if no identifier, not a simple C function
33 return;
34
35 if (II->isStr("main")) {
36 ExplodedNode *N = C.generateErrorNode();
37 if (!N)
38 return;
39
40 auto report =
41 std::make_unique<PathSensitiveBugReport>(BT, BT.getDescription(), N);
42 report->addRange(Callee->getSourceRange());
43 C.emitReport(std::move(report));
44 }
45}
46
47// Register plugin!
48extern "C" void clang_registerCheckers(CheckerRegistry &Registry) {
49 Registry.addChecker<MainCallChecker>("example.MainCallChecker",
50 "Example Description");
51}
52
53extern "C" const char clang_analyzerAPIVersionString[] =
#define CLANG_ANALYZER_API_VERSION_STRING
void clang_registerCheckers(CheckerRegistry &Registry)
const char clang_analyzerAPIVersionString[]
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2879
Expr * getCallee()
Definition: Expr.h:3026
This represents one expression.
Definition: Expr.h:112
Represents a function declaration or definition.
Definition: Decl.h:1999
One of these records is kept for each identifier that is lexed.
bool isStr(const char(&Str)[StrLen]) const
Return true if this is the identifier for the specified string.
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition: Decl.h:294
Manages a set of available checkers for running a static analysis.
void addChecker(RegisterCheckerFn Fn, ShouldRegisterFunction Sfn, StringRef FullName, StringRef Desc, StringRef DocsUri="NoDocsUri", bool IsHidden=false)
Adds a checker to the registry.
Simple checker classes that implement one frontend (i.e.
Definition: Checker.h:553
The JSON file list parser is used to communicate input to InstallAPI.