[clang-tools-extra] [clang-tidy] Add IgnoreAboveThreshold option to readability-function-cognitive-complexity (PR #178965)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Jan 31 04:31:26 PST 2026
https://github.com/mugiwaraluffy56 updated https://github.com/llvm/llvm-project/pull/178965
>From ff134dc4152d263f9f7eac6d06dc0bf90c6de794 Mon Sep 17 00:00:00 2001
From: mugiwaraluffy56 <myakampuneeth at gmail.com>
Date: Sat, 31 Jan 2026 02:46:07 +0530
Subject: [PATCH] [clang-tidy] Add IgnoreAboveThreshold option to
readability-function-cognitive-complexity
This adds a new option IgnoreAboveThreshold that allows users to skip
functions with cognitive complexity at or above a certain threshold.
This is useful for ignoring "hopelessly" complex functions that would
be too costly to refactor.
When set to 0 (the default), this option is disabled.
Fixes #178959
---
.../readability/FunctionCognitiveComplexityCheck.cpp | 6 ++++++
.../readability/FunctionCognitiveComplexityCheck.h | 4 ++++
clang-tools-extra/docs/ReleaseNotes.rst | 7 +++++++
.../checks/readability/function-cognitive-complexity.rst | 7 +++++++
.../readability/function-cognitive-complexity-flags.cpp | 8 ++++++++
5 files changed, 32 insertions(+)
diff --git a/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp b/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp
index 1d8c4f8b58d8f..c0a9d5329b023 100644
--- a/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.cpp
@@ -474,12 +474,14 @@ FunctionCognitiveComplexityCheck::FunctionCognitiveComplexityCheck(
StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
Threshold(Options.get("Threshold", CognitiveComplexity::DefaultLimit)),
+ IgnoreAboveThreshold(Options.get("IgnoreAboveThreshold", 0U)),
DescribeBasicIncrements(Options.get("DescribeBasicIncrements", true)),
IgnoreMacros(Options.get("IgnoreMacros", false)) {}
void FunctionCognitiveComplexityCheck::storeOptions(
ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "Threshold", Threshold);
+ Options.store(Opts, "IgnoreAboveThreshold", IgnoreAboveThreshold);
Options.store(Opts, "DescribeBasicIncrements", DescribeBasicIncrements);
Options.store(Opts, "IgnoreMacros", IgnoreMacros);
}
@@ -514,6 +516,10 @@ void FunctionCognitiveComplexityCheck::check(
if (Visitor.CC.Total <= Threshold)
return;
+ // Skip functions that are "hopelessly" complex.
+ if (IgnoreAboveThreshold != 0 && Visitor.CC.Total >= IgnoreAboveThreshold)
+ return;
+
if (TheDecl)
diag(Loc, "function %0 has cognitive complexity of %1 (threshold %2)")
<< TheDecl << Visitor.CC.Total << Threshold;
diff --git a/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.h b/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.h
index 046c6e162af88..d3c6ae1322655 100644
--- a/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.h
+++ b/clang-tools-extra/clang-tidy/readability/FunctionCognitiveComplexityCheck.h
@@ -19,6 +19,9 @@ namespace clang::tidy::readability {
///
/// * `Threshold` - flag functions with Cognitive Complexity exceeding
/// this number. The default is `25`.
+/// * `IgnoreAboveThreshold` - flag functions with Cognitive Complexity
+/// not exceeding this number. If set to `0` (the default), this option is
+/// disabled. This can be used to skip "hopelessly" complex functions.
/// * `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
@@ -42,6 +45,7 @@ class FunctionCognitiveComplexityCheck : public ClangTidyCheck {
private:
const unsigned Threshold;
+ const unsigned IgnoreAboveThreshold;
const bool DescribeBasicIncrements;
const bool IgnoreMacros;
};
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 04c970402d4e1..517a4c5dc7499 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -177,6 +177,13 @@ Changes in existing checks
now uses separate note diagnostics for each uninitialized enumerator, making
it easier to see which specific enumerators need explicit initialization.
+- Improved :doc:`readability-function-cognitive-complexity
+ <clang-tidy/checks/readability/function-cognitive-complexity>` check:
+
+ - Added `IgnoreAboveThreshold` option to skip functions with complexity
+ at or above a certain threshold, allowing users to ignore "hopelessly"
+ complex functions.
+
- Improved :doc:`readability-non-const-parameter
<clang-tidy/checks/readability/non-const-parameter>` check by avoiding false
positives on parameters used in dependent expressions.
diff --git a/clang-tools-extra/docs/clang-tidy/checks/readability/function-cognitive-complexity.rst b/clang-tools-extra/docs/clang-tidy/checks/readability/function-cognitive-complexity.rst
index 430006e68ceb4..2fb4838ee8cca 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/readability/function-cognitive-complexity.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/readability/function-cognitive-complexity.rst
@@ -17,6 +17,13 @@ Options
Flag functions with Cognitive Complexity exceeding this number.
The default is `25`.
+.. option:: IgnoreAboveThreshold
+
+ Do not flag functions with Cognitive Complexity at or above this number.
+ This can be used to skip "hopelessly" complex functions that would be too
+ costly to refactor. If set to `0` (the default), this option is disabled
+ and all functions exceeding `Threshold` are flagged.
+
.. option:: DescribeBasicIncrements
If set to `true`, then for each function exceeding the complexity threshold
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/function-cognitive-complexity-flags.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/function-cognitive-complexity-flags.cpp
index 196f3181141f9..af74a9ea626c6 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/readability/function-cognitive-complexity-flags.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/function-cognitive-complexity-flags.cpp
@@ -11,10 +11,16 @@
// RUN: {readability-function-cognitive-complexity.Threshold: 0, \
// RUN: readability-function-cognitive-complexity.IgnoreMacros: "true", \
// RUN: readability-function-cognitive-complexity.DescribeBasicIncrements: "false"}}'
+// RUN: %check_clang_tidy -check-suffix=IGNORE-ABOVE %s readability-function-cognitive-complexity %t -- \
+// RUN: -config='{CheckOptions: \
+// RUN: {readability-function-cognitive-complexity.Threshold: 0, \
+// RUN: readability-function-cognitive-complexity.IgnoreAboveThreshold: 10, \
+// RUN: readability-function-cognitive-complexity.DescribeBasicIncrements: "false"}}'
void func_of_complexity_4() {
// CHECK-NOTES: :[[@LINE-1]]:6: warning: function 'func_of_complexity_4' has cognitive complexity of 4 (threshold 0) [readability-function-cognitive-complexity]
// CHECK-NOTES-IGNORE-MACROS: :[[@LINE-2]]:6: warning: function 'func_of_complexity_4' has cognitive complexity of 4 (threshold 0) [readability-function-cognitive-complexity]
+ // CHECK-NOTES-IGNORE-ABOVE: :[[@LINE-3]]:6: warning: function 'func_of_complexity_4' has cognitive complexity of 4 (threshold 0) [readability-function-cognitive-complexity]
if (1) {
if (1) {
}
@@ -55,6 +61,7 @@ void function_with_macro() {
void func_macro_1() {
// CHECK-NOTES: :[[@LINE-1]]:6: warning: function 'func_macro_1' has cognitive complexity of 2 (threshold 0) [readability-function-cognitive-complexity]
// CHECK-NOTES-IGNORE-MACROS: :[[@LINE-2]]:6: warning: function 'func_macro_1' has cognitive complexity of 1 (threshold 0) [readability-function-cognitive-complexity]
+ // CHECK-NOTES-IGNORE-ABOVE: :[[@LINE-3]]:6: warning: function 'func_macro_1' has cognitive complexity of 2 (threshold 0) [readability-function-cognitive-complexity]
if (1) {
}
@@ -64,6 +71,7 @@ void func_macro_1() {
void func_macro_2() {
// CHECK-NOTES: :[[@LINE-1]]:6: warning: function 'func_macro_2' has cognitive complexity of 4 (threshold 0) [readability-function-cognitive-complexity]
// CHECK-NOTES-IGNORE-MACROS: :[[@LINE-2]]:6: warning: function 'func_macro_2' has cognitive complexity of 1 (threshold 0) [readability-function-cognitive-complexity]
+ // CHECK-NOTES-IGNORE-ABOVE: :[[@LINE-3]]:6: warning: function 'func_macro_2' has cognitive complexity of 4 (threshold 0) [readability-function-cognitive-complexity]
if (1) {
}
More information about the cfe-commits
mailing list