Skip to content

Commit ffa6ffd

Browse files
HerrCai0907SimplyDanny
authored andcommitted
[clang-tidy] add option to avoid "no checks enabled" error (llvm#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 <[email protected]>
1 parent cff9f93 commit ffa6ffd

File tree

6 files changed

+40
-1
lines changed

6 files changed

+40
-1
lines changed

clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,14 @@ option is recognized.
325325
)"),
326326
cl::init(false), cl::cat(ClangTidyCategory));
327327

328+
static cl::opt<bool> AllowNoChecks("allow-no-checks", desc(R"(
329+
Allow empty enabled checks. This suppresses
330+
the "no checks enabled" error when disabling
331+
all of the checks.
332+
)"),
333+
cl::init(false),
334+
cl::cat(ClangTidyCategory));
335+
328336
namespace clang::tidy {
329337

330338
static void printStats(const ClangTidyStats &Stats) {
@@ -598,7 +606,7 @@ int clangTidyMain(int argc, const char **argv) {
598606
}
599607

600608
if (ListChecks) {
601-
if (EnabledChecks.empty()) {
609+
if (EnabledChecks.empty() && !AllowNoChecks) {
602610
llvm::errs() << "No checks enabled.\n";
603611
return 1;
604612
}
@@ -652,6 +660,10 @@ int clangTidyMain(int argc, const char **argv) {
652660
}
653661

654662
if (EnabledChecks.empty()) {
663+
if (AllowNoChecks) {
664+
llvm::outs() << "No checks enabled.\n";
665+
return 0;
666+
}
655667
llvm::errs() << "Error: no checks enabled.\n";
656668
llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true);
657669
return 1;

clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,11 @@ def main():
229229
default=[],
230230
help="Load the specified plugin in clang-tidy.",
231231
)
232+
parser.add_argument(
233+
"-allow-no-checks",
234+
action="store_true",
235+
help="Allow empty enabled checks.",
236+
)
232237

233238
clang_tidy_args = []
234239
argv = sys.argv[1:]
@@ -327,6 +332,8 @@ def main():
327332
common_clang_tidy_args.append("-p=%s" % args.build_path)
328333
if args.use_color:
329334
common_clang_tidy_args.append("--use-color")
335+
if args.allow_no_checks:
336+
common_clang_tidy_args.append("--allow-no-checks")
330337
for arg in args.extra_arg:
331338
common_clang_tidy_args.append("-extra-arg=%s" % arg)
332339
for arg in args.extra_arg_before:

clang-tools-extra/clang-tidy/tool/run-clang-tidy.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ def get_tidy_invocation(
107107
plugins,
108108
warnings_as_errors,
109109
exclude_header_filter,
110+
allow_no_checks,
110111
):
111112
"""Gets a command line for clang-tidy."""
112113
start = [clang_tidy_binary]
@@ -147,6 +148,8 @@ def get_tidy_invocation(
147148
start.append("-load=" + plugin)
148149
if warnings_as_errors:
149150
start.append("--warnings-as-errors=" + warnings_as_errors)
151+
if allow_no_checks:
152+
start.append("--allow-no-checks")
150153
start.append(f)
151154
return start
152155

@@ -232,6 +235,7 @@ def run_tidy(args, clang_tidy_binary, tmpdir, build_path, queue, lock, failed_fi
232235
args.plugins,
233236
args.warnings_as_errors,
234237
args.exclude_header_filter,
238+
args.allow_no_checks,
235239
)
236240

237241
proc = subprocess.Popen(
@@ -405,6 +409,11 @@ def main():
405409
default=None,
406410
help="Upgrades warnings to errors. Same format as '-checks'.",
407411
)
412+
parser.add_argument(
413+
"-allow-no-checks",
414+
action="store_true",
415+
help="Allow empty enabled checks.",
416+
)
408417
args = parser.parse_args()
409418

410419
db_path = "compile_commands.json"
@@ -466,6 +475,7 @@ def main():
466475
args.plugins,
467476
args.warnings_as_errors,
468477
args.exclude_header_filter,
478+
args.allow_no_checks,
469479
)
470480
invocation.append("-list-checks")
471481
invocation.append("-")

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ Improvements to clang-tidy
125125
- Added argument `--exclude-header-filter` and config option `ExcludeHeaderFilterRegex`
126126
to exclude headers from analysis via a RegEx.
127127

128+
- Added argument `--allow-no-checks` to suppress "no checks enabled" error
129+
when disabling all of the checks by `--checks='-*'`.
130+
128131
New checks
129132
^^^^^^^^^^
130133

clang-tools-extra/docs/clang-tidy/index.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ An overview of all the command-line options:
240240
This option's value is appended to the value of
241241
the 'WarningsAsErrors' option in .clang-tidy
242242
file, if any.
243+
--allow-no-checks - Allow empty enabled checks. This suppresses
244+
the "no checks enabled" error when disabling
245+
all of the checks.
243246
244247
-p <build-path> is used to read a compile command database.
245248
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// RUN: not clang-tidy %s -checks='-*'
2+
// RUN: clang-tidy %s -checks='-*' --allow-no-checks | FileCheck --match-full-lines %s
3+
4+
// CHECK: No checks enabled.

0 commit comments

Comments
 (0)