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, IS_HIDDEN) \ 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 CheckerName, 105 StringRef OptionName, 106 bool SearchInParents) const { 107 assert(!CheckerName.empty() && 108 "Empty checker name! Make sure the checker object (including it's " 109 "bases!) if fully initialized before calling this function!"); 110 111 ConfigTable::const_iterator E = Config.end(); 112 do { 113 ConfigTable::const_iterator I = 114 Config.find((Twine(CheckerName) + ":" + OptionName).str()); 115 if (I != E) 116 return StringRef(I->getValue()); 117 size_t Pos = CheckerName.rfind('.'); 118 if (Pos == StringRef::npos) 119 break; 120 121 CheckerName = CheckerName.substr(0, Pos); 122 } while (!CheckerName.empty() && SearchInParents); 123 124 llvm_unreachable("Unknown checker option! Did you call getChecker*Option " 125 "with incorrect parameters? User input must've been " 126 "verified by CheckerRegistry."); 127 128 return ""; 129 } 130 131 StringRef AnalyzerOptions::getCheckerStringOption(const ento::CheckerBase *C, 132 StringRef OptionName, 133 bool SearchInParents) const { 134 return getCheckerStringOption( 135 C->getTagDescription(), OptionName, SearchInParents); 136 } 137 138 bool AnalyzerOptions::getCheckerBooleanOption(StringRef CheckerName, 139 StringRef OptionName, 140 bool SearchInParents) const { 141 auto Ret = llvm::StringSwitch<llvm::Optional<bool>>( 142 getCheckerStringOption(CheckerName, OptionName, 143 SearchInParents)) 144 .Case("true", true) 145 .Case("false", false) 146 .Default(None); 147 148 assert(Ret && 149 "This option should be either 'true' or 'false', and should've been " 150 "validated by CheckerRegistry!"); 151 152 return *Ret; 153 } 154 155 bool AnalyzerOptions::getCheckerBooleanOption(const ento::CheckerBase *C, 156 StringRef OptionName, 157 bool SearchInParents) const { 158 return getCheckerBooleanOption( 159 C->getTagDescription(), OptionName, SearchInParents); 160 } 161 162 int AnalyzerOptions::getCheckerIntegerOption(StringRef CheckerName, 163 StringRef OptionName, 164 bool SearchInParents) const { 165 int Ret = 0; 166 bool HasFailed = getCheckerStringOption(CheckerName, OptionName, 167 SearchInParents) 168 .getAsInteger(0, Ret); 169 assert(!HasFailed && 170 "This option should be numeric, and should've been validated by " 171 "CheckerRegistry!"); 172 (void)HasFailed; 173 return Ret; 174 } 175 176 int AnalyzerOptions::getCheckerIntegerOption(const ento::CheckerBase *C, 177 StringRef OptionName, 178 bool SearchInParents) const { 179 return getCheckerIntegerOption( 180 C->getTagDescription(), OptionName, SearchInParents); 181 } 182