forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-clang-tidy-on-compile-commands.py
executable file
·486 lines (394 loc) · 15.1 KB
/
run-clang-tidy-on-compile-commands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#!/usr/bin/env python
#
# Runs clang-tidy on files based on a `compile_commands.json` file
#
"""
Run clang-tidy in parallel on compile databases.
Example run:
# This prepares the build. NOTE this is `build` not `gen` because the build
# step generates required header files (this can be simplified if needed
# to invoke ninja to compile only generated files if needed)
./scripts/build/build_examples.py --target linux-x64-chip-tool-clang build
# Actually running clang-tidy to check status
./scripts/run-clang-tidy-on-compile-commands.py check
# Run and output a fix yaml
./scripts/run-clang-tidy-on-compile-commands.py --export-fixes out/fixes.yaml check
# Apply the fixes
clang-apply-replacements out/fixes.yaml
"""
import glob
import json
import logging
import multiprocessing
import os
import queue
import re
import shlex
import subprocess
import sys
import tempfile
import threading
import traceback
import click
import coloredlogs
import yaml
class TidyResult:
def __init__(self, path: str, ok: bool):
self.path = path
self.ok = ok
def __repr__(self):
if self.ok:
status = "OK"
else:
status = "FAIL"
return "%s(%s)" % (status, self.path)
def __str__(self):
return self.__repr__()
class ClangTidyEntry:
"""Represents a single entry for running clang-tidy based
on a compile_commands.json item.
"""
def __init__(self, json_entry, gcc_sysroot=None):
# Entries in compile_commands:
# - "directory": location to run the compile
# - "file": a relative path to directory
# - "command": full compilation command
self.directory = json_entry["directory"]
self.file = json_entry["file"]
self.valid = False
self.clang_arguments = []
self.tidy_arguments = []
command = json_entry["command"]
command_items = shlex.split(command)
compiler = os.path.basename(command_items[0])
# Allow gcc/g++ invocations to also be tidied - arguments should be
# compatible and on darwin gcc/g++ is actually a symlink to clang
if compiler in ['clang++', 'clang', 'gcc', 'g++']:
self.valid = True
self.clang_arguments = command_items[1:]
else:
logging.warning(
"Cannot tidy %s - not a clang compile command", self.file)
return
if compiler in ['gcc', 'g++'] and gcc_sysroot:
self.clang_arguments.insert(0, '--sysroot='+gcc_sysroot)
@property
def full_path(self):
return os.path.abspath(os.path.join(self.directory, self.file))
def ExportFixesTo(self, f: str):
self.tidy_arguments.append("--export-fixes")
self.tidy_arguments.append(f)
def SetChecks(self, checks: str):
self.tidy_arguments.append("--checks")
self.tidy_arguments.append(checks)
def Check(self):
logging.debug("Running tidy on %s from %s", self.file, self.directory)
try:
cmd = ["clang-tidy", self.file] + \
self.tidy_arguments + ["--"] + self.clang_arguments
logging.debug("Executing: %r" % cmd)
proc = subprocess.Popen(
cmd,
cwd=self.directory,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
output, err = proc.communicate()
if output:
# Output generally contains validation data. Print it out as-is
logging.info("TIDY %s: %s", self.file, output.decode("utf-8"))
if err:
# Most (all?) of our files do contain errors in system-headers so lines like these
# are expected:
#
# ```
# 59 warnings generated.
# Suppressed 59 warnings (59 in non-user code).
# Use -header-filter=.* to display errors from all non-system headers.
# Use -system-headers to display errors from system headers as well.
# ```
#
# The list below ignores those expected output lines.
skip_strings = [
"warnings generated",
"in non-user code",
"Use -header-filter=.* to display errors from all non-system headers.",
"Use -system-headers to display errors from system headers as well.",
]
for line in err.decode('utf-8').split('\n'):
line = line.strip()
if any(map(lambda s: s in line, skip_strings)):
continue
if not line:
continue # no empty lines
logging.warning('TIDY %s: %s', self.file, line)
if proc.returncode != 0:
if proc.returncode < 0:
logging.error(
"Failed %s with signal %d", self.file, -proc.returncode
)
else:
logging.warning(
"Tidy %s ended with code %d", self.file, proc.returncode
)
return TidyResult(self.full_path, False)
except Exception:
traceback.print_exc()
return TidyResult(self.full_path, False)
return TidyResult(self.full_path, True)
class TidyState:
def __init__(self):
self.successes = 0
self.failures = 0
self.lock = threading.Lock()
self.failed_files = []
def Success(self):
with self.lock:
self.successes += 1
def Failure(self, path: str):
with self.lock:
self.failures += 1
self.failed_files.append(path)
logging.error("Failed to process %s", path)
def find_darwin_gcc_sysroot():
for line in subprocess.check_output('xcodebuild -sdk -version'.split()).decode('utf8').split('\n'):
if not line.startswith('Path: '):
continue
path = line[line.find(': ')+2:]
if '/MacOSX.platform/' not in path:
continue
logging.info("Found %s" % path)
return path
# A hard-coded value that works on default installations
logging.warning("Using default platform sdk path. This may be incorrect.")
return '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk'
class ClangTidyRunner:
"""Handles running clang-tidy"""
def __init__(self):
self.entries = []
self.state = TidyState()
self.fixes_file = None
self.fixes_temporary_file_dir = None
self.gcc_sysroot = None
self.file_names_to_check = set()
if sys.platform == 'darwin':
# Darwin gcc invocation will auto select a system root, however clang requires an explicit path since
# we are using the built-in pigweed clang-tidy.
logging.info(
'Searching for a MacOS system root for gcc invocations...')
self.gcc_sysroot = find_darwin_gcc_sysroot()
logging.info(' Chose: %s' % self.gcc_sysroot)
def AddDatabase(self, compile_commands_json):
database = json.load(open(compile_commands_json))
for entry in database:
item = ClangTidyEntry(entry, self.gcc_sysroot)
if not item.valid:
continue
if item.file in self.file_names_to_check:
logging.info('Ignoring additional request for checking %s', item.file)
continue
self.file_names_to_check.add(item.file)
self.entries.append(item)
def Cleanup(self):
if self.fixes_temporary_file_dir:
all_diagnostics = []
# When running over several files, fixes may be applied to the same
# file over and over again, like 'append override' can result in the
# same override being appended multiple times.
already_seen = set()
for name in glob.iglob(
os.path.join(self.fixes_temporary_file_dir.name, "*.yaml")
):
content = yaml.safe_load(open(name, "r"))
if not content:
continue
diagnostics = content.get("Diagnostics", [])
# Allow all diagnostics for distinct paths to be applied
# at once but never again for future paths
for d in diagnostics:
if d['DiagnosticMessage']['FilePath'] not in already_seen:
all_diagnostics.append(d)
# in the future assume these files were already processed
for d in diagnostics:
already_seen.add(d['DiagnosticMessage']['FilePath'])
if all_diagnostics:
with open(self.fixes_file, "w") as out:
yaml.safe_dump(
{"MainSourceFile": "", "Diagnostics": all_diagnostics}, out
)
else:
open(self.fixes_file, "w").close()
logging.info(
"Cleaning up directory: %r", self.fixes_temporary_file_dir.name
)
self.fixes_temporary_file_dir.cleanup()
self.fixes_temporary_file_dir = None
def ExportFixesTo(self, f):
# use absolute path since running things will change working directories
self.fixes_file = os.path.abspath(f)
self.fixes_temporary_file_dir = tempfile.TemporaryDirectory(
prefix="tidy-", suffix="-fixes"
)
logging.info(
"Storing temporary fix files into %s", self.fixes_temporary_file_dir.name
)
for idx, e in enumerate(self.entries):
e.ExportFixesTo(
os.path.join(
self.fixes_temporary_file_dir.name, "fixes%d.yaml" % (
idx + 1,)
)
)
def SetChecks(self, checks: str):
for e in self.entries:
e.SetChecks(checks)
def FilterEntries(self, f):
for e in self.entries:
if not f(e):
logging.info("Skipping %s in %s", e.file, e.directory)
self.entries = [e for e in self.entries if f(e)]
def CheckThread(self, task_queue):
while True:
entry = task_queue.get()
status = entry.Check()
if status.ok:
self.state.Success()
else:
self.state.Failure(status.path)
task_queue.task_done()
def Check(self):
count = multiprocessing.cpu_count()
task_queue = queue.Queue(count)
for _ in range(count):
t = threading.Thread(target=self.CheckThread, args=(task_queue,))
t.daemon = True
t.start()
for e in self.entries:
task_queue.put(e)
task_queue.join()
logging.info("Successfully processed %d path(s)", self.state.successes)
if self.state.failures:
logging.warning("Failed to process %d path(s)", self.state.failures)
logging.warning("The following paths failed clang-tidy checks:")
for name in self.state.failed_files:
logging.warning(" - %s", name)
return self.state.failures == 0
# Supported log levels, mapping string values required for argument
# parsing into logging constants
__LOG_LEVELS__ = {
"debug": logging.DEBUG,
"info": logging.INFO,
"warn": logging.WARN,
"fatal": logging.FATAL,
}
@click.group(chain=True)
@click.option(
"--compile-database",
default=[],
multiple=True,
help="Path to `compile_commands.json` to use for executing clang-tidy.",
)
@click.option(
"--file-include-regex",
default="/(src|examples)/",
help="regular expression to apply to the file paths for running.",
)
@click.option(
"--file-exclude-regex",
# NOTE: if trying '/third_party/' note that a lot of sources are routed through
# paths like `../../examples/chip-tool/third_party/connectedhomeip/src/`
default="/(repo|zzz_generated)/",
help="Regular expression to apply to the file paths for running. Skip overrides includes.",
)
@click.option(
"--log-level",
default="INFO",
type=click.Choice(__LOG_LEVELS__.keys(), case_sensitive=False),
help="Determines the verbosity of script output.",
)
@click.option(
"--no-log-timestamps",
default=False,
is_flag=True,
help="Skip timestaps in log output",
)
@click.option(
"--export-fixes",
default=None,
type=click.Path(),
help="Where to export fixes to apply.",
)
@click.option(
"--checks",
default=None,
type=str,
help="Checks to run (passed in to clang-tidy). If not set the .clang-tidy file is used.",
)
@click.pass_context
def main(
context,
compile_database,
file_include_regex,
file_exclude_regex,
log_level,
no_log_timestamps,
export_fixes,
checks,
):
log_fmt = "%(asctime)s %(levelname)-7s %(message)s"
if no_log_timestamps:
log_fmt = "%(levelname)-7s %(message)s"
coloredlogs.install(level=__LOG_LEVELS__[log_level], fmt=log_fmt)
if not compile_database:
logging.warning(
"Compilation database file not provided. Searching for first item in ./out"
)
compile_database = next(
glob.iglob("./out/**/compile_commands.json", recursive=True)
)
if not compile_database:
raise Exception("Could not find `compile_commands.json` in ./out")
logging.info("Will use %s for compile", compile_database)
compile_database = [compile_database]
context.obj = runner = ClangTidyRunner()
@context.call_on_close
def cleanup():
runner.Cleanup()
for name in compile_database:
runner.AddDatabase(name)
if file_include_regex:
r = re.compile(file_include_regex)
runner.FilterEntries(lambda e: r.search(e.file))
if file_exclude_regex:
r = re.compile(file_exclude_regex)
runner.FilterEntries(lambda e: not r.search(e.file))
if export_fixes:
runner.ExportFixesTo(export_fixes)
if checks:
runner.SetChecks(checks)
for e in context.obj.entries:
logging.info("Will tidy %s", e.full_path)
@main.command("check", help="Run clang-tidy check")
@click.pass_context
def cmd_check(context):
if not context.obj.Check():
sys.exit(1)
@main.command("fix", help="Run check followd by fix")
@click.pass_context
def cmd_fix(context):
runner = context.obj
with tempfile.TemporaryDirectory(prefix="tidy-apply-fixes") as tmpdir:
if not runner.fixes_file:
runner.ExportFixesTo(os.path.join(tmpdir, "fixes.tmp"))
runner.Check()
runner.Cleanup()
if runner.state.failures:
fixes_yaml = os.path.join(tmpdir, "fixes.yaml")
with open(fixes_yaml, "w") as out:
out.write(open(runner.fixes_file, "r").read())
logging.info("Applying fixes in %s", tmpdir)
subprocess.check_call(["clang-apply-replacements", tmpdir])
else:
logging.info("No failures detected, no fixes to apply.")
if __name__ == "__main__":
main(auto_envvar_prefix='CHIP')