clang 22.0.0git
AnalyzerOptions.cpp
Go to the documentation of this file.
1//===- AnalyzerOptions.cpp - Analysis Engine Options ----------------------===//
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// This file contains special accessors for analyzer configuration options
10// with string representations.
11//
12//===----------------------------------------------------------------------===//
13
16#include "llvm/ADT/StringRef.h"
17#include "llvm/ADT/StringSwitch.h"
18#include "llvm/ADT/Twine.h"
19#include "llvm/Support/ErrorHandling.h"
20#include "llvm/Support/FormattedStream.h"
21#include "llvm/Support/raw_ostream.h"
22#include <cassert>
23#include <cstddef>
24#include <optional>
25#include <utility>
26
27using namespace clang;
28using namespace ento;
29using namespace llvm;
30
32 llvm::raw_ostream &Out,
33 std::pair<StringRef, StringRef> EntryDescPair,
34 size_t InitialPad, size_t EntryWidth, size_t MinLineWidth) {
35
36 llvm::formatted_raw_ostream FOut(Out);
37
38 const size_t PadForDesc = InitialPad + EntryWidth;
39
40 FOut.PadToColumn(InitialPad) << EntryDescPair.first;
41 // If the buffer's length is greater than PadForDesc, print a newline.
42 if (FOut.getColumn() > PadForDesc)
43 FOut << '\n';
44
45 FOut.PadToColumn(PadForDesc);
46
47 if (MinLineWidth == 0) {
48 FOut << EntryDescPair.second;
49 return;
50 }
51
52 for (char C : EntryDescPair.second) {
53 if (FOut.getColumn() > MinLineWidth && C == ' ') {
54 FOut << '\n';
55 FOut.PadToColumn(PadForDesc);
56 continue;
57 }
58 FOut << C;
59 }
60}
61
64 auto K =
65 llvm::StringSwitch<std::optional<ExplorationStrategyKind>>(
66 ExplorationStrategy)
69 .Case("unexplored_first", ExplorationStrategyKind::UnexploredFirst)
70 .Case("unexplored_first_queue",
72 .Case("unexplored_first_location_queue",
74 .Case("bfs_block_dfs_contents",
76 .Default(std::nullopt);
77 assert(K && "User mode is invalid.");
78 return *K;
79}
80
82 auto K = llvm::StringSwitch<std::optional<CTUPhase1InliningKind>>(
83 CTUPhase1InliningMode)
84 .Case("none", CTUPhase1InliningKind::None)
85 .Case("small", CTUPhase1InliningKind::Small)
86 .Case("all", CTUPhase1InliningKind::All)
87 .Default(std::nullopt);
88 assert(K && "CTU inlining mode is invalid.");
89 return *K;
90}
91
93 auto K = llvm::StringSwitch<std::optional<IPAKind>>(IPAMode)
94 .Case("none", IPAK_None)
95 .Case("basic-inlining", IPAK_BasicInlining)
96 .Case("inlining", IPAK_Inlining)
97 .Case("dynamic", IPAK_DynamicDispatch)
98 .Case("dynamic-bifurcate", IPAK_DynamicDispatchBifurcate)
99 .Default(std::nullopt);
100 assert(K && "IPA Mode is invalid.");
101
102 return *K;
103}
104
105bool
107 CXXInlineableMemberKind Param) const {
109 return false;
110
111 auto K = llvm::StringSwitch<std::optional<CXXInlineableMemberKind>>(
112 CXXMemberInliningMode)
113 .Case("constructors", CIMK_Constructors)
114 .Case("destructors", CIMK_Destructors)
115 .Case("methods", CIMK_MemberFunctions)
116 .Case("none", CIMK_None)
117 .Default(std::nullopt);
118
119 assert(K && "Invalid c++ member function inlining mode.");
120
121 return *K >= Param;
122}
123
124StringRef AnalyzerOptions::getCheckerStringOption(StringRef CheckerName,
125 StringRef OptionName,
126 bool SearchInParents) const {
127 assert(!CheckerName.empty() &&
128 "Empty checker name! Make sure the checker object (including it's "
129 "bases!) if fully initialized before calling this function!");
130
131 ConfigTable::const_iterator E = Config.end();
132 do {
133 ConfigTable::const_iterator I =
134 Config.find((Twine(CheckerName) + ":" + OptionName).str());
135 if (I != E)
136 return StringRef(I->getValue());
137 size_t Pos = CheckerName.rfind('.');
138 if (Pos == StringRef::npos)
139 break;
140
141 CheckerName = CheckerName.substr(0, Pos);
142 } while (!CheckerName.empty() && SearchInParents);
143
144 llvm_unreachable("Unknown checker option! Did you call getChecker*Option "
145 "with incorrect parameters? User input must've been "
146 "verified by CheckerRegistry.");
147
148 return "";
149}
150
152 StringRef OptionName,
153 bool SearchInParents) const {
154 return getCheckerStringOption(C->getName(), OptionName, SearchInParents);
155}
156
158 StringRef OptionName,
159 bool SearchInParents) const {
160 auto Ret =
161 llvm::StringSwitch<std::optional<bool>>(
162 getCheckerStringOption(CheckerName, OptionName, SearchInParents))
163 .Case("true", true)
164 .Case("false", false)
165 .Default(std::nullopt);
166
167 assert(Ret &&
168 "This option should be either 'true' or 'false', and should've been "
169 "validated by CheckerRegistry!");
170
171 return *Ret;
172}
173
175 StringRef OptionName,
176 bool SearchInParents) const {
177 return getCheckerBooleanOption(C->getName(), OptionName, SearchInParents);
178}
179
181 StringRef OptionName,
182 bool SearchInParents) const {
183 int Ret = 0;
184 bool HasFailed = getCheckerStringOption(CheckerName, OptionName,
185 SearchInParents)
186 .getAsInteger(0, Ret);
187 assert(!HasFailed &&
188 "This option should be numeric, and should've been validated by "
189 "CheckerRegistry!");
190 (void)HasFailed;
191 return Ret;
192}
193
195 StringRef OptionName,
196 bool SearchInParents) const {
197 return getCheckerIntegerOption(C->getName(), OptionName, SearchInParents);
198}
Expr * E
bool mayInlineCXXMemberFunction(CXXInlineableMemberKind K) const
Returns the option controlling which C++ member functions will be considered for inlining.
ConfigTable Config
A key-value table of use-specified configuration values.
bool getCheckerBooleanOption(StringRef CheckerName, StringRef OptionName, bool SearchInParents=false) const
Interprets an option's string value as a boolean.
IPAKind getIPAMode() const
Returns the inter-procedural analysis mode.
CTUPhase1InliningKind getCTUPhase1Inlining() const
int getCheckerIntegerOption(StringRef CheckerName, StringRef OptionName, bool SearchInParents=false) const
Interprets an option's string value as an integer value.
static void printFormattedEntry(llvm::raw_ostream &Out, std::pair< StringRef, StringRef > EntryDescPair, size_t InitialPad, size_t EntryWidth, size_t MinLineWidth=0)
Convenience function for printing options or checkers and their description in a formatted manner.
ExplorationStrategyKind getExplorationStrategy() const
StringRef getCheckerStringOption(StringRef CheckerName, StringRef OptionName, bool SearchInParents=false) const
Query an option's string value.
The non-templated common ancestor of all the simple Checker<...> classes.
Definition: Checker.h:541
The JSON file list parser is used to communicate input to InstallAPI.
IPAKind
Describes the different modes of inter-procedural analysis.
@ IPAK_Inlining
Inline callees(C, C++, ObjC) when their definitions are available.
@ IPAK_BasicInlining
Inline C functions and blocks when their definitions are available.
@ IPAK_None
Perform only intra-procedural analysis.
@ IPAK_DynamicDispatch
Enable inlining of dynamically dispatched methods.
@ IPAK_DynamicDispatchBifurcate
Enable inlining of dynamically dispatched methods, bifurcate paths when exact type info is unavailabl...
CXXInlineableMemberKind
Describes the different kinds of C++ member functions which can be considered for inlining by the ana...
@ CIMK_Destructors
Refers to destructors (implicit or explicit).
@ CIMK_MemberFunctions
Refers to regular member function and operator calls.
@ CIMK_Constructors
Refers to constructors (implicit or explicit).
@ CIMK_None
A dummy mode in which no C++ inlining is enabled.
CTUPhase1InliningKind
ExplorationStrategyKind
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30