| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# Determines which clang-tidy checks are "fast enough" to run in clangd. |
| 4 |
# This runs clangd --check --check-tidy-time and parses the output. |
| 5 |
# This program outputs a header fragment specifying which checks are fast: |
| 6 |
# FAST(bugprone-argument-comment, 5) |
| 7 |
# SLOW(misc-const-correctness, 200) |
| 8 |
# If given the old header fragment as input, we lean to preserve its choices. |
| 9 |
# |
| 10 |
# This is not deterministic or hermetic, but should be run occasionally to |
| 11 |
# update the list of allowed checks. From llvm-project: |
| 12 |
# clang-tools-extra/clangd/TidyFastChecks.py --clangd=build-opt/bin/clangd |
| 13 |
# Be sure to use an optimized, no-asserts, tidy-enabled build of clangd! |
| 14 |
|
| 15 |
import argparse |
| 16 |
import os |
| 17 |
import re |
| 18 |
import subprocess |
| 19 |
import sys |
| 20 |
|
| 21 |
# Checks faster than FAST_THRESHOLD are fast, slower than SLOW_THRESHOLD slow. |
| 22 |
# If a check is in between, we stick with our previous decision. This avoids |
| 23 |
# enabling/disabling checks between releases due to random measurement jitter. |
| 24 |
FAST_THRESHOLD = 8 # percent |
| 25 |
SLOW_THRESHOLD = 15 |
| 26 |
|
| 27 |
parser = argparse.ArgumentParser() |
| 28 |
parser.add_argument('--target', help='X-macro output file. ' |
| 29 |
'If it exists, existing contents will be used for hysteresis', |
| 30 |
default='clang-tools-extra/clangd/TidyFastChecks.inc') |
| 31 |
parser.add_argument('--source', help='Source file to benchmark tidy checks', |
| 32 |
default='clang/lib/Sema/Sema.cpp') |
| 33 |
parser.add_argument('--clangd', help='clangd binary to invoke', |
| 34 |
default='build/bin/clangd') |
| 35 |
parser.add_argument('--checks', help='check glob to run', default='*') |
| 36 |
parser.add_argument('--verbose', help='log clangd output', action='store_true') |
| 37 |
args = parser.parse_args() |
| 38 |
|
| 39 |
# Use the preprocessor to extract the list of previously-fast checks. |
| 40 |
def read_old_fast(path): |
| 41 |
text = subprocess.check_output(["cpp", |
| 42 |
"-P", # Omit GNU line markers |
| 43 |
"-nostdinc", # Don't include stdc-predef.h |
| 44 |
"-DFAST(C,T)=C", # Print fast checks only |
| 45 |
path]) |
| 46 |
for line in text.splitlines(): |
| 47 |
if line.strip(): |
| 48 |
yield line.strip().decode('utf-8') |
| 49 |
old_fast = list(read_old_fast(args.target)) if os.path.exists(args.target) else [] |
| 50 |
print(f"Old fast checks: {old_fast}", file=sys.stderr) |
| 51 |
|
| 52 |
# Runs clangd --check --check-tidy-time. |
| 53 |
# Yields (check, percent-overhead) pairs. |
| 54 |
def measure(): |
| 55 |
process = subprocess.Popen([args.clangd, |
| 56 |
"--check=" + args.source, |
| 57 |
"--check-locations=0", # Skip useless slow steps. |
| 58 |
"--check-tidy-time=" + args.checks], |
| 59 |
stderr=subprocess.PIPE) |
| 60 |
recording = False |
| 61 |
for line in iter(process.stderr.readline, b""): |
| 62 |
if args.verbose: |
| 63 |
print("clangd> ", line, file=sys.stderr) |
| 64 |
if not recording: |
| 65 |
if b'Timing AST build with individual clang-tidy checks' in line: |
| 66 |
recording = True |
| 67 |
continue |
| 68 |
if b'Finished individual clang-tidy checks' in line: |
| 69 |
return |
| 70 |
match = re.search(rb'(\S+) = (\S+)%', line) |
| 71 |
if match: |
| 72 |
yield (match.group(1).decode('utf-8'), float(match.group(2))) |
| 73 |
|
| 74 |
with open(args.target, 'w', buffering=1) as target: |
| 75 |
# Produce an includable X-macros fragment with our decisions. |
| 76 |
print(f"""// This file is generated, do not edit it directly! |
| 77 |
// Deltas are percentage regression in parsing {args.source} |
| 78 |
#ifndef FAST |
| 79 |
#define FAST(CHECK, DELTA) |
| 80 |
#endif |
| 81 |
#ifndef SLOW |
| 82 |
#define SLOW(CHECK, DELTA) |
| 83 |
#endif |
| 84 |
""", file=target) |
| 85 |
|
| 86 |
for check, time in measure(): |
| 87 |
threshold = SLOW_THRESHOLD if check in old_fast else FAST_THRESHOLD |
| 88 |
decision = "FAST" if time <= threshold else "SLOW" |
| 89 |
print(f"{decision} {check} {time}% <= {threshold}%", file=sys.stderr) |
| 90 |
print(f"{decision}({check}, {time})", file=target) |
| 91 |
|
| 92 |
print(""" |
| 93 |
#undef FAST |
| 94 |
#undef SLOW |
| 95 |
""", file=target) |