This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] add readability-compound-statement-size check.
AbandonedPublic

Authored by lebedev.ri on Mar 22 2017, 12:33 PM.

Details

Summary

This check is quite similar to the readability-function-size
check. But it is more generic. It works for each compound
statement, excluding the ones directly related to the function,
i.e. it does not duplicate the readability-function-size output.

The rationale behind the check is the same as behind
readability-function-size check, it may make sense to be able
to control sizes of the compound statements, not just the
size of the functions.

Eventually, i would like these two checkers to be one, and
handle more cases and be more configurable, e.g. to be able to
apply such a check to the 'True' block of 'If' and so on.

But since this is my first attempt at any llvm/clang patch,
this is what it is. Please do review carefully.

Diff Detail

Event Timeline

lebedev.ri created this revision.Mar 22 2017, 12:33 PM

Do note that clang-tidy/readability/CompoundStatementSizeCheck.cpp is *very* similar to the clang-tidy/readability/FunctionSizeCheck.cpp
I'm not sure if it makes sense to have such duplication. But i'm also not sure yet how to deduplicate them.
But in the long run, i would like these two checks to be one, and much more configurable.

Hi and welcome to the clang/llvm community :)

It seems that you took readability-size as starting point and modified it. Thats a good way to learn!

My opinion is, that this should land in readability-function-size. This is somewhat recursive to function size. Whenever there is a compundstmt, the same (or maybe other constraints) exist for that compound statement.
Thats, in my opinion, the connecting factor to function-size.
It should be configurable. Maybe something like "global" constaint on the function (e.g. max 800 lines in total) and "local" constraints for subcomponents. These two layers of configurability kinda already exist. Your check would implement the recursive and local part, and function size the global.

What are your thoughts on that? But generally i like that approach to limit subparts of the functions as well.
Recommending to refactor such code to use functions would be a good diagnostic in my opinion.

When creating or modifying checks it would be nice to include a note in the ReleaseNotes as well (in doc/ i believe), but i think we should discuss the placement of this functionality first.

Another thought from me:

Maybe it would be sensible to create a check like complexity-limits where different forms of complicated constructs are examined. This could include extreme inheritance, excessive amount of members in classes (violation of single responsibility), very long conditions and these kind of things.
Function size would be another part of complexity and the traversing of all statements in the code would be done once. This would be good for performance as well, one problem would be the complexity of the check though ;D

Maybe we could even move this to the mail list, there would be good input as well.

JonasToth retitled this revision from Clang-tidy: add readability-compound-statement-size check. to [clang-tidy] add readability-compound-statement-size check..Mar 22 2017, 1:06 PM

Please mention this check in docs/ReleaseNotes.rst (in alphabetical order).

Will be good idea to run Clang-tidy modernize and readability checks over new code.

clang-tidy/readability/CompoundStatementSizeCheck.cpp
42

Please use LLVM_FALLTHROUGH instead (defined in llvm/Support/Compiler.h)

65

Please use default member initializers and = default;

Eugene.Zelenko edited reviewers, added: malcolm.parsons, aaron.ballman; removed: Restricted Project.Mar 22 2017, 4:27 PM
Eugene.Zelenko set the repository for this revision to rL LLVM.
Eugene.Zelenko added a subscriber: Prazek.
lebedev.ri updated this revision to Diff 92774.Mar 23 2017, 2:58 AM

Addressing review notes.

lebedev.ri updated this revision to Diff 92775.Mar 23 2017, 3:04 AM
lebedev.ri marked 2 inline comments as done.

No changes compared to v2, just correctly rebased the master branch now.

Please mention this check in docs/ReleaseNotes.rst (in alphabetical order).

Done

Will be good idea to run Clang-tidy modernize and readability checks over new code.

I did run them (-checks=*) over CompoundStatementSizeCheck.cpp, there is no modernize notes, and no meaningful readability notes.

alexfh requested changes to this revision.Mar 23 2017, 7:28 AM

Hi Roman,

Welcome to the community! As others noted, adding a separate check so similar functionally and implementation-wise to the existing one is not the best way to go here. A single check for all similar complexity limits would be a better solution. However, first I'd like to understand the specific use case you have for this check. Is there a recommendation of a certain style document to impose a complexity limit per-compound statement instead of a function? Should both limits be used? What would be the specific numbers and how should they interact with regard to nesting (e.g. should the warning be issued only on the innermost compound statement violating the rule or should it be issued on all levels)?

This revision now requires changes to proceed.Mar 23 2017, 7:28 AM
lebedev.ri abandoned this revision.Apr 27 2017, 10:19 AM

Thank you all!
After some thought, i have come to conclusion that it is best to start with something less controversial, and simpler.