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
|
#!/usr/bin/env vpython3
# Copyright 2019 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
r"""Automatically fetch, build, and run clang-tidy from source.
This script seeks to automate the steps detailed in docs/clang_tidy.md.
Example: the following command disables clang-tidy's default checks (-*) and
enables the clang static analyzer checks.
tools/clang/scripts/clang_tidy_tool.py \\
--checks='-*,clang-analyzer-*,-clang-analyzer-alpha*' \\
--header-filter='.*' \\
out/Release chrome
The same, but checks the changes only.
git diff -U5 | tools/clang/scripts/clang_tidy_tool.py \\
--diff \\
--checks='-*,clang-analyzer-*,-clang-analyzer-alpha*' \\
--header-filter='.*' \\
out/Release chrome
"""
from __future__ import print_function
import argparse
import os
import subprocess
import sys
import update
import build_clang_tools_extra
def GetBinaryPath(build_dir, binary):
if sys.platform == 'win32':
binary += '.exe'
return os.path.join(build_dir, 'bin', binary)
def BuildNinjaTarget(out_dir, ninja_target):
args = ['autoninja', '-C', out_dir, ninja_target]
subprocess.check_call(args, shell=sys.platform == 'win32')
def GenerateCompDb(out_dir):
gen_compdb_script = os.path.join(
os.path.dirname(__file__), 'generate_compdb.py')
comp_db_file_path = os.path.join(out_dir, 'compile_commands.json')
args = [
sys.executable,
gen_compdb_script,
'-p',
out_dir,
'-o',
comp_db_file_path,
]
subprocess.check_call(args)
# The resulting CompDb file includes /showIncludes which causes clang-tidy to
# output a lot of unnecessary text to the console.
with open(comp_db_file_path, 'r') as comp_db_file:
comp_db_data = comp_db_file.read();
# The trailing space on /showIncludes helps keep single-spaced flags.
comp_db_data = comp_db_data.replace('/showIncludes ', '')
with open(comp_db_file_path, 'w') as comp_db_file:
comp_db_file.write(comp_db_data)
def RunClangTidy(checks, header_filter, auto_fix, clang_src_dir,
clang_build_dir, out_dir, ninja_target):
"""Invoke the |run-clang-tidy.py| script."""
run_clang_tidy_script = os.path.join(
clang_src_dir, 'clang-tools-extra', 'clang-tidy', 'tool',
'run-clang-tidy.py')
clang_tidy_binary = GetBinaryPath(clang_build_dir, 'clang-tidy')
clang_apply_rep_binary = GetBinaryPath(clang_build_dir,
'clang-apply-replacements')
args = [
sys.executable,
run_clang_tidy_script,
'-quiet',
'-p',
out_dir,
'-clang-tidy-binary',
clang_tidy_binary,
'-clang-apply-replacements-binary',
clang_apply_rep_binary,
]
if checks:
args.append('-checks={}'.format(checks))
if header_filter:
args.append('-header-filter={}'.format(header_filter))
if auto_fix:
args.append('-fix')
args.append(ninja_target)
subprocess.check_call(args)
def RunClangTidyDiff(checks, auto_fix, clang_src_dir, clang_build_dir, out_dir):
"""Invoke the |clang-tidy-diff.py| script over the diff from stdin."""
clang_tidy_diff_script = os.path.join(
clang_src_dir, 'clang-tools-extra', 'clang-tidy', 'tool',
'clang-tidy-diff.py')
clang_tidy_binary = GetBinaryPath(clang_build_dir, 'clang-tidy')
args = [
clang_tidy_diff_script,
'-quiet',
'-p1',
'-path',
out_dir,
'-clang-tidy-binary',
clang_tidy_binary,
]
if checks:
args.append('-checks={}'.format(checks))
if auto_fix:
args.append('-fix')
subprocess.check_call(args)
def main():
script_name = sys.argv[0]
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter, epilog=__doc__)
parser.add_argument(
'--fetch',
nargs='?',
const=update.CLANG_REVISION,
help='Fetch and build clang sources')
parser.add_argument(
'--build',
action='store_true',
help='build clang sources to get clang-tidy')
parser.add_argument(
'--diff',
action='store_true',
default=False,
help ='read diff from the stdin and check it')
parser.add_argument('--clang-src-dir', type=str,
help='override llvm and clang checkout location')
parser.add_argument('--clang-build-dir', type=str,
help='override clang build dir location')
parser.add_argument('--checks', help='passed to clang-tidy')
parser.add_argument('--header-filter', help='passed to clang-tidy')
parser.add_argument(
'--auto-fix',
action='store_true',
help='tell clang-tidy to auto-fix errors')
parser.add_argument('OUT_DIR', help='where we are building Chrome')
parser.add_argument('NINJA_TARGET', help='ninja target')
args = parser.parse_args()
steps = []
# If the user hasn't provided a clang checkout and build dir, checkout and
# build clang-tidy where update.py would.
if not args.clang_src_dir:
args.clang_src_dir = build_clang_tools_extra.GetCheckoutDir(args.OUT_DIR)
if not args.clang_build_dir:
args.clang_build_dir = build_clang_tools_extra.GetBuildDir(args.OUT_DIR)
elif (args.clang_build_dir and not
os.path.isfile(GetBinaryPath(args.clang_build_dir, 'clang-tidy'))):
sys.exit('clang-tidy binary doesn\'t exist at ' +
GetBinaryPath(args.clang_build_dir, 'clang-tidy'))
if args.fetch:
steps.append(('Fetching LLVM sources', lambda:
build_clang_tools_extra.FetchLLVM(args.clang_src_dir,
args.fetch)))
if args.build:
steps.append(('Building clang-tidy',
lambda: build_clang_tools_extra.BuildTargets(
args.clang_build_dir,
['clang-tidy', 'clang-apply-replacements'])))
steps += [
('Building ninja target: %s' % args.NINJA_TARGET,
lambda: BuildNinjaTarget(args.OUT_DIR, args.NINJA_TARGET)),
('Generating compilation DB', lambda: GenerateCompDb(args.OUT_DIR))
]
if args.diff:
steps += [
('Running clang-tidy on diff', lambda: RunClangTidyDiff(
args.checks, args.auto_fix, args.clang_src_dir, args.
clang_build_dir, args.OUT_DIR)),
]
else:
steps += [
('Running clang-tidy',
lambda: RunClangTidy(args.checks, args.header_filter,
args.auto_fix, args.clang_src_dir,
args.clang_build_dir, args.OUT_DIR,
args.NINJA_TARGET)),
]
# Run the steps in sequence.
for i, (msg, step_func) in enumerate(steps):
# Print progress message
print('-- %s %s' % (script_name, '-' * (80 - len(script_name) - 4)))
print('-- [%d/%d] %s' % (i + 1, len(steps), msg))
print(80 * '-')
step_func()
return 0
if __name__ == '__main__':
sys.exit(main())
|