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/AnalyzerHelpFlags.h"
14 #include "clang/Basic/Diagnostic.h"
15 #include "clang/Frontend/CompilerInstance.h"
16 #include "clang/Frontend/FrontendDiagnostic.h"
17 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
18 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
19 #include "clang/StaticAnalyzer/Frontend/CheckerRegistry.h"
20 #include "clang/StaticAnalyzer/Frontend/FrontendActions.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include <memory>
24 
25 using namespace clang;
26 using namespace ento;
27 
28 void ento::printCheckerHelp(raw_ostream &out, CompilerInstance &CI) {
29   out << "OVERVIEW: Clang Static Analyzer Checkers List\n\n";
30   out << "USAGE: -analyzer-checker <CHECKER or PACKAGE,...>\n\n";
31 
32   auto CheckerMgr = std::make_unique<CheckerManager>(
33       *CI.getAnalyzerOpts(), CI.getLangOpts(), CI.getDiagnostics(),
34       CI.getFrontendOpts().Plugins);
35 
36   CheckerMgr->getCheckerRegistry().printCheckerWithDescList(out);
37 }
38 
39 void ento::printEnabledCheckerList(raw_ostream &out, CompilerInstance &CI) {
40   out << "OVERVIEW: Clang Static Analyzer Enabled Checkers List\n\n";
41 
42   auto CheckerMgr = std::make_unique<CheckerManager>(
43       *CI.getAnalyzerOpts(), CI.getLangOpts(), CI.getDiagnostics(),
44       CI.getFrontendOpts().Plugins);
45 
46   CheckerMgr->getCheckerRegistry().printEnabledCheckerList(out);
47 }
48 
49 void ento::printCheckerConfigList(raw_ostream &out, CompilerInstance &CI) {
50 
51   auto CheckerMgr = std::make_unique<CheckerManager>(
52       *CI.getAnalyzerOpts(), CI.getLangOpts(), CI.getDiagnostics(),
53       CI.getFrontendOpts().Plugins);
54 
55   CheckerMgr->getCheckerRegistry().printCheckerOptionList(out);
56 }
57 
58 void ento::printAnalyzerConfigList(raw_ostream &out) {
59   // FIXME: This message sounds scary, should be scary, but incorrectly states
60   // that all configs are super dangerous. In reality, many of them should be
61   // accessible to the user. We should create a user-facing subset of config
62   // options under a different frontend flag.
63   out << R"(
64 OVERVIEW: Clang Static Analyzer -analyzer-config Option List
65 
66 The following list of configurations are meant for development purposes only, as
67 some of the variables they define are set to result in the most optimal
68 analysis. Setting them to other values may drastically change how the analyzer
69 behaves, and may even result in instabilities, crashes!
70 
71 USAGE: -analyzer-config <OPTION1=VALUE,OPTION2=VALUE,...>
72        -analyzer-config OPTION1=VALUE, -analyzer-config OPTION2=VALUE, ...
73 OPTIONS:
74 )";
75 
76   using OptionAndDescriptionTy = std::pair<StringRef, std::string>;
77   OptionAndDescriptionTy PrintableOptions[] = {
78 #define ANALYZER_OPTION(TYPE, NAME, CMDFLAG, DESC, DEFAULT_VAL)                \
79     {                                                                          \
80       CMDFLAG,                                                                 \
81       llvm::Twine(llvm::Twine() + "(" +                                        \
82                   (StringRef(#TYPE) == "StringRef" ? "string" : #TYPE ) +      \
83                   ") " DESC                                                    \
84                   " (default: " #DEFAULT_VAL ")").str()                        \
85     },
86 
87 #define ANALYZER_OPTION_DEPENDS_ON_USER_MODE(TYPE, NAME, CMDFLAG, DESC,        \
88                                              SHALLOW_VAL, DEEP_VAL)            \
89     {                                                                          \
90       CMDFLAG,                                                                 \
91       llvm::Twine(llvm::Twine() + "(" +                                        \
92                   (StringRef(#TYPE) == "StringRef" ? "string" : #TYPE ) +      \
93                   ") " DESC                                                    \
94                   " (default: " #SHALLOW_VAL " in shallow mode, " #DEEP_VAL    \
95                   " in deep mode)").str()                                      \
96     },
97 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.def"
98 #undef ANALYZER_OPTION
99 #undef ANALYZER_OPTION_DEPENDS_ON_USER_MODE
100   };
101 
102   llvm::sort(PrintableOptions, [](const OptionAndDescriptionTy &LHS,
103                                   const OptionAndDescriptionTy &RHS) {
104     return LHS.first < RHS.first;
105   });
106 
107   for (const auto &Pair : PrintableOptions) {
108     AnalyzerOptions::printFormattedEntry(out, Pair, /*InitialPad*/ 2,
109                                          /*EntryWidth*/ 30,
110                                          /*MinLineWidth*/ 70);
111     out << "\n\n";
112   }
113 }
114