Skip to content

Conversation

@wonbinbk
Copy link
Contributor

A change list may include files that are not part of the compile
database, which can cause clang-tidy to fail (e.g., due to missing
included headers). To prevent false negatives, we should allow to skip
processing these files.

@github-actions
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 Dec 18, 2024

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

@llvm/pr-subscribers-clang-tidy

Author: zotnhucucbot (wonbinbk)

Changes

A change list may include files that are not part of the compile
database, which can cause clang-tidy to fail (e.g., due to missing
included headers). To prevent false negatives, we should allow to skip
processing these files.


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

1 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py (+32-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 62cb4297c50f75..f1560ef892a60c 100755
--- a/clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py
+++ b/clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py
@@ -35,6 +35,7 @@
 import tempfile
 import threading
 import traceback
+from pathlib import Path
 
 try:
     import yaml
@@ -124,6 +125,19 @@ def merge_replacement_files(tmpdir, mergefile):
         open(mergefile, "w").close()
 
 
+def get_compiling_file_list(compiledb : Path) -> list[Path]:
+    """ Read a compile_commands.json database and return a list of file paths """
+    file_list = []
+    with open(compiledb) as db_file:
+        db_json = json.load(db_file)
+        for entry in db_json:
+            if "file" not in entry:
+                continue
+            if entry["file"] not in file_list:
+                file_list.append(Path(entry["file"]))
+    return file_list
+
+
 def main():
     parser = argparse.ArgumentParser(
         description="Run clang-tidy against changed files, and "
@@ -234,6 +248,13 @@ def main():
         action="store_true",
         help="Allow empty enabled checks.",
     )
+    parser.add_argument(
+        "-only-check-in-db",
+        dest="skip_non_compiling",
+        default=False,
+        action="store_true",
+        help="Only check files in compile database",
+    )
 
     clang_tidy_args = []
     argv = sys.argv[1:]
@@ -243,11 +264,17 @@ def main():
 
     args = parser.parse_args(argv)
 
+    # Read compile_commands.json database to extract a compiling file list
+    current_dir = Path.cwd()
+    compile_commands_json = (current_dir / args.build_path) if args.build_path else current_dir
+    compile_commands_json = (compile_commands_json / "compile_commands.json")
+    compiling_files = get_compiling_file_list(compile_commands_json)
+
     # Extract changed lines for each file.
     filename = None
     lines_by_file = {}
     for line in sys.stdin:
-        match = re.search('^\\+\\+\\+\\ "?(.*?/){%s}([^ \t\n"]*)' % args.p, line)
+        match = re.search(r'^\+\+\+\ "?(.*?/){%s}([^ \t\n"]*)' % args.p, line)
         if match:
             filename = match.group(2)
         if filename is None:
@@ -260,6 +287,10 @@ def main():
             if not re.match("^%s$" % args.iregex, filename, re.IGNORECASE):
                 continue
 
+        # Skip any files not in the compiling list
+        if args.skip_non_compiling and (current_dir / filename) not in compiling_files:
+            continue
+
         match = re.search(r"^@@.*\+(\d+)(,(\d+))?", line)
         if match:
             start_line = int(match.group(1))

@EugeneZelenko
Copy link
Contributor

Please mention changes in Release Notes.

@wonbinbk wonbinbk force-pushed the main branch 2 times, most recently from 5d912c8 to b2bac6d Compare December 23, 2024 21:10
@wonbinbk
Copy link
Contributor Author

Please mention changes in Release Notes.

Thanks. I have updated the Release Notes in clang-tools-extra/docs/ReleaseNotes.rst. Please tell me if there is other files need to be updated.

@wonbinbk wonbinbk force-pushed the main branch 2 times, most recently from 24d5372 to d95df90 Compare December 29, 2024 09:50
@wonbinbk
Copy link
Contributor Author

Rebased.

Copy link
Contributor

@HerrCai0907 HerrCai0907 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
Copy link
Contributor

feel free to ping me when you want to merge it.

@github-actions
Copy link

github-actions bot commented Jan 11, 2025

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

@wonbinbk
Copy link
Contributor Author

feel free to ping me when you want to merge it.

Thank you! I just reformatted the change to make darker happy. Should be all good to merge now if you don't see anything else need to be changed.

…ation database

A change list may include files that are not part of the compilation
database, which can cause clang-tidy to fail (e.g., due to missing
included headers). To prevent false negatives, we should allow to skip
processing these files.
@wonbinbk
Copy link
Contributor Author

wonbinbk commented Jan 14, 2025

Pinging you @HerrCai0907 😃 Please merge this PR if you don't see anything else need to be fixed.

@HerrCai0907 HerrCai0907 merged commit bc74625 into llvm:main Jan 14, 2025
7 of 8 checks passed
@github-actions
Copy link

@wonbinbk 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 receive 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!

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