blob: d9d5bae11a40b7c610a9f9ed28a985e7eed3e1fe [file] [log] [blame]
Argyrios Kyrtzidis43dee222011-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 Kyrtzidis27af04b2011-02-15 16:54:12 +000014#include "clang/StaticAnalyzer/Frontend/CheckerRegistration.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000015#include "clang/Basic/Diagnostic.h"
16#include "clang/Frontend/FrontendDiagnostic.h"
Jordy Rose08b86532011-08-16 21:24:21 +000017#include "clang/StaticAnalyzer/Checkers/ClangCheckers.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000018#include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
Argyrios Kyrtzidis43dee222011-02-14 18:13:31 +000019#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Jordy Rose08b86532011-08-16 21:24:21 +000020#include "clang/StaticAnalyzer/Core/CheckerOptInfo.h"
21#include "clang/StaticAnalyzer/Core/CheckerRegistry.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000022#include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000023#include "llvm/ADT/SmallVector.h"
Jordy Rose77a33a72011-08-17 01:30:59 +000024#include "llvm/Support/DynamicLibrary.h"
Jordy Rosea8fd0bc2011-08-17 04:56:03 +000025#include "llvm/Support/Path.h"
Argyrios Kyrtzidis116f3642011-02-25 00:09:51 +000026#include "llvm/Support/raw_ostream.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070027#include <memory>
Argyrios Kyrtzidis43dee222011-02-14 18:13:31 +000028
29using namespace clang;
30using namespace ento;
Jordy Rose77a33a72011-08-17 01:30:59 +000031using llvm::sys::DynamicLibrary;
Argyrios Kyrtzidis43dee222011-02-14 18:13:31 +000032
Jordy Rose77a33a72011-08-17 01:30:59 +000033namespace {
34class ClangCheckerRegistry : public CheckerRegistry {
35 typedef void (*RegisterCheckersFn)(CheckerRegistry &);
Jordy Rose08b86532011-08-16 21:24:21 +000036
Jordy Rose77a33a72011-08-17 01:30:59 +000037 static bool isCompatibleAPIVersion(const char *versionString);
David Blaikied6471f72011-09-25 23:23:43 +000038 static void warnIncompatible(DiagnosticsEngine *diags, StringRef pluginPath,
Jordy Rosea8fd0bc2011-08-17 04:56:03 +000039 const char *pluginAPIVersion);
40
41public:
David Blaikied6471f72011-09-25 23:23:43 +000042 ClangCheckerRegistry(ArrayRef<std::string> plugins,
43 DiagnosticsEngine *diags = 0);
Jordy Rose77a33a72011-08-17 01:30:59 +000044};
45
46} // end anonymous namespace
47
Jordy Rosea8fd0bc2011-08-17 04:56:03 +000048ClangCheckerRegistry::ClangCheckerRegistry(ArrayRef<std::string> plugins,
David Blaikied6471f72011-09-25 23:23:43 +000049 DiagnosticsEngine *diags) {
Jordy Rose77a33a72011-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.
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");
Jordy Rosea8fd0bc2011-08-17 04:56:03 +000060 if (!isCompatibleAPIVersion(pluginAPIVersion)) {
61 warnIncompatible(diags, *i, pluginAPIVersion);
Jordy Rose77a33a72011-08-17 01:30:59 +000062 continue;
Jordy Rosea8fd0bc2011-08-17 04:56:03 +000063 }
Jordy Rose77a33a72011-08-17 01:30:59 +000064
65 // Register its checkers.
66 RegisterCheckersFn registerPluginCheckers =
Benjamin Kramer68753252011-08-17 04:22:25 +000067 (RegisterCheckersFn) (intptr_t) lib.getAddressOfSymbol(
68 "clang_registerCheckers");
Jordy Rose77a33a72011-08-17 01:30:59 +000069 if (registerPluginCheckers)
70 registerPluginCheckers(*this);
71 }
Jordy Rose08b86532011-08-16 21:24:21 +000072}
73
Jordy Rose77a33a72011-08-17 01:30:59 +000074bool ClangCheckerRegistry::isCompatibleAPIVersion(const char *versionString) {
75 // If the version string is null, it's not an analyzer plugin.
76 if (versionString == 0)
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
Jordy Rose77a33a72011-08-17 01:30:59 +000084 return false;
85}
86
David Blaikied6471f72011-09-25 23:23:43 +000087void ClangCheckerRegistry::warnIncompatible(DiagnosticsEngine *diags,
Jordy Rosea8fd0bc2011-08-17 04:56:03 +000088 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)
98 << CLANG_ANALYZER_API_VERSION_STRING
99 << pluginAPIVersion;
100}
101
Jordy Rose77a33a72011-08-17 01:30:59 +0000102
Ted Kremenek8dae1282013-04-16 21:10:05 +0000103CheckerManager *ento::createCheckerManager(AnalyzerOptions &opts,
Jordy Rose08b86532011-08-16 21:24:21 +0000104 const LangOptions &langOpts,
105 ArrayRef<std::string> plugins,
David Blaikied6471f72011-09-25 23:23:43 +0000106 DiagnosticsEngine &diags) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700107 std::unique_ptr<CheckerManager> checkerMgr(
108 new CheckerManager(langOpts, &opts));
Argyrios Kyrtzidis43dee222011-02-14 18:13:31 +0000109
Chris Lattner5f9e2722011-07-23 10:55:15 +0000110 SmallVector<CheckerOptInfo, 8> checkerOpts;
Argyrios Kyrtzidis43dee222011-02-14 18:13:31 +0000111 for (unsigned i = 0, e = opts.CheckersControlList.size(); i != e; ++i) {
112 const std::pair<std::string, bool> &opt = opts.CheckersControlList[i];
113 checkerOpts.push_back(CheckerOptInfo(opt.first.c_str(), opt.second));
114 }
115
Jordy Rosea8fd0bc2011-08-17 04:56:03 +0000116 ClangCheckerRegistry allCheckers(plugins, &diags);
117 allCheckers.initializeManager(*checkerMgr, checkerOpts);
Argyrios Kyrtzidisdeb64472011-02-28 17:36:09 +0000118 checkerMgr->finishedCheckerRegistration();
119
Argyrios Kyrtzidis43dee222011-02-14 18:13:31 +0000120 for (unsigned i = 0, e = checkerOpts.size(); i != e; ++i) {
121 if (checkerOpts[i].isUnclaimed())
Ted Kremenekc0a43772012-07-25 07:12:13 +0000122 diags.Report(diag::err_unknown_analyzer_checker)
Argyrios Kyrtzidis43dee222011-02-14 18:13:31 +0000123 << checkerOpts[i].getName();
124 }
125
Stephen Hines651f13c2014-04-23 16:59:28 -0700126 return checkerMgr.release();
Argyrios Kyrtzidis43dee222011-02-14 18:13:31 +0000127}
Argyrios Kyrtzidis116f3642011-02-25 00:09:51 +0000128
Jordy Rose08b86532011-08-16 21:24:21 +0000129void ento::printCheckerHelp(raw_ostream &out, ArrayRef<std::string> plugins) {
130 out << "OVERVIEW: Clang Static Analyzer Checkers List\n\n";
131 out << "USAGE: -analyzer-checker <CHECKER or PACKAGE,...>\n\n";
Argyrios Kyrtzidis116f3642011-02-25 00:09:51 +0000132
Jordy Rose77a33a72011-08-17 01:30:59 +0000133 ClangCheckerRegistry(plugins).printHelp(out);
Argyrios Kyrtzidis116f3642011-02-25 00:09:51 +0000134}