AnalyzerOptions.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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, HELPTEXT, DOC_URI) \
  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. ExplorationStrategyKind
  49. AnalyzerOptions::getExplorationStrategy() const {
  50. auto K =
  51. llvm::StringSwitch<llvm::Optional<ExplorationStrategyKind>>(
  52. ExplorationStrategy)
  53. .Case("dfs", ExplorationStrategyKind::DFS)
  54. .Case("bfs", ExplorationStrategyKind::BFS)
  55. .Case("unexplored_first",
  56. ExplorationStrategyKind::UnexploredFirst)
  57. .Case("unexplored_first_queue",
  58. ExplorationStrategyKind::UnexploredFirstQueue)
  59. .Case("unexplored_first_location_queue",
  60. ExplorationStrategyKind::UnexploredFirstLocationQueue)
  61. .Case("bfs_block_dfs_contents",
  62. ExplorationStrategyKind::BFSBlockDFSContents)
  63. .Default(None);
  64. assert(K.hasValue() && "User mode is invalid.");
  65. return K.getValue();
  66. }
  67. IPAKind AnalyzerOptions::getIPAMode() const {
  68. auto K = llvm::StringSwitch<llvm::Optional<IPAKind>>(IPAMode)
  69. .Case("none", IPAK_None)
  70. .Case("basic-inlining", IPAK_BasicInlining)
  71. .Case("inlining", IPAK_Inlining)
  72. .Case("dynamic", IPAK_DynamicDispatch)
  73. .Case("dynamic-bifurcate", IPAK_DynamicDispatchBifurcate)
  74. .Default(None);
  75. assert(K.hasValue() && "IPA Mode is invalid.");
  76. return K.getValue();
  77. }
  78. bool
  79. AnalyzerOptions::mayInlineCXXMemberFunction(
  80. CXXInlineableMemberKind Param) const {
  81. if (getIPAMode() < IPAK_Inlining)
  82. return false;
  83. auto K =
  84. llvm::StringSwitch<llvm::Optional<CXXInlineableMemberKind>>(
  85. CXXMemberInliningMode)
  86. .Case("constructors", CIMK_Constructors)
  87. .Case("destructors", CIMK_Destructors)
  88. .Case("methods", CIMK_MemberFunctions)
  89. .Case("none", CIMK_None)
  90. .Default(None);
  91. assert(K.hasValue() && "Invalid c++ member function inlining mode.");
  92. return *K >= Param;
  93. }
  94. StringRef AnalyzerOptions::getCheckerStringOption(StringRef OptionName,
  95. StringRef DefaultVal,
  96. const CheckerBase *C,
  97. bool SearchInParents) const {
  98. assert(C);
  99. // Search for a package option if the option for the checker is not specified
  100. // and search in parents is enabled.
  101. StringRef CheckerName = C->getTagDescription();
  102. assert(!CheckerName.empty() &&
  103. "Empty checker name! Make sure the checker object (including it's "
  104. "bases!) if fully initialized before calling this function!");
  105. ConfigTable::const_iterator E = Config.end();
  106. do {
  107. ConfigTable::const_iterator I =
  108. Config.find((Twine(CheckerName) + ":" + OptionName).str());
  109. if (I != E)
  110. return StringRef(I->getValue());
  111. size_t Pos = CheckerName.rfind('.');
  112. if (Pos == StringRef::npos)
  113. return DefaultVal;
  114. CheckerName = CheckerName.substr(0, Pos);
  115. } while (!CheckerName.empty() && SearchInParents);
  116. return DefaultVal;
  117. }
  118. bool AnalyzerOptions::getCheckerBooleanOption(StringRef Name, bool DefaultVal,
  119. const CheckerBase *C,
  120. bool SearchInParents) const {
  121. // FIXME: We should emit a warning here if the value is something other than
  122. // "true", "false", or the empty string (meaning the default value),
  123. // but the AnalyzerOptions doesn't have access to a diagnostic engine.
  124. assert(C);
  125. return llvm::StringSwitch<bool>(
  126. getCheckerStringOption(Name, DefaultVal ? "true" : "false", C,
  127. SearchInParents))
  128. .Case("true", true)
  129. .Case("false", false)
  130. .Default(DefaultVal);
  131. }
  132. int AnalyzerOptions::getCheckerIntegerOption(StringRef Name, int DefaultVal,
  133. const CheckerBase *C,
  134. bool SearchInParents) const {
  135. int Ret = DefaultVal;
  136. bool HasFailed = getCheckerStringOption(Name, std::to_string(DefaultVal), C,
  137. SearchInParents)
  138. .getAsInteger(10, Ret);
  139. assert(!HasFailed && "analyzer-config option should be numeric");
  140. (void)HasFailed;
  141. return Ret;
  142. }