clang  9.0.0svn
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 
14 #include "clang/Basic/Diagnostic.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/Support/FormattedStream.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include <memory>
24 
25 using namespace clang;
26 using namespace ento;
27 
28 std::unique_ptr<CheckerManager> ento::createCheckerManager(
29  ASTContext &context,
30  AnalyzerOptions &opts,
31  ArrayRef<std::string> plugins,
32  ArrayRef<std::function<void(CheckerRegistry &)>> checkerRegistrationFns,
33  DiagnosticsEngine &diags) {
34  auto checkerMgr = llvm::make_unique<CheckerManager>(context, opts);
35 
36  CheckerRegistry allCheckers(plugins, diags, opts, context.getLangOpts(),
37  checkerRegistrationFns);
38 
39  allCheckers.initializeManager(*checkerMgr);
40  allCheckers.validateCheckerOptions();
41  checkerMgr->finishedCheckerRegistration();
42 
43  return checkerMgr;
44 }
45 
46 void ento::printCheckerHelp(raw_ostream &out, ArrayRef<std::string> plugins,
47  AnalyzerOptions &anopts,
48  DiagnosticsEngine &diags,
49  const LangOptions &langOpts) {
50  out << "OVERVIEW: Clang Static Analyzer Checkers List\n\n";
51  out << "USAGE: -analyzer-checker <CHECKER or PACKAGE,...>\n\n";
52 
53  CheckerRegistry(plugins, diags, anopts, langOpts)
55 }
56 
57 void ento::printEnabledCheckerList(raw_ostream &out,
58  ArrayRef<std::string> plugins,
59  AnalyzerOptions &anopts,
60  DiagnosticsEngine &diags,
61  const LangOptions &langOpts) {
62  out << "OVERVIEW: Clang Static Analyzer Enabled Checkers List\n\n";
63 
64  CheckerRegistry(plugins, diags, anopts, langOpts)
66 }
67 
68 void ento::printAnalyzerConfigList(raw_ostream &out) {
69  out << "OVERVIEW: Clang Static Analyzer -analyzer-config Option List\n\n";
70  out << "USAGE: clang -cc1 [CLANG_OPTIONS] -analyzer-config "
71  "<OPTION1=VALUE,OPTION2=VALUE,...>\n\n";
72  out << " clang -cc1 [CLANG_OPTIONS] -analyzer-config OPTION1=VALUE, "
73  "-analyzer-config OPTION2=VALUE, ...\n\n";
74  out << " clang [CLANG_OPTIONS] -Xclang -analyzer-config -Xclang"
75  "<OPTION1=VALUE,OPTION2=VALUE,...>\n\n";
76  out << " clang [CLANG_OPTIONS] -Xclang -analyzer-config -Xclang "
77  "OPTION1=VALUE, -Xclang -analyzer-config -Xclang "
78  "OPTION2=VALUE, ...\n\n";
79  out << "OPTIONS:\n\n";
80 
81  using OptionAndDescriptionTy = std::pair<StringRef, std::string>;
82  OptionAndDescriptionTy PrintableOptions[] = {
83 #define ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, DEFAULT_VAL) \
84  { \
85  CMDFLAG, \
86  llvm::Twine(llvm::Twine() + "(" + \
87  (StringRef(#TYPE) == "StringRef" ? "string" : #TYPE ) + \
88  ") " DESC \
89  " (default: " #DEFAULT_VAL ")").str() \
90  },
91 
92 #define ANALYZER_OPTION_DEPENDS_ON_USER_MODE(TYPE, NAME, CMDFLAG, DESC, \
93  SHALLOW_VAL, DEEP_VAL) \
94  { \
95  CMDFLAG, \
96  llvm::Twine(llvm::Twine() + "(" + \
97  (StringRef(#TYPE) == "StringRef" ? "string" : #TYPE ) + \
98  ") " DESC \
99  " (default: " #SHALLOW_VAL " in shallow mode, " #DEEP_VAL \
100  " in deep mode)").str() \
101  },
102 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.def"
103 #undef ANALYZER_OPTION
104 #undef ANALYZER_OPTION_DEPENDS_ON_USER_MODE
105  };
106 
107  llvm::sort(PrintableOptions, [](const OptionAndDescriptionTy &LHS,
108  const OptionAndDescriptionTy &RHS) {
109  return LHS.first < RHS.first;
110  });
111 
112  constexpr size_t MinLineWidth = 70;
113  constexpr size_t PadForOpt = 2;
114  constexpr size_t OptionWidth = 30;
115  constexpr size_t PadForDesc = PadForOpt + OptionWidth;
116  static_assert(MinLineWidth > PadForDesc, "MinLineWidth must be greater!");
117 
118  llvm::formatted_raw_ostream FOut(out);
119 
120  for (const auto &Pair : PrintableOptions) {
121  FOut.PadToColumn(PadForOpt) << Pair.first;
122 
123  // If the buffer's length is greater then PadForDesc, print a newline.
124  if (FOut.getColumn() > PadForDesc)
125  FOut << '\n';
126 
127  FOut.PadToColumn(PadForDesc);
128 
129  for (char C : Pair.second) {
130  if (FOut.getColumn() > MinLineWidth && C == ' ') {
131  FOut << '\n';
132  FOut.PadToColumn(PadForDesc);
133  continue;
134  }
135  FOut << C;
136  }
137  FOut << "\n\n";
138  }
139 }
Manages a set of available checkers for running a static analysis.
void printEnabledCheckerList(raw_ostream &OS, ArrayRef< std::string > plugins, AnalyzerOptions &opts, DiagnosticsEngine &diags, const LangOptions &LangOpts)
void printCheckerWithDescList(raw_ostream &Out, size_t MaxNameChars=30) const
Prints the name and description of all checkers in this registry.
void printEnabledCheckerList(raw_ostream &Out) const
void printAnalyzerConfigList(raw_ostream &OS)
std::unique_ptr< CheckerManager > createCheckerManager(ASTContext &context, AnalyzerOptions &opts, ArrayRef< std::string > plugins, ArrayRef< std::function< void(CheckerRegistry &)>> checkerRegistrationFns, DiagnosticsEngine &diags)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:154
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:49
void printCheckerHelp(raw_ostream &OS, ArrayRef< std::string > plugins, AnalyzerOptions &opts, DiagnosticsEngine &diags, const LangOptions &LangOpts)
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:148
Defines the Diagnostic-related interfaces.
Dataflow Directional Tag Classes.
Stores options for the analyzer from the command line.