[clang-tools-extra] [clang-tidy] add option to avoid "no checks enabled" error (PR #96122)

Congcong Cai via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 24 06:06:27 PDT 2024


https://github.com/HerrCai0907 updated https://github.com/llvm/llvm-project/pull/96122

>From 41993ea6903668c41eef8a4477f5914c894f7109 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Wed, 19 Jun 2024 23:20:09 +0000
Subject: [PATCH 1/8] [clang-tidy] add option to avoid "no checks enabled"
 error

When clang-tidy get an empty checks, it will throw "no checks enabled" error and exit with non-zero return value.
It make clang-tidy's wrapper program confused when in big project some files don't want to be checked and use `-checks=-*` to disable all checks.
---
 clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp | 12 ++++++++++--
 clang-tools-extra/docs/ReleaseNotes.rst             |  3 +++
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp b/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
index 7388f20ef288e..b579aff4394c9 100644
--- a/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
+++ b/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
@@ -325,6 +325,14 @@ option is recognized.
 )"),
                                   cl::init(false), cl::cat(ClangTidyCategory));
 
+static cl::opt<bool> AllowEmptyCheckList("allow-empty-checks", desc(R"(
+Allow empty enabled checks. This suppresses
+the "no checks enabled" error when disabling
+all of the checks.
+)"),
+                                         cl::init(false),
+                                         cl::cat(ClangTidyCategory));
+
 namespace clang::tidy {
 
 static void printStats(const ClangTidyStats &Stats) {
@@ -598,7 +606,7 @@ int clangTidyMain(int argc, const char **argv) {
   }
 
   if (ListChecks) {
-    if (EnabledChecks.empty()) {
+    if (EnabledChecks.empty() && !AllowEmptyCheckList) {
       llvm::errs() << "No checks enabled.\n";
       return 1;
     }
@@ -651,7 +659,7 @@ int clangTidyMain(int argc, const char **argv) {
     return 0;
   }
 
-  if (EnabledChecks.empty()) {
+  if (EnabledChecks.empty() && !AllowEmptyCheckList) {
     llvm::errs() << "Error: no checks enabled.\n";
     llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true);
     return 1;
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 3bdd735f7dcf7..54cfcafd121b6 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -125,6 +125,9 @@ Improvements to clang-tidy
 - Added argument `--exclude-header-filter` and config option `ExcludeHeaderFilterRegex`
   to exclude headers from analysis via a RegEx.
 
+- Added argument `--allow-empty-checks` and config option `AllowEmptyCheckList`
+  to suppress "no checks enabled" error when disabling all of the checks.
+
 New checks
 ^^^^^^^^^^
 

>From 9302feee8fa9d19711ad2126dddbd73c044502b0 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Thu, 20 Jun 2024 17:58:10 +0800
Subject: [PATCH 2/8] fix acc comment

---
 clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp | 6 +++---
 clang-tools-extra/docs/ReleaseNotes.rst             | 4 ++--
 clang-tools-extra/docs/clang-tidy/index.rst         | 3 +++
 3 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp b/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
index b579aff4394c9..1475816827ac4 100644
--- a/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
+++ b/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
@@ -325,7 +325,7 @@ option is recognized.
 )"),
                                   cl::init(false), cl::cat(ClangTidyCategory));
 
-static cl::opt<bool> AllowEmptyCheckList("allow-empty-checks", desc(R"(
+static cl::opt<bool> AllowNoChecks("allow-no-checks", desc(R"(
 Allow empty enabled checks. This suppresses
 the "no checks enabled" error when disabling
 all of the checks.
@@ -606,7 +606,7 @@ int clangTidyMain(int argc, const char **argv) {
   }
 
   if (ListChecks) {
-    if (EnabledChecks.empty() && !AllowEmptyCheckList) {
+    if (EnabledChecks.empty() && !AllowNoChecks) {
       llvm::errs() << "No checks enabled.\n";
       return 1;
     }
@@ -659,7 +659,7 @@ int clangTidyMain(int argc, const char **argv) {
     return 0;
   }
 
-  if (EnabledChecks.empty() && !AllowEmptyCheckList) {
+  if (EnabledChecks.empty() && !AllowNoChecks) {
     llvm::errs() << "Error: no checks enabled.\n";
     llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true);
     return 1;
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 54cfcafd121b6..e9a9cd47e9215 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -125,8 +125,8 @@ Improvements to clang-tidy
 - Added argument `--exclude-header-filter` and config option `ExcludeHeaderFilterRegex`
   to exclude headers from analysis via a RegEx.
 
-- Added argument `--allow-empty-checks` and config option `AllowEmptyCheckList`
-  to suppress "no checks enabled" error when disabling all of the checks.
+- Added argument `--allow-no-checks` to suppress "no checks enabled" error
+  when disabling all of the checks.
 
 New checks
 ^^^^^^^^^^
diff --git a/clang-tools-extra/docs/clang-tidy/index.rst b/clang-tools-extra/docs/clang-tidy/index.rst
index 9ccacefa3c2c5..c8fc34c61caeb 100644
--- a/clang-tools-extra/docs/clang-tidy/index.rst
+++ b/clang-tools-extra/docs/clang-tidy/index.rst
@@ -240,6 +240,9 @@ An overview of all the command-line options:
                                        This option's value is appended to the value of
                                        the 'WarningsAsErrors' option in .clang-tidy
                                        file, if any.
+    --allow-no-checks                - Allow empty enabled checks. This suppresses
+                                       the "no checks enabled" error when disabling
+                                       all of the checks.
 
   -p <build-path> is used to read a compile command database.
 

>From 8171fb5ae9920274b5cc94bc14c72188c2c49628 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Thu, 20 Jun 2024 14:13:20 +0000
Subject: [PATCH 3/8] fix

---
 clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp         | 6 +++++-
 .../test/clang-tidy/infrastructure/allow-no-checks.cpp      | 5 +++++
 2 files changed, 10 insertions(+), 1 deletion(-)
 create mode 100644 clang-tools-extra/test/clang-tidy/infrastructure/allow-no-checks.cpp

diff --git a/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp b/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
index 1475816827ac4..d42dafa8ffc36 100644
--- a/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
+++ b/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
@@ -659,7 +659,11 @@ int clangTidyMain(int argc, const char **argv) {
     return 0;
   }
 
-  if (EnabledChecks.empty() && !AllowNoChecks) {
+  if (EnabledChecks.empty()) {
+    if (AllowNoChecks) {
+      llvm::outs() << "No checks enabled.\n";
+      return 0;
+    }
     llvm::errs() << "Error: no checks enabled.\n";
     llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true);
     return 1;
diff --git a/clang-tools-extra/test/clang-tidy/infrastructure/allow-no-checks.cpp b/clang-tools-extra/test/clang-tidy/infrastructure/allow-no-checks.cpp
new file mode 100644
index 0000000000000..61776ae17044e
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/infrastructure/allow-no-checks.cpp
@@ -0,0 +1,5 @@
+// RUN: not clang-tidy %s -checks='-*'
+// RUN: clang-tidy %s -checks='-*' --allow-no-checks | FileCheck --match-full-lines %s
+
+
+// CHECK: No checks enabled.
\ No newline at end of file

>From 78cb07b907e176fc4c16358216a31921dfdfeebb Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Thu, 20 Jun 2024 14:19:05 +0000
Subject: [PATCH 4/8] support in run-clang-tidy

---
 clang-tools-extra/clang-tidy/tool/run-clang-tidy.py | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py b/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
index 4dd20bec81d3b..7063a18cf9f5d 100755
--- a/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
+++ b/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
@@ -107,6 +107,7 @@ def get_tidy_invocation(
     plugins,
     warnings_as_errors,
     exclude_header_filter,
+    allow_no_checks,
 ):
     """Gets a command line for clang-tidy."""
     start = [clang_tidy_binary]
@@ -147,6 +148,8 @@ def get_tidy_invocation(
         start.append("-load=" + plugin)
     if warnings_as_errors:
         start.append("--warnings-as-errors=" + warnings_as_errors)
+    if allow_no_checks:
+        start.append("--allow-no-checks")
     start.append(f)
     return start
 
@@ -402,6 +405,11 @@ def main():
         default=None,
         help="Upgrades warnings to errors. Same format as '-checks'",
     )
+    parser.add_argument(
+        "-allow-no-checks",
+        action="store_true",
+        help="Allow empty enabled checks.",
+    )
     args = parser.parse_args()
 
     db_path = "compile_commands.json"
@@ -463,6 +471,7 @@ def main():
             args.plugins,
             args.warnings_as_errors,
             args.exclude_header_filter,
+            args.allow_no_checks,
         )
         invocation.append("-list-checks")
         invocation.append("-")

>From 586283ebf94af4862af514251372ad6a80d1e5a7 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcong.cai at bmw.com>
Date: Sat, 22 Jun 2024 09:22:55 +0800
Subject: [PATCH 5/8] fix

---
 clang-tools-extra/clang-tidy/tool/run-clang-tidy.py | 1 +
 1 file changed, 1 insertion(+)

diff --git a/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py b/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
index 7063a18cf9f5d..c9379586682d9 100755
--- a/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
+++ b/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
@@ -235,6 +235,7 @@ def run_tidy(args, clang_tidy_binary, tmpdir, build_path, queue, lock, failed_fi
             args.plugins,
             args.warnings_as_errors,
             args.exclude_header_filter,
+            args.allow_no_checks,
         )
 
         proc = subprocess.Popen(

>From fb7b211a50a81d17689831f56629fd6daf894920 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Sun, 23 Jun 2024 15:05:28 +0800
Subject: [PATCH 6/8] Update clang-tools-extra/docs/ReleaseNotes.rst
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Co-authored-by: Danny Mösch <danny.moesch at icloud.com>
---
 clang-tools-extra/docs/ReleaseNotes.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 2a2eabe575b0b..39c78e66c6cab 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -126,7 +126,7 @@ Improvements to clang-tidy
   to exclude headers from analysis via a RegEx.
 
 - Added argument `--allow-no-checks` to suppress "no checks enabled" error
-  when disabling all of the checks.
+  when disabling all of the checks by `--checks='-*'`.
 
 New checks
 ^^^^^^^^^^

>From cd9077aa3dc4617832ce0435bd2e7f5da59f0f16 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcong.cai at bmw.com>
Date: Mon, 24 Jun 2024 20:59:11 +0800
Subject: [PATCH 7/8] new line

---
 .../test/clang-tidy/infrastructure/allow-no-checks.cpp         | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/clang-tools-extra/test/clang-tidy/infrastructure/allow-no-checks.cpp b/clang-tools-extra/test/clang-tidy/infrastructure/allow-no-checks.cpp
index 61776ae17044e..a1f059b92384d 100644
--- a/clang-tools-extra/test/clang-tidy/infrastructure/allow-no-checks.cpp
+++ b/clang-tools-extra/test/clang-tidy/infrastructure/allow-no-checks.cpp
@@ -1,5 +1,4 @@
 // RUN: not clang-tidy %s -checks='-*'
 // RUN: clang-tidy %s -checks='-*' --allow-no-checks | FileCheck --match-full-lines %s
 
-
-// CHECK: No checks enabled.
\ No newline at end of file
+// CHECK: No checks enabled.

>From d70e54daa6612ca5c49eb0163f2edf4e56d73089 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcong.cai at bmw.com>
Date: Mon, 24 Jun 2024 21:02:25 +0800
Subject: [PATCH 8/8] add option in clang-tidy-diff.py

---
 clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py b/clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py
index b048460abf2fc..62cb4297c50f7 100755
--- a/clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py
+++ b/clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py
@@ -229,6 +229,11 @@ def main():
         default=[],
         help="Load the specified plugin in clang-tidy.",
     )
+    parser.add_argument(
+        "-allow-no-checks",
+        action="store_true",
+        help="Allow empty enabled checks.",
+    )
 
     clang_tidy_args = []
     argv = sys.argv[1:]
@@ -327,6 +332,8 @@ def main():
         common_clang_tidy_args.append("-p=%s" % args.build_path)
     if args.use_color:
         common_clang_tidy_args.append("--use-color")
+    if args.allow_no_checks:
+        common_clang_tidy_args.append("--allow-no-checks")
     for arg in args.extra_arg:
         common_clang_tidy_args.append("-extra-arg=%s" % arg)
     for arg in args.extra_arg_before:



More information about the cfe-commits mailing list