blob: 89b99f98079f1f7fd9d21dd377ee16caeef8ff75 [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"
Argyrios Kyrtzidis116f3642011-02-25 00:09:51 +000015#include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
Jordy Rose08b86532011-08-16 21:24:21 +000016#include "clang/StaticAnalyzer/Checkers/ClangCheckers.h"
Argyrios Kyrtzidis43dee222011-02-14 18:13:31 +000017#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Jordy Rose08b86532011-08-16 21:24:21 +000018#include "clang/StaticAnalyzer/Core/CheckerOptInfo.h"
19#include "clang/StaticAnalyzer/Core/CheckerRegistry.h"
Argyrios Kyrtzidis43dee222011-02-14 18:13:31 +000020#include "clang/Frontend/AnalyzerOptions.h"
21#include "clang/Frontend/FrontendDiagnostic.h"
22#include "clang/Basic/Diagnostic.h"
Jordy Rose77a33a72011-08-17 01:30:59 +000023#include "llvm/Support/DynamicLibrary.h"
Jordy Rosea8fd0bc2011-08-17 04:56:03 +000024#include "llvm/Support/Path.h"
Argyrios Kyrtzidis116f3642011-02-25 00:09:51 +000025#include "llvm/Support/raw_ostream.h"
Argyrios Kyrtzidis43dee222011-02-14 18:13:31 +000026#include "llvm/ADT/OwningPtr.h"
27#include "llvm/ADT/SmallVector.h"
28
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);
Jordy Rosea8fd0bc2011-08-17 04:56:03 +000038 static void warnIncompatible(Diagnostic *diags, StringRef pluginPath,
39 const char *pluginAPIVersion);
40
41public:
42 ClangCheckerRegistry(ArrayRef<std::string> plugins, Diagnostic *diags = 0);
Jordy Rose77a33a72011-08-17 01:30:59 +000043};
44
45} // end anonymous namespace
46
Jordy Rosea8fd0bc2011-08-17 04:56:03 +000047ClangCheckerRegistry::ClangCheckerRegistry(ArrayRef<std::string> plugins,
48 Diagnostic *diags) {
Jordy Rose77a33a72011-08-17 01:30:59 +000049 registerBuiltinCheckers(*this);
50
51 for (ArrayRef<std::string>::iterator i = plugins.begin(), e = plugins.end();
52 i != e; ++i) {
53 // Get access to the plugin.
54 DynamicLibrary lib = DynamicLibrary::getPermanentLibrary(i->c_str());
55
56 // See if it's compatible with this build of clang.
57 const char *pluginAPIVersion =
58 (const char *) lib.getAddressOfSymbol("clang_analyzerAPIVersionString");
Jordy Rosea8fd0bc2011-08-17 04:56:03 +000059 if (!isCompatibleAPIVersion(pluginAPIVersion)) {
60 warnIncompatible(diags, *i, pluginAPIVersion);
Jordy Rose77a33a72011-08-17 01:30:59 +000061 continue;
Jordy Rosea8fd0bc2011-08-17 04:56:03 +000062 }
Jordy Rose77a33a72011-08-17 01:30:59 +000063
64 // Register its checkers.
65 RegisterCheckersFn registerPluginCheckers =
Benjamin Kramer68753252011-08-17 04:22:25 +000066 (RegisterCheckersFn) (intptr_t) lib.getAddressOfSymbol(
67 "clang_registerCheckers");
Jordy Rose77a33a72011-08-17 01:30:59 +000068 if (registerPluginCheckers)
69 registerPluginCheckers(*this);
70 }
Jordy Rose08b86532011-08-16 21:24:21 +000071}
72
Jordy Rose77a33a72011-08-17 01:30:59 +000073bool ClangCheckerRegistry::isCompatibleAPIVersion(const char *versionString) {
74 // If the version string is null, it's not an analyzer plugin.
75 if (versionString == 0)
76 return false;
77
78 // For now, none of the static analyzer API is considered stable.
79 // Versions must match exactly.
80 if (strcmp(versionString, CLANG_ANALYZER_API_VERSION_STRING) == 0)
81 return true;
82
Jordy Rose77a33a72011-08-17 01:30:59 +000083 return false;
84}
85
Jordy Rosea8fd0bc2011-08-17 04:56:03 +000086void ClangCheckerRegistry::warnIncompatible(Diagnostic *diags,
87 StringRef pluginPath,
88 const char *pluginAPIVersion) {
89 if (!diags)
90 return;
91 if (!pluginAPIVersion)
92 return;
93
94 diags->Report(diag::warn_incompatible_analyzer_plugin_api)
95 << llvm::sys::path::filename(pluginPath);
96 diags->Report(diag::note_incompatible_analyzer_plugin_api)
97 << CLANG_ANALYZER_API_VERSION_STRING
98 << pluginAPIVersion;
99}
100
Jordy Rose77a33a72011-08-17 01:30:59 +0000101
Jordy Rose08b86532011-08-16 21:24:21 +0000102CheckerManager *ento::createCheckerManager(const AnalyzerOptions &opts,
103 const LangOptions &langOpts,
104 ArrayRef<std::string> plugins,
105 Diagnostic &diags) {
Argyrios Kyrtzidis2e471a32011-02-23 07:19:14 +0000106 llvm::OwningPtr<CheckerManager> checkerMgr(new CheckerManager(langOpts));
Argyrios Kyrtzidis43dee222011-02-14 18:13:31 +0000107
Chris Lattner5f9e2722011-07-23 10:55:15 +0000108 SmallVector<CheckerOptInfo, 8> checkerOpts;
Argyrios Kyrtzidis43dee222011-02-14 18:13:31 +0000109 for (unsigned i = 0, e = opts.CheckersControlList.size(); i != e; ++i) {
110 const std::pair<std::string, bool> &opt = opts.CheckersControlList[i];
111 checkerOpts.push_back(CheckerOptInfo(opt.first.c_str(), opt.second));
112 }
113
Jordy Rosea8fd0bc2011-08-17 04:56:03 +0000114 ClangCheckerRegistry allCheckers(plugins, &diags);
115 allCheckers.initializeManager(*checkerMgr, checkerOpts);
Argyrios Kyrtzidisdeb64472011-02-28 17:36:09 +0000116 checkerMgr->finishedCheckerRegistration();
117
Argyrios Kyrtzidis43dee222011-02-14 18:13:31 +0000118 for (unsigned i = 0, e = checkerOpts.size(); i != e; ++i) {
119 if (checkerOpts[i].isUnclaimed())
Jordy Roseb7b06082011-08-17 05:00:56 +0000120 diags.Report(diag::warn_unknown_analyzer_checker)
Argyrios Kyrtzidis43dee222011-02-14 18:13:31 +0000121 << checkerOpts[i].getName();
122 }
123
124 return checkerMgr.take();
125}
Argyrios Kyrtzidis116f3642011-02-25 00:09:51 +0000126
Jordy Rose08b86532011-08-16 21:24:21 +0000127void ento::printCheckerHelp(raw_ostream &out, ArrayRef<std::string> plugins) {
128 out << "OVERVIEW: Clang Static Analyzer Checkers List\n\n";
129 out << "USAGE: -analyzer-checker <CHECKER or PACKAGE,...>\n\n";
Argyrios Kyrtzidis116f3642011-02-25 00:09:51 +0000130
Jordy Rose77a33a72011-08-17 01:30:59 +0000131 ClangCheckerRegistry(plugins).printHelp(out);
Argyrios Kyrtzidis116f3642011-02-25 00:09:51 +0000132}