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 "clang/StaticAnalyzer/Core/Checker.h" 17 #include "llvm/ADT/SmallString.h" 18 #include "llvm/ADT/StringSwitch.h" 19 #include "llvm/Support/ErrorHandling.h" 20 #include "llvm/Support/raw_ostream.h" 21 22 using namespace clang; 23 using namespace ento; 24 using namespace llvm; 25 26 std::vector<StringRef> 27 AnalyzerOptions::getRegisteredCheckers(bool IncludeExperimental /* = false */) { 28 static const StringRef StaticAnalyzerChecks[] = { 29 #define GET_CHECKERS 30 #define CHECKER(FULLNAME, CLASS, DESCFILE, HELPTEXT, GROUPINDEX, HIDDEN) \ 31 FULLNAME, 32 #include "clang/StaticAnalyzer/Checkers/Checkers.inc" 33 #undef CHECKER 34 #undef GET_CHECKERS 35 }; 36 std::vector<StringRef> Result; 37 for (StringRef CheckName : StaticAnalyzerChecks) { 38 if (!CheckName.startswith("debug.") && 39 (IncludeExperimental || !CheckName.startswith("alpha."))) 40 Result.push_back(CheckName); 41 } 42 return Result; 43 } 44 45 AnalyzerOptions::UserModeKind AnalyzerOptions::getUserMode() { 46 if (UserMode == UMK_NotSet) { 47 StringRef ModeStr = 48 Config.insert(std::make_pair("mode", "deep")).first->second; 49 UserMode = llvm::StringSwitch<UserModeKind>(ModeStr) 50 .Case("shallow", UMK_Shallow) 51 .Case("deep", UMK_Deep) 52 .Default(UMK_NotSet); 53 assert(UserMode != UMK_NotSet && "User mode is invalid."); 54 } 55 return UserMode; 56 } 57 58 IPAKind AnalyzerOptions::getIPAMode() { 59 if (IPAMode == IPAK_NotSet) { 60 61 // Use the User Mode to set the default IPA value. 62 // Note, we have to add the string to the Config map for the ConfigDumper 63 // checker to function properly. 64 const char *DefaultIPA = nullptr; 65 UserModeKind HighLevelMode = getUserMode(); 66 if (HighLevelMode == UMK_Shallow) 67 DefaultIPA = "inlining"; 68 else if (HighLevelMode == UMK_Deep) 69 DefaultIPA = "dynamic-bifurcate"; 70 assert(DefaultIPA); 71 72 // Lookup the ipa configuration option, use the default from User Mode. 73 StringRef ModeStr = 74 Config.insert(std::make_pair("ipa", DefaultIPA)).first->second; 75 IPAKind IPAConfig = llvm::StringSwitch<IPAKind>(ModeStr) 76 .Case("none", IPAK_None) 77 .Case("basic-inlining", IPAK_BasicInlining) 78 .Case("inlining", IPAK_Inlining) 79 .Case("dynamic", IPAK_DynamicDispatch) 80 .Case("dynamic-bifurcate", IPAK_DynamicDispatchBifurcate) 81 .Default(IPAK_NotSet); 82 assert(IPAConfig != IPAK_NotSet && "IPA Mode is invalid."); 83 84 // Set the member variable. 85 IPAMode = IPAConfig; 86 } 87 88 return IPAMode; 89 } 90 91 bool 92 AnalyzerOptions::mayInlineCXXMemberFunction(CXXInlineableMemberKind K) { 93 if (getIPAMode() < IPAK_Inlining) 94 return false; 95 96 if (!CXXMemberInliningMode) { 97 static const char *ModeKey = "c++-inlining"; 98 99 StringRef ModeStr = 100 Config.insert(std::make_pair(ModeKey, "destructors")).first->second; 101 102 CXXInlineableMemberKind &MutableMode = 103 const_cast<CXXInlineableMemberKind &>(CXXMemberInliningMode); 104 105 MutableMode = llvm::StringSwitch<CXXInlineableMemberKind>(ModeStr) 106 .Case("constructors", CIMK_Constructors) 107 .Case("destructors", CIMK_Destructors) 108 .Case("none", CIMK_None) 109 .Case("methods", CIMK_MemberFunctions) 110 .Default(CXXInlineableMemberKind()); 111 112 if (!MutableMode) { 113 // FIXME: We should emit a warning here about an unknown inlining kind, 114 // but the AnalyzerOptions doesn't have access to a diagnostic engine. 115 MutableMode = CIMK_None; 116 } 117 } 118 119 return CXXMemberInliningMode >= K; 120 } 121 122 static StringRef toString(bool b) { return b ? "true" : "false"; } 123 124 StringRef AnalyzerOptions::getCheckerOption(StringRef CheckerName, 125 StringRef OptionName, 126 StringRef Default, 127 bool SearchInParents) { 128 // Search for a package option if the option for the checker is not specified 129 // and search in parents is enabled. 130 ConfigTable::const_iterator E = Config.end(); 131 do { 132 ConfigTable::const_iterator I = 133 Config.find((Twine(CheckerName) + ":" + OptionName).str()); 134 if (I != E) 135 return StringRef(I->getValue()); 136 size_t Pos = CheckerName.rfind('.'); 137 if (Pos == StringRef::npos) 138 return Default; 139 CheckerName = CheckerName.substr(0, Pos); 140 } while (!CheckerName.empty() && SearchInParents); 141 return Default; 142 } 143 144 bool AnalyzerOptions::getBooleanOption(StringRef Name, bool DefaultVal, 145 const CheckerBase *C, 146 bool SearchInParents) { 147 // FIXME: We should emit a warning here if the value is something other than 148 // "true", "false", or the empty string (meaning the default value), 149 // but the AnalyzerOptions doesn't have access to a diagnostic engine. 150 StringRef Default = toString(DefaultVal); 151 StringRef V = 152 C ? getCheckerOption(C->getTagDescription(), Name, Default, 153 SearchInParents) 154 : StringRef(Config.insert(std::make_pair(Name, Default)).first->second); 155 return llvm::StringSwitch<bool>(V) 156 .Case("true", true) 157 .Case("false", false) 158 .Default(DefaultVal); 159 } 160 161 bool AnalyzerOptions::getBooleanOption(Optional<bool> &V, StringRef Name, 162 bool DefaultVal, const CheckerBase *C, 163 bool SearchInParents) { 164 if (!V.hasValue()) 165 V = getBooleanOption(Name, DefaultVal, C, SearchInParents); 166 return V.getValue(); 167 } 168 169 bool AnalyzerOptions::includeTemporaryDtorsInCFG() { 170 return getBooleanOption(IncludeTemporaryDtorsInCFG, 171 "cfg-temporary-dtors", 172 /* Default = */ false); 173 } 174 175 bool AnalyzerOptions::mayInlineCXXStandardLibrary() { 176 return getBooleanOption(InlineCXXStandardLibrary, 177 "c++-stdlib-inlining", 178 /*Default=*/true); 179 } 180 181 bool AnalyzerOptions::mayInlineTemplateFunctions() { 182 return getBooleanOption(InlineTemplateFunctions, 183 "c++-template-inlining", 184 /*Default=*/true); 185 } 186 187 bool AnalyzerOptions::mayInlineCXXAllocator() { 188 return getBooleanOption(InlineCXXAllocator, 189 "c++-allocator-inlining", 190 /*Default=*/false); 191 } 192 193 bool AnalyzerOptions::mayInlineCXXContainerMethods() { 194 return getBooleanOption(InlineCXXContainerMethods, 195 "c++-container-inlining", 196 /*Default=*/false); 197 } 198 199 bool AnalyzerOptions::mayInlineCXXSharedPtrDtor() { 200 return getBooleanOption(InlineCXXSharedPtrDtor, 201 "c++-shared_ptr-inlining", 202 /*Default=*/false); 203 } 204 205 206 bool AnalyzerOptions::mayInlineObjCMethod() { 207 return getBooleanOption(ObjCInliningMode, 208 "objc-inlining", 209 /* Default = */ true); 210 } 211 212 bool AnalyzerOptions::shouldSuppressNullReturnPaths() { 213 return getBooleanOption(SuppressNullReturnPaths, 214 "suppress-null-return-paths", 215 /* Default = */ true); 216 } 217 218 bool AnalyzerOptions::shouldAvoidSuppressingNullArgumentPaths() { 219 return getBooleanOption(AvoidSuppressingNullArgumentPaths, 220 "avoid-suppressing-null-argument-paths", 221 /* Default = */ false); 222 } 223 224 bool AnalyzerOptions::shouldSuppressInlinedDefensiveChecks() { 225 return getBooleanOption(SuppressInlinedDefensiveChecks, 226 "suppress-inlined-defensive-checks", 227 /* Default = */ true); 228 } 229 230 bool AnalyzerOptions::shouldSuppressFromCXXStandardLibrary() { 231 return getBooleanOption(SuppressFromCXXStandardLibrary, 232 "suppress-c++-stdlib", 233 /* Default = */ true); 234 } 235 236 bool AnalyzerOptions::shouldReportIssuesInMainSourceFile() { 237 return getBooleanOption(ReportIssuesInMainSourceFile, 238 "report-in-main-source-file", 239 /* Default = */ false); 240 } 241 242 243 bool AnalyzerOptions::shouldWriteStableReportFilename() { 244 return getBooleanOption(StableReportFilename, 245 "stable-report-filename", 246 /* Default = */ false); 247 } 248 249 int AnalyzerOptions::getOptionAsInteger(StringRef Name, int DefaultVal, 250 const CheckerBase *C, 251 bool SearchInParents) { 252 SmallString<10> StrBuf; 253 llvm::raw_svector_ostream OS(StrBuf); 254 OS << DefaultVal; 255 256 StringRef V = C ? getCheckerOption(C->getTagDescription(), Name, OS.str(), 257 SearchInParents) 258 : StringRef(Config.insert(std::make_pair(Name, OS.str())) 259 .first->second); 260 261 int Res = DefaultVal; 262 bool b = V.getAsInteger(10, Res); 263 assert(!b && "analyzer-config option should be numeric"); 264 (void)b; 265 return Res; 266 } 267 268 StringRef AnalyzerOptions::getOptionAsString(StringRef Name, 269 StringRef DefaultVal, 270 const CheckerBase *C, 271 bool SearchInParents) { 272 return C ? getCheckerOption(C->getTagDescription(), Name, DefaultVal, 273 SearchInParents) 274 : StringRef( 275 Config.insert(std::make_pair(Name, DefaultVal)).first->second); 276 } 277 278 unsigned AnalyzerOptions::getAlwaysInlineSize() { 279 if (!AlwaysInlineSize.hasValue()) 280 AlwaysInlineSize = getOptionAsInteger("ipa-always-inline-size", 3); 281 return AlwaysInlineSize.getValue(); 282 } 283 284 unsigned AnalyzerOptions::getMaxInlinableSize() { 285 if (!MaxInlinableSize.hasValue()) { 286 287 int DefaultValue = 0; 288 UserModeKind HighLevelMode = getUserMode(); 289 switch (HighLevelMode) { 290 default: 291 llvm_unreachable("Invalid mode."); 292 case UMK_Shallow: 293 DefaultValue = 4; 294 break; 295 case UMK_Deep: 296 DefaultValue = 50; 297 break; 298 } 299 300 MaxInlinableSize = getOptionAsInteger("max-inlinable-size", DefaultValue); 301 } 302 return MaxInlinableSize.getValue(); 303 } 304 305 unsigned AnalyzerOptions::getGraphTrimInterval() { 306 if (!GraphTrimInterval.hasValue()) 307 GraphTrimInterval = getOptionAsInteger("graph-trim-interval", 1000); 308 return GraphTrimInterval.getValue(); 309 } 310 311 unsigned AnalyzerOptions::getMaxTimesInlineLarge() { 312 if (!MaxTimesInlineLarge.hasValue()) 313 MaxTimesInlineLarge = getOptionAsInteger("max-times-inline-large", 32); 314 return MaxTimesInlineLarge.getValue(); 315 } 316 317 unsigned AnalyzerOptions::getMinCFGSizeTreatFunctionsAsLarge() { 318 if (!MinCFGSizeTreatFunctionsAsLarge.hasValue()) 319 MinCFGSizeTreatFunctionsAsLarge = getOptionAsInteger( 320 "min-cfg-size-treat-functions-as-large", 14); 321 return MinCFGSizeTreatFunctionsAsLarge.getValue(); 322 } 323 324 unsigned AnalyzerOptions::getMaxNodesPerTopLevelFunction() { 325 if (!MaxNodesPerTopLevelFunction.hasValue()) { 326 int DefaultValue = 0; 327 UserModeKind HighLevelMode = getUserMode(); 328 switch (HighLevelMode) { 329 default: 330 llvm_unreachable("Invalid mode."); 331 case UMK_Shallow: 332 DefaultValue = 75000; 333 break; 334 case UMK_Deep: 335 DefaultValue = 150000; 336 break; 337 } 338 MaxNodesPerTopLevelFunction = getOptionAsInteger("max-nodes", DefaultValue); 339 } 340 return MaxNodesPerTopLevelFunction.getValue(); 341 } 342 343 bool AnalyzerOptions::shouldSynthesizeBodies() { 344 return getBooleanOption("faux-bodies", true); 345 } 346 347 bool AnalyzerOptions::shouldPrunePaths() { 348 return getBooleanOption("prune-paths", true); 349 } 350 351 bool AnalyzerOptions::shouldConditionalizeStaticInitializers() { 352 return getBooleanOption("cfg-conditional-static-initializers", true); 353 } 354 355 bool AnalyzerOptions::shouldInlineLambdas() { 356 if (!InlineLambdas.hasValue()) 357 InlineLambdas = getBooleanOption("inline-lambdas", /*Default=*/true); 358 return InlineLambdas.getValue(); 359 } 360 361 bool AnalyzerOptions::shouldWidenLoops() { 362 if (!WidenLoops.hasValue()) 363 WidenLoops = getBooleanOption("widen-loops", /*Default=*/false); 364 return WidenLoops.getValue(); 365 } 366 367 bool AnalyzerOptions::shouldDisplayNotesAsEvents() { 368 if (!DisplayNotesAsEvents.hasValue()) 369 DisplayNotesAsEvents = 370 getBooleanOption("notes-as-events", /*Default=*/false); 371 return DisplayNotesAsEvents.getValue(); 372 } 373