1 //===-- AnalyzerOptions.cpp - Analysis Engine Options -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains special accessors for analyzer configuration options
11 // with string representations.
13 //===----------------------------------------------------------------------===//
15 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/StringSwitch.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/raw_ostream.h"
21 using namespace clang;
24 AnalyzerOptions::UserModeKind AnalyzerOptions::getUserMode() {
25 if (UserMode == UMK_NotSet) {
27 Config.insert(std::make_pair("mode", "deep")).first->second;
28 UserMode = llvm::StringSwitch<UserModeKind>(ModeStr)
29 .Case("shallow", UMK_Shallow)
30 .Case("deep", UMK_Deep)
32 assert(UserMode != UMK_NotSet && "User mode is invalid.");
37 IPAKind AnalyzerOptions::getIPAMode() {
38 if (IPAMode == IPAK_NotSet) {
40 // Use the User Mode to set the default IPA value.
41 // Note, we have to add the string to the Config map for the ConfigDumper
42 // checker to function properly.
43 const char *DefaultIPA = nullptr;
44 UserModeKind HighLevelMode = getUserMode();
45 if (HighLevelMode == UMK_Shallow)
46 DefaultIPA = "inlining";
47 else if (HighLevelMode == UMK_Deep)
48 DefaultIPA = "dynamic-bifurcate";
51 // Lookup the ipa configuration option, use the default from User Mode.
53 Config.insert(std::make_pair("ipa", DefaultIPA)).first->second;
54 IPAKind IPAConfig = llvm::StringSwitch<IPAKind>(ModeStr)
55 .Case("none", IPAK_None)
56 .Case("basic-inlining", IPAK_BasicInlining)
57 .Case("inlining", IPAK_Inlining)
58 .Case("dynamic", IPAK_DynamicDispatch)
59 .Case("dynamic-bifurcate", IPAK_DynamicDispatchBifurcate)
60 .Default(IPAK_NotSet);
61 assert(IPAConfig != IPAK_NotSet && "IPA Mode is invalid.");
63 // Set the member variable.
71 AnalyzerOptions::mayInlineCXXMemberFunction(CXXInlineableMemberKind K) {
72 if (getIPAMode() < IPAK_Inlining)
75 if (!CXXMemberInliningMode) {
76 static const char *ModeKey = "c++-inlining";
79 Config.insert(std::make_pair(ModeKey, "destructors")).first->second;
81 CXXInlineableMemberKind &MutableMode =
82 const_cast<CXXInlineableMemberKind &>(CXXMemberInliningMode);
84 MutableMode = llvm::StringSwitch<CXXInlineableMemberKind>(ModeStr)
85 .Case("constructors", CIMK_Constructors)
86 .Case("destructors", CIMK_Destructors)
87 .Case("none", CIMK_None)
88 .Case("methods", CIMK_MemberFunctions)
89 .Default(CXXInlineableMemberKind());
92 // FIXME: We should emit a warning here about an unknown inlining kind,
93 // but the AnalyzerOptions doesn't have access to a diagnostic engine.
94 MutableMode = CIMK_None;
98 return CXXMemberInliningMode >= K;
101 static StringRef toString(bool b) { return b ? "true" : "false"; }
103 bool AnalyzerOptions::getBooleanOption(StringRef Name, bool DefaultVal) {
104 // FIXME: We should emit a warning here if the value is something other than
105 // "true", "false", or the empty string (meaning the default value),
106 // but the AnalyzerOptions doesn't have access to a diagnostic engine.
108 Config.insert(std::make_pair(Name, toString(DefaultVal))).first->second;
109 return llvm::StringSwitch<bool>(V)
111 .Case("false", false)
112 .Default(DefaultVal);
115 bool AnalyzerOptions::getBooleanOption(Optional<bool> &V, StringRef Name,
118 V = getBooleanOption(Name, DefaultVal);
122 bool AnalyzerOptions::includeTemporaryDtorsInCFG() {
123 return getBooleanOption(IncludeTemporaryDtorsInCFG,
124 "cfg-temporary-dtors",
125 /* Default = */ false);
128 bool AnalyzerOptions::mayInlineCXXStandardLibrary() {
129 return getBooleanOption(InlineCXXStandardLibrary,
130 "c++-stdlib-inlining",
134 bool AnalyzerOptions::mayInlineTemplateFunctions() {
135 return getBooleanOption(InlineTemplateFunctions,
136 "c++-template-inlining",
140 bool AnalyzerOptions::mayInlineCXXAllocator() {
141 return getBooleanOption(InlineCXXAllocator,
142 "c++-allocator-inlining",
146 bool AnalyzerOptions::mayInlineCXXContainerMethods() {
147 return getBooleanOption(InlineCXXContainerMethods,
148 "c++-container-inlining",
152 bool AnalyzerOptions::mayInlineCXXSharedPtrDtor() {
153 return getBooleanOption(InlineCXXSharedPtrDtor,
154 "c++-shared_ptr-inlining",
159 bool AnalyzerOptions::mayInlineObjCMethod() {
160 return getBooleanOption(ObjCInliningMode,
162 /* Default = */ true);
165 bool AnalyzerOptions::shouldSuppressNullReturnPaths() {
166 return getBooleanOption(SuppressNullReturnPaths,
167 "suppress-null-return-paths",
168 /* Default = */ true);
171 bool AnalyzerOptions::shouldAvoidSuppressingNullArgumentPaths() {
172 return getBooleanOption(AvoidSuppressingNullArgumentPaths,
173 "avoid-suppressing-null-argument-paths",
174 /* Default = */ false);
177 bool AnalyzerOptions::shouldSuppressInlinedDefensiveChecks() {
178 return getBooleanOption(SuppressInlinedDefensiveChecks,
179 "suppress-inlined-defensive-checks",
180 /* Default = */ true);
183 bool AnalyzerOptions::shouldSuppressFromCXXStandardLibrary() {
184 return getBooleanOption(SuppressFromCXXStandardLibrary,
185 "suppress-c++-stdlib",
186 /* Default = */ false);
189 bool AnalyzerOptions::shouldReportIssuesInMainSourceFile() {
190 return getBooleanOption(ReportIssuesInMainSourceFile,
191 "report-in-main-source-file",
192 /* Default = */ false);
196 bool AnalyzerOptions::shouldWriteStableReportFilename() {
197 return getBooleanOption(StableReportFilename,
198 "stable-report-filename",
199 /* Default = */ false);
202 int AnalyzerOptions::getOptionAsInteger(StringRef Name, int DefaultVal) {
203 SmallString<10> StrBuf;
204 llvm::raw_svector_ostream OS(StrBuf);
207 StringRef V = Config.insert(std::make_pair(Name, OS.str())).first->second;
208 int Res = DefaultVal;
209 bool b = V.getAsInteger(10, Res);
210 assert(!b && "analyzer-config option should be numeric");
215 unsigned AnalyzerOptions::getAlwaysInlineSize() {
216 if (!AlwaysInlineSize.hasValue())
217 AlwaysInlineSize = getOptionAsInteger("ipa-always-inline-size", 3);
218 return AlwaysInlineSize.getValue();
221 unsigned AnalyzerOptions::getMaxInlinableSize() {
222 if (!MaxInlinableSize.hasValue()) {
224 int DefaultValue = 0;
225 UserModeKind HighLevelMode = getUserMode();
226 switch (HighLevelMode) {
228 llvm_unreachable("Invalid mode.");
237 MaxInlinableSize = getOptionAsInteger("max-inlinable-size", DefaultValue);
239 return MaxInlinableSize.getValue();
242 unsigned AnalyzerOptions::getGraphTrimInterval() {
243 if (!GraphTrimInterval.hasValue())
244 GraphTrimInterval = getOptionAsInteger("graph-trim-interval", 1000);
245 return GraphTrimInterval.getValue();
248 unsigned AnalyzerOptions::getMaxTimesInlineLarge() {
249 if (!MaxTimesInlineLarge.hasValue())
250 MaxTimesInlineLarge = getOptionAsInteger("max-times-inline-large", 32);
251 return MaxTimesInlineLarge.getValue();
254 unsigned AnalyzerOptions::getMaxNodesPerTopLevelFunction() {
255 if (!MaxNodesPerTopLevelFunction.hasValue()) {
256 int DefaultValue = 0;
257 UserModeKind HighLevelMode = getUserMode();
258 switch (HighLevelMode) {
260 llvm_unreachable("Invalid mode.");
262 DefaultValue = 75000;
265 DefaultValue = 150000;
268 MaxNodesPerTopLevelFunction = getOptionAsInteger("max-nodes", DefaultValue);
270 return MaxNodesPerTopLevelFunction.getValue();
273 bool AnalyzerOptions::shouldSynthesizeBodies() {
274 return getBooleanOption("faux-bodies", true);
277 bool AnalyzerOptions::shouldPrunePaths() {
278 return getBooleanOption("prune-paths", true);
281 bool AnalyzerOptions::shouldConditionalizeStaticInitializers() {
282 return getBooleanOption("cfg-conditional-static-initializers", true);