Files
clang-p2996/clang/examples/analyzer-plugin/MainCallChecker.cpp
Alexander Kornienko 4aca9b1cd8 Expose the name of the checker producing each diagnostic message.
Summary:
In clang-tidy we'd like to know the name of the checker producing each
diagnostic message. PathDiagnostic has BugType and Category fields, which are
both arbitrary human-readable strings, but we need to know the exact name of the
checker in the form that can be used in the CheckersControlList option to
enable/disable the specific checker.

This patch adds the CheckName field to the CheckerBase class, and sets it in
the CheckerManager::registerChecker() method, which gets them from the
CheckerRegistry.

Checkers that implement multiple checks have to store the names of each check
in the respective registerXXXChecker method.

Reviewers: jordan_rose, krememek

Reviewed By: jordan_rose

CC: cfe-commits

Differential Revision: http://llvm-reviews.chandlerc.com/D2557

llvm-svn: 201186
2014-02-11 21:49:21 +00:00

54 lines
1.6 KiB
C++

#include "clang/StaticAnalyzer/Core/Checker.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
#include "clang/StaticAnalyzer/Core/CheckerRegistry.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
using namespace clang;
using namespace ento;
namespace {
class MainCallChecker : public Checker < check::PreStmt<CallExpr> > {
mutable OwningPtr<BugType> BT;
public:
void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
};
} // end anonymous namespace
void MainCallChecker::checkPreStmt(const CallExpr *CE, CheckerContext &C) const {
const ProgramStateRef state = C.getState();
const LocationContext *LC = C.getLocationContext();
const Expr *Callee = CE->getCallee();
const FunctionDecl *FD = state->getSVal(Callee, LC).getAsFunctionDecl();
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)
BT.reset(new BugType(this, "call to main", "example analyzer plugin"));
BugReport *report = new BugReport(*BT, BT->getName(), N);
report->addRange(Callee->getSourceRange());
C.emitReport(report);
}
}
// Register plugin!
extern "C"
void clang_registerCheckers (CheckerRegistry &registry) {
registry.addChecker<MainCallChecker>("example.MainCallChecker", "Disallows calls to functions called main");
}
extern "C"
const char clang_analyzerAPIVersionString[] = CLANG_ANALYZER_API_VERSION_STRING;