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 skip processing these files.

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 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 17, 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 skip processing these files.


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

1 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py (+24)
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..00a8c98506216b 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 "
@@ -243,6 +257,12 @@ def main():
 
     args = parser.parse_args(argv)
 
+    # Read compile_commands.json database to extract a compiling file list
+    current_dir = Path(__file__).parent.resolve()
+    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 = {}
@@ -260,6 +280,10 @@ def main():
             if not re.match("^%s$" % args.iregex, filename, re.IGNORECASE):
                 continue
 
+        # Skip any files not in the compiling list
+        if (current_dir / filename) not in compiling_files:
+            continue
+
         match = re.search(r"^@@.*\+(\d+)(,(\d+))?", line)
         if match:
             start_line = int(match.group(1))

@wonbinbk wonbinbk closed this Dec 17, 2024
@wonbinbk wonbinbk changed the title [clang-tidy] Exclude Files Not Present in the Compile Database [clang-tidy] Add option to exclude files not present in the compile database Dec 18, 2024
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.

2 participants