[PATCH] D72362: [clang-tidy] misc-no-recursion: a new check

Roman Lebedev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 7 14:24:57 PST 2020


lebedev.ri created this revision.
lebedev.ri added reviewers: JonasToth, aaron.ballman, ffrankies, Eugene.Zelenko, erichkeane, NoQ.
lebedev.ri added a project: LLVM.
Herald added subscribers: xazax.hun, Anastasia, mgorny.
Herald added a project: clang.

Recursion is a powerful tool, but like any tool
without care it can be dangerous. For example,
if the recursion is unbounded, you will
eventually run out of stack and crash.

You can of course track the recursion depth
but if it is hardcoded, there can always be some
other environment when that depth is too large,
so said magic number would need to be env-dependent.
But then your program's behavior is suddenly more env-dependent.

Also, recursion, while it does not outright stop optimization,
recursive calls are less great than normal calls,
for example they hinder inlining.

Recursion is banned in some coding guidelines:

- SEI CERT DCL56-CPP. Avoid cycles during initialization of static objects
- JPL 2.4 Do not use direct or indirect recursion.
- I'd say it is frowned upon in LLVM, although not banned

And is plain unsupported in some cases:

- OpenCL 1.2, 6.9 Restrictions: i. Recursion is not supported.

So there's clearly a lot of reasons why one might want to
avoid recursion, and replace it with worklist handling.
It would be great to have a enforcement for it though.

This implements such a check.
Here we detect both direct and indirect recursive calls.

The algorithm is pretty straight-forward:

1. Build call-graph for the entire TU. For that, the existing `clang::CallGraph` is re-used, although it had to be modified to also track the location of the call.
2. Then, the hard problem: how do we detect recursion? Since we have a graph, let's just do the sane thing, and look for Strongly Connected Function Declarations - widely known as `SCC`. For that LLVM provides `llvm::scc_iterator`, which is internally an Tarjan's DFS algorithm, and is used throught LLVM, so this should be as performant as possible.
3. Now that we've got SCC's, we discard those that don't contain loops. Note that there may be more than one loop in SCC!
4. For each loopy SCC, we call out each function, and print a single example call graph that shows recursion -- it didn't seem worthwhile enumerating every possible loop in SCC, although i suppose it could be implemented.
  - To come up with that call graph cycle example, we start at first SCC node, see which callee of the node is within SCC (and is thus known to be in cycle), and recurse into it until we hit the callee that is already in call stack.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D72362

Files:
  clang-tools-extra/clang-move/HelperDeclRefGraph.cpp
  clang-tools-extra/clang-tidy/misc/CMakeLists.txt
  clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp
  clang-tools-extra/clang-tidy/misc/NoRecursionCheck.cpp
  clang-tools-extra/clang-tidy/misc/NoRecursionCheck.h
  clang-tools-extra/docs/ReleaseNotes.rst
  clang-tools-extra/docs/clang-tidy/checks/list.rst
  clang-tools-extra/docs/clang-tidy/checks/misc-no-recursion.rst
  clang-tools-extra/test/clang-tidy/checkers/misc-no-recursion.cpp
  clang/include/clang/Analysis/CallGraph.h
  clang/lib/Analysis/CallGraph.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D72362.236681.patch
Type: text/x-patch
Size: 29337 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200107/5538f737/attachment-0001.bin>


More information about the cfe-commits mailing list