Jordy Rose | 77a33a7 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 1 | #include "clang/StaticAnalyzer/Core/Checker.h" |
| 2 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
| 3 | #include "clang/StaticAnalyzer/Core/CheckerRegistry.h" |
| 4 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
| 5 | |
| 6 | using namespace clang; |
| 7 | using namespace ento; |
| 8 | |
| 9 | namespace { |
| 10 | class MainCallChecker : public Checker < check::PreStmt<CallExpr> > { |
Dylan Noblesmith | 1e4c01b | 2012-02-13 12:32:21 +0000 | [diff] [blame] | 11 | mutable OwningPtr<BugType> BT; |
Jordy Rose | 77a33a7 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 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, CheckerContext &C) const { |
Dylan Noblesmith | 8f79e3f | 2012-02-09 20:02:49 +0000 | [diff] [blame] | 19 | const ProgramStateRef state = C.getState(); |
| 20 | const LocationContext *LC = C.getLocationContext(); |
Jordy Rose | 77a33a7 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 21 | const Expr *Callee = CE->getCallee(); |
Dylan Noblesmith | 8f79e3f | 2012-02-09 20:02:49 +0000 | [diff] [blame] | 22 | const FunctionDecl *FD = state->getSVal(Callee, LC).getAsFunctionDecl(); |
Jordy Rose | 77a33a7 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 23 | |
| 24 | if (!FD) |
| 25 | return; |
| 26 | |
| 27 | // Get the name of the callee. |
| 28 | IdentifierInfo *II = FD->getIdentifier(); |
| 29 | if (!II) // if no identifier, not a simple C function |
| 30 | return; |
| 31 | |
| 32 | if (II->isStr("main")) { |
| 33 | ExplodedNode *N = C.generateSink(); |
| 34 | if (!N) |
| 35 | return; |
| 36 | |
| 37 | if (!BT) |
Jordy Rose | 26fb4cb | 2011-08-17 03:23:51 +0000 | [diff] [blame] | 38 | BT.reset(new BugType("call to main", "example analyzer plugin")); |
Jordy Rose | 77a33a7 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 39 | |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 40 | BugReport *report = new BugReport(*BT, BT->getName(), N); |
Jordy Rose | 77a33a7 | 2011-08-17 01:30:59 +0000 | [diff] [blame] | 41 | report->addRange(Callee->getSourceRange()); |
| 42 | C.EmitReport(report); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // Register plugin! |
| 47 | extern "C" |
| 48 | void clang_registerCheckers (CheckerRegistry ®istry) { |
| 49 | registry.addChecker<MainCallChecker>("example.MainCallChecker", "Disallows calls to functions called main"); |
| 50 | } |
| 51 | |
| 52 | extern "C" |
| 53 | const char clang_analyzerAPIVersionString[] = CLANG_ANALYZER_API_VERSION_STRING; |