CheckerRegistry.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //===--- CheckerRegistry.cpp - Maintains all available checkers -*- C++ -*-===//
  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. #include "clang/StaticAnalyzer/Core/CheckerRegistry.h"
  10. #include "clang/StaticAnalyzer/Core/CheckerOptInfo.h"
  11. #include "llvm/ADT/SetVector.h"
  12. #include "llvm/Support/raw_ostream.h"
  13. using namespace clang;
  14. using namespace ento;
  15. static const char PackageSeparator = '.';
  16. typedef llvm::SetVector<const CheckerRegistry::CheckerInfo *> CheckerInfoSet;
  17. static bool checkerNameLT(const CheckerRegistry::CheckerInfo &a,
  18. const CheckerRegistry::CheckerInfo &b) {
  19. return a.FullName < b.FullName;
  20. }
  21. static bool isInPackage(const CheckerRegistry::CheckerInfo &checker,
  22. StringRef packageName) {
  23. // Does the checker's full name have the package as a prefix?
  24. if (!checker.FullName.startswith(packageName))
  25. return false;
  26. // Is the package actually just the name of a specific checker?
  27. if (checker.FullName.size() == packageName.size())
  28. return true;
  29. // Is the checker in the package (or a subpackage)?
  30. if (checker.FullName[packageName.size()] == PackageSeparator)
  31. return true;
  32. return false;
  33. }
  34. static void collectCheckers(const CheckerRegistry::CheckerInfoList &checkers,
  35. const llvm::StringMap<size_t> &packageSizes,
  36. CheckerOptInfo &opt, CheckerInfoSet &collected) {
  37. // Use a binary search to find the possible start of the package.
  38. CheckerRegistry::CheckerInfo packageInfo(nullptr, opt.getName(), "");
  39. CheckerRegistry::CheckerInfoList::const_iterator e = checkers.end();
  40. CheckerRegistry::CheckerInfoList::const_iterator i =
  41. std::lower_bound(checkers.begin(), e, packageInfo, checkerNameLT);
  42. // If we didn't even find a possible package, give up.
  43. if (i == e)
  44. return;
  45. // If what we found doesn't actually start the package, give up.
  46. if (!isInPackage(*i, opt.getName()))
  47. return;
  48. // There is at least one checker in the package; claim the option.
  49. opt.claim();
  50. // See how large the package is.
  51. // If the package doesn't exist, assume the option refers to a single checker.
  52. size_t size = 1;
  53. llvm::StringMap<size_t>::const_iterator packageSize =
  54. packageSizes.find(opt.getName());
  55. if (packageSize != packageSizes.end())
  56. size = packageSize->getValue();
  57. // Step through all the checkers in the package.
  58. for (e = i+size; i != e; ++i) {
  59. if (opt.isEnabled())
  60. collected.insert(&*i);
  61. else
  62. collected.remove(&*i);
  63. }
  64. }
  65. void CheckerRegistry::addChecker(InitializationFunction fn, StringRef name,
  66. StringRef desc) {
  67. Checkers.push_back(CheckerInfo(fn, name, desc));
  68. // Record the presence of the checker in its packages.
  69. StringRef packageName, leafName;
  70. std::tie(packageName, leafName) = name.rsplit(PackageSeparator);
  71. while (!leafName.empty()) {
  72. Packages[packageName] += 1;
  73. std::tie(packageName, leafName) = packageName.rsplit(PackageSeparator);
  74. }
  75. }
  76. void CheckerRegistry::initializeManager(CheckerManager &checkerMgr,
  77. SmallVectorImpl<CheckerOptInfo> &opts) const {
  78. // Sort checkers for efficient collection.
  79. std::sort(Checkers.begin(), Checkers.end(), checkerNameLT);
  80. // Collect checkers enabled by the options.
  81. CheckerInfoSet enabledCheckers;
  82. for (SmallVectorImpl<CheckerOptInfo>::iterator
  83. i = opts.begin(), e = opts.end(); i != e; ++i) {
  84. collectCheckers(Checkers, Packages, *i, enabledCheckers);
  85. }
  86. // Initialize the CheckerManager with all enabled checkers.
  87. for (CheckerInfoSet::iterator
  88. i = enabledCheckers.begin(), e = enabledCheckers.end(); i != e; ++i) {
  89. checkerMgr.setCurrentCheckName(CheckName((*i)->FullName));
  90. (*i)->Initialize(checkerMgr);
  91. }
  92. }
  93. void CheckerRegistry::printHelp(raw_ostream &out,
  94. size_t maxNameChars) const {
  95. // FIXME: Alphabetical sort puts 'experimental' in the middle.
  96. // Would it be better to name it '~experimental' or something else
  97. // that's ASCIIbetically last?
  98. std::sort(Checkers.begin(), Checkers.end(), checkerNameLT);
  99. // FIXME: Print available packages.
  100. out << "CHECKERS:\n";
  101. // Find the maximum option length.
  102. size_t optionFieldWidth = 0;
  103. for (CheckerInfoList::const_iterator i = Checkers.begin(), e = Checkers.end();
  104. i != e; ++i) {
  105. // Limit the amount of padding we are willing to give up for alignment.
  106. // Package.Name Description [Hidden]
  107. size_t nameLength = i->FullName.size();
  108. if (nameLength <= maxNameChars)
  109. optionFieldWidth = std::max(optionFieldWidth, nameLength);
  110. }
  111. const size_t initialPad = 2;
  112. for (CheckerInfoList::const_iterator i = Checkers.begin(), e = Checkers.end();
  113. i != e; ++i) {
  114. out.indent(initialPad) << i->FullName;
  115. int pad = optionFieldWidth - i->FullName.size();
  116. // Break on long option names.
  117. if (pad < 0) {
  118. out << '\n';
  119. pad = optionFieldWidth + initialPad;
  120. }
  121. out.indent(pad + 2) << i->Desc;
  122. out << '\n';
  123. }
  124. }