Skip to content

[run-clang-tidy.py] Add -directory-filter option to run-clang-tidy.py #89302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

[run-clang-tidy.py] Add -directory-filter option to run-clang-tidy.py #89302

wants to merge 1 commit into from

Conversation

edunad
Copy link

@edunad edunad commented Apr 18, 2024

Similar to #82416, but it allows you to filter full directories without the need of a complex regex in -source-filter

For example:

python run-clang-tidy.py -fix -p \"build\" -j 12 -directory-filter build -directory-filter bin -directory-filter tests

To filter out bin, build and tests folders

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Apr 18, 2024

@llvm/pr-subscribers-clang-tidy

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

Author: FailCake (edunad)

Changes

Similar to #82416, but it allows you to filter full directories without the need of a complex regex in -source-filter

For example:

python run-clang-tidy.py -fix -p \"build\" -j 12 -directory-filter build -directory-filter bin -directory-filter tests

To filter out bin, build and tests folders


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

1 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/tool/run-clang-tidy.py (+10-1)
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 1bd4a5b283091c..4ef9b0a8ebf5e2 100755
--- a/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
+++ b/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
@@ -307,6 +307,14 @@ def main():
         "source files from compilation database to output "
         "diagnostics from.",
     )
+    parser.add_argument(
+        "-directory-filter",
+        dest="directory_filter",
+        action="append",
+        default=[],
+        help="List of directories matching the names of the "
+        "compilation database to filter.",
+    )
     parser.add_argument(
         "-line-filter",
         default=None,
@@ -488,6 +496,7 @@ def main():
 
     # Build up a big regexy filter from all command line arguments.
     file_name_re = re.compile("|".join(args.files))
+    directory_filters_re = re.compile("|".join(args.directory_filters))
 
     return_code = 0
     try:
@@ -514,7 +523,7 @@ def main():
 
         # Fill the queue with files.
         for name in files:
-            if file_name_re.search(name):
+            if file_name_re.search(name) and not directory_filters_re.search(name):
                 task_queue.put(name)
 
         # Wait for all threads to be done.

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.

Personally I do not see use case for it, as source-filter also apply to directories, and better would be to just change source-filter to accept list of regexes.

Other thing that directory-filter works as "exclude", not "include", and for me this could be just --exclude-files or something like that, but still users may use negative regexp.

Release notes change missing.

action="append",
default=[],
help="List of directories matching the names of the "
"compilation database to filter.",
Copy link
Member

Choose a reason for hiding this comment

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

filter in, or filter out ?

Comment on lines +315 to +316
help="List of directories matching the names of the "
"compilation database to filter.",
Copy link
Member

Choose a reason for hiding this comment

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

use regexp instead of list of directories

@@ -307,6 +307,14 @@ def main():
"source files from compilation database to output "
"diagnostics from.",
)
parser.add_argument(
"-directory-filter",
Copy link
Member

Choose a reason for hiding this comment

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

this basically work opposite to -source-filter, specially because it work on full path, not just on directory path.
Therefore I could filter out with this an file also, not only directory.

For me this option is just an negative source-filter, and as you can do negative regexp in python, i do not see a reason for it.

Specially that for what this option describes there is already this:

    parser.add_argument(
        "files", nargs="*", default=[".*"], help="files to be processed (regex on path)"
    )

Comment on lines -517 to +526
if file_name_re.search(name):
if file_name_re.search(name) and not directory_filters_re.search(name):
Copy link
Member

Choose a reason for hiding this comment

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

to be honest those lines should be handled like current filtering in line 491

@edunad
Copy link
Author

edunad commented Apr 23, 2024

@nicovank You're right ^^, it's better to just use negative regex, i'll close this PR then

@edunad edunad closed this Apr 23, 2024
@edunad edunad deleted the patch-1 branch April 23, 2024 11:43
PiotrZSL pushed a commit that referenced this pull request Jul 22, 2024
…89490)

[There is
work](https://discourse.llvm.org/t/rfc-upgrading-llvms-minimum-required-python-version/67571)
to make Python 3.8 the minimum Python version for LLVM.

I edited this script because I wanted some indicator of progress while
going through files.
It now outputs `[XX/YYY]` with the number of processed and total files
after each completion.

The current version of this script is compatible downto Python 3.6 (this
is PyYAML's minimum version).
It would probably work with older Python 3 versions with an older PyYAML
or when YAML is disabled.

With the updates here, it is compatible downto Python 3.7. Python 3.7
was released June 2018.

#89302 is also touching this
file, I don't mind rebasing on top of that work if needed.

### Summary

 -  Add type annotations.
 -  Replace `threading` + `queue` with `asyncio`.
- **Add indicator of processed files over total files**. This is what I
set out to do initially.
- Only print the filename after completion, not the entire Clang-Tidy
invocation command. I find this neater but the behavior can easily be
restored.
yuxuanchen1997 pushed a commit that referenced this pull request Jul 25, 2024
…89490)

Summary:
[There is
work](https://discourse.llvm.org/t/rfc-upgrading-llvms-minimum-required-python-version/67571)
to make Python 3.8 the minimum Python version for LLVM.

I edited this script because I wanted some indicator of progress while
going through files.
It now outputs `[XX/YYY]` with the number of processed and total files
after each completion.

The current version of this script is compatible downto Python 3.6 (this
is PyYAML's minimum version).
It would probably work with older Python 3 versions with an older PyYAML
or when YAML is disabled.

With the updates here, it is compatible downto Python 3.7. Python 3.7
was released June 2018.

#89302 is also touching this
file, I don't mind rebasing on top of that work if needed.

### Summary

 -  Add type annotations.
 -  Replace `threading` + `queue` with `asyncio`.
- **Add indicator of processed files over total files**. This is what I
set out to do initially.
- Only print the filename after completion, not the entire Clang-Tidy
invocation command. I find this neater but the behavior can easily be
restored.

Test Plan: 

Reviewers: 

Subscribers: 

Tasks: 

Tags: 


Differential Revision: https://phabricator.intern.facebook.com/D60251392
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants