blob: 836007bf46c1665300b25b3e6d03c958b9e31bfd [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/CheckerOptInfo.h"
21#include "clang/StaticAnalyzer/Core/CheckerRegistry.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
23#include "llvm/ADT/OwningPtr.h"
24#include "llvm/ADT/SmallVector.h"
Jordy Rose93b86e42011-08-17 01:30:59 +000025#include "llvm/Support/DynamicLibrary.h"
Jordy Rose075d73b2011-08-17 04:56:03 +000026#include "llvm/Support/Path.h"
Argyrios Kyrtzidis17bee3e2011-02-25 00:09:51 +000027#include "llvm/Support/raw_ostream.h"
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,
43 DiagnosticsEngine *diags = 0);
Jordy Rose93b86e42011-08-17 01:30:59 +000044};
45
46} // 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.
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 Rose075d73b2011-08-17 04:56:03 +000060 if (!isCompatibleAPIVersion(pluginAPIVersion)) {
61 warnIncompatible(diags, *i, pluginAPIVersion);
Jordy Rose93b86e42011-08-17 01:30:59 +000062 continue;
Jordy Rose075d73b2011-08-17 04:56:03 +000063 }
Jordy Rose93b86e42011-08-17 01:30:59 +000064
65 // Register its checkers.
66 RegisterCheckersFn registerPluginCheckers =
Benjamin Kramer8b3929d2011-08-17 04:22:25 +000067 (RegisterCheckersFn) (intptr_t) lib.getAddressOfSymbol(
68 "clang_registerCheckers");
Jordy Rose93b86e42011-08-17 01:30:59 +000069 if (registerPluginCheckers)
70 registerPluginCheckers(*this);
71 }
Jordy Rose59cce712011-08-16 21:24:21 +000072}
73
Jordy Rose93b86e42011-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 Rose93b86e42011-08-17 01:30:59 +000084 return false;
85}
86
David Blaikie9c902b52011-09-25 23:23:43 +000087void ClangCheckerRegistry::warnIncompatible(DiagnosticsEngine *diags,
Jordy Rose075d73b2011-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 Rose93b86e42011-08-17 01:30:59 +0000102
Ted Kremenek8a282952013-04-16 21:10:05 +0000103CheckerManager *ento::createCheckerManager(AnalyzerOptions &opts,
Jordy Rose59cce712011-08-16 21:24:21 +0000104 const LangOptions &langOpts,
105 ArrayRef<std::string> plugins,
David Blaikie9c902b52011-09-25 23:23:43 +0000106 DiagnosticsEngine &diags) {
Ahmed Charlesb8984322014-03-07 20:03:18 +0000107 std::unique_ptr<CheckerManager> checkerMgr(
108 new CheckerManager(langOpts, &opts));
Argyrios Kyrtzidis556c45e2011-02-14 18:13:31 +0000109
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000110 SmallVector<CheckerOptInfo, 8> checkerOpts;
Argyrios Kyrtzidis556c45e2011-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 Rose075d73b2011-08-17 04:56:03 +0000116 ClangCheckerRegistry allCheckers(plugins, &diags);
117 allCheckers.initializeManager(*checkerMgr, checkerOpts);
Argyrios Kyrtzidisa15dfec2011-02-28 17:36:09 +0000118 checkerMgr->finishedCheckerRegistration();
119
Argyrios Kyrtzidis556c45e2011-02-14 18:13:31 +0000120 for (unsigned i = 0, e = checkerOpts.size(); i != e; ++i) {
121 if (checkerOpts[i].isUnclaimed())
Ted Kremenek2b619662012-07-25 07:12:13 +0000122 diags.Report(diag::err_unknown_analyzer_checker)
Argyrios Kyrtzidis556c45e2011-02-14 18:13:31 +0000123 << checkerOpts[i].getName();
124 }
125
Ahmed Charles9a16beb2014-03-07 19:33:25 +0000126 return checkerMgr.release();
Argyrios Kyrtzidis556c45e2011-02-14 18:13:31 +0000127}
Argyrios Kyrtzidis17bee3e2011-02-25 00:09:51 +0000128
Jordy Rose59cce712011-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 Kyrtzidis17bee3e2011-02-25 00:09:51 +0000132
Jordy Rose93b86e42011-08-17 01:30:59 +0000133 ClangCheckerRegistry(plugins).printHelp(out);
Argyrios Kyrtzidis17bee3e2011-02-25 00:09:51 +0000134}