clang  8.0.0svn
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  std::string err;
56  DynamicLibrary lib = DynamicLibrary::getPermanentLibrary(i->c_str(), &err);
57  if (!lib.isValid()) {
58  diags->Report(diag::err_fe_unable_to_load_plugin) << *i << err;
59  continue;
60  }
61 
62  // See if it's compatible with this build of clang.
63  const char *pluginAPIVersion =
64  (const char *) lib.getAddressOfSymbol("clang_analyzerAPIVersionString");
65  if (!isCompatibleAPIVersion(pluginAPIVersion)) {
66  warnIncompatible(diags, *i, pluginAPIVersion);
67  continue;
68  }
69 
70  // Register its checkers.
71  RegisterCheckersFn registerPluginCheckers =
72  (RegisterCheckersFn) (intptr_t) lib.getAddressOfSymbol(
73  "clang_registerCheckers");
74  if (registerPluginCheckers)
75  registerPluginCheckers(*this);
76  }
77 }
78 
79 bool ClangCheckerRegistry::isCompatibleAPIVersion(const char *versionString) {
80  // If the version string is null, it's not an analyzer plugin.
81  if (!versionString)
82  return false;
83 
84  // For now, none of the static analyzer API is considered stable.
85  // Versions must match exactly.
86  return strcmp(versionString, CLANG_ANALYZER_API_VERSION_STRING) == 0;
87 }
88 
89 void ClangCheckerRegistry::warnIncompatible(DiagnosticsEngine *diags,
90  StringRef pluginPath,
91  const char *pluginAPIVersion) {
92  if (!diags)
93  return;
94  if (!pluginAPIVersion)
95  return;
96 
97  diags->Report(diag::warn_incompatible_analyzer_plugin_api)
98  << llvm::sys::path::filename(pluginPath);
99  diags->Report(diag::note_incompatible_analyzer_plugin_api)
101  << pluginAPIVersion;
102 }
103 
106  SmallVector<CheckerOptInfo, 8> checkerOpts;
107  for (unsigned i = 0, e = opts.CheckersControlList.size(); i != e; ++i) {
108  const std::pair<std::string, bool> &opt = opts.CheckersControlList[i];
109  checkerOpts.push_back(CheckerOptInfo(opt.first, opt.second));
110  }
111  return checkerOpts;
112 }
113 
114 std::unique_ptr<CheckerManager> ento::createCheckerManager(
115  ASTContext &context,
116  AnalyzerOptions &opts,
117  ArrayRef<std::string> plugins,
118  ArrayRef<std::function<void(CheckerRegistry &)>> checkerRegistrationFns,
119  DiagnosticsEngine &diags) {
120  auto checkerMgr = llvm::make_unique<CheckerManager>(context, opts);
121 
123 
124  ClangCheckerRegistry allCheckers(plugins, &diags);
125 
126  for (const auto &Fn : checkerRegistrationFns)
127  Fn(allCheckers);
128 
129  allCheckers.initializeManager(*checkerMgr, checkerOpts);
130  allCheckers.validateCheckerOptions(opts, diags);
131  checkerMgr->finishedCheckerRegistration();
132 
133  for (unsigned i = 0, e = checkerOpts.size(); i != e; ++i) {
134  if (checkerOpts[i].isUnclaimed()) {
135  diags.Report(diag::err_unknown_analyzer_checker)
136  << checkerOpts[i].getName();
137  diags.Report(diag::note_suggest_disabling_all_checkers);
138  }
139 
140  }
141 
142  return checkerMgr;
143 }
144 
145 void ento::printCheckerHelp(raw_ostream &out, ArrayRef<std::string> plugins) {
146  out << "OVERVIEW: Clang Static Analyzer Checkers List\n\n";
147  out << "USAGE: -analyzer-checker <CHECKER or PACKAGE,...>\n\n";
148 
149  ClangCheckerRegistry(plugins).printHelp(out);
150 }
151 
152 void ento::printEnabledCheckerList(raw_ostream &out,
153  ArrayRef<std::string> plugins,
154  const AnalyzerOptions &opts) {
155  out << "OVERVIEW: Clang Static Analyzer Enabled Checkers List\n\n";
156 
158  ClangCheckerRegistry(plugins).printList(out, checkerOpts);
159 }
Manages a set of available checkers for running a static analysis.
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1294
void printEnabledCheckerList(raw_ostream &OS, ArrayRef< std::string > plugins, const AnalyzerOptions &opts)
#define CLANG_ANALYZER_API_VERSION_STRING
std::unique_ptr< CheckerManager > createCheckerManager(ASTContext &context, AnalyzerOptions &opts, ArrayRef< std::string > plugins, ArrayRef< std::function< void(CheckerRegistry &)>> checkerRegistrationFns, DiagnosticsEngine &diags)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:154
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:149
Defines the Diagnostic-related interfaces.
std::vector< std::pair< std::string, bool > > CheckersControlList
Pair of checker name and enable/disable.
__INTPTR_TYPE__ intptr_t
A signed integer type with the property that any valid pointer to void can be converted to this type...
Definition: opencl-c.h:75
static SmallVector< CheckerOptInfo, 8 > getCheckerOptList(const AnalyzerOptions &opts)
void printCheckerHelp(raw_ostream &OS, ArrayRef< std::string > plugins)
Dataflow Directional Tag Classes.
void registerBuiltinCheckers(CheckerRegistry &registry)