• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
getUserMode()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 
getIPAMode()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
mayInlineCXXMemberFunction(CXXInlineableMemberKind K)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                                               "constructors").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 
toString(bool b)99 static StringRef toString(bool b) { return b ? "true" : "false"; }
100 
getBooleanOption(StringRef Name,bool DefaultVal)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 
getBooleanOption(Optional<bool> & V,StringRef Name,bool DefaultVal)112 bool AnalyzerOptions::getBooleanOption(Optional<bool> &V, StringRef Name,
113                                        bool DefaultVal) {
114   if (!V.hasValue())
115     V = getBooleanOption(Name, DefaultVal);
116   return V.getValue();
117 }
118 
includeTemporaryDtorsInCFG()119 bool AnalyzerOptions::includeTemporaryDtorsInCFG() {
120   return getBooleanOption(IncludeTemporaryDtorsInCFG,
121                           "cfg-temporary-dtors",
122                           /* Default = */ false);
123 }
124 
mayInlineCXXStandardLibrary()125 bool AnalyzerOptions::mayInlineCXXStandardLibrary() {
126   return getBooleanOption(InlineCXXStandardLibrary,
127                           "c++-stdlib-inlining",
128                           /*Default=*/true);
129 }
130 
mayInlineTemplateFunctions()131 bool AnalyzerOptions::mayInlineTemplateFunctions() {
132   return getBooleanOption(InlineTemplateFunctions,
133                           "c++-template-inlining",
134                           /*Default=*/true);
135 }
136 
mayInlineObjCMethod()137 bool AnalyzerOptions::mayInlineObjCMethod() {
138   return getBooleanOption(ObjCInliningMode,
139                           "objc-inlining",
140                           /* Default = */ true);
141 }
142 
shouldSuppressNullReturnPaths()143 bool AnalyzerOptions::shouldSuppressNullReturnPaths() {
144   return getBooleanOption(SuppressNullReturnPaths,
145                           "suppress-null-return-paths",
146                           /* Default = */ true);
147 }
148 
shouldAvoidSuppressingNullArgumentPaths()149 bool AnalyzerOptions::shouldAvoidSuppressingNullArgumentPaths() {
150   return getBooleanOption(AvoidSuppressingNullArgumentPaths,
151                           "avoid-suppressing-null-argument-paths",
152                           /* Default = */ false);
153 }
154 
shouldSuppressInlinedDefensiveChecks()155 bool AnalyzerOptions::shouldSuppressInlinedDefensiveChecks() {
156   return getBooleanOption(SuppressInlinedDefensiveChecks,
157                           "suppress-inlined-defensive-checks",
158                           /* Default = */ true);
159 }
160 
getOptionAsInteger(StringRef Name,int DefaultVal)161 int AnalyzerOptions::getOptionAsInteger(StringRef Name, int DefaultVal) {
162   SmallString<10> StrBuf;
163   llvm::raw_svector_ostream OS(StrBuf);
164   OS << DefaultVal;
165 
166   StringRef V(Config.GetOrCreateValue(Name, OS.str()).getValue());
167   int Res = DefaultVal;
168   bool b = V.getAsInteger(10, Res);
169   assert(!b && "analyzer-config option should be numeric");
170   (void) b;
171   return Res;
172 }
173 
getAlwaysInlineSize()174 unsigned AnalyzerOptions::getAlwaysInlineSize() {
175   if (!AlwaysInlineSize.hasValue())
176     AlwaysInlineSize = getOptionAsInteger("ipa-always-inline-size", 3);
177   return AlwaysInlineSize.getValue();
178 }
179 
getMaxInlinableSize()180 unsigned AnalyzerOptions::getMaxInlinableSize() {
181   if (!MaxInlinableSize.hasValue()) {
182 
183     int DefaultValue = 0;
184     UserModeKind HighLevelMode = getUserMode();
185     switch (HighLevelMode) {
186       default:
187         llvm_unreachable("Invalid mode.");
188       case UMK_Shallow:
189         DefaultValue = 4;
190         break;
191       case UMK_Deep:
192         DefaultValue = 50;
193         break;
194     }
195 
196     MaxInlinableSize = getOptionAsInteger("max-inlinable-size", DefaultValue);
197   }
198   return MaxInlinableSize.getValue();
199 }
200 
getGraphTrimInterval()201 unsigned AnalyzerOptions::getGraphTrimInterval() {
202   if (!GraphTrimInterval.hasValue())
203     GraphTrimInterval = getOptionAsInteger("graph-trim-interval", 1000);
204   return GraphTrimInterval.getValue();
205 }
206 
getMaxTimesInlineLarge()207 unsigned AnalyzerOptions::getMaxTimesInlineLarge() {
208   if (!MaxTimesInlineLarge.hasValue())
209     MaxTimesInlineLarge = getOptionAsInteger("max-times-inline-large", 32);
210   return MaxTimesInlineLarge.getValue();
211 }
212 
getMaxNodesPerTopLevelFunction()213 unsigned AnalyzerOptions::getMaxNodesPerTopLevelFunction() {
214   if (!MaxNodesPerTopLevelFunction.hasValue()) {
215     int DefaultValue = 0;
216     UserModeKind HighLevelMode = getUserMode();
217     switch (HighLevelMode) {
218       default:
219         llvm_unreachable("Invalid mode.");
220       case UMK_Shallow:
221         DefaultValue = 75000;
222         break;
223       case UMK_Deep:
224         DefaultValue = 150000;
225         break;
226     }
227     MaxNodesPerTopLevelFunction = getOptionAsInteger("max-nodes", DefaultValue);
228   }
229   return MaxNodesPerTopLevelFunction.getValue();
230 }
231 
shouldSynthesizeBodies()232 bool AnalyzerOptions::shouldSynthesizeBodies() {
233   return getBooleanOption("faux-bodies", true);
234 }
235 
shouldPrunePaths()236 bool AnalyzerOptions::shouldPrunePaths() {
237   return getBooleanOption("prune-paths", true);
238 }
239