NoRecursionCheck.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. //===--- NoRecursionCheck.cpp - clang-tidy --------------------------------===//
  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. #include "NoRecursionCheck.h"
  9. #include "clang/AST/ASTContext.h"
  10. #include "clang/ASTMatchers/ASTMatchFinder.h"
  11. #include "clang/Analysis/CallGraph.h"
  12. #include "llvm/ADT/DenseMapInfo.h"
  13. #include "llvm/ADT/SCCIterator.h"
  14. using namespace clang::ast_matchers;
  15. namespace clang::tidy::misc {
  16. namespace {
  17. /// Much like SmallSet, with two differences:
  18. /// 1. It can *only* be constructed from an ArrayRef<>. If the element count
  19. /// is small, there is no copy and said storage *must* outlive us.
  20. /// 2. it is immutable, the way it was constructed it will stay.
  21. template <typename T, unsigned SmallSize> class ImmutableSmallSet {
  22. ArrayRef<T> Vector;
  23. llvm::DenseSet<T> Set;
  24. static_assert(SmallSize <= 32, "N should be small");
  25. bool isSmall() const { return Set.empty(); }
  26. public:
  27. using size_type = size_t;
  28. ImmutableSmallSet() = delete;
  29. ImmutableSmallSet(const ImmutableSmallSet &) = delete;
  30. ImmutableSmallSet(ImmutableSmallSet &&) = delete;
  31. T &operator=(const ImmutableSmallSet &) = delete;
  32. T &operator=(ImmutableSmallSet &&) = delete;
  33. // WARNING: Storage *must* outlive us if we decide that the size is small.
  34. ImmutableSmallSet(ArrayRef<T> Storage) {
  35. // Is size small-enough to just keep using the existing storage?
  36. if (Storage.size() <= SmallSize) {
  37. Vector = Storage;
  38. return;
  39. }
  40. // We've decided that it isn't performant to keep using vector.
  41. // Let's migrate the data into Set.
  42. Set.reserve(Storage.size());
  43. Set.insert(Storage.begin(), Storage.end());
  44. }
  45. /// count - Return 1 if the element is in the set, 0 otherwise.
  46. size_type count(const T &V) const {
  47. if (isSmall()) {
  48. // Since the collection is small, just do a linear search.
  49. return llvm::is_contained(Vector, V) ? 1 : 0;
  50. }
  51. return Set.count(V);
  52. }
  53. };
  54. /// Much like SmallSetVector, but with one difference:
  55. /// when the size is \p SmallSize or less, when checking whether an element is
  56. /// already in the set or not, we perform linear search over the vector,
  57. /// but if the size is larger than \p SmallSize, we look in set.
  58. /// FIXME: upstream this into SetVector/SmallSetVector itself.
  59. template <typename T, unsigned SmallSize> class SmartSmallSetVector {
  60. public:
  61. using size_type = size_t;
  62. private:
  63. SmallVector<T, SmallSize> Vector;
  64. llvm::DenseSet<T> Set;
  65. static_assert(SmallSize <= 32, "N should be small");
  66. // Are we still using Vector for uniqness tracking?
  67. bool isSmall() const { return Set.empty(); }
  68. // Will one more entry cause Vector to switch away from small-size storage?
  69. bool entiretyOfVectorSmallSizeIsOccupied() const {
  70. assert(isSmall() && Vector.size() <= SmallSize &&
  71. "Shouldn't ask if we have already [should have] migrated into Set.");
  72. return Vector.size() == SmallSize;
  73. }
  74. void populateSet() {
  75. assert(Set.empty() && "Should not have already utilized the Set.");
  76. // Magical growth factor prediction - to how many elements do we expect to
  77. // sanely grow after switching away from small-size storage?
  78. const size_t NewMaxElts = 4 * Vector.size();
  79. Vector.reserve(NewMaxElts);
  80. Set.reserve(NewMaxElts);
  81. Set.insert(Vector.begin(), Vector.end());
  82. }
  83. /// count - Return 1 if the element is in the set, 0 otherwise.
  84. size_type count(const T &V) const {
  85. if (isSmall()) {
  86. // Since the collection is small, just do a linear search.
  87. return llvm::is_contained(Vector, V) ? 1 : 0;
  88. }
  89. // Look-up in the Set.
  90. return Set.count(V);
  91. }
  92. bool setInsert(const T &V) {
  93. if (count(V) != 0)
  94. return false; // Already exists.
  95. // Does not exist, Can/need to record it.
  96. if (isSmall()) { // Are we still using Vector for uniqness tracking?
  97. // Will one more entry fit within small-sized Vector?
  98. if (!entiretyOfVectorSmallSizeIsOccupied())
  99. return true; // We'll insert into vector right afterwards anyway.
  100. // Time to switch to Set.
  101. populateSet();
  102. }
  103. // Set time!
  104. // Note that this must be after `populateSet()` might have been called.
  105. bool SetInsertionSucceeded = Set.insert(V).second;
  106. (void)SetInsertionSucceeded;
  107. assert(SetInsertionSucceeded && "We did check that no such value existed");
  108. return true;
  109. }
  110. public:
  111. /// Insert a new element into the SmartSmallSetVector.
  112. /// \returns true if the element was inserted into the SmartSmallSetVector.
  113. bool insert(const T &X) {
  114. bool Result = setInsert(X);
  115. if (Result)
  116. Vector.push_back(X);
  117. return Result;
  118. }
  119. /// Clear the SmartSmallSetVector and return the underlying vector.
  120. decltype(Vector) takeVector() {
  121. Set.clear();
  122. return std::move(Vector);
  123. }
  124. };
  125. constexpr unsigned SmallCallStackSize = 16;
  126. constexpr unsigned SmallSCCSize = 32;
  127. using CallStackTy =
  128. llvm::SmallVector<CallGraphNode::CallRecord, SmallCallStackSize>;
  129. // In given SCC, find *some* call stack that will be cyclic.
  130. // This will only find *one* such stack, it might not be the smallest one,
  131. // and there may be other loops.
  132. CallStackTy pathfindSomeCycle(ArrayRef<CallGraphNode *> SCC) {
  133. // We'll need to be able to performantly look up whether some CallGraphNode
  134. // is in SCC or not, so cache all the SCC elements in a set.
  135. const ImmutableSmallSet<CallGraphNode *, SmallSCCSize> SCCElts(SCC);
  136. // Is node N part if the current SCC?
  137. auto NodeIsPartOfSCC = [&SCCElts](CallGraphNode *N) {
  138. return SCCElts.count(N) != 0;
  139. };
  140. // Track the call stack that will cause a cycle.
  141. SmartSmallSetVector<CallGraphNode::CallRecord, SmallCallStackSize>
  142. CallStackSet;
  143. // Arbitrarily take the first element of SCC as entry point.
  144. CallGraphNode::CallRecord EntryNode(SCC.front(), /*CallExpr=*/nullptr);
  145. // Continue recursing into subsequent callees that are part of this SCC,
  146. // and are thus known to be part of the call graph loop, until loop forms.
  147. CallGraphNode::CallRecord *Node = &EntryNode;
  148. while (true) {
  149. // Did we see this node before?
  150. if (!CallStackSet.insert(*Node))
  151. break; // Cycle completed! Note that didn't insert the node into stack!
  152. // Else, perform depth-first traversal: out of all callees, pick first one
  153. // that is part of this SCC. This is not guaranteed to yield shortest cycle.
  154. Node = llvm::find_if(Node->Callee->callees(), NodeIsPartOfSCC);
  155. }
  156. // Note that we failed to insert the last node, that completes the cycle.
  157. // But we really want to have it. So insert it manually into stack only.
  158. CallStackTy CallStack = CallStackSet.takeVector();
  159. CallStack.emplace_back(*Node);
  160. return CallStack;
  161. }
  162. } // namespace
  163. void NoRecursionCheck::registerMatchers(MatchFinder *Finder) {
  164. Finder->addMatcher(translationUnitDecl().bind("TUDecl"), this);
  165. }
  166. void NoRecursionCheck::handleSCC(ArrayRef<CallGraphNode *> SCC) {
  167. assert(!SCC.empty() && "Empty SCC does not make sense.");
  168. // First of all, call out every strongly connected function.
  169. for (CallGraphNode *N : SCC) {
  170. FunctionDecl *D = N->getDefinition();
  171. diag(D->getLocation(), "function %0 is within a recursive call chain") << D;
  172. }
  173. // Now, SCC only tells us about strongly connected function declarations in
  174. // the call graph. It doesn't *really* tell us about the cycles they form.
  175. // And there may be more than one cycle in SCC.
  176. // So let's form a call stack that eventually exposes *some* cycle.
  177. const CallStackTy EventuallyCyclicCallStack = pathfindSomeCycle(SCC);
  178. assert(!EventuallyCyclicCallStack.empty() && "We should've found the cycle");
  179. // While last node of the call stack does cause a loop, due to the way we
  180. // pathfind the cycle, the loop does not necessarily begin at the first node
  181. // of the call stack, so drop front nodes of the call stack until it does.
  182. const auto CyclicCallStack =
  183. ArrayRef<CallGraphNode::CallRecord>(EventuallyCyclicCallStack)
  184. .drop_until([LastNode = EventuallyCyclicCallStack.back()](
  185. CallGraphNode::CallRecord FrontNode) {
  186. return FrontNode == LastNode;
  187. });
  188. assert(CyclicCallStack.size() >= 2 && "Cycle requires at least 2 frames");
  189. // Which function we decided to be the entry point that lead to the recursion?
  190. FunctionDecl *CycleEntryFn = CyclicCallStack.front().Callee->getDefinition();
  191. // And now, for ease of understanding, let's print the call sequence that
  192. // forms the cycle in question.
  193. diag(CycleEntryFn->getLocation(),
  194. "example recursive call chain, starting from function %0",
  195. DiagnosticIDs::Note)
  196. << CycleEntryFn;
  197. for (int CurFrame = 1, NumFrames = CyclicCallStack.size();
  198. CurFrame != NumFrames; ++CurFrame) {
  199. CallGraphNode::CallRecord PrevNode = CyclicCallStack[CurFrame - 1];
  200. CallGraphNode::CallRecord CurrNode = CyclicCallStack[CurFrame];
  201. Decl *PrevDecl = PrevNode.Callee->getDecl();
  202. Decl *CurrDecl = CurrNode.Callee->getDecl();
  203. diag(CurrNode.CallExpr->getBeginLoc(),
  204. "Frame #%0: function %1 calls function %2 here:", DiagnosticIDs::Note)
  205. << CurFrame << cast<NamedDecl>(PrevDecl) << cast<NamedDecl>(CurrDecl);
  206. }
  207. diag(CyclicCallStack.back().CallExpr->getBeginLoc(),
  208. "... which was the starting point of the recursive call chain; there "
  209. "may be other cycles",
  210. DiagnosticIDs::Note);
  211. }
  212. void NoRecursionCheck::check(const MatchFinder::MatchResult &Result) {
  213. // Build call graph for the entire translation unit.
  214. const auto *TU = Result.Nodes.getNodeAs<TranslationUnitDecl>("TUDecl");
  215. CallGraph CG;
  216. CG.addToCallGraph(const_cast<TranslationUnitDecl *>(TU));
  217. // Look for cycles in call graph,
  218. // by looking for Strongly Connected Components (SCC's)
  219. for (llvm::scc_iterator<CallGraph *> SCCI = llvm::scc_begin(&CG),
  220. SCCE = llvm::scc_end(&CG);
  221. SCCI != SCCE; ++SCCI) {
  222. if (!SCCI.hasCycle()) // We only care about cycles, not standalone nodes.
  223. continue;
  224. handleSCC(*SCCI);
  225. }
  226. }
  227. } // namespace clang::tidy::misc