CheckerRegistration.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. std::string err;
  48. DynamicLibrary lib = DynamicLibrary::getPermanentLibrary(i->c_str(), &err);
  49. if (!lib.isValid()) {
  50. diags->Report(diag::err_fe_unable_to_load_plugin) << *i << err;
  51. continue;
  52. }
  53. // See if it's compatible with this build of clang.
  54. const char *pluginAPIVersion =
  55. (const char *) lib.getAddressOfSymbol("clang_analyzerAPIVersionString");
  56. if (!isCompatibleAPIVersion(pluginAPIVersion)) {
  57. warnIncompatible(diags, *i, pluginAPIVersion);
  58. continue;
  59. }
  60. // Register its checkers.
  61. RegisterCheckersFn registerPluginCheckers =
  62. (RegisterCheckersFn) (intptr_t) lib.getAddressOfSymbol(
  63. "clang_registerCheckers");
  64. if (registerPluginCheckers)
  65. registerPluginCheckers(*this);
  66. }
  67. }
  68. bool ClangCheckerRegistry::isCompatibleAPIVersion(const char *versionString) {
  69. // If the version string is null, it's not an analyzer plugin.
  70. if (!versionString)
  71. return false;
  72. // For now, none of the static analyzer API is considered stable.
  73. // Versions must match exactly.
  74. if (strcmp(versionString, CLANG_ANALYZER_API_VERSION_STRING) == 0)
  75. return true;
  76. return false;
  77. }
  78. void ClangCheckerRegistry::warnIncompatible(DiagnosticsEngine *diags,
  79. StringRef pluginPath,
  80. const char *pluginAPIVersion) {
  81. if (!diags)
  82. return;
  83. if (!pluginAPIVersion)
  84. return;
  85. diags->Report(diag::warn_incompatible_analyzer_plugin_api)
  86. << llvm::sys::path::filename(pluginPath);
  87. diags->Report(diag::note_incompatible_analyzer_plugin_api)
  88. << CLANG_ANALYZER_API_VERSION_STRING
  89. << pluginAPIVersion;
  90. }
  91. std::unique_ptr<CheckerManager>
  92. ento::createCheckerManager(AnalyzerOptions &opts, const LangOptions &langOpts,
  93. ArrayRef<std::string> plugins,
  94. DiagnosticsEngine &diags) {
  95. std::unique_ptr<CheckerManager> checkerMgr(
  96. new CheckerManager(langOpts, &opts));
  97. SmallVector<CheckerOptInfo, 8> checkerOpts;
  98. for (unsigned i = 0, e = opts.CheckersControlList.size(); i != e; ++i) {
  99. const std::pair<std::string, bool> &opt = opts.CheckersControlList[i];
  100. checkerOpts.push_back(CheckerOptInfo(opt.first.c_str(), opt.second));
  101. }
  102. ClangCheckerRegistry allCheckers(plugins, &diags);
  103. allCheckers.initializeManager(*checkerMgr, checkerOpts);
  104. allCheckers.validateCheckerOptions(opts, diags);
  105. checkerMgr->finishedCheckerRegistration();
  106. for (unsigned i = 0, e = checkerOpts.size(); i != e; ++i) {
  107. if (checkerOpts[i].isUnclaimed()) {
  108. diags.Report(diag::err_unknown_analyzer_checker)
  109. << checkerOpts[i].getName();
  110. diags.Report(diag::note_suggest_disabling_all_checkers);
  111. }
  112. }
  113. return checkerMgr;
  114. }
  115. void ento::printCheckerHelp(raw_ostream &out, ArrayRef<std::string> plugins) {
  116. out << "OVERVIEW: Clang Static Analyzer Checkers List\n\n";
  117. out << "USAGE: -analyzer-checker <CHECKER or PACKAGE,...>\n\n";
  118. ClangCheckerRegistry(plugins).printHelp(out);
  119. }