Files
TheRock/compiler/amd-llvm/clang-tools-extra/docs/clang-tidy/checks/readability/function-cognitive-complexity.rst
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

167 lines
4.0 KiB
ReStructuredText

.. title:: clang-tidy - readability-function-cognitive-complexity
readability-function-cognitive-complexity
=========================================
Checks function Cognitive Complexity metric.
The metric is implemented as per the `COGNITIVE COMPLEXITY by SonarSource
<https://www.sonarsource.com/docs/CognitiveComplexity.pdf>`_ specification
version 1.2 (19 April 2017).
Options
-------
.. option:: Threshold
Flag functions with Cognitive Complexity exceeding this number.
The default is `25`.
.. option:: DescribeBasicIncrements
If set to `true`, then for each function exceeding the complexity threshold
the check will issue additional diagnostics on every piece of code (loop,
`if` statement, etc.) which contributes to that complexity. See also the
examples below. Default is `true`.
.. option:: IgnoreMacros
If set to `true`, the check will ignore code inside macros. Note, that also
any macro arguments are ignored, even if they should count to the complexity.
As this might change in the future, this option isn't guaranteed to be
forward-compatible. Default is `false`.
Building blocks
---------------
There are three basic building blocks of a Cognitive Complexity metric:
Increment
^^^^^^^^^
The following structures increase the function's Cognitive Complexity metric
(by `1`):
* Conditional operators:
- ``if()``
- ``else if()``
- ``else``
- ``cond ? true : false``
* ``switch()``
* Loops:
- ``for()``
- C++11 range-based ``for()``
- ``while()``
- ``do while()``
* ``catch ()``
* ``goto LABEL``, ``goto *(&&LABEL))``,
* sequences of binary logical operators:
- ``boolean1 || boolean2``
- ``boolean1 && boolean2``
Nesting level
^^^^^^^^^^^^^
While by itself the nesting level does not change the function's Cognitive
Complexity metric, it is tracked, and is used by the next, third building
block. The following structures increase the nesting level (by `1`):
* Conditional operators:
- ``if()``
- ``else if()``
- ``else``
- ``cond ? true : false``
* ``switch()``
* Loops:
- ``for()``
- C++11 range-based ``for()``
- ``while()``
- ``do while()``
* ``catch ()``
* Nested functions:
- C++11 Lambda
- Nested ``class``
- Nested ``struct``
* GNU statement expression
* Apple Block Declaration
Nesting increment
^^^^^^^^^^^^^^^^^
This is where the previous basic building block, `Nesting level`_, matters.
The following structures increase the function's Cognitive Complexity metric by
the current `Nesting level`_:
* Conditional operators:
- ``if()``
- ``cond ? true : false``
* ``switch()``
* Loops:
- ``for()``
- C++11 range-based ``for()``
- ``while()``
- ``do while()``
* ``catch ()``
Examples
--------
The simplest case. This function has Cognitive Complexity of `0`.
.. code-block:: c++
void function0() {}
Slightly better example. This function has Cognitive Complexity of `1`.
.. code-block:: c++
int function1(bool var) {
if(var) // +1, nesting level +1
return 42;
return 0;
}
Full example. This function has Cognitive Complexity of `3`.
.. code-block:: c++
int function3(bool var1, bool var2) {
if(var1) { // +1, nesting level +1
if(var2) // +2 (1 + current nesting level of 1), nesting level +1
return 42;
}
return 0;
}
In the last example, the check will flag `function3` if the option Threshold is
set to `2` or smaller. If the option DescribeBasicIncrements is set to `true`,
it will additionally flag the two `if` statements with the amounts by which they
increase to the complexity of the function and the current nesting level.
Limitations
-----------
The metric is implemented with two notable exceptions:
* `preprocessor conditionals` (``#ifdef``, ``#if``, ``#elif``, ``#else``,
``#endif``) are not accounted for.
* `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 currently not possible in clang-tidy.