This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Add option to ignore macros in readability-function-cognitive-complexity check.
ClosedPublic

Authored by massberg on Mar 5 2021, 12:45 PM.

Details

Summary

(this was originally part of https://reviews.llvm.org/D96281 and has been split off into its own patch)

If a macro is used within a function, the code inside the macro
doesn't make the code less readable. Instead, for a reader a macro is
more like a function that is called. Thus the code inside a macro
shouldn't increase the complexity of the function in which it is called.
Thus the flag 'IgnoreMacros' is added. If set to 'true' code inside
macros isn't considered during analysis.

This isn't perfect, as now the code of a macro isn't considered at all,
even if it has a high cognitive complexity itself. It might be better if
a macro is considered in the analysis like a function and gets its own
cognitive complexity. Implementing such an analysis seems to be very
complex (if possible at all with the given AST), so we give the user the
option to either ignore macros completely or to let the expanded code
count to the calling function's complexity.

See the code example from vgeof (originally added as note in https://reviews.llvm.org/D96281)

bool doStuff(myClass* objectPtr){
      if(objectPtr == nullptr){
          LOG_WARNING("empty object");
          return false;
      }
      if(objectPtr->getAttribute() == nullptr){
          LOG_WARNING("empty object");
          return false;
      }
      use(objectPtr->getAttribute());
  }

The LOG_WARNING macro itself might have a high complexity, but it do not make the
the function more complex to understand like e.g. a 'printf'.

By default 'IgnoreMacros' is set to 'false', which is the original behavior of the check.

Diff Detail

Event Timeline

massberg created this revision.Mar 5 2021, 12:45 PM
massberg requested review of this revision.Mar 5 2021, 12:45 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 5 2021, 12:45 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
massberg edited the summary of this revision. (Show Details)Mar 5 2021, 12:53 PM
massberg added a reviewer: alexfh.
massberg added a subscriber: vgeof.
Eugene.Zelenko added a project: Restricted Project.
massberg updated this revision to Diff 329559.Mar 10 2021, 12:25 AM

Sync to head.

Please also add a test with global IgnoreMacros=1 and readability-function-cognitive-complexity.IgnoreMacros unset.
(The code is correct as-is, global IgnoreMacros should not affect the check here.)

I'm also somewhat worried about forward compatibility.
If in future a mode will be implemented that allows to count the cost of macro arguments,
how'd we expose it in check's params, without breaking the existing IgnoreMacros=1 config support?

lebedev.ri requested changes to this revision.Apr 4 2021, 8:48 AM

Please also add a test with global IgnoreMacros=1 and readability-function-cognitive-complexity.IgnoreMacros unset.
(The code is correct as-is, global IgnoreMacros should not affect the check here.)

I'm also somewhat worried about forward compatibility.
If in future a mode will be implemented that allows to count the cost of macro arguments,
how'd we expose it in check's params, without breaking the existing IgnoreMacros=1 config support?

This revision now requires changes to proceed.Apr 4 2021, 8:48 AM
massberg updated this revision to Diff 336831.Apr 12 2021, 7:56 AM

Add test with global IgnoreMacros=true and readability-function-cognitive-complexity.IgnoreMacros unset.

lebedev.ri accepted this revision.Apr 12 2021, 8:03 AM

K, thx.

clang-tools-extra/docs/clang-tidy/checks/readability-function-cognitive-complexity.rst
31

I'd add something about this option being not guaranteed to forward-compatible.

This revision is now accepted and ready to land.Apr 12 2021, 8:03 AM
massberg updated this revision to Diff 336848.Apr 12 2021, 8:45 AM

Add note that the new IgnoreMarcos options isn't guaranteed to be forward compatible.

massberg marked an inline comment as done.Apr 12 2021, 8:48 AM
massberg added inline comments.
clang-tools-extra/docs/clang-tidy/checks/readability-function-cognitive-complexity.rst
31

I have added a note, thanks!

alexfh accepted this revision.Apr 12 2021, 9:13 AM

LG with a couple of nits.

clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp
230

nit: Please remove the semicolon and clang-format.

clang-tools-extra/docs/clang-tidy/checks/readability-function-cognitive-complexity.rst
31

typo: "guarantueed" -> "guaranteed".

This revision was landed with ongoing or failed builds.Apr 12 2021, 9:46 AM
This revision was automatically updated to reflect the committed changes.