CheckerRegistration.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //===--- CheckerRegistration.cpp - Registration for the Analyzer Checkers -===//
  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. // Defines the registration function for the analyzer checkers.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/StaticAnalyzer/Frontend/CheckerRegistration.h"
  14. #include "clang/Basic/Diagnostic.h"
  15. #include "clang/Frontend/FrontendDiagnostic.h"
  16. #include "clang/StaticAnalyzer/Checkers/ClangCheckers.h"
  17. #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
  18. #include "clang/StaticAnalyzer/Core/CheckerManager.h"
  19. #include "clang/StaticAnalyzer/Core/CheckerOptInfo.h"
  20. #include "clang/StaticAnalyzer/Core/CheckerRegistry.h"
  21. #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
  22. #include "llvm/ADT/SmallVector.h"
  23. #include "llvm/Support/DynamicLibrary.h"
  24. #include "llvm/Support/Path.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. #include <memory>
  27. using namespace clang;
  28. using namespace ento;
  29. using llvm::sys::DynamicLibrary;
  30. namespace {
  31. class ClangCheckerRegistry : public CheckerRegistry {
  32. typedef void (*RegisterCheckersFn)(CheckerRegistry &);
  33. static bool isCompatibleAPIVersion(const char *versionString);
  34. static void warnIncompatible(DiagnosticsEngine *diags, StringRef pluginPath,
  35. const char *pluginAPIVersion);
  36. public:
  37. ClangCheckerRegistry(ArrayRef<std::string> plugins,
  38. DiagnosticsEngine *diags = nullptr);
  39. };
  40. } // end anonymous namespace
  41. ClangCheckerRegistry::ClangCheckerRegistry(ArrayRef<std::string> plugins,
  42. DiagnosticsEngine *diags) {
  43. registerBuiltinCheckers(*this);
  44. for (ArrayRef<std::string>::iterator i = plugins.begin(), e = plugins.end();
  45. i != e; ++i) {
  46. // Get access to the plugin.
  47. DynamicLibrary lib = DynamicLibrary::getPermanentLibrary(i->c_str());
  48. // See if it's compatible with this build of clang.
  49. const char *pluginAPIVersion =
  50. (const char *) lib.getAddressOfSymbol("clang_analyzerAPIVersionString");
  51. if (!isCompatibleAPIVersion(pluginAPIVersion)) {
  52. warnIncompatible(diags, *i, pluginAPIVersion);
  53. continue;
  54. }
  55. // Register its checkers.
  56. RegisterCheckersFn registerPluginCheckers =
  57. (RegisterCheckersFn) (intptr_t) lib.getAddressOfSymbol(
  58. "clang_registerCheckers");
  59. if (registerPluginCheckers)
  60. registerPluginCheckers(*this);
  61. }
  62. }
  63. bool ClangCheckerRegistry::isCompatibleAPIVersion(const char *versionString) {
  64. // If the version string is null, it's not an analyzer plugin.
  65. if (!versionString)
  66. return false;
  67. // For now, none of the static analyzer API is considered stable.
  68. // Versions must match exactly.
  69. if (strcmp(versionString, CLANG_ANALYZER_API_VERSION_STRING) == 0)
  70. return true;
  71. return false;
  72. }
  73. void ClangCheckerRegistry::warnIncompatible(DiagnosticsEngine *diags,
  74. StringRef pluginPath,
  75. const char *pluginAPIVersion) {
  76. if (!diags)
  77. return;
  78. if (!pluginAPIVersion)
  79. return;
  80. diags->Report(diag::warn_incompatible_analyzer_plugin_api)
  81. << llvm::sys::path::filename(pluginPath);
  82. diags->Report(diag::note_incompatible_analyzer_plugin_api)
  83. << CLANG_ANALYZER_API_VERSION_STRING
  84. << pluginAPIVersion;
  85. }
  86. CheckerManager *ento::createCheckerManager(AnalyzerOptions &opts,
  87. const LangOptions &langOpts,
  88. ArrayRef<std::string> plugins,
  89. DiagnosticsEngine &diags) {
  90. std::unique_ptr<CheckerManager> checkerMgr(
  91. new CheckerManager(langOpts, &opts));
  92. SmallVector<CheckerOptInfo, 8> checkerOpts;
  93. for (unsigned i = 0, e = opts.CheckersControlList.size(); i != e; ++i) {
  94. const std::pair<std::string, bool> &opt = opts.CheckersControlList[i];
  95. checkerOpts.push_back(CheckerOptInfo(opt.first.c_str(), opt.second));
  96. }
  97. ClangCheckerRegistry allCheckers(plugins, &diags);
  98. allCheckers.initializeManager(*checkerMgr, checkerOpts);
  99. checkerMgr->finishedCheckerRegistration();
  100. for (unsigned i = 0, e = checkerOpts.size(); i != e; ++i) {
  101. if (checkerOpts[i].isUnclaimed())
  102. diags.Report(diag::err_unknown_analyzer_checker)
  103. << checkerOpts[i].getName();
  104. }
  105. return checkerMgr.release();
  106. }
  107. void ento::printCheckerHelp(raw_ostream &out, ArrayRef<std::string> plugins) {
  108. out << "OVERVIEW: Clang Static Analyzer Checkers List\n\n";
  109. out << "USAGE: -analyzer-checker <CHECKER or PACKAGE,...>\n\n";
  110. ClangCheckerRegistry(plugins).printHelp(out);
  111. }