AnalyzerOptions.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
  14. #include "clang/StaticAnalyzer/Core/Checker.h"
  15. #include "llvm/ADT/SmallString.h"
  16. #include "llvm/ADT/StringSwitch.h"
  17. #include "llvm/ADT/StringRef.h"
  18. #include "llvm/ADT/Twine.h"
  19. #include "llvm/Support/ErrorHandling.h"
  20. #include "llvm/Support/FileSystem.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. #include <cassert>
  23. #include <cstddef>
  24. #include <utility>
  25. #include <vector>
  26. using namespace clang;
  27. using namespace ento;
  28. using namespace llvm;
  29. std::vector<StringRef>
  30. AnalyzerOptions::getRegisteredCheckers(bool IncludeExperimental /* = false */) {
  31. static const StringRef StaticAnalyzerChecks[] = {
  32. #define GET_CHECKERS
  33. #define CHECKER(FULLNAME, CLASS, HELPTEXT, DOC_URI, IS_HIDDEN) \
  34. FULLNAME,
  35. #include "clang/StaticAnalyzer/Checkers/Checkers.inc"
  36. #undef CHECKER
  37. #undef GET_CHECKERS
  38. };
  39. std::vector<StringRef> Result;
  40. for (StringRef CheckName : StaticAnalyzerChecks) {
  41. if (!CheckName.startswith("debug.") &&
  42. (IncludeExperimental || !CheckName.startswith("alpha.")))
  43. Result.push_back(CheckName);
  44. }
  45. return Result;
  46. }
  47. ExplorationStrategyKind
  48. AnalyzerOptions::getExplorationStrategy() const {
  49. auto K =
  50. llvm::StringSwitch<llvm::Optional<ExplorationStrategyKind>>(
  51. ExplorationStrategy)
  52. .Case("dfs", ExplorationStrategyKind::DFS)
  53. .Case("bfs", ExplorationStrategyKind::BFS)
  54. .Case("unexplored_first",
  55. ExplorationStrategyKind::UnexploredFirst)
  56. .Case("unexplored_first_queue",
  57. ExplorationStrategyKind::UnexploredFirstQueue)
  58. .Case("unexplored_first_location_queue",
  59. ExplorationStrategyKind::UnexploredFirstLocationQueue)
  60. .Case("bfs_block_dfs_contents",
  61. ExplorationStrategyKind::BFSBlockDFSContents)
  62. .Default(None);
  63. assert(K.hasValue() && "User mode is invalid.");
  64. return K.getValue();
  65. }
  66. IPAKind AnalyzerOptions::getIPAMode() const {
  67. auto K = llvm::StringSwitch<llvm::Optional<IPAKind>>(IPAMode)
  68. .Case("none", IPAK_None)
  69. .Case("basic-inlining", IPAK_BasicInlining)
  70. .Case("inlining", IPAK_Inlining)
  71. .Case("dynamic", IPAK_DynamicDispatch)
  72. .Case("dynamic-bifurcate", IPAK_DynamicDispatchBifurcate)
  73. .Default(None);
  74. assert(K.hasValue() && "IPA Mode is invalid.");
  75. return K.getValue();
  76. }
  77. bool
  78. AnalyzerOptions::mayInlineCXXMemberFunction(
  79. CXXInlineableMemberKind Param) const {
  80. if (getIPAMode() < IPAK_Inlining)
  81. return false;
  82. auto K =
  83. llvm::StringSwitch<llvm::Optional<CXXInlineableMemberKind>>(
  84. CXXMemberInliningMode)
  85. .Case("constructors", CIMK_Constructors)
  86. .Case("destructors", CIMK_Destructors)
  87. .Case("methods", CIMK_MemberFunctions)
  88. .Case("none", CIMK_None)
  89. .Default(None);
  90. assert(K.hasValue() && "Invalid c++ member function inlining mode.");
  91. return *K >= Param;
  92. }
  93. StringRef AnalyzerOptions::getCheckerStringOption(StringRef CheckerName,
  94. StringRef OptionName,
  95. bool SearchInParents) const {
  96. assert(!CheckerName.empty() &&
  97. "Empty checker name! Make sure the checker object (including it's "
  98. "bases!) if fully initialized before calling this function!");
  99. ConfigTable::const_iterator E = Config.end();
  100. do {
  101. ConfigTable::const_iterator I =
  102. Config.find((Twine(CheckerName) + ":" + OptionName).str());
  103. if (I != E)
  104. return StringRef(I->getValue());
  105. size_t Pos = CheckerName.rfind('.');
  106. if (Pos == StringRef::npos)
  107. break;
  108. CheckerName = CheckerName.substr(0, Pos);
  109. } while (!CheckerName.empty() && SearchInParents);
  110. llvm_unreachable("Unknown checker option! Did you call getChecker*Option "
  111. "with incorrect parameters? User input must've been "
  112. "verified by CheckerRegistry.");
  113. return "";
  114. }
  115. StringRef AnalyzerOptions::getCheckerStringOption(const ento::CheckerBase *C,
  116. StringRef OptionName,
  117. bool SearchInParents) const {
  118. return getCheckerStringOption(
  119. C->getTagDescription(), OptionName, SearchInParents);
  120. }
  121. bool AnalyzerOptions::getCheckerBooleanOption(StringRef CheckerName,
  122. StringRef OptionName,
  123. bool SearchInParents) const {
  124. auto Ret = llvm::StringSwitch<llvm::Optional<bool>>(
  125. getCheckerStringOption(CheckerName, OptionName,
  126. SearchInParents))
  127. .Case("true", true)
  128. .Case("false", false)
  129. .Default(None);
  130. assert(Ret &&
  131. "This option should be either 'true' or 'false', and should've been "
  132. "validated by CheckerRegistry!");
  133. return *Ret;
  134. }
  135. bool AnalyzerOptions::getCheckerBooleanOption(const ento::CheckerBase *C,
  136. StringRef OptionName,
  137. bool SearchInParents) const {
  138. return getCheckerBooleanOption(
  139. C->getTagDescription(), OptionName, SearchInParents);
  140. }
  141. int AnalyzerOptions::getCheckerIntegerOption(StringRef CheckerName,
  142. StringRef OptionName,
  143. bool SearchInParents) const {
  144. int Ret = 0;
  145. bool HasFailed = getCheckerStringOption(CheckerName, OptionName,
  146. SearchInParents)
  147. .getAsInteger(0, Ret);
  148. assert(!HasFailed &&
  149. "This option should be numeric, and should've been validated by "
  150. "CheckerRegistry!");
  151. (void)HasFailed;
  152. return Ret;
  153. }
  154. int AnalyzerOptions::getCheckerIntegerOption(const ento::CheckerBase *C,
  155. StringRef OptionName,
  156. bool SearchInParents) const {
  157. return getCheckerIntegerOption(
  158. C->getTagDescription(), OptionName, SearchInParents);
  159. }