AnalysisOrderChecker.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //===- AnalysisOrderChecker - Print callbacks called ------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This checker prints callbacks that are called during analysis.
  11. // This is required to ensure that callbacks are fired in order
  12. // and do not duplicate or get lost.
  13. // Feel free to extend this checker with any callback you need to check.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "ClangSACheckers.h"
  17. #include "clang/StaticAnalyzer/Core/Checker.h"
  18. #include "clang/StaticAnalyzer/Core/CheckerManager.h"
  19. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  20. using namespace clang;
  21. using namespace ento;
  22. namespace {
  23. class AnalysisOrderChecker : public Checker< check::PreStmt<CastExpr>,
  24. check::PostStmt<CastExpr>,
  25. check::PreStmt<ArraySubscriptExpr>,
  26. check::PostStmt<ArraySubscriptExpr>> {
  27. bool isCallbackEnabled(CheckerContext &C, StringRef CallbackName) const {
  28. AnalyzerOptions &Opts = C.getAnalysisManager().getAnalyzerOptions();
  29. return Opts.getBooleanOption("*", false, this) ||
  30. Opts.getBooleanOption(CallbackName, false, this);
  31. }
  32. public:
  33. void checkPreStmt(const CastExpr *CE, CheckerContext &C) const {
  34. if (isCallbackEnabled(C, "PreStmtCastExpr"))
  35. llvm::errs() << "PreStmt<CastExpr> (Kind : " << CE->getCastKindName()
  36. << ")\n";
  37. }
  38. void checkPostStmt(const CastExpr *CE, CheckerContext &C) const {
  39. if (isCallbackEnabled(C, "PostStmtCastExpr"))
  40. llvm::errs() << "PostStmt<CastExpr> (Kind : " << CE->getCastKindName()
  41. << ")\n";
  42. }
  43. void checkPreStmt(const ArraySubscriptExpr *SubExpr, CheckerContext &C) const {
  44. if (isCallbackEnabled(C, "PreStmtArraySubscriptExpr"))
  45. llvm::errs() << "PreStmt<ArraySubscriptExpr>\n";
  46. }
  47. void checkPostStmt(const ArraySubscriptExpr *SubExpr, CheckerContext &C) const {
  48. if (isCallbackEnabled(C, "PostStmtArraySubscriptExpr"))
  49. llvm::errs() << "PostStmt<ArraySubscriptExpr>\n";
  50. }
  51. };
  52. }
  53. //===----------------------------------------------------------------------===//
  54. // Registration.
  55. //===----------------------------------------------------------------------===//
  56. void ento::registerAnalysisOrderChecker(CheckerManager &mgr) {
  57. mgr.registerChecker<AnalysisOrderChecker>();
  58. }