clang  16.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/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 <utility>
27 #include <vector>
28 
29 using namespace clang;
30 using namespace ento;
31 using namespace llvm;
32 
34  llvm::raw_ostream &Out,
35  std::pair<StringRef, StringRef> EntryDescPair,
36  size_t InitialPad, size_t EntryWidth, size_t MinLineWidth) {
37 
38  llvm::formatted_raw_ostream FOut(Out);
39 
40  const size_t PadForDesc = InitialPad + EntryWidth;
41 
42  FOut.PadToColumn(InitialPad) << EntryDescPair.first;
43  // If the buffer's length is greater than PadForDesc, print a newline.
44  if (FOut.getColumn() > PadForDesc)
45  FOut << '\n';
46 
47  FOut.PadToColumn(PadForDesc);
48 
49  if (MinLineWidth == 0) {
50  FOut << EntryDescPair.second;
51  return;
52  }
53 
54  for (char C : EntryDescPair.second) {
55  if (FOut.getColumn() > MinLineWidth && C == ' ') {
56  FOut << '\n';
57  FOut.PadToColumn(PadForDesc);
58  continue;
59  }
60  FOut << C;
61  }
62 }
63 
66  auto K =
67  llvm::StringSwitch<llvm::Optional<ExplorationStrategyKind>>(
68  ExplorationStrategy)
69  .Case("dfs", ExplorationStrategyKind::DFS)
70  .Case("bfs", ExplorationStrategyKind::BFS)
71  .Case("unexplored_first",
73  .Case("unexplored_first_queue",
75  .Case("unexplored_first_location_queue",
77  .Case("bfs_block_dfs_contents",
79  .Default(None);
80  assert(K && "User mode is invalid.");
81  return K.value();
82 }
83 
85  auto K = llvm::StringSwitch<llvm::Optional<CTUPhase1InliningKind>>(
86  CTUPhase1InliningMode)
87  .Case("none", CTUPhase1InliningKind::None)
88  .Case("small", CTUPhase1InliningKind::Small)
89  .Case("all", CTUPhase1InliningKind::All)
90  .Default(None);
91  assert(K && "CTU inlining mode is invalid.");
92  return K.value();
93 }
94 
96  auto K = llvm::StringSwitch<llvm::Optional<IPAKind>>(IPAMode)
97  .Case("none", IPAK_None)
98  .Case("basic-inlining", IPAK_BasicInlining)
99  .Case("inlining", IPAK_Inlining)
100  .Case("dynamic", IPAK_DynamicDispatch)
101  .Case("dynamic-bifurcate", IPAK_DynamicDispatchBifurcate)
102  .Default(None);
103  assert(K && "IPA Mode is invalid.");
104 
105  return K.value();
106 }
107 
108 bool
110  CXXInlineableMemberKind Param) const {
111  if (getIPAMode() < IPAK_Inlining)
112  return false;
113 
114  auto K =
115  llvm::StringSwitch<llvm::Optional<CXXInlineableMemberKind>>(
116  CXXMemberInliningMode)
117  .Case("constructors", CIMK_Constructors)
118  .Case("destructors", CIMK_Destructors)
119  .Case("methods", CIMK_MemberFunctions)
120  .Case("none", CIMK_None)
121  .Default(None);
122 
123  assert(K && "Invalid c++ member function inlining mode.");
124 
125  return *K >= Param;
126 }
127 
128 StringRef AnalyzerOptions::getCheckerStringOption(StringRef CheckerName,
129  StringRef OptionName,
130  bool SearchInParents) const {
131  assert(!CheckerName.empty() &&
132  "Empty checker name! Make sure the checker object (including it's "
133  "bases!) if fully initialized before calling this function!");
134 
135  ConfigTable::const_iterator E = Config.end();
136  do {
137  ConfigTable::const_iterator I =
138  Config.find((Twine(CheckerName) + ":" + OptionName).str());
139  if (I != E)
140  return StringRef(I->getValue());
141  size_t Pos = CheckerName.rfind('.');
142  if (Pos == StringRef::npos)
143  break;
144 
145  CheckerName = CheckerName.substr(0, Pos);
146  } while (!CheckerName.empty() && SearchInParents);
147 
148  llvm_unreachable("Unknown checker option! Did you call getChecker*Option "
149  "with incorrect parameters? User input must've been "
150  "verified by CheckerRegistry.");
151 
152  return "";
153 }
154 
156  StringRef OptionName,
157  bool SearchInParents) const {
158  return getCheckerStringOption(
159  C->getTagDescription(), OptionName, SearchInParents);
160 }
161 
162 bool AnalyzerOptions::getCheckerBooleanOption(StringRef CheckerName,
163  StringRef OptionName,
164  bool SearchInParents) const {
165  auto Ret = llvm::StringSwitch<llvm::Optional<bool>>(
166  getCheckerStringOption(CheckerName, OptionName,
167  SearchInParents))
168  .Case("true", true)
169  .Case("false", false)
170  .Default(None);
171 
172  assert(Ret &&
173  "This option should be either 'true' or 'false', and should've been "
174  "validated by CheckerRegistry!");
175 
176  return *Ret;
177 }
178 
180  StringRef OptionName,
181  bool SearchInParents) const {
182  return getCheckerBooleanOption(
183  C->getTagDescription(), OptionName, SearchInParents);
184 }
185 
187  StringRef OptionName,
188  bool SearchInParents) const {
189  int Ret = 0;
190  bool HasFailed = getCheckerStringOption(CheckerName, OptionName,
191  SearchInParents)
192  .getAsInteger(0, Ret);
193  assert(!HasFailed &&
194  "This option should be numeric, and should've been validated by "
195  "CheckerRegistry!");
196  (void)HasFailed;
197  return Ret;
198 }
199 
201  StringRef OptionName,
202  bool SearchInParents) const {
203  return getCheckerIntegerOption(
204  C->getTagDescription(), OptionName, SearchInParents);
205 }
clang::IPAK_BasicInlining
@ IPAK_BasicInlining
Inline C functions and blocks when their definitions are available.
Definition: AnalyzerOptions.h:95
AnalyzerOptions.h
llvm
YAML serialization mapping.
Definition: Dominators.h:30
clang::CTUPhase1InliningKind::None
@ None
Ret
static bool Ret(InterpState &S, CodePtr &PC, APValue &Result)
Definition: Interp.cpp:34
clang::ExplorationStrategyKind::DFS
@ DFS
clang::ExplorationStrategyKind::UnexploredFirstLocationQueue
@ UnexploredFirstLocationQueue
clang::ExplorationStrategyKind::BFS
@ BFS
clang::AnalyzerOptions::getCTUPhase1Inlining
CTUPhase1InliningKind getCTUPhase1Inlining() const
Definition: AnalyzerOptions.cpp:84
clang::CTUPhase1InliningKind::Small
@ Small
clang::IPAK_Inlining
@ IPAK_Inlining
Inline callees(C, C++, ObjC) when their definitions are available.
Definition: AnalyzerOptions.h:98
clang::AnalyzerOptions::getExplorationStrategy
ExplorationStrategyKind getExplorationStrategy() const
Definition: AnalyzerOptions.cpp:65
clang::CIMK_Constructors
@ CIMK_Constructors
Refers to constructors (implicit or explicit).
Definition: AnalyzerOptions.h:83
clang::AnalyzerOptions::getCheckerStringOption
StringRef getCheckerStringOption(StringRef CheckerName, StringRef OptionName, bool SearchInParents=false) const
Query an option's string value.
Definition: AnalyzerOptions.cpp:128
clang::IPAK_DynamicDispatchBifurcate
@ IPAK_DynamicDispatchBifurcate
Enable inlining of dynamically dispatched methods, bifurcate paths when exact type info is unavailabl...
Definition: AnalyzerOptions.h:105
clang::AnalyzerOptions::mayInlineCXXMemberFunction
bool mayInlineCXXMemberFunction(CXXInlineableMemberKind K) const
Returns the option controlling which C++ member functions will be considered for inlining.
Definition: AnalyzerOptions.cpp:109
clang::CIMK_None
@ CIMK_None
A dummy mode in which no C++ inlining is enabled.
Definition: AnalyzerOptions.h:74
clang::IPAK_DynamicDispatch
@ IPAK_DynamicDispatch
Enable inlining of dynamically dispatched methods.
Definition: AnalyzerOptions.h:101
clang::CXXInlineableMemberKind
CXXInlineableMemberKind
Describes the different kinds of C++ member functions which can be considered for inlining by the ana...
Definition: AnalyzerOptions.h:70
clang::CTUPhase1InliningKind::All
@ All
clang::AnalyzerOptions::getCheckerIntegerOption
int getCheckerIntegerOption(StringRef CheckerName, StringRef OptionName, bool SearchInParents=false) const
Interprets an option's string value as an integer value.
Definition: AnalyzerOptions.cpp:186
clang::ExplorationStrategyKind
ExplorationStrategyKind
Definition: AnalyzerOptions.h:108
clang::IPAK_None
@ IPAK_None
Perform only intra-procedural analysis.
Definition: AnalyzerOptions.h:92
clang::AnalyzerOptions::getIPAMode
IPAKind getIPAMode() const
Returns the inter-procedural analysis mode.
Definition: AnalyzerOptions.cpp:95
clang::AnalyzerOptions::printFormattedEntry
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.
Definition: AnalyzerOptions.cpp:33
clang::ExplorationStrategyKind::BFSBlockDFSContents
@ BFSBlockDFSContents
clang::ento::CheckerBase
Definition: Checker.h:492
clang::ExplorationStrategyKind::UnexploredFirstQueue
@ UnexploredFirstQueue
clang::CTUPhase1InliningKind
CTUPhase1InliningKind
Definition: AnalyzerOptions.h:126
clang::ExplorationStrategyKind::UnexploredFirst
@ UnexploredFirst
clang::AnalyzerOptions::getCheckerBooleanOption
bool getCheckerBooleanOption(StringRef CheckerName, StringRef OptionName, bool SearchInParents=false) const
Interprets an option's string value as a boolean.
Definition: AnalyzerOptions.cpp:162
Checker.h
clang
Definition: CalledOnceCheck.h:17
clang::CIMK_Destructors
@ CIMK_Destructors
Refers to destructors (implicit or explicit).
Definition: AnalyzerOptions.h:86
clang::CIMK_MemberFunctions
@ CIMK_MemberFunctions
Refers to regular member function and operator calls.
Definition: AnalyzerOptions.h:77
clang::IPAKind
IPAKind
Describes the different modes of inter-procedural analysis.
Definition: AnalyzerOptions.h:90