AnalyzerOptions.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. //===- AnalyzerOptions.cpp - Analysis Engine Options ----------------------===//
  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. #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. using namespace clang;
  28. using namespace ento;
  29. using namespace llvm;
  30. std::vector<StringRef>
  31. AnalyzerOptions::getRegisteredCheckers(bool IncludeExperimental /* = false */) {
  32. static const StringRef StaticAnalyzerChecks[] = {
  33. #define GET_CHECKERS
  34. #define CHECKER(FULLNAME, CLASS, DESCFILE, HELPTEXT, GROUPINDEX, HIDDEN) \
  35. FULLNAME,
  36. #include "clang/StaticAnalyzer/Checkers/Checkers.inc"
  37. #undef CHECKER
  38. #undef GET_CHECKERS
  39. };
  40. std::vector<StringRef> Result;
  41. for (StringRef CheckName : StaticAnalyzerChecks) {
  42. if (!CheckName.startswith("debug.") &&
  43. (IncludeExperimental || !CheckName.startswith("alpha.")))
  44. Result.push_back(CheckName);
  45. }
  46. return Result;
  47. }
  48. UserModeKind AnalyzerOptions::getUserMode() {
  49. if (!UserMode.hasValue()) {
  50. UserMode = getStringOption("mode", "deep");
  51. }
  52. auto K = llvm::StringSwitch<llvm::Optional<UserModeKind>>(*UserMode)
  53. .Case("shallow", UMK_Shallow)
  54. .Case("deep", UMK_Deep)
  55. .Default(None);
  56. assert(UserMode.hasValue() && "User mode is invalid.");
  57. return K.getValue();
  58. }
  59. ExplorationStrategyKind
  60. AnalyzerOptions::getExplorationStrategy() {
  61. if (!ExplorationStrategy.hasValue()) {
  62. ExplorationStrategy = getStringOption("exploration_strategy",
  63. "unexplored_first_queue");
  64. }
  65. auto K =
  66. llvm::StringSwitch<llvm::Optional<ExplorationStrategyKind>>(
  67. *ExplorationStrategy)
  68. .Case("dfs", ExplorationStrategyKind::DFS)
  69. .Case("bfs", ExplorationStrategyKind::BFS)
  70. .Case("unexplored_first",
  71. ExplorationStrategyKind::UnexploredFirst)
  72. .Case("unexplored_first_queue",
  73. ExplorationStrategyKind::UnexploredFirstQueue)
  74. .Case("unexplored_first_location_queue",
  75. ExplorationStrategyKind::UnexploredFirstLocationQueue)
  76. .Case("bfs_block_dfs_contents",
  77. ExplorationStrategyKind::BFSBlockDFSContents)
  78. .Default(None);
  79. assert(K.hasValue() && "User mode is invalid.");
  80. return K.getValue();
  81. }
  82. IPAKind AnalyzerOptions::getIPAMode() {
  83. if (!IPAMode.hasValue()) {
  84. switch (getUserMode()) {
  85. case UMK_Shallow:
  86. IPAMode = getStringOption("ipa", "inlining");
  87. break;
  88. case UMK_Deep:
  89. IPAMode = getStringOption("ipa", "dynamic-bifurcate");
  90. break;
  91. }
  92. }
  93. auto K = llvm::StringSwitch<llvm::Optional<IPAKind>>(*IPAMode)
  94. .Case("none", IPAK_None)
  95. .Case("basic-inlining", IPAK_BasicInlining)
  96. .Case("inlining", IPAK_Inlining)
  97. .Case("dynamic", IPAK_DynamicDispatch)
  98. .Case("dynamic-bifurcate", IPAK_DynamicDispatchBifurcate)
  99. .Default(None);
  100. assert(K.hasValue() && "IPA Mode is invalid.");
  101. return K.getValue();
  102. }
  103. bool
  104. AnalyzerOptions::mayInlineCXXMemberFunction(CXXInlineableMemberKind Param) {
  105. if (!CXXMemberInliningMode.hasValue()) {
  106. CXXMemberInliningMode = getStringOption("c++-inlining", "destructors");
  107. }
  108. if (getIPAMode() < IPAK_Inlining)
  109. return false;
  110. auto K =
  111. llvm::StringSwitch<llvm::Optional<CXXInlineableMemberKind>>(
  112. *CXXMemberInliningMode)
  113. .Case("constructors", CIMK_Constructors)
  114. .Case("destructors", CIMK_Destructors)
  115. .Case("methods", CIMK_MemberFunctions)
  116. .Case("none", CIMK_None)
  117. .Default(None);
  118. assert(K.hasValue() && "Invalid c++ member function inlining mode.");
  119. return *K >= Param;
  120. }
  121. StringRef AnalyzerOptions::getStringOption(StringRef OptionName,
  122. StringRef DefaultVal) {
  123. return Config.insert({OptionName, DefaultVal}).first->second;
  124. }
  125. static StringRef toString(bool B) { return (B ? "true" : "false"); }
  126. template <typename T>
  127. static StringRef toString(T) = delete;
  128. void AnalyzerOptions::initOption(Optional<StringRef> &V, StringRef Name,
  129. StringRef DefaultVal) {
  130. if (V.hasValue())
  131. return;
  132. V = getStringOption(Name, DefaultVal);
  133. }
  134. void AnalyzerOptions::initOption(Optional<bool> &V, StringRef Name,
  135. bool DefaultVal) {
  136. if (V.hasValue())
  137. return;
  138. // FIXME: We should emit a warning here if the value is something other than
  139. // "true", "false", or the empty string (meaning the default value),
  140. // but the AnalyzerOptions doesn't have access to a diagnostic engine.
  141. V = llvm::StringSwitch<bool>(getStringOption(Name, toString(DefaultVal)))
  142. .Case("true", true)
  143. .Case("false", false)
  144. .Default(DefaultVal);
  145. }
  146. void AnalyzerOptions::initOption(Optional<unsigned> &V, StringRef Name,
  147. unsigned DefaultVal) {
  148. if (V.hasValue())
  149. return;
  150. V = DefaultVal;
  151. bool HasFailed = getStringOption(Name, std::to_string(DefaultVal))
  152. .getAsInteger(10, *V);
  153. assert(!HasFailed && "analyzer-config option should be numeric");
  154. (void)HasFailed;
  155. }
  156. StringRef AnalyzerOptions::getCheckerStringOption(StringRef OptionName,
  157. StringRef DefaultVal,
  158. const CheckerBase *C,
  159. bool SearchInParents) const {
  160. assert(C);
  161. // Search for a package option if the option for the checker is not specified
  162. // and search in parents is enabled.
  163. StringRef CheckerName = C->getTagDescription();
  164. assert(!CheckerName.empty() &&
  165. "Empty checker name! Make sure the checker object (including it's "
  166. "bases!) if fully initialized before calling this function!");
  167. ConfigTable::const_iterator E = Config.end();
  168. do {
  169. ConfigTable::const_iterator I =
  170. Config.find((Twine(CheckerName) + ":" + OptionName).str());
  171. if (I != E)
  172. return StringRef(I->getValue());
  173. size_t Pos = CheckerName.rfind('.');
  174. if (Pos == StringRef::npos)
  175. return DefaultVal;
  176. CheckerName = CheckerName.substr(0, Pos);
  177. } while (!CheckerName.empty() && SearchInParents);
  178. return DefaultVal;
  179. }
  180. bool AnalyzerOptions::getCheckerBooleanOption(StringRef Name, bool DefaultVal,
  181. const CheckerBase *C,
  182. bool SearchInParents) const {
  183. // FIXME: We should emit a warning here if the value is something other than
  184. // "true", "false", or the empty string (meaning the default value),
  185. // but the AnalyzerOptions doesn't have access to a diagnostic engine.
  186. assert(C);
  187. return llvm::StringSwitch<bool>(
  188. getCheckerStringOption(Name, toString(DefaultVal), C, SearchInParents))
  189. .Case("true", true)
  190. .Case("false", false)
  191. .Default(DefaultVal);
  192. }
  193. int AnalyzerOptions::getCheckerIntegerOption(StringRef Name, int DefaultVal,
  194. const CheckerBase *C,
  195. bool SearchInParents) const {
  196. int Ret = DefaultVal;
  197. bool HasFailed = getCheckerStringOption(Name, std::to_string(DefaultVal), C,
  198. SearchInParents)
  199. .getAsInteger(10, Ret);
  200. assert(!HasFailed && "analyzer-config option should be numeric");
  201. (void)HasFailed;
  202. return Ret;
  203. }
  204. StringRef AnalyzerOptions::getCTUDir() {
  205. if (!CTUDir.hasValue()) {
  206. CTUDir = getStringOption("ctu-dir", "");
  207. if (!llvm::sys::fs::is_directory(*CTUDir))
  208. CTUDir = "";
  209. }
  210. return CTUDir.getValue();
  211. }