Skip to content

Conversation

@HerrCai0907
Copy link
Contributor

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.

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.
@llvmbot
Copy link
Member

llvmbot commented Jun 19, 2024

@llvm/pr-subscribers-clang-tools-extra

@llvm/pr-subscribers-clang-tidy

Author: Congcong Cai (HerrCai0907)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/96122.diff

2 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp (+10-2)
  • (modified) clang-tools-extra/docs/ReleaseNotes.rst (+3)
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 ^^^^^^^^^^ 
Copy link
Member

@PiotrZSL PiotrZSL left a comment

Choose a reason for hiding this comment

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

if a reason for this parameter is not to fail wrapper scripts, then maybe run-clang-tidy.py / run-clang-tidy-diff.py should also be updated.

@HerrCai0907
Copy link
Contributor Author

if a reason for this parameter is not to fail wrapper scripts, then maybe run-clang-tidy.py / run-clang-tidy-diff.py should also be updated.

Both of them is fine because they only count the failed file and don't do any check about failure.

Copy link
Contributor

@5chmidti 5chmidti left a comment

Choose a reason for hiding this comment

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

´run-clang-tidy.pyandclang-tidy-diff.py` should probably expose an option to enable this option via the script (maybe with an adjustment to the release notes).

Comment on lines 662 to 665
if (EnabledChecks.empty() && !AllowNoChecks) {
llvm::errs() << "Error: no checks enabled.\n";
llvm::cl::PrintHelpMessage(/*Hidden=*/false, /*Categorized=*/true);
return 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be better to exit clang-tidy at this point, when there are no checks present and AllowNoChecks is enabled. The current behavior of this PR for invoking clang-tidy to check a file would be:

  • if no checks are enabled and AllowNoChecks is true
    • continue executing clang-tidy even though no checks are enabled

I think it should be:

  • if no checks are enabled
    • if AllowNoChecks is true then return 0, else error out with return 1
Co-authored-by: Danny Mösch <danny.moesch@icloud.com>
// RUN: clang-tidy %s -checks='-*' --allow-no-checks | FileCheck --match-full-lines %s


// CHECK: No checks enabled. No newline at end of file
Copy link
Contributor

Choose a reason for hiding this comment

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

missing newline

Copy link
Contributor

Choose a reason for hiding this comment

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

the same should probably be done for diff-clang-tidy.py

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Where is diff-clang-tidy.py?

Copy link
Contributor Author

@HerrCai0907 HerrCai0907 Jun 24, 2024

Choose a reason for hiding this comment

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

I found it. It is named clang-tidy-diff.py

Copy link
Contributor

@5chmidti 5chmidti left a comment

Choose a reason for hiding this comment

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

LGTM

@HerrCai0907 HerrCai0907 merged commit 4558e45 into llvm:main Jun 26, 2024
@HerrCai0907 HerrCai0907 deleted the tidy-support-empty-checks branch June 26, 2024 23:28
AlexisPerry pushed a commit to llvm-project-tlp/llvm-project that referenced this pull request Jul 9, 2024
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@icloud.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment