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
14#include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
15#include "clang/StaticAnalyzer/Core/Checker.h"
16#include "llvm/ADT/SmallString.h"
17#include "llvm/ADT/StringSwitch.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/ADT/Twine.h"
20#include "llvm/Support/ErrorHandling.h"
21#include "llvm/Support/FileSystem.h"
22#include "llvm/Support/FormattedStream.h"
23#include "llvm/Support/raw_ostream.h"
24#include <cassert>
25#include <cstddef>
26#include <optional>
27#include <utility>
28#include <vector>
29
30using namespace clang;
31using namespace ento;
32using namespace llvm;
33
34void AnalyzerOptions::printFormattedEntry(
35 llvm::raw_ostream &Out,
36 std::pair<StringRef, StringRef> EntryDescPair,
37 size_t InitialPad, size_t EntryWidth, size_t MinLineWidth) {
38
39 llvm::formatted_raw_ostream FOut(Out);
40
41 const size_t PadForDesc = InitialPad + EntryWidth;
42
43 FOut.PadToColumn(NewCol: InitialPad) << EntryDescPair.first;
44 // If the buffer's length is greater than PadForDesc, print a newline.
45 if (FOut.getColumn() > PadForDesc)
46 FOut << '\n';
47
48 FOut.PadToColumn(NewCol: PadForDesc);
49
50 if (MinLineWidth == 0) {
51 FOut << EntryDescPair.second;
52 return;
53 }
54
55 for (char C : EntryDescPair.second) {
56 if (FOut.getColumn() > MinLineWidth && C == ' ') {
57 FOut << '\n';
58 FOut.PadToColumn(NewCol: PadForDesc);
59 continue;
60 }
61 FOut << C;
62 }
63}
64
65ExplorationStrategyKind
66AnalyzerOptions::getExplorationStrategy() const {
67 auto K =
68 llvm::StringSwitch<std::optional<ExplorationStrategyKind>>(
69 ExplorationStrategy)
70 .Case(S: "dfs", Value: ExplorationStrategyKind::DFS)
71 .Case(S: "bfs", Value: ExplorationStrategyKind::BFS)
72 .Case(S: "unexplored_first", Value: ExplorationStrategyKind::UnexploredFirst)
73 .Case(S: "unexplored_first_queue",
74 Value: ExplorationStrategyKind::UnexploredFirstQueue)
75 .Case(S: "unexplored_first_location_queue",
76 Value: ExplorationStrategyKind::UnexploredFirstLocationQueue)
77 .Case(S: "bfs_block_dfs_contents",
78 Value: ExplorationStrategyKind::BFSBlockDFSContents)
79 .Default(Value: std::nullopt);
80 assert(K && "User mode is invalid.");
81 return *K;
82}
83
84CTUPhase1InliningKind AnalyzerOptions::getCTUPhase1Inlining() const {
85 auto K = llvm::StringSwitch<std::optional<CTUPhase1InliningKind>>(
86 CTUPhase1InliningMode)
87 .Case(S: "none", Value: CTUPhase1InliningKind::None)
88 .Case(S: "small", Value: CTUPhase1InliningKind::Small)
89 .Case(S: "all", Value: CTUPhase1InliningKind::All)
90 .Default(Value: std::nullopt);
91 assert(K && "CTU inlining mode is invalid.");
92 return *K;
93}
94
95IPAKind AnalyzerOptions::getIPAMode() const {
96 auto K = llvm::StringSwitch<std::optional<IPAKind>>(IPAMode)
97 .Case(S: "none", Value: IPAK_None)
98 .Case(S: "basic-inlining", Value: IPAK_BasicInlining)
99 .Case(S: "inlining", Value: IPAK_Inlining)
100 .Case(S: "dynamic", Value: IPAK_DynamicDispatch)
101 .Case(S: "dynamic-bifurcate", Value: IPAK_DynamicDispatchBifurcate)
102 .Default(Value: std::nullopt);
103 assert(K && "IPA Mode is invalid.");
104
105 return *K;
106}
107
108bool
109AnalyzerOptions::mayInlineCXXMemberFunction(
110 CXXInlineableMemberKind Param) const {
111 if (getIPAMode() < IPAK_Inlining)
112 return false;
113
114 auto K = llvm::StringSwitch<std::optional<CXXInlineableMemberKind>>(
115 CXXMemberInliningMode)
116 .Case(S: "constructors", Value: CIMK_Constructors)
117 .Case(S: "destructors", Value: CIMK_Destructors)
118 .Case(S: "methods", Value: CIMK_MemberFunctions)
119 .Case(S: "none", Value: CIMK_None)
120 .Default(Value: std::nullopt);
121
122 assert(K && "Invalid c++ member function inlining mode.");
123
124 return *K >= Param;
125}
126
127StringRef AnalyzerOptions::getCheckerStringOption(StringRef CheckerName,
128 StringRef OptionName,
129 bool SearchInParents) const {
130 assert(!CheckerName.empty() &&
131 "Empty checker name! Make sure the checker object (including it's "
132 "bases!) if fully initialized before calling this function!");
133
134 ConfigTable::const_iterator E = Config.end();
135 do {
136 ConfigTable::const_iterator I =
137 Config.find(Key: (Twine(CheckerName) + ":" + OptionName).str());
138 if (I != E)
139 return StringRef(I->getValue());
140 size_t Pos = CheckerName.rfind(C: '.');
141 if (Pos == StringRef::npos)
142 break;
143
144 CheckerName = CheckerName.substr(Start: 0, N: Pos);
145 } while (!CheckerName.empty() && SearchInParents);
146
147 llvm_unreachable("Unknown checker option! Did you call getChecker*Option "
148 "with incorrect parameters? User input must've been "
149 "verified by CheckerRegistry.");
150
151 return "";
152}
153
154StringRef AnalyzerOptions::getCheckerStringOption(const ento::CheckerBase *C,
155 StringRef OptionName,
156 bool SearchInParents) const {
157 return getCheckerStringOption(
158 CheckerName: C->getTagDescription(), OptionName, SearchInParents);
159}
160
161bool AnalyzerOptions::getCheckerBooleanOption(StringRef CheckerName,
162 StringRef OptionName,
163 bool SearchInParents) const {
164 auto Ret =
165 llvm::StringSwitch<std::optional<bool>>(
166 getCheckerStringOption(CheckerName, OptionName, SearchInParents))
167 .Case(S: "true", Value: true)
168 .Case(S: "false", Value: false)
169 .Default(Value: std::nullopt);
170
171 assert(Ret &&
172 "This option should be either 'true' or 'false', and should've been "
173 "validated by CheckerRegistry!");
174
175 return *Ret;
176}
177
178bool AnalyzerOptions::getCheckerBooleanOption(const ento::CheckerBase *C,
179 StringRef OptionName,
180 bool SearchInParents) const {
181 return getCheckerBooleanOption(
182 CheckerName: C->getTagDescription(), OptionName, SearchInParents);
183}
184
185int AnalyzerOptions::getCheckerIntegerOption(StringRef CheckerName,
186 StringRef OptionName,
187 bool SearchInParents) const {
188 int Ret = 0;
189 bool HasFailed = getCheckerStringOption(CheckerName, OptionName,
190 SearchInParents)
191 .getAsInteger(Radix: 0, Result&: Ret);
192 assert(!HasFailed &&
193 "This option should be numeric, and should've been validated by "
194 "CheckerRegistry!");
195 (void)HasFailed;
196 return Ret;
197}
198
199int AnalyzerOptions::getCheckerIntegerOption(const ento::CheckerBase *C,
200 StringRef OptionName,
201 bool SearchInParents) const {
202 return getCheckerIntegerOption(
203 CheckerName: C->getTagDescription(), OptionName, SearchInParents);
204}
205

source code of clang/lib/StaticAnalyzer/Core/AnalyzerOptions.cpp