1 //===-- AnalyzerOptions.cpp - Analysis Engine Options -----------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains special accessors for analyzer configuration options
11 // with string representations.
12 //
13 //===----------------------------------------------------------------------===//
14 
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"
20 
21 using namespace clang;
22 using namespace llvm;
23 
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)
30       .Default(UMK_NotSet);
31     assert(UserMode != UMK_NotSet && "User mode is invalid.");
32   }
33   return UserMode;
34 }
35 
36 IPAKind AnalyzerOptions::getIPAMode() {
37   if (IPAMode == IPAK_NotSet) {
38 
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";
48     assert(DefaultIPA);
49 
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.");
60 
61     // Set the member variable.
62     IPAMode = IPAConfig;
63   }
64 
65   return IPAMode;
66 }
67 
68 bool
69 AnalyzerOptions::mayInlineCXXMemberFunction(CXXInlineableMemberKind K) {
70   if (getIPAMode() < IPAK_Inlining)
71     return false;
72 
73   if (!CXXMemberInliningMode) {
74     static const char *ModeKey = "c++-inlining";
75 
76     StringRef ModeStr(Config.GetOrCreateValue(ModeKey,
77                                               "methods").getValue());
78 
79     CXXInlineableMemberKind &MutableMode =
80       const_cast<CXXInlineableMemberKind &>(CXXMemberInliningMode);
81 
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());
88 
89     if (!MutableMode) {
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;
93     }
94   }
95 
96   return CXXMemberInliningMode >= K;
97 }
98 
99 static StringRef toString(bool b) { return b ? "true" : "false"; }
100 
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)
107       .Case("true", true)
108       .Case("false", false)
109       .Default(DefaultVal);
110 }
111 
112 bool AnalyzerOptions::getBooleanOption(llvm::Optional<bool> &V,
113                                        StringRef Name,
114                                        bool DefaultVal) {
115   if (!V.hasValue())
116     V = getBooleanOption(Name, DefaultVal);
117   return V.getValue();
118 }
119 
120 bool AnalyzerOptions::includeTemporaryDtorsInCFG() {
121   return getBooleanOption(IncludeTemporaryDtorsInCFG,
122                           "cfg-temporary-dtors",
123                           /* Default = */ false);
124 }
125 
126 bool AnalyzerOptions::mayInlineCXXStandardLibrary() {
127   return getBooleanOption(InlineCXXStandardLibrary,
128                           "c++-stdlib-inlining",
129                           /*Default=*/true);
130 }
131 
132 bool AnalyzerOptions::mayInlineTemplateFunctions() {
133   return getBooleanOption(InlineTemplateFunctions,
134                           "c++-template-inlining",
135                           /*Default=*/true);
136 }
137 
138 bool AnalyzerOptions::mayInlineObjCMethod() {
139   return getBooleanOption(ObjCInliningMode,
140                           "objc-inlining",
141                           /* Default = */ true);
142 }
143 
144 bool AnalyzerOptions::shouldSuppressNullReturnPaths() {
145   return getBooleanOption(SuppressNullReturnPaths,
146                           "suppress-null-return-paths",
147                           /* Default = */ true);
148 }
149 
150 bool AnalyzerOptions::shouldAvoidSuppressingNullArgumentPaths() {
151   return getBooleanOption(AvoidSuppressingNullArgumentPaths,
152                           "avoid-suppressing-null-argument-paths",
153                           /* Default = */ false);
154 }
155 
156 int AnalyzerOptions::getOptionAsInteger(StringRef Name, int DefaultVal) {
157   SmallString<10> StrBuf;
158   llvm::raw_svector_ostream OS(StrBuf);
159   OS << DefaultVal;
160 
161   StringRef V(Config.GetOrCreateValue(Name, OS.str()).getValue());
162   int Res = DefaultVal;
163   bool b = V.getAsInteger(10, Res);
164   assert(!b && "analyzer-config option should be numeric");
165   (void) b;
166   return Res;
167 }
168 
169 unsigned AnalyzerOptions::getAlwaysInlineSize() {
170   if (!AlwaysInlineSize.hasValue())
171     AlwaysInlineSize = getOptionAsInteger("ipa-always-inline-size", 3);
172   return AlwaysInlineSize.getValue();
173 }
174 
175 unsigned AnalyzerOptions::getMaxInlinableSize() {
176   if (!MaxInlinableSize.hasValue()) {
177 
178     int DefaultValue = 0;
179     UserModeKind HighLevelMode = getUserMode();
180     switch (HighLevelMode) {
181       default:
182         llvm_unreachable("Invalid mode.");
183       case UMK_Shallow:
184         DefaultValue = 4;
185         break;
186       case UMK_Deep:
187         DefaultValue = 50;
188         break;
189     }
190 
191     MaxInlinableSize = getOptionAsInteger("max-inlinable-size", DefaultValue);
192   }
193   return MaxInlinableSize.getValue();
194 }
195 
196 unsigned AnalyzerOptions::getGraphTrimInterval() {
197   if (!GraphTrimInterval.hasValue())
198     GraphTrimInterval = getOptionAsInteger("graph-trim-interval", 1000);
199   return GraphTrimInterval.getValue();
200 }
201 
202 unsigned AnalyzerOptions::getMaxTimesInlineLarge() {
203   if (!MaxTimesInlineLarge.hasValue())
204     MaxTimesInlineLarge = getOptionAsInteger("max-times-inline-large", 32);
205   return MaxTimesInlineLarge.getValue();
206 }
207 
208 unsigned AnalyzerOptions::getMaxNodesPerTopLevelFunction() {
209   if (!MaxNodesPerTopLevelFunction.hasValue()) {
210     int DefaultValue = 0;
211     UserModeKind HighLevelMode = getUserMode();
212     switch (HighLevelMode) {
213       default:
214         llvm_unreachable("Invalid mode.");
215       case UMK_Shallow:
216         DefaultValue = 75000;
217         break;
218       case UMK_Deep:
219         DefaultValue = 150000;
220         break;
221     }
222     MaxNodesPerTopLevelFunction = getOptionAsInteger("max-nodes", DefaultValue);
223   }
224   return MaxNodesPerTopLevelFunction.getValue();
225 }
226 
227 bool AnalyzerOptions::shouldSynthesizeBodies() {
228   return getBooleanOption("faux-bodies", true);
229 }
230 
231 bool AnalyzerOptions::shouldPrunePaths() {
232   return getBooleanOption("prune-paths", true);
233 }
234