MainCallChecker.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 ProgramStateRef state = C.getState();
  16. const LocationContext *LC = C.getLocationContext();
  17. const Expr *Callee = CE->getCallee();
  18. const FunctionDecl *FD = state->getSVal(Callee, LC).getAsFunctionDecl();
  19. if (!FD)
  20. return;
  21. // Get the name of the callee.
  22. IdentifierInfo *II = FD->getIdentifier();
  23. if (!II) // if no identifier, not a simple C function
  24. return;
  25. if (II->isStr("main")) {
  26. ExplodedNode *N = C.generateSink();
  27. if (!N)
  28. return;
  29. if (!BT)
  30. BT.reset(new BugType(this, "call to main", "example analyzer plugin"));
  31. std::unique_ptr<BugReport> report =
  32. llvm::make_unique<BugReport>(*BT, BT->getName(), N);
  33. report->addRange(Callee->getSourceRange());
  34. C.emitReport(std::move(report));
  35. }
  36. }
  37. // Register plugin!
  38. extern "C"
  39. void clang_registerCheckers (CheckerRegistry &registry) {
  40. registry.addChecker<MainCallChecker>("example.MainCallChecker", "Disallows calls to functions called main");
  41. }
  42. extern "C"
  43. const char clang_analyzerAPIVersionString[] = CLANG_ANALYZER_API_VERSION_STRING;