AnalyzerOptions.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. StringRef DefaultVal,
  96. bool SearchInParents ) const {
  97. assert(!CheckerName.empty() &&
  98. "Empty checker name! Make sure the checker object (including it's "
  99. "bases!) if fully initialized before calling this function!");
  100. ConfigTable::const_iterator E = Config.end();
  101. do {
  102. ConfigTable::const_iterator I =
  103. Config.find((Twine(CheckerName) + ":" + OptionName).str());
  104. if (I != E)
  105. return StringRef(I->getValue());
  106. size_t Pos = CheckerName.rfind('.');
  107. if (Pos == StringRef::npos)
  108. return DefaultVal;
  109. CheckerName = CheckerName.substr(0, Pos);
  110. } while (!CheckerName.empty() && SearchInParents);
  111. return DefaultVal;
  112. }
  113. StringRef AnalyzerOptions::getCheckerStringOption(const ento::CheckerBase *C,
  114. StringRef OptionName,
  115. StringRef DefaultVal,
  116. bool SearchInParents ) const {
  117. return getCheckerStringOption(
  118. C->getTagDescription(), OptionName, DefaultVal, SearchInParents);
  119. }
  120. bool AnalyzerOptions::getCheckerBooleanOption(StringRef CheckerName,
  121. StringRef OptionName,
  122. bool DefaultVal,
  123. bool SearchInParents ) const {
  124. // FIXME: We should emit a warning here if the value is something other than
  125. // "true", "false", or the empty string (meaning the default value),
  126. // but the AnalyzerOptions doesn't have access to a diagnostic engine.
  127. return llvm::StringSwitch<bool>(
  128. getCheckerStringOption(CheckerName, OptionName,
  129. DefaultVal ? "true" : "false",
  130. SearchInParents))
  131. .Case("true", true)
  132. .Case("false", false)
  133. .Default(DefaultVal);
  134. }
  135. bool AnalyzerOptions::getCheckerBooleanOption(const ento::CheckerBase *C,
  136. StringRef OptionName,
  137. bool DefaultVal,
  138. bool SearchInParents ) const {
  139. return getCheckerBooleanOption(
  140. C->getTagDescription(), OptionName, DefaultVal, SearchInParents);
  141. }
  142. int AnalyzerOptions::getCheckerIntegerOption(StringRef CheckerName,
  143. StringRef OptionName,
  144. int DefaultVal,
  145. bool SearchInParents ) const {
  146. int Ret = DefaultVal;
  147. bool HasFailed = getCheckerStringOption(CheckerName, OptionName,
  148. std::to_string(DefaultVal),
  149. SearchInParents)
  150. .getAsInteger(10, Ret);
  151. assert(!HasFailed && "analyzer-config option should be numeric");
  152. (void)HasFailed;
  153. return Ret;
  154. }
  155. int AnalyzerOptions::getCheckerIntegerOption(const ento::CheckerBase *C,
  156. StringRef OptionName,
  157. int DefaultVal,
  158. bool SearchInParents ) const {
  159. return getCheckerIntegerOption(
  160. C->getTagDescription(), OptionName, DefaultVal, SearchInParents);
  161. }