1 //===- AnalyzerOptions.cpp - Analysis Engine Options ----------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains special accessors for analyzer configuration options 10 // with string representations. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h" 15 #include "clang/StaticAnalyzer/Core/Checker.h" 16 #include "llvm/ADT/SmallString.h" 17 #include "llvm/ADT/StringSwitch.h" 18 #include "llvm/ADT/StringRef.h" 19 #include "llvm/ADT/Twine.h" 20 #include "llvm/Support/ErrorHandling.h" 21 #include "llvm/Support/FileSystem.h" 22 #include "llvm/Support/raw_ostream.h" 23 #include <cassert> 24 #include <cstddef> 25 #include <utility> 26 #include <vector> 27 28 using namespace clang; 29 using namespace ento; 30 using namespace llvm; 31 32 std::vector<StringRef> 33 AnalyzerOptions::getRegisteredCheckers(bool IncludeExperimental /* = false */) { 34 static const StringRef StaticAnalyzerChecks[] = { 35 #define GET_CHECKERS 36 #define CHECKER(FULLNAME, CLASS, HELPTEXT, DOC_URI) \ 37 FULLNAME, 38 #include "clang/StaticAnalyzer/Checkers/Checkers.inc" 39 #undef CHECKER 40 #undef GET_CHECKERS 41 }; 42 std::vector<StringRef> Result; 43 for (StringRef CheckName : StaticAnalyzerChecks) { 44 if (!CheckName.startswith("debug.") && 45 (IncludeExperimental || !CheckName.startswith("alpha."))) 46 Result.push_back(CheckName); 47 } 48 return Result; 49 } 50 51 ExplorationStrategyKind 52 AnalyzerOptions::getExplorationStrategy() const { 53 auto K = 54 llvm::StringSwitch<llvm::Optional<ExplorationStrategyKind>>( 55 ExplorationStrategy) 56 .Case("dfs", ExplorationStrategyKind::DFS) 57 .Case("bfs", ExplorationStrategyKind::BFS) 58 .Case("unexplored_first", 59 ExplorationStrategyKind::UnexploredFirst) 60 .Case("unexplored_first_queue", 61 ExplorationStrategyKind::UnexploredFirstQueue) 62 .Case("unexplored_first_location_queue", 63 ExplorationStrategyKind::UnexploredFirstLocationQueue) 64 .Case("bfs_block_dfs_contents", 65 ExplorationStrategyKind::BFSBlockDFSContents) 66 .Default(None); 67 assert(K.hasValue() && "User mode is invalid."); 68 return K.getValue(); 69 } 70 71 IPAKind AnalyzerOptions::getIPAMode() const { 72 auto K = llvm::StringSwitch<llvm::Optional<IPAKind>>(IPAMode) 73 .Case("none", IPAK_None) 74 .Case("basic-inlining", IPAK_BasicInlining) 75 .Case("inlining", IPAK_Inlining) 76 .Case("dynamic", IPAK_DynamicDispatch) 77 .Case("dynamic-bifurcate", IPAK_DynamicDispatchBifurcate) 78 .Default(None); 79 assert(K.hasValue() && "IPA Mode is invalid."); 80 81 return K.getValue(); 82 } 83 84 bool 85 AnalyzerOptions::mayInlineCXXMemberFunction( 86 CXXInlineableMemberKind Param) const { 87 if (getIPAMode() < IPAK_Inlining) 88 return false; 89 90 auto K = 91 llvm::StringSwitch<llvm::Optional<CXXInlineableMemberKind>>( 92 CXXMemberInliningMode) 93 .Case("constructors", CIMK_Constructors) 94 .Case("destructors", CIMK_Destructors) 95 .Case("methods", CIMK_MemberFunctions) 96 .Case("none", CIMK_None) 97 .Default(None); 98 99 assert(K.hasValue() && "Invalid c++ member function inlining mode."); 100 101 return *K >= Param; 102 } 103 104 StringRef AnalyzerOptions::getCheckerStringOption(StringRef OptionName, 105 StringRef DefaultVal, 106 const CheckerBase *C, 107 bool SearchInParents) const { 108 assert(C); 109 // Search for a package option if the option for the checker is not specified 110 // and search in parents is enabled. 111 StringRef CheckerName = C->getTagDescription(); 112 113 assert(!CheckerName.empty() && 114 "Empty checker name! Make sure the checker object (including it's " 115 "bases!) if fully initialized before calling this function!"); 116 ConfigTable::const_iterator E = Config.end(); 117 do { 118 ConfigTable::const_iterator I = 119 Config.find((Twine(CheckerName) + ":" + OptionName).str()); 120 if (I != E) 121 return StringRef(I->getValue()); 122 size_t Pos = CheckerName.rfind('.'); 123 if (Pos == StringRef::npos) 124 return DefaultVal; 125 CheckerName = CheckerName.substr(0, Pos); 126 } while (!CheckerName.empty() && SearchInParents); 127 return DefaultVal; 128 } 129 130 bool AnalyzerOptions::getCheckerBooleanOption(StringRef Name, bool DefaultVal, 131 const CheckerBase *C, 132 bool SearchInParents) const { 133 // FIXME: We should emit a warning here if the value is something other than 134 // "true", "false", or the empty string (meaning the default value), 135 // but the AnalyzerOptions doesn't have access to a diagnostic engine. 136 assert(C); 137 return llvm::StringSwitch<bool>( 138 getCheckerStringOption(Name, DefaultVal ? "true" : "false", C, 139 SearchInParents)) 140 .Case("true", true) 141 .Case("false", false) 142 .Default(DefaultVal); 143 } 144 145 int AnalyzerOptions::getCheckerIntegerOption(StringRef Name, int DefaultVal, 146 const CheckerBase *C, 147 bool SearchInParents) const { 148 int Ret = DefaultVal; 149 bool HasFailed = getCheckerStringOption(Name, std::to_string(DefaultVal), C, 150 SearchInParents) 151 .getAsInteger(10, Ret); 152 assert(!HasFailed && "analyzer-config option should be numeric"); 153 (void)HasFailed; 154 return Ret; 155 } 156