clang  9.0.0
MainCallChecker.cpp
Go to the documentation of this file.
5 
6 using namespace clang;
7 using namespace ento;
8 
9 namespace {
10 class MainCallChecker : public Checker<check::PreStmt<CallExpr>> {
11  mutable std::unique_ptr<BugType> BT;
12 
13 public:
14  void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
15 };
16 } // end anonymous namespace
17 
18 void MainCallChecker::checkPreStmt(const CallExpr *CE,
19  CheckerContext &C) const {
20  const Expr *Callee = CE->getCallee();
21  const FunctionDecl *FD = C.getSVal(Callee).getAsFunctionDecl();
22 
23  if (!FD)
24  return;
25 
26  // Get the name of the callee.
27  IdentifierInfo *II = FD->getIdentifier();
28  if (!II) // if no identifier, not a simple C function
29  return;
30 
31  if (II->isStr("main")) {
33  if (!N)
34  return;
35 
36  if (!BT)
37  BT.reset(new BugType(this, "call to main", "example analyzer plugin"));
38 
39  std::unique_ptr<BugReport> report =
40  llvm::make_unique<BugReport>(*BT, BT->getName(), N);
41  report->addRange(Callee->getSourceRange());
42  C.emitReport(std::move(report));
43  }
44 }
45 
46 // Register plugin!
47 extern "C" void clang_registerCheckers(CheckerRegistry &registry) {
48  registry.addChecker<MainCallChecker>(
49  "example.MainCallChecker", "Disallows calls to functions called main",
50  "");
51 }
52 
53 extern "C" const char clang_analyzerAPIVersionString[] =
Manages a set of available checkers for running a static analysis.
Represents a function declaration or definition.
Definition: Decl.h:1748
ExplodedNode * generateErrorNode(ProgramStateRef State=nullptr, const ProgramPointTag *Tag=nullptr)
Generate a transition to a node that will be used to report an error.
void addChecker(InitializationFunction Fn, ShouldRegisterFunction sfn, StringRef FullName, StringRef Desc, StringRef DocsUri, bool IsHidden)
Adds a checker to the registry.
#define CLANG_ANALYZER_API_VERSION_STRING
SVal getSVal(const Stmt *S) const
Get the value of arbitrary expressions at this point in the path.
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition: Decl.h:269
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.
This represents one expression.
Definition: Expr.h:108
void clang_registerCheckers(CheckerRegistry &registry)
Expr * getCallee()
Definition: Expr.h:2634
const char clang_analyzerAPIVersionString[]
void emitReport(std::unique_ptr< BugReport > R)
Emit the diagnostics report.
Dataflow Directional Tag Classes.
const FunctionDecl * getAsFunctionDecl() const
getAsFunctionDecl - If this SVal is a MemRegionVal and wraps a CodeTextRegion wrapping a FunctionDecl...
Definition: SVals.cpp:63
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition: Stmt.cpp:251
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2516