Skip to content

[run-clang-tidy.py] Add option to ignore source files from compilation database #82416

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

Merged
merged 5 commits into from
Feb 25, 2024

Conversation

duddel
Copy link
Contributor

@duddel duddel commented Feb 20, 2024

I added the option -source-ignore -source-filter to the run-clang-tidy.py script in the clang-tools-extra.

This option allows for handing over a regex, to filter out source files from the compilation database (not run clang-tidy on them).

Why "ignore" instead of "filter"?
I found it more useful to actively filter out (ignore) source files, rather than the inverse logic. One usually knows where the source files reside and can now easily ignore e.g. the thirdparty/ directory.

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 Feb 20, 2024

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

@llvm/pr-subscribers-clang-tidy

Author: Alexander (duddel)

Changes

I added the option -source-ignore to the run-clang-tidy.py script in the clang-tools-extra.

This option allows for handing over a regex, to ignore matching source files from the compilation database (not run clang-tidy on them).

Why "ignore" instead of "filter"?
I found it more useful to actively filter out (ignore) source files, rather than the inverse logic. One usually knows where the source files reside and can now easily ignore e.g. the thirdparty/ directory.


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

1 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/tool/run-clang-tidy.py (+15)
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 70f8cbcdcb2f11..ba4314dfb50aa1 100755
--- a/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
+++ b/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
@@ -300,6 +300,12 @@ def main():
         "the main file of each translation unit are always "
         "displayed.",
     )
+    parser.add_argument(
+        "-source-ignore",
+        default=None,
+        help="Regular expression matching the names of the "
+        "source files from compilation database to ignore.",
+    )
     parser.add_argument(
         "-line-filter",
         default=None,
@@ -462,6 +468,15 @@ def main():
         [make_absolute(entry["file"], entry["directory"]) for entry in database]
     )
 
+    # Remove source file to be ignored from database.
+    if args.source_ignore:
+        try:
+            source_ignore_re = re.compile(args.source_ignore)
+        except:
+            print("Error: unable to compile regex from arg -source-ignore.", file=sys.stderr)
+            sys.exit(1)
+        files = {f for f in files if not source_ignore_re.match(f)}
+
     max_task = args.j
     if max_task == 0:
         max_task = multiprocessing.cpu_count()

Copy link

⚠️ We detected that you are using a GitHub private e-mail address to contribute to the repo.
Please turn off Keep my email addresses private setting in your account.
See LLVM Discourse for more information.

Copy link

github-actions bot commented Feb 20, 2024

✅ With the latest revision this PR passed the Python code formatter.

@carlosgalvezp
Copy link
Contributor

Why "ignore" instead of "filter"?

Why can't we make "filter" use a full regex that supports negative expressions instead?

@duddel
Copy link
Contributor Author

duddel commented Feb 21, 2024

Why "ignore" instead of "filter"?

Why can't we make "filter" use a full regex that supports negative expressions instead?

We can. I provide a patch that inverts the logic (-source-filter), so it is aligned with -header-filter.

@PiotrZSL PiotrZSL merged commit 8dfc023 into llvm:main Feb 25, 2024
Copy link

@duddel Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested
by our build bots. If there is a problem with a build, you may recieve a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as
the builds can include changes from many authors. It is not uncommon for your
change to be included in a build that fails due to someone else's changes, or
infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself.
This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@firewave
Copy link

firewave commented Mar 8, 2024

Why can't we make "filter" use a full regex that supports negative expressions instead?

How do you do that? I thought llvm::RegEx doesn't support negative expressions.

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.

5 participants