[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
Sun Dec 29 01:50:18 PST 2024
https://github.com/wonbinbk updated https://github.com/llvm/llvm-project/pull/120348
>From 062cecdcec24a807bd4bab5985a6ad339de3cfe6 Mon Sep 17 00:00:00 2001
From: wonbinbk <thaiphd at gmail.com>
Date: Wed, 18 Dec 2024 15:33:55 +1300
Subject: [PATCH 1/2] [clang-tidy] Add an option to exclude files not present
in the compilation 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.
---
.../clang-tidy/tool/clang-tidy-diff.py | 29 +++++++++++++++++++
clang-tools-extra/docs/ReleaseNotes.rst | 3 ++
2 files changed, 32 insertions(+)
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..a1d3598422bf24 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,21 @@ def merge_replacement_files(tmpdir, mergefile):
open(mergefile, "w").close()
+def get_compiling_files(args):
+ """ Read a compile_commands.json database and return a set of file paths """
+ 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")
+ files = set()
+ with open(compile_commands_json) as db_file:
+ db_json = json.load(db_file)
+ for entry in db_json:
+ if "file" not in entry:
+ continue
+ files.add(Path(entry["file"]))
+ return files
+
+
def main():
parser = argparse.ArgumentParser(
description="Run clang-tidy against changed files, and "
@@ -234,6 +250,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 the compilation database",
+ )
clang_tidy_args = []
argv = sys.argv[1:]
@@ -243,6 +266,8 @@ def main():
args = parser.parse_args(argv)
+ compiling_files = get_compiling_files(args) if args.skip_non_compiling else None
+
# Extract changed lines for each file.
filename = None
lines_by_file = {}
@@ -260,6 +285,10 @@ def main():
if not re.match("^%s$" % args.iregex, filename, re.IGNORECASE):
continue
+ # Skip any files not in the compiling list
+ if compiling_files is not None and (Path.cwd() / filename) not in compiling_files:
+ continue
+
match = re.search(r"^@@.*\+(\d+)(,(\d+))?", line)
if match:
start_line = int(match.group(1))
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index fabd0cc78ac645..dc2d5871148b35 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -115,6 +115,9 @@ Improvements to clang-tidy
- Improved :program:`run-clang-tidy.py` script. Fixed minor shutdown noise
happening on certain platforms when interrupting the script.
+- Improved :program:`clang-tidy-diff.py` script. Add an option to exclude files
+ not present in the compilation database to avoid false negative results.
+
- Improved :program:`clang-tidy` by accepting parameters file in command line.
- Removed :program:`clang-tidy`'s global options for most of checks. All options
>From d95df90ab3ea7f8ec4280d6a7a35f3f2aa926f84 Mon Sep 17 00:00:00 2001
From: wonbinbk <thaiphd at gmail.com>
Date: Tue, 24 Dec 2024 09:54:05 +1300
Subject: [PATCH 2/2] [clang-tidy] Use raw string for regex pattern
---
clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
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 a1d3598422bf24..2cb1cc4598073b 100755
--- a/clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py
+++ b/clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py
@@ -272,7 +272,7 @@ def main():
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:
More information about the cfe-commits
mailing list