[PATCH] D36836: [clang-tidy] Implement readability-function-cognitive-complexity check

Roman Lebedev via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 23 02:35:39 PDT 2017


Since i initially forgot to include cfe-commits as a subscriber into
the review, the first mail was not sent to the list.
So maybe, for history and clarity, i should forward the mail to the list:
(i'm keeping the reviewers as CC since phabricator, unlike github,
does not add mail-based reviews as comments...)

On Thu, Aug 17, 2017 at 6:57 PM, Roman Lebedev via Phabricator
<reviews at reviews.llvm.org> wrote:

lebedev.ri created this revision.
lebedev.ri added a project: clang-tools-extra.
Herald added subscribers: xazax.hun, JDevlieghere, mgorny.

Currently, there is basically just one clang-tidy check to impose some
sanity limits on functions - `clang-tidy-readability-function-size`.
It is nice, allows to limit line count, total number of statements,
number of branches, number of function parameters (not counting
implicit `this`), nesting level.
However, those are simple generic metrics. It is still trivially
possible to write a function, which does not violate any of these
metrics, yet is still rather unreadable.

Thus, some additional, slightly more complicated metric is needed.
There is a well-known Cyclomatic complexity
<https://en.wikipedia.org/wiki/Cyclomatic_complexity>, but certainly
has its downsides.
And there is a COGNITIVE COMPLEXITY by SonarSource
<https://www.sonarsource.com/docs/CognitiveComplexity.pdf>, which is
available for opensource on https://sonarcloud.io/.
I did ask them, and received an answer that it is it can be
implemented in clang-tidy.

This check checks function Cognitive Complexity metric, and flags the
functions with Cognitive Complexity exceeding the configured limit.
The default limit is `25`, same as in 'upstream'.

The metric is implemented as per COGNITIVE COMPLEXITY by SonarSource
<https://www.sonarsource.com/docs/CognitiveComplexity.pdf>
specification version 1.2 (19 April 2017), with two notable
exceptions:

- `preprocessor conditionals` (`#ifdef`, `#if`, `#elif`, `#else`,
`#endif`) are not accounted for. Could be done. Currently, upstream
does not account for them either.
- `each method in a recursion cycle` is not accounted for. It can't be
fully implemented, because cross-translational-unit analysis would be
needed, which is not possible in clang-tidy. Thus, at least right now,
i completely avoided implementing it.


Repository:
  rL LLVM

https://reviews.llvm.org/D36836

Files:
  clang-tidy/readability/CMakeLists.txt
  clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp
  clang-tidy/readability/FunctionCognitiveComplexityCheck.h
  clang-tidy/readability/ReadabilityTidyModule.cpp
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/readability-function-cognitive-complexity.rst
  test/clang-tidy/check_clang_tidy.py
  test/clang-tidy/readability-function-cognitive-complexity.cpp
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D36836.111523.patch
Type: text/x-patch
Size: 53982 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170823/20ac5172/attachment-0001.bin>


More information about the cfe-commits mailing list