[clang-tools-extra] [clang-tidy] Add an option to exclude files not present in the compile database (PR #120348)

via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 17 18:47:24 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-tidy

Author: zotnhucucbot (wonbinbk)

<details>
<summary>Changes</summary>

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
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))

``````````

</details>


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


More information about the cfe-commits mailing list