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 
14 #include "clang/StaticAnalyzer/Frontend/CheckerRegistration.h"
15 #include "clang/Basic/Diagnostic.h"
16 #include "clang/Frontend/FrontendDiagnostic.h"
17 #include "clang/StaticAnalyzer/Checkers/ClangCheckers.h"
18 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
19 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
20 #include "clang/StaticAnalyzer/Core/CheckerOptInfo.h"
21 #include "clang/StaticAnalyzer/Core/CheckerRegistry.h"
22 #include "clang/StaticAnalyzer/Frontend/FrontendActions.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 
ClangCheckerRegistry(ArrayRef<std::string> plugins,DiagnosticsEngine * diags)48 ClangCheckerRegistry::ClangCheckerRegistry(ArrayRef<std::string> plugins,
49                                            DiagnosticsEngine *diags) {
50   registerBuiltinCheckers(*this);
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 
isCompatibleAPIVersion(const char * versionString)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   if (strcmp(versionString, CLANG_ANALYZER_API_VERSION_STRING) == 0)
87     return true;
88 
89   return false;
90 }
91 
warnIncompatible(DiagnosticsEngine * diags,StringRef pluginPath,const char * pluginAPIVersion)92 void ClangCheckerRegistry::warnIncompatible(DiagnosticsEngine *diags,
93                                             StringRef pluginPath,
94                                             const char *pluginAPIVersion) {
95   if (!diags)
96     return;
97   if (!pluginAPIVersion)
98     return;
99 
100   diags->Report(diag::warn_incompatible_analyzer_plugin_api)
101       << llvm::sys::path::filename(pluginPath);
102   diags->Report(diag::note_incompatible_analyzer_plugin_api)
103       << CLANG_ANALYZER_API_VERSION_STRING
104       << pluginAPIVersion;
105 }
106 
107 std::unique_ptr<CheckerManager>
createCheckerManager(AnalyzerOptions & opts,const LangOptions & langOpts,ArrayRef<std::string> plugins,DiagnosticsEngine & diags)108 ento::createCheckerManager(AnalyzerOptions &opts, const LangOptions &langOpts,
109                            ArrayRef<std::string> plugins,
110                            DiagnosticsEngine &diags) {
111   std::unique_ptr<CheckerManager> checkerMgr(
112       new CheckerManager(langOpts, &opts));
113 
114   SmallVector<CheckerOptInfo, 8> checkerOpts;
115   for (unsigned i = 0, e = opts.CheckersControlList.size(); i != e; ++i) {
116     const std::pair<std::string, bool> &opt = opts.CheckersControlList[i];
117     checkerOpts.push_back(CheckerOptInfo(opt.first.c_str(), opt.second));
118   }
119 
120   ClangCheckerRegistry allCheckers(plugins, &diags);
121   allCheckers.initializeManager(*checkerMgr, checkerOpts);
122   allCheckers.validateCheckerOptions(opts, diags);
123   checkerMgr->finishedCheckerRegistration();
124 
125   for (unsigned i = 0, e = checkerOpts.size(); i != e; ++i) {
126     if (checkerOpts[i].isUnclaimed()) {
127       diags.Report(diag::err_unknown_analyzer_checker)
128           << checkerOpts[i].getName();
129       diags.Report(diag::note_suggest_disabling_all_checkers);
130     }
131 
132   }
133 
134   return checkerMgr;
135 }
136 
printCheckerHelp(raw_ostream & out,ArrayRef<std::string> plugins)137 void ento::printCheckerHelp(raw_ostream &out, ArrayRef<std::string> plugins) {
138   out << "OVERVIEW: Clang Static Analyzer Checkers List\n\n";
139   out << "USAGE: -analyzer-checker <CHECKER or PACKAGE,...>\n\n";
140 
141   ClangCheckerRegistry(plugins).printHelp(out);
142 }
143