blob: 70997e34a18af38fdc9cb1d4dffb9aced1c481e5 [file] [log] [blame]
Argyrios Kyrtzidis556c45e2011-02-14 18:13:31 +00001//===--- 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
Argyrios Kyrtzidis6fa0d202011-02-15 16:54:12 +000014#include "clang/StaticAnalyzer/Frontend/CheckerRegistration.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "clang/Basic/Diagnostic.h"
16#include "clang/Frontend/FrontendDiagnostic.h"
Jordy Rose59cce712011-08-16 21:24:21 +000017#include "clang/StaticAnalyzer/Checkers/ClangCheckers.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
Argyrios Kyrtzidis556c45e2011-02-14 18:13:31 +000019#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Jordy Rose59cce712011-08-16 21:24:21 +000020#include "clang/StaticAnalyzer/Core/CheckerRegistry.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "llvm/ADT/SmallVector.h"
Jordy Rose93b86e42011-08-17 01:30:59 +000023#include "llvm/Support/DynamicLibrary.h"
Kristof Umannf1f351c2018-11-02 15:59:37 +000024#include "llvm/Support/FormattedStream.h"
Jordy Rose075d73b2011-08-17 04:56:03 +000025#include "llvm/Support/Path.h"
Argyrios Kyrtzidis17bee3e2011-02-25 00:09:51 +000026#include "llvm/Support/raw_ostream.h"
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000027#include <memory>
Argyrios Kyrtzidis556c45e2011-02-14 18:13:31 +000028
29using namespace clang;
30using namespace ento;
Jordy Rose93b86e42011-08-17 01:30:59 +000031using llvm::sys::DynamicLibrary;
Argyrios Kyrtzidis556c45e2011-02-14 18:13:31 +000032
Jordy Rose93b86e42011-08-17 01:30:59 +000033namespace {
34class ClangCheckerRegistry : public CheckerRegistry {
35 typedef void (*RegisterCheckersFn)(CheckerRegistry &);
Jordy Rose59cce712011-08-16 21:24:21 +000036
Jordy Rose93b86e42011-08-17 01:30:59 +000037 static bool isCompatibleAPIVersion(const char *versionString);
David Blaikie9c902b52011-09-25 23:23:43 +000038 static void warnIncompatible(DiagnosticsEngine *diags, StringRef pluginPath,
Jordy Rose075d73b2011-08-17 04:56:03 +000039 const char *pluginAPIVersion);
40
41public:
David Blaikie9c902b52011-09-25 23:23:43 +000042 ClangCheckerRegistry(ArrayRef<std::string> plugins,
Craig Topper0dbb7832014-05-27 02:45:47 +000043 DiagnosticsEngine *diags = nullptr);
Jordy Rose93b86e42011-08-17 01:30:59 +000044};
Ted Kremenek3a0678e2015-09-08 03:50:52 +000045
Jordy Rose93b86e42011-08-17 01:30:59 +000046} // end anonymous namespace
47
Jordy Rose075d73b2011-08-17 04:56:03 +000048ClangCheckerRegistry::ClangCheckerRegistry(ArrayRef<std::string> plugins,
David Blaikie9c902b52011-09-25 23:23:43 +000049 DiagnosticsEngine *diags) {
Jordy Rose93b86e42011-08-17 01:30:59 +000050 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.
Gabor Horvatha61bb642015-07-15 20:32:07 +000055 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 }
Jordy Rose93b86e42011-08-17 01:30:59 +000061
62 // See if it's compatible with this build of clang.
63 const char *pluginAPIVersion =
64 (const char *) lib.getAddressOfSymbol("clang_analyzerAPIVersionString");
Jordy Rose075d73b2011-08-17 04:56:03 +000065 if (!isCompatibleAPIVersion(pluginAPIVersion)) {
66 warnIncompatible(diags, *i, pluginAPIVersion);
Jordy Rose93b86e42011-08-17 01:30:59 +000067 continue;
Jordy Rose075d73b2011-08-17 04:56:03 +000068 }
Jordy Rose93b86e42011-08-17 01:30:59 +000069
70 // Register its checkers.
71 RegisterCheckersFn registerPluginCheckers =
Benjamin Kramer8b3929d2011-08-17 04:22:25 +000072 (RegisterCheckersFn) (intptr_t) lib.getAddressOfSymbol(
73 "clang_registerCheckers");
Jordy Rose93b86e42011-08-17 01:30:59 +000074 if (registerPluginCheckers)
75 registerPluginCheckers(*this);
76 }
Jordy Rose59cce712011-08-16 21:24:21 +000077}
78
Jordy Rose93b86e42011-08-17 01:30:59 +000079bool ClangCheckerRegistry::isCompatibleAPIVersion(const char *versionString) {
80 // If the version string is null, it's not an analyzer plugin.
Craig Topper0dbb7832014-05-27 02:45:47 +000081 if (!versionString)
Jordy Rose93b86e42011-08-17 01:30:59 +000082 return false;
83
84 // For now, none of the static analyzer API is considered stable.
85 // Versions must match exactly.
Alexander Kornienko44a784f2015-12-28 15:19:39 +000086 return strcmp(versionString, CLANG_ANALYZER_API_VERSION_STRING) == 0;
Jordy Rose93b86e42011-08-17 01:30:59 +000087}
88
David Blaikie9c902b52011-09-25 23:23:43 +000089void ClangCheckerRegistry::warnIncompatible(DiagnosticsEngine *diags,
Jordy Rose075d73b2011-08-17 04:56:03 +000090 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)
100 << CLANG_ANALYZER_API_VERSION_STRING
101 << pluginAPIVersion;
102}
103
Alexander Kornienkod00ed8e2018-06-27 14:56:12 +0000104std::unique_ptr<CheckerManager> ento::createCheckerManager(
George Karpenkov4ece68a2018-08-06 23:09:07 +0000105 ASTContext &context,
106 AnalyzerOptions &opts,
Alexander Kornienkod00ed8e2018-06-27 14:56:12 +0000107 ArrayRef<std::string> plugins,
108 ArrayRef<std::function<void(CheckerRegistry &)>> checkerRegistrationFns,
109 DiagnosticsEngine &diags) {
George Karpenkov4ece68a2018-08-06 23:09:07 +0000110 auto checkerMgr = llvm::make_unique<CheckerManager>(context, opts);
Argyrios Kyrtzidis556c45e2011-02-14 18:13:31 +0000111
Jordy Rose075d73b2011-08-17 04:56:03 +0000112 ClangCheckerRegistry allCheckers(plugins, &diags);
Alexander Kornienkod00ed8e2018-06-27 14:56:12 +0000113
114 for (const auto &Fn : checkerRegistrationFns)
115 Fn(allCheckers);
116
Kristof Umann45beaa02018-11-18 12:47:03 +0000117 allCheckers.initializeManager(*checkerMgr, opts, diags);
Gabor Horvathfc4c4d42015-07-09 21:43:45 +0000118 allCheckers.validateCheckerOptions(opts, diags);
Argyrios Kyrtzidisa15dfec2011-02-28 17:36:09 +0000119 checkerMgr->finishedCheckerRegistration();
120
Richard Trieud4b05ce2015-01-17 00:46:55 +0000121 return checkerMgr;
Argyrios Kyrtzidis556c45e2011-02-14 18:13:31 +0000122}
Argyrios Kyrtzidis17bee3e2011-02-25 00:09:51 +0000123
Jordy Rose59cce712011-08-16 21:24:21 +0000124void 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";
Argyrios Kyrtzidis17bee3e2011-02-25 00:09:51 +0000127
Jordy Rose93b86e42011-08-17 01:30:59 +0000128 ClangCheckerRegistry(plugins).printHelp(out);
Argyrios Kyrtzidis17bee3e2011-02-25 00:09:51 +0000129}
Gabor Horvathc4309902016-08-08 13:41:04 +0000130
131void 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
Kristof Umann45beaa02018-11-18 12:47:03 +0000136 ClangCheckerRegistry(plugins).printList(out, opts);
Gabor Horvathc4309902016-08-08 13:41:04 +0000137}
Kristof Umannf1f351c2018-11-02 15:59:37 +0000138
139void 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() + "(" + \
Kristof Umann37829b52018-11-02 19:48:56 +0000158 (StringRef(#TYPE) == "StringRef" ? "string" : #TYPE ) + \
159 ") " DESC \
Kristof Umannf1f351c2018-11-02 15:59:37 +0000160 " (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() + "(" + \
Kristof Umann37829b52018-11-02 19:48:56 +0000168 (StringRef(#TYPE) == "StringRef" ? "string" : #TYPE ) + \
169 ") " DESC \
Kristof Umannf1f351c2018-11-02 15:59:37 +0000170 " (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}