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"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/Support/DynamicLibrary.h"
24 #include "llvm/Support/FormattedStream.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 
104 std::unique_ptr<CheckerManager> ento::createCheckerManager(
105  ASTContext &context,
106  AnalyzerOptions &opts,
107  ArrayRef<std::string> plugins,
108  ArrayRef<std::function<void(CheckerRegistry &)>> checkerRegistrationFns,
109  DiagnosticsEngine &diags) {
110  auto checkerMgr = llvm::make_unique<CheckerManager>(context, opts);
111 
112  ClangCheckerRegistry allCheckers(plugins, &diags);
113 
114  for (const auto &Fn : checkerRegistrationFns)
115  Fn(allCheckers);
116 
117  allCheckers.initializeManager(*checkerMgr, opts, diags);
118  allCheckers.validateCheckerOptions(opts, diags);
119  checkerMgr->finishedCheckerRegistration();
120 
121  return checkerMgr;
122 }
123 
124 void ento::printCheckerHelp(raw_ostream &out, ArrayRef<std::string> plugins) {
125  out << "OVERVIEW: Clang Static Analyzer Checkers List\n\n";
126  out << "USAGE: -analyzer-checker <CHECKER or PACKAGE,...>\n\n";
127 
128  ClangCheckerRegistry(plugins).printHelp(out);
129 }
130 
131 void ento::printEnabledCheckerList(raw_ostream &out,
132  ArrayRef<std::string> plugins,
133  const AnalyzerOptions &opts) {
134  out << "OVERVIEW: Clang Static Analyzer Enabled Checkers List\n\n";
135 
136  ClangCheckerRegistry(plugins).printList(out, opts);
137 }
138 
139 void ento::printAnalyzerConfigList(raw_ostream &out) {
140  out << "OVERVIEW: Clang Static Analyzer -analyzer-config Option List\n\n";
141  out << "USAGE: clang -cc1 [CLANG_OPTIONS] -analyzer-config "
142  "<OPTION1=VALUE,OPTION2=VALUE,...>\n\n";
143  out << " clang -cc1 [CLANG_OPTIONS] -analyzer-config OPTION1=VALUE, "
144  "-analyzer-config OPTION2=VALUE, ...\n\n";
145  out << " clang [CLANG_OPTIONS] -Xclang -analyzer-config -Xclang"
146  "<OPTION1=VALUE,OPTION2=VALUE,...>\n\n";
147  out << " clang [CLANG_OPTIONS] -Xclang -analyzer-config -Xclang "
148  "OPTION1=VALUE, -Xclang -analyzer-config -Xclang "
149  "OPTION2=VALUE, ...\n\n";
150  out << "OPTIONS:\n\n";
151 
152  using OptionAndDescriptionTy = std::pair<StringRef, std::string>;
153  OptionAndDescriptionTy PrintableOptions[] = {
154 #define ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, DEFAULT_VAL) \
155  { \
156  CMDFLAG, \
157  llvm::Twine(llvm::Twine() + "(" + \
158  (StringRef(#TYPE) == "StringRef" ? "string" : #TYPE ) + \
159  ") " DESC \
160  " (default: " #DEFAULT_VAL ")").str() \
161  },
162 
163 #define ANALYZER_OPTION_DEPENDS_ON_USER_MODE(TYPE, NAME, CMDFLAG, DESC, \
164  SHALLOW_VAL, DEEP_VAL) \
165  { \
166  CMDFLAG, \
167  llvm::Twine(llvm::Twine() + "(" + \
168  (StringRef(#TYPE) == "StringRef" ? "string" : #TYPE ) + \
169  ") " DESC \
170  " (default: " #SHALLOW_VAL " in shallow mode, " #DEEP_VAL \
171  " in deep mode)").str() \
172  },
173 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.def"
174 #undef ANALYZER_OPTION
175 #undef ANALYZER_OPTION_DEPENDS_ON_USER_MODE
176  };
177 
178  llvm::sort(PrintableOptions, [](const OptionAndDescriptionTy &LHS,
179  const OptionAndDescriptionTy &RHS) {
180  return LHS.first < RHS.first;
181  });
182 
183  constexpr size_t MinLineWidth = 70;
184  constexpr size_t PadForOpt = 2;
185  constexpr size_t OptionWidth = 30;
186  constexpr size_t PadForDesc = PadForOpt + OptionWidth;
187  static_assert(MinLineWidth > PadForDesc, "MinLineWidth must be greater!");
188 
189  llvm::formatted_raw_ostream FOut(out);
190 
191  for (const auto &Pair : PrintableOptions) {
192  FOut.PadToColumn(PadForOpt) << Pair.first;
193 
194  // If the buffer's length is greater then PadForDesc, print a newline.
195  if (FOut.getColumn() > PadForDesc)
196  FOut << '\n';
197 
198  FOut.PadToColumn(PadForDesc);
199 
200  for (char C : Pair.second) {
201  if (FOut.getColumn() > MinLineWidth && C == ' ') {
202  FOut << '\n';
203  FOut.PadToColumn(PadForDesc);
204  continue;
205  }
206  FOut << C;
207  }
208  FOut << "\n\n";
209  }
210 }
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:1292
void printEnabledCheckerList(raw_ostream &OS, ArrayRef< std::string > plugins, const AnalyzerOptions &opts)
#define CLANG_ANALYZER_API_VERSION_STRING
void printAnalyzerConfigList(raw_ostream &OS)
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.
__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:83
void printCheckerHelp(raw_ostream &OS, ArrayRef< std::string > plugins)
Dataflow Directional Tag Classes.
void registerBuiltinCheckers(CheckerRegistry &registry)
Stores options for the analyzer from the command line.