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

via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 26 16:28:00 PDT 2024


Author: Congcong Cai
Date: 2024-06-27T07:27:58+08:00
New Revision: 4558e45e7e33d1cfc1a54af761085e358dbab64b

URL: https://github.com/llvm/llvm-project/commit/4558e45e7e33d1cfc1a54af761085e358dbab64b
DIFF: https://github.com/llvm/llvm-project/commit/4558e45e7e33d1cfc1a54af761085e358dbab64b.diff

LOG: [clang-tidy] add option to avoid "no checks enabled" error (#96122)

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.

---------

Co-authored-by: Danny Mösch <danny.moesch at icloud.com>

Added: 
    clang-tools-extra/test/clang-tidy/infrastructure/allow-no-checks.cpp

Modified: 
    clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
    clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py
    clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/docs/clang-tidy/index.rst

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp b/clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
index 7388f20ef288e..d42dafa8ffc36 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> AllowNoChecks("allow-no-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() && !AllowNoChecks) {
       llvm::errs() << "No checks enabled.\n";
       return 1;
     }
@@ -652,6 +660,10 @@ int clangTidyMain(int argc, const char **argv) {
   }
 
   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/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:

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 6e7cc8a873a22..0dc35ad587362 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
 
@@ -232,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(
@@ -405,6 +409,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"
@@ -466,6 +475,7 @@ def main():
             args.plugins,
             args.warnings_as_errors,
             args.exclude_header_filter,
+            args.allow_no_checks,
         )
         invocation.append("-list-checks")
         invocation.append("-")

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 7fcf5cfbe3783..7e94c9cc46bfe 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-no-checks` to suppress "no checks enabled" error
+  when disabling all of the checks by `--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.
 

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..a1f059b92384d
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/infrastructure/allow-no-checks.cpp
@@ -0,0 +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.


        


More information about the cfe-commits mailing list