clang 10.0.0git

CheckerRegistration.cpp
Go to the documentation of this file.
1 //===--- CheckerRegistration.cpp - Registration for the Analyzer Checkers -===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Defines the registration function for the analyzer checkers.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "clang/StaticAnalyzer/Frontend/CheckerRegistration.h"
14 #include "clang/Basic/Diagnostic.h"
15 #include "clang/Frontend/FrontendDiagnostic.h"
16 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
17 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
18 #include "clang/StaticAnalyzer/Frontend/CheckerRegistry.h"
19 #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include <memory>
23
24 using namespace clang;
25 using namespace ento;
26
27 std::unique_ptr<CheckerManager> ento::createCheckerManager(
28 ASTContext &context,
29 AnalyzerOptions &opts,
30 ArrayRef<std::string> plugins,
31 ArrayRef<std::function<void(CheckerRegistry &)>> checkerRegistrationFns,
32 DiagnosticsEngine &diags) {
33 auto checkerMgr = std::make_unique<CheckerManager>(context, opts);
34
35 CheckerRegistry allCheckers(plugins, diags, opts, context.getLangOpts(),
36 checkerRegistrationFns);
37
38 allCheckers.initializeManager(*checkerMgr);
39 allCheckers.validateCheckerOptions();
40 checkerMgr->finishedCheckerRegistration();
41
42 return checkerMgr;
43 }
44
45 void ento::printCheckerHelp(raw_ostream &out, ArrayRef<std::string> plugins,
46 AnalyzerOptions &anopts,
47 DiagnosticsEngine &diags,
48 const LangOptions &langOpts) {
49 out << "OVERVIEW: Clang Static Analyzer Checkers List\n\n";
50 out << "USAGE: -analyzer-checker <CHECKER or PACKAGE,...>\n\n";
51
52 CheckerRegistry(plugins, diags, anopts, langOpts)
53 .printCheckerWithDescList(out);
54 }
55
56 void ento::printEnabledCheckerList(raw_ostream &out,
57 ArrayRef<std::string> plugins,
58 AnalyzerOptions &anopts,
59 DiagnosticsEngine &diags,
60 const LangOptions &langOpts) {
61 out << "OVERVIEW: Clang Static Analyzer Enabled Checkers List\n\n";
62
63 CheckerRegistry(plugins, diags, anopts, langOpts)
64 .printEnabledCheckerList(out);
65 }
66
67 void ento::printCheckerConfigList(raw_ostream &OS,
68 ArrayRef<std::string> plugins,
69 AnalyzerOptions &opts,
70 DiagnosticsEngine &diags,
71 const LangOptions &LangOpts) {
72 CheckerRegistry(plugins, diags, opts, LangOpts)
73 .printCheckerOptionList(OS);
74 }
75
76 void ento::printAnalyzerConfigList(raw_ostream &out) {
77 // FIXME: This message sounds scary, should be scary, but incorrectly states
78 // that all configs are super dangerous. In reality, many of them should be
79 // accessible to the user. We should create a user-facing subset of config
80 // options under a different frontend flag.
81 out << R"(
82 OVERVIEW: Clang Static Analyzer -analyzer-config Option List
83
84 The following list of configurations are meant for development purposes only, as
85 some of the variables they define are set to result in the most optimal
86 analysis. Setting them to other values may drastically change how the analyzer
87 behaves, and may even result in instabilities, crashes!
88
89 USAGE: -analyzer-config <OPTION1=VALUE,OPTION2=VALUE,...>
90 -analyzer-config OPTION1=VALUE, -analyzer-config OPTION2=VALUE, ...
91 OPTIONS:
92 )";
93
94 using OptionAndDescriptionTy = std::pair<StringRef, std::string>;
95 OptionAndDescriptionTy PrintableOptions[] = {
96 #define ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, DEFAULT_VAL) \
97 { \
98 CMDFLAG, \
99 llvm::Twine(llvm::Twine() + "(" + \
100 (StringRef(#TYPE) == "StringRef" ? "string" : #TYPE ) + \
101 ") " DESC \
102 " (default: " #DEFAULT_VAL ")").str() \
103 },
104
105 #define ANALYZER_OPTION_DEPENDS_ON_USER_MODE(TYPE, NAME, CMDFLAG, DESC, \
106 SHALLOW_VAL, DEEP_VAL) \
107 { \
108 CMDFLAG, \
109 llvm::Twine(llvm::Twine() + "(" + \
110 (StringRef(#TYPE) == "StringRef" ? "string" : #TYPE ) + \
111 ") " DESC \
112 " (default: " #SHALLOW_VAL " in shallow mode, " #DEEP_VAL \
113 " in deep mode)").str() \
114 },
115 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.def"
116 #undef ANALYZER_OPTION
117 #undef ANALYZER_OPTION_DEPENDS_ON_USER_MODE
118 };
119
120 llvm::sort(PrintableOptions, [](const OptionAndDescriptionTy &LHS,
121 const OptionAndDescriptionTy &RHS) {
122 return LHS.first < RHS.first;
123 });
124
125 for (const auto &Pair : PrintableOptions) {
126 AnalyzerOptions::printFormattedEntry(out, Pair, /*InitialPad*/ 2,
127 /*EntryWidth*/ 30,
128 /*MinLineWidth*/ 70);
129 out << "\n\n";
130 }
131 }
clang::ento::CheckerRegistry
Manages a set of available checkers for running a static analysis.
Definition: CheckerRegistry.h:82
ento
clang::ento::printEnabledCheckerList
void printEnabledCheckerList(raw_ostream &OS, ArrayRef< std::string > plugins, AnalyzerOptions &opts, DiagnosticsEngine &diags, const LangOptions &LangOpts)
Definition: CheckerRegistration.cpp:56
clang::ento::CheckerRegistry::printCheckerWithDescList
void printCheckerWithDescList(raw_ostream &Out, size_t MaxNameChars=30) const
Prints the name and description of all checkers in this registry.
Definition: CheckerRegistry.cpp:508
clang::ento::CheckerRegistry::printEnabledCheckerList
void printEnabledCheckerList(raw_ostream &Out) const
Definition: CheckerRegistry.cpp:558
clang::ento::printAnalyzerConfigList
void printAnalyzerConfigList(raw_ostream &OS)
Definition: CheckerRegistration.cpp:76
clang::ento::createCheckerManager
std::unique_ptr< CheckerManager > createCheckerManager(ASTContext &context, AnalyzerOptions &opts, ArrayRef< std::string > plugins, ArrayRef< std::function< void(CheckerRegistry &)>> checkerRegistrationFns, DiagnosticsEngine &diags)
Definition: CheckerRegistration.cpp:27
CheckerRegistry.h
AnalyzerOptions.h
clang::AnalyzerOptions::printFormattedEntry
static void printFormattedEntry(llvm::raw_ostream &Out, std::pair< StringRef, StringRef > EntryDescPair, size_t EntryWidth, size_t InitialPad, size_t MinLineWidth=0)
Convenience function for printing options or checkers and their description in a formatted manner...
Definition: AnalyzerOptions.cpp:33
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:168
CheckerManager.h
clang::LangOptions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:53
clang::ento::printCheckerHelp
void printCheckerHelp(raw_ostream &OS, ArrayRef< std::string > plugins, AnalyzerOptions &opts, DiagnosticsEngine &diags, const LangOptions &LangOpts)
Definition: CheckerRegistration.cpp:45
clang::DiagnosticsEngine
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:149
Diagnostic.h
Defines the Diagnostic-related interfaces.
llvm::ArrayRef< std::string >
FrontendDiagnostic.h
clang::ento::printCheckerConfigList
void printCheckerConfigList(raw_ostream &OS, ArrayRef< std::string > plugins, AnalyzerOptions &opts, DiagnosticsEngine &diags, const LangOptions &LangOpts)
Definition: CheckerRegistration.cpp:67
FrontendActions.h
CheckerRegistration.h
clang::ento::CheckerRegistry::printCheckerOptionList
void printCheckerOptionList(raw_ostream &Out) const
Definition: CheckerRegistry.cpp:566
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
clang::AnalyzerOptions
Stores options for the analyzer from the command line.
Definition: AnalyzerOptions.h:162