MainCallChecker.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. using namespace clang;
  6. using namespace ento;
  7. namespace {
  8. class MainCallChecker : public Checker < check::PreStmt<CallExpr> > {
  9. mutable llvm::OwningPtr<BugType> BT;
  10. public:
  11. void checkPreStmt(const CallExpr *CE, CheckerContext &C) const;
  12. };
  13. } // end anonymous namespace
  14. void MainCallChecker::checkPreStmt(const CallExpr *CE, CheckerContext &C) const {
  15. const ProgramState *state = C.getState();
  16. const Expr *Callee = CE->getCallee();
  17. const FunctionDecl *FD = state->getSVal(Callee).getAsFunctionDecl();
  18. if (!FD)
  19. return;
  20. // Get the name of the callee.
  21. IdentifierInfo *II = FD->getIdentifier();
  22. if (!II) // if no identifier, not a simple C function
  23. return;
  24. if (II->isStr("main")) {
  25. ExplodedNode *N = C.generateSink();
  26. if (!N)
  27. return;
  28. if (!BT)
  29. BT.reset(new BugType("call to main", "example analyzer plugin"));
  30. BugReport *report = new BugReport(*BT, BT->getName(), N);
  31. report->addRange(Callee->getSourceRange());
  32. C.EmitReport(report);
  33. }
  34. }
  35. // Register plugin!
  36. extern "C"
  37. void clang_registerCheckers (CheckerRegistry &registry) {
  38. registry.addChecker<MainCallChecker>("example.MainCallChecker", "Disallows calls to functions called main");
  39. }
  40. extern "C"
  41. const char clang_analyzerAPIVersionString[] = CLANG_ANALYZER_API_VERSION_STRING;