MainCallChecker.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "clang/StaticAnalyzer/Core/Checker.h"
  2. #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
  3. #include "clang/StaticAnalyzer/Core/CheckerRegistry.h"
  4. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  5. using namespace clang;
  6. using namespace ento;
  7. namespace {
  8. class MainCallChecker : public Checker < check::PreStmt<CallExpr> > {
  9. mutable std::unique_ptr<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 Expr *Callee = CE->getCallee();
  16. const FunctionDecl *FD = C.getSVal(Callee).getAsFunctionDecl();
  17. if (!FD)
  18. return;
  19. // Get the name of the callee.
  20. IdentifierInfo *II = FD->getIdentifier();
  21. if (!II) // if no identifier, not a simple C function
  22. return;
  23. if (II->isStr("main")) {
  24. ExplodedNode *N = C.generateErrorNode();
  25. if (!N)
  26. return;
  27. if (!BT)
  28. BT.reset(new BugType(this, "call to main", "example analyzer plugin"));
  29. std::unique_ptr<BugReport> report =
  30. llvm::make_unique<BugReport>(*BT, BT->getName(), N);
  31. report->addRange(Callee->getSourceRange());
  32. C.emitReport(std::move(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;