Skip to content

Commit

Permalink
Automerge: [clang-tidy] Add an option to exclude files not present in…
Browse files Browse the repository at this point in the history
… the compile database (#120348)

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.
  • Loading branch information
wonbinbk authored and github-actions[bot] committed Jan 15, 2025
2 parents a4a84e6 + bc74625 commit 30832f3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
36 changes: 35 additions & 1 deletion clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import tempfile
import threading
import traceback
from pathlib import Path

try:
import yaml
Expand Down Expand Up @@ -124,6 +125,23 @@ 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 "
Expand Down Expand Up @@ -234,6 +252,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:]
Expand All @@ -243,11 +268,13 @@ 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 = {}
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:
Expand All @@ -260,6 +287,13 @@ 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))
Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ Improvements to clang-query
Improvements to clang-tidy
--------------------------

- Improved :program:`clang-tidy-diff.py` script. Add the `-only-check-in-db`
option to exclude files not present in the compilation database, avoiding
false-negative results.

- Improved :program:`run-clang-tidy.py` script. Fixed minor shutdown noise
happening on certain platforms when interrupting the script.

Expand Down

0 comments on commit 30832f3

Please sign in to comment.