Skip to content

Commit f301d36

Browse files
committed
[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.
1 parent edcf75f commit f301d36

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import tempfile
3636
import threading
3737
import traceback
38+
from pathlib import Path
3839

3940
try:
4041
import yaml
@@ -124,6 +125,21 @@ def merge_replacement_files(tmpdir, mergefile):
124125
open(mergefile, "w").close()
125126

126127

128+
def get_compiling_files(args):
129+
""" Read a compile_commands.json database and return a set of file paths """
130+
current_dir = Path.cwd()
131+
compile_commands_json = (current_dir / args.build_path) if args.build_path else current_dir
132+
compile_commands_json = (compile_commands_json / "compile_commands.json")
133+
files = set()
134+
with open(compile_commands_json) as db_file:
135+
db_json = json.load(db_file)
136+
for entry in db_json:
137+
if "file" not in entry:
138+
continue
139+
files.add(Path(entry["file"]))
140+
return files
141+
142+
127143
def main():
128144
parser = argparse.ArgumentParser(
129145
description="Run clang-tidy against changed files, and "
@@ -234,6 +250,13 @@ def main():
234250
action="store_true",
235251
help="Allow empty enabled checks.",
236252
)
253+
parser.add_argument(
254+
"-only-check-in-db",
255+
dest="skip_non_compiling",
256+
default=False,
257+
action="store_true",
258+
help="Only check files in the compilation database",
259+
)
237260

238261
clang_tidy_args = []
239262
argv = sys.argv[1:]
@@ -243,6 +266,8 @@ def main():
243266

244267
args = parser.parse_args(argv)
245268

269+
compiling_files = get_compiling_files(args) if args.skip_non_compiling else None
270+
246271
# Extract changed lines for each file.
247272
filename = None
248273
lines_by_file = {}
@@ -260,6 +285,10 @@ def main():
260285
if not re.match("^%s$" % args.iregex, filename, re.IGNORECASE):
261286
continue
262287

288+
# Skip any files not in the compiling list
289+
if compiling_files is not None and (Path.cwd() / filename) not in compiling_files:
290+
continue
291+
263292
match = re.search(r"^@@.*\+(\d+)(,(\d+))?", line)
264293
if match:
265294
start_line = int(match.group(1))

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ Improvements to clang-tidy
114114

115115
- Improved :program:`clang-tidy` by accepting parameters file in command line.
116116

117+
- Improved :program:`clang-tidy-diff.py` script. Add the `-only-check-in-db`
118+
option to exclude files not present in the compilation database, avoiding
119+
false-negative results.
120+
117121
- Improved :program:`run-clang-tidy.py` script. Fixed minor shutdown noise
118122
happening on certain platforms when interrupting the script.
119123

0 commit comments

Comments
 (0)