Skip to content

[clang-tidy] Add IgnoreMacros to readability-function-size #131406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions clang-tools-extra/clang-tidy/readability/FunctionSizeCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class FunctionASTVisitor : public RecursiveASTVisitor<FunctionASTVisitor> {
using Base = RecursiveASTVisitor<FunctionASTVisitor>;

public:
FunctionASTVisitor(bool IgnoreMacros) : IgnoreMacros(IgnoreMacros) {}

bool VisitVarDecl(VarDecl *VD) {
// Do not count function params.
// Do not count decomposition declarations (C++17's structured bindings).
Expand All @@ -39,6 +41,9 @@ class FunctionASTVisitor : public RecursiveASTVisitor<FunctionASTVisitor> {
if (!Node)
return Base::TraverseStmt(Node);

if (IgnoreMacros && Node->getBeginLoc().isMacroID())
return true;
Comment on lines +44 to +45
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we check the whole Stmt is macro here. It will cause some unexpected behavior when the stmt is start with macro.
btw. I am not sure whether we should let macro increase statement count.


if (TrackedParent.back() && !isa<CompoundStmt>(Node))
++Info.Statements;

Expand Down Expand Up @@ -120,6 +125,9 @@ class FunctionASTVisitor : public RecursiveASTVisitor<FunctionASTVisitor> {
llvm::BitVector TrackedParent;
unsigned StructNesting = 0;
unsigned CurrentNestingLevel = 0;

private:
const bool IgnoreMacros;
};

} // namespace
Expand All @@ -135,7 +143,9 @@ FunctionSizeCheck::FunctionSizeCheck(StringRef Name, ClangTidyContext *Context)
NestingThreshold(
Options.get("NestingThreshold", DefaultNestingThreshold)),
VariableThreshold(
Options.get("VariableThreshold", DefaultVariableThreshold)) {}
Options.get("VariableThreshold", DefaultVariableThreshold)),
IgnoreMacros(
Options.getLocalOrGlobal("IgnoreMacros", DefaultIgnoreMacros)) {}

void FunctionSizeCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "LineThreshold", LineThreshold);
Expand All @@ -144,6 +154,7 @@ void FunctionSizeCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "ParameterThreshold", ParameterThreshold);
Options.store(Opts, "NestingThreshold", NestingThreshold);
Options.store(Opts, "VariableThreshold", VariableThreshold);
Options.store(Opts, "IgnoreMacros", IgnoreMacros);
}

void FunctionSizeCheck::registerMatchers(MatchFinder *Finder) {
Expand All @@ -158,7 +169,7 @@ void FunctionSizeCheck::registerMatchers(MatchFinder *Finder) {
void FunctionSizeCheck::check(const MatchFinder::MatchResult &Result) {
const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>("func");

FunctionASTVisitor Visitor;
FunctionASTVisitor Visitor(IgnoreMacros);
Visitor.Info.NestingThreshold = NestingThreshold.value_or(-1);
Visitor.TraverseDecl(const_cast<FunctionDecl *>(Func));
auto &FI = Visitor.Info;
Expand Down
2 changes: 2 additions & 0 deletions clang-tools-extra/clang-tidy/readability/FunctionSizeCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class FunctionSizeCheck : public ClangTidyCheck {
const std::optional<unsigned> ParameterThreshold;
const std::optional<unsigned> NestingThreshold;
const std::optional<unsigned> VariableThreshold;
const bool IgnoreMacros;

static constexpr std::optional<unsigned> DefaultLineThreshold = std::nullopt;
static constexpr std::optional<unsigned> DefaultStatementThreshold = 800U;
Expand All @@ -58,6 +59,7 @@ class FunctionSizeCheck : public ClangTidyCheck {
std::nullopt;
static constexpr std::optional<unsigned> DefaultVariableThreshold =
std::nullopt;
static constexpr bool DefaultIgnoreMacros = false;
};

} // namespace clang::tidy::readability
Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ Changes in existing checks
tolerating fix-it breaking compilation when functions is used as pointers
to avoid matching usage of functions within the current compilation unit.

- Improved :doc:`readability-function-size
<clang-tidy/checks/readability/function-size>` check by adding the option
`IgnoreMacros` to allow ignoring code inside macros.

Removed checks
^^^^^^^^^^^^^^

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,10 @@ Options
The default is `none` (ignore the number of variables).
Please note that function parameters and variables declared in lambdas,
GNU Statement Expressions, and nested class inline functions are not counted.

.. 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`.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
// RUN: readability-function-size.BranchThreshold: "5", \
// RUN: readability-function-size.ParameterThreshold: "none", \
// RUN: readability-function-size.NestingThreshold: "", \
// RUN: readability-function-size.VariableThreshold: "" \
// RUN: readability-function-size.VariableThreshold: "", \
// RUN: readability-function-size.IgnoreMacros: "true" \
// RUN: }}'

// Bad formatting is intentional, don't run clang-format over the whole file!
Expand Down Expand Up @@ -319,3 +320,15 @@ void variables_16() {
// CHECK-MESSAGES: :[[@LINE-5]]:6: note: 3 lines including whitespace and comments (threshold 0)
// CHECK-MESSAGES: :[[@LINE-6]]:6: note: 4 statements (threshold 0)
// CHECK-MESSAGES: :[[@LINE-7]]:6: note: 2 variables (threshold 1)

#define CONDITION(value) if (value) { return value; }

int macro_test(int v) {
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: function 'macro_test' exceeds recommended size/complexity thresholds [readability-function-size]
// CHECK-MESSAGES: :[[@LINE-2]]:5: note: 8 lines including whitespace and comments (threshold 0)
// CHECK-MESSAGES: :[[@LINE-3]]:5: note: 31 statements (threshold 0)
// CHECK-MESSAGES: :[[@LINE-4]]:5: note: 8 branches (threshold 0)

CONDITION(v);CONDITION(v);CONDITION(v);CONDITION(v);CONDITION(v);CONDITION(v);CONDITION(v);
if (v) { return v; }
}