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) {
26 StringRef ModeStr(Config.GetOrCreateValue("mode", "deep").getValue());
27 UserMode = llvm::StringSwitch<UserModeKind>(ModeStr)
28 .Case("shallow", UMK_Shallow)
29 .Case("deep", UMK_Deep)
31 assert(UserMode != UMK_NotSet && "User mode is invalid.");
36 IPAKind AnalyzerOptions::getIPAMode() {
37 if (IPAMode == IPAK_NotSet) {
39 // Use the User Mode to set the default IPA value.
40 // Note, we have to add the string to the Config map for the ConfigDumper
41 // checker to function properly.
42 const char *DefaultIPA = 0;
43 UserModeKind HighLevelMode = getUserMode();
44 if (HighLevelMode == UMK_Shallow)
45 DefaultIPA = "inlining";
46 else if (HighLevelMode == UMK_Deep)
47 DefaultIPA = "dynamic-bifurcate";
50 // Lookup the ipa configuration option, use the default from User Mode.
51 StringRef ModeStr(Config.GetOrCreateValue("ipa", DefaultIPA).getValue());
52 IPAKind IPAConfig = llvm::StringSwitch<IPAKind>(ModeStr)
53 .Case("none", IPAK_None)
54 .Case("basic-inlining", IPAK_BasicInlining)
55 .Case("inlining", IPAK_Inlining)
56 .Case("dynamic", IPAK_DynamicDispatch)
57 .Case("dynamic-bifurcate", IPAK_DynamicDispatchBifurcate)
58 .Default(IPAK_NotSet);
59 assert(IPAConfig != IPAK_NotSet && "IPA Mode is invalid.");
61 // Set the member variable.
69 AnalyzerOptions::mayInlineCXXMemberFunction(CXXInlineableMemberKind K) {
70 if (getIPAMode() < IPAK_Inlining)
73 if (!CXXMemberInliningMode) {
74 static const char *ModeKey = "c++-inlining";
76 StringRef ModeStr(Config.GetOrCreateValue(ModeKey,
77 "destructors").getValue());
79 CXXInlineableMemberKind &MutableMode =
80 const_cast<CXXInlineableMemberKind &>(CXXMemberInliningMode);
82 MutableMode = llvm::StringSwitch<CXXInlineableMemberKind>(ModeStr)
83 .Case("constructors", CIMK_Constructors)
84 .Case("destructors", CIMK_Destructors)
85 .Case("none", CIMK_None)
86 .Case("methods", CIMK_MemberFunctions)
87 .Default(CXXInlineableMemberKind());
90 // FIXME: We should emit a warning here about an unknown inlining kind,
91 // but the AnalyzerOptions doesn't have access to a diagnostic engine.
92 MutableMode = CIMK_None;
96 return CXXMemberInliningMode >= K;
99 static StringRef toString(bool b) { return b ? "true" : "false"; }
101 bool AnalyzerOptions::getBooleanOption(StringRef Name, bool DefaultVal) {
102 // FIXME: We should emit a warning here if the value is something other than
103 // "true", "false", or the empty string (meaning the default value),
104 // but the AnalyzerOptions doesn't have access to a diagnostic engine.
105 StringRef V(Config.GetOrCreateValue(Name, toString(DefaultVal)).getValue());
106 return llvm::StringSwitch<bool>(V)
108 .Case("false", false)
109 .Default(DefaultVal);
112 bool AnalyzerOptions::getBooleanOption(Optional<bool> &V, StringRef Name,
115 V = getBooleanOption(Name, DefaultVal);
119 bool AnalyzerOptions::includeTemporaryDtorsInCFG() {
120 return getBooleanOption(IncludeTemporaryDtorsInCFG,
121 "cfg-temporary-dtors",
122 /* Default = */ false);
125 bool AnalyzerOptions::mayInlineCXXStandardLibrary() {
126 return getBooleanOption(InlineCXXStandardLibrary,
127 "c++-stdlib-inlining",
131 bool AnalyzerOptions::mayInlineTemplateFunctions() {
132 return getBooleanOption(InlineTemplateFunctions,
133 "c++-template-inlining",
137 bool AnalyzerOptions::mayInlineCXXContainerCtorsAndDtors() {
138 return getBooleanOption(InlineCXXContainerCtorsAndDtors,
139 "c++-container-inlining",
143 bool AnalyzerOptions::mayInlineCXXSharedPtrDtor() {
144 return getBooleanOption(InlineCXXSharedPtrDtor,
145 "c++-shared_ptr-inlining",
150 bool AnalyzerOptions::mayInlineObjCMethod() {
151 return getBooleanOption(ObjCInliningMode,
153 /* Default = */ true);
156 bool AnalyzerOptions::shouldSuppressNullReturnPaths() {
157 return getBooleanOption(SuppressNullReturnPaths,
158 "suppress-null-return-paths",
159 /* Default = */ true);
162 bool AnalyzerOptions::shouldAvoidSuppressingNullArgumentPaths() {
163 return getBooleanOption(AvoidSuppressingNullArgumentPaths,
164 "avoid-suppressing-null-argument-paths",
165 /* Default = */ false);
168 bool AnalyzerOptions::shouldSuppressInlinedDefensiveChecks() {
169 return getBooleanOption(SuppressInlinedDefensiveChecks,
170 "suppress-inlined-defensive-checks",
171 /* Default = */ true);
174 bool AnalyzerOptions::shouldSuppressFromCXXStandardLibrary() {
175 return getBooleanOption(SuppressFromCXXStandardLibrary,
176 "suppress-c++-stdlib",
177 /* Default = */ false);
180 bool AnalyzerOptions::shouldReportIssuesInMainSourceFile() {
181 return getBooleanOption(ReportIssuesInMainSourceFile,
182 "report-in-main-source-file",
183 /* Default = */ false);
186 int AnalyzerOptions::getOptionAsInteger(StringRef Name, int DefaultVal) {
187 SmallString<10> StrBuf;
188 llvm::raw_svector_ostream OS(StrBuf);
191 StringRef V(Config.GetOrCreateValue(Name, OS.str()).getValue());
192 int Res = DefaultVal;
193 bool b = V.getAsInteger(10, Res);
194 assert(!b && "analyzer-config option should be numeric");
199 unsigned AnalyzerOptions::getAlwaysInlineSize() {
200 if (!AlwaysInlineSize.hasValue())
201 AlwaysInlineSize = getOptionAsInteger("ipa-always-inline-size", 3);
202 return AlwaysInlineSize.getValue();
205 unsigned AnalyzerOptions::getMaxInlinableSize() {
206 if (!MaxInlinableSize.hasValue()) {
208 int DefaultValue = 0;
209 UserModeKind HighLevelMode = getUserMode();
210 switch (HighLevelMode) {
212 llvm_unreachable("Invalid mode.");
221 MaxInlinableSize = getOptionAsInteger("max-inlinable-size", DefaultValue);
223 return MaxInlinableSize.getValue();
226 unsigned AnalyzerOptions::getGraphTrimInterval() {
227 if (!GraphTrimInterval.hasValue())
228 GraphTrimInterval = getOptionAsInteger("graph-trim-interval", 1000);
229 return GraphTrimInterval.getValue();
232 unsigned AnalyzerOptions::getMaxTimesInlineLarge() {
233 if (!MaxTimesInlineLarge.hasValue())
234 MaxTimesInlineLarge = getOptionAsInteger("max-times-inline-large", 32);
235 return MaxTimesInlineLarge.getValue();
238 unsigned AnalyzerOptions::getMaxNodesPerTopLevelFunction() {
239 if (!MaxNodesPerTopLevelFunction.hasValue()) {
240 int DefaultValue = 0;
241 UserModeKind HighLevelMode = getUserMode();
242 switch (HighLevelMode) {
244 llvm_unreachable("Invalid mode.");
246 DefaultValue = 75000;
249 DefaultValue = 150000;
252 MaxNodesPerTopLevelFunction = getOptionAsInteger("max-nodes", DefaultValue);
254 return MaxNodesPerTopLevelFunction.getValue();
257 bool AnalyzerOptions::shouldSynthesizeBodies() {
258 return getBooleanOption("faux-bodies", true);
261 bool AnalyzerOptions::shouldPrunePaths() {
262 return getBooleanOption("prune-paths", true);
265 bool AnalyzerOptions::shouldConditionalizeStaticInitializers() {
266 return getBooleanOption("cfg-conditional-static-initializers", true);