clang  3.7.0
CheckerRegistration.cpp
Go to the documentation of this file.
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 
15 #include "clang/Basic/Diagnostic.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/Support/DynamicLibrary.h"
25 #include "llvm/Support/Path.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include <memory>
28 
29 using namespace clang;
30 using namespace ento;
31 using llvm::sys::DynamicLibrary;
32 
33 namespace {
34 class ClangCheckerRegistry : public CheckerRegistry {
35  typedef void (*RegisterCheckersFn)(CheckerRegistry &);
36 
37  static bool isCompatibleAPIVersion(const char *versionString);
38  static void warnIncompatible(DiagnosticsEngine *diags, StringRef pluginPath,
39  const char *pluginAPIVersion);
40 
41 public:
42  ClangCheckerRegistry(ArrayRef<std::string> plugins,
43  DiagnosticsEngine *diags = nullptr);
44 };
45 
46 } // end anonymous namespace
47 
48 ClangCheckerRegistry::ClangCheckerRegistry(ArrayRef<std::string> plugins,
49  DiagnosticsEngine *diags) {
51 
52  for (ArrayRef<std::string>::iterator i = plugins.begin(), e = plugins.end();
53  i != e; ++i) {
54  // Get access to the plugin.
55  DynamicLibrary lib = DynamicLibrary::getPermanentLibrary(i->c_str());
56 
57  // See if it's compatible with this build of clang.
58  const char *pluginAPIVersion =
59  (const char *) lib.getAddressOfSymbol("clang_analyzerAPIVersionString");
60  if (!isCompatibleAPIVersion(pluginAPIVersion)) {
61  warnIncompatible(diags, *i, pluginAPIVersion);
62  continue;
63  }
64 
65  // Register its checkers.
66  RegisterCheckersFn registerPluginCheckers =
67  (RegisterCheckersFn) (intptr_t) lib.getAddressOfSymbol(
68  "clang_registerCheckers");
69  if (registerPluginCheckers)
70  registerPluginCheckers(*this);
71  }
72 }
73 
74 bool ClangCheckerRegistry::isCompatibleAPIVersion(const char *versionString) {
75  // If the version string is null, it's not an analyzer plugin.
76  if (!versionString)
77  return false;
78 
79  // For now, none of the static analyzer API is considered stable.
80  // Versions must match exactly.
81  if (strcmp(versionString, CLANG_ANALYZER_API_VERSION_STRING) == 0)
82  return true;
83 
84  return false;
85 }
86 
87 void ClangCheckerRegistry::warnIncompatible(DiagnosticsEngine *diags,
88  StringRef pluginPath,
89  const char *pluginAPIVersion) {
90  if (!diags)
91  return;
92  if (!pluginAPIVersion)
93  return;
94 
95  diags->Report(diag::warn_incompatible_analyzer_plugin_api)
96  << llvm::sys::path::filename(pluginPath);
97  diags->Report(diag::note_incompatible_analyzer_plugin_api)
99  << pluginAPIVersion;
100 }
101 
102 std::unique_ptr<CheckerManager>
104  ArrayRef<std::string> plugins,
105  DiagnosticsEngine &diags) {
106  std::unique_ptr<CheckerManager> checkerMgr(
107  new CheckerManager(langOpts, &opts));
108 
109  SmallVector<CheckerOptInfo, 8> checkerOpts;
110  for (unsigned i = 0, e = opts.CheckersControlList.size(); i != e; ++i) {
111  const std::pair<std::string, bool> &opt = opts.CheckersControlList[i];
112  checkerOpts.push_back(CheckerOptInfo(opt.first.c_str(), opt.second));
113  }
114 
115  ClangCheckerRegistry allCheckers(plugins, &diags);
116  allCheckers.initializeManager(*checkerMgr, checkerOpts);
117  allCheckers.validateCheckerOptions(opts, diags);
118  checkerMgr->finishedCheckerRegistration();
119 
120  for (unsigned i = 0, e = checkerOpts.size(); i != e; ++i) {
121  if (checkerOpts[i].isUnclaimed()) {
122  diags.Report(diag::err_unknown_analyzer_checker)
123  << checkerOpts[i].getName();
124  diags.Report(diag::note_suggest_disabling_all_checkers);
125  }
126 
127  }
128 
129  return checkerMgr;
130 }
131 
132 void ento::printCheckerHelp(raw_ostream &out, ArrayRef<std::string> plugins) {
133  out << "OVERVIEW: Clang Static Analyzer Checkers List\n\n";
134  out << "USAGE: -analyzer-checker <CHECKER or PACKAGE,...>\n\n";
135 
136  ClangCheckerRegistry(plugins).printHelp(out);
137 }
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1118
#define CLANG_ANALYZER_API_VERSION_STRING
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:48
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:135
std::vector< std::pair< std::string, bool > > CheckersControlList
Pair of checker name and enable/disable.
void printCheckerHelp(raw_ostream &OS, ArrayRef< std::string > plugins)
void registerBuiltinCheckers(CheckerRegistry &registry)
Defines the Diagnostic-related interfaces.
std::unique_ptr< CheckerManager > createCheckerManager(AnalyzerOptions &opts, const LangOptions &langOpts, ArrayRef< std::string > plugins, DiagnosticsEngine &diags)