diff options
author | Jordy Rose <jediknil@belkadan.com> | 2011-08-17 01:30:59 +0000 |
---|---|---|
committer | Jordy Rose <jediknil@belkadan.com> | 2011-08-17 01:30:59 +0000 |
commit | 93b86e494d99db21c217e2ca447380911559707a (patch) | |
tree | 46bc9f686c59198077790f84b97df285e368910e /clang/examples/analyzer-plugin/MainCallChecker.cpp | |
parent | 99f0b8f9351889901fbe7fd16104543bd4c1bbf1 (diff) | |
download | bcm5719-llvm-93b86e494d99db21c217e2ca447380911559707a.tar.gz bcm5719-llvm-93b86e494d99db21c217e2ca447380911559707a.zip |
[analyzer] Add basic support for pluggable checkers.
llvm-svn: 137802
Diffstat (limited to 'clang/examples/analyzer-plugin/MainCallChecker.cpp')
-rw-r--r-- | clang/examples/analyzer-plugin/MainCallChecker.cpp | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/clang/examples/analyzer-plugin/MainCallChecker.cpp b/clang/examples/analyzer-plugin/MainCallChecker.cpp new file mode 100644 index 00000000000..bf753899c21 --- /dev/null +++ b/clang/examples/analyzer-plugin/MainCallChecker.cpp @@ -0,0 +1,52 @@ +#include "clang/StaticAnalyzer/Core/Checker.h" +#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" +#include "clang/StaticAnalyzer/Core/CheckerRegistry.h" +#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" + +using namespace clang; +using namespace ento; + +namespace { +class MainCallChecker : public Checker < check::PreStmt<CallExpr> > { + mutable llvm::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 ProgramState *state = C.getState(); + const Expr *Callee = CE->getCallee(); + const FunctionDecl *FD = state->getSVal(Callee).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 BuiltinBug("call to main")); + + RangedBugReport *report = new RangedBugReport(*BT, BT->getName(), N); + report->addRange(Callee->getSourceRange()); + C.EmitReport(report); + } +} + +// Register plugin! +extern "C" +void clang_registerCheckers (CheckerRegistry ®istry) { + registry.addChecker<MainCallChecker>("example.MainCallChecker", "Disallows calls to functions called main"); +} + +extern "C" +const char clang_analyzerAPIVersionString[] = CLANG_ANALYZER_API_VERSION_STRING; |