blob: c42463b7f4121e76aacf3d6e909996c75ce7fc28 [file] [log] [blame]
Ed Tanous7d3dba42017-04-05 13:04:39 -07001#!/usr/bin/env python3
2#
3# ===- run-clang-tidy.py - Parallel clang-tidy runner ---------*- python -*--===#
4#
5# The LLVM Compiler Infrastructure
6#
7# This file is distributed under the University of Illinois Open Source
8# License. See LICENSE.TXT for details.
9#
10# ===------------------------------------------------------------------------===#
11# FIXME: Integrate with clang-tidy-diff.py
12
13"""
14Parallel clang-tidy runner
15==========================
16
17Runs clang-tidy over all files in a compilation database. Requires clang-tidy
18and clang-apply-replacements in $PATH.
19
20Example invocations.
21- Run clang-tidy on all files in the current working directory with a default
22 set of checks and show warnings in the cpp files and all project headers.
23 run-clang-tidy.py $PWD
24
25- Fix all header guards.
26 run-clang-tidy.py -fix -checks=-*,llvm-header-guard
27
28- Fix all header guards included from clang-tidy and header guards
29 for clang-tidy headers.
30 run-clang-tidy.py -fix -checks=-*,llvm-header-guard extra/clang-tidy \
31 -header-filter=extra/clang-tidy
32
33Compilation database setup:
34http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html
35"""
36
37import argparse
38import json
39import multiprocessing
40import os
41import queue
42import re
43import shutil
44import subprocess
45import sys
46import tempfile
47import threading
48
49
50def find_compilation_database(path):
51 """Adjusts the directory until a compilation database is found."""
52 result = './'
53 while not os.path.isfile(os.path.join(result, path)):
54 if os.path.realpath(result) == '/':
55 print('Error: could not find compilation database.')
56 sys.exit(1)
57 result += '../'
58 return os.path.realpath(result)
59
60
61def get_tidy_invocation(f, clang_tidy_binary, checks, tmpdir, build_path,
62 header_filter, extra_arg, extra_arg_before, quiet):
63 """Gets a command line for clang-tidy."""
64 start = [clang_tidy_binary]
65 if header_filter is not None:
66 start.append('-header-filter=' + header_filter)
67 else:
68 # Show warnings in all in-project headers by default.
69 start.append('-header-filter=^' + build_path + '/.*')
70 if checks:
71 start.append('-checks=' + checks)
72 if tmpdir is not None:
73 start.append('-export-fixes')
74 # Get a temporary file. We immediately close the handle so clang-tidy can
75 # overwrite it.
76 (handle, name) = tempfile.mkstemp(suffix='.yaml', dir=tmpdir)
77 os.close(handle)
78 start.append(name)
79 for arg in extra_arg:
80 start.append('-extra-arg=%s' % arg)
81 for arg in extra_arg_before:
82 start.append('-extra-arg-before=%s' % arg)
83 start.append('-p=' + build_path)
84 if quiet:
85 start.append('-quiet')
86 start.append(f)
87 return start
88
89
90def apply_fixes(args, tmpdir):
91 """Calls clang-apply-fixes on a given directory. Deletes the dir when done."""
92 invocation = [args.clang_apply_replacements_binary]
93 if args.format:
94 invocation.append('-format')
95 invocation.append(tmpdir)
96 subprocess.call(invocation)
97 shutil.rmtree(tmpdir)
98
99
100def run_tidy(args, tmpdir, build_path, this_queue):
101 """Takes filenames out of queue and runs clang-tidy on them."""
102 while True:
103 name = this_queue.get()
104 invocation = get_tidy_invocation(
105 name, args.clang_tidy_binary, args.checks,
106 tmpdir, build_path, args.header_filter,
107 args.extra_arg, args.extra_arg_before,
108 args.quiet)
109 sys.stdout.write(' '.join(invocation) + '\n')
110 subprocess.call(invocation)
111 this_queue.task_done()
112
113
114def main():
115 parser = argparse.ArgumentParser(description='Runs clang-tidy over all files '
116 'in a compilation database. Requires '
117 'clang-tidy and clang-apply-replacements in '
118 '$PATH.')
119 parser.add_argument('-clang-tidy-binary', metavar='PATH',
120 default='clang-tidy',
121 help='path to clang-tidy binary')
122 parser.add_argument('-clang-apply-replacements-binary', metavar='PATH',
123 default='clang-apply-replacements',
124 help='path to clang-apply-replacements binary')
125 parser.add_argument('-checks', default=None,
126 help='checks filter, when not specified, use clang-tidy '
127 'default')
128 parser.add_argument('-header-filter', default=None,
129 help='regular expression matching the names of the '
130 'headers to output diagnostics from. Diagnostics from '
131 'the main file of each translation unit are always '
132 'displayed.')
133 parser.add_argument('-j', type=int, default=0,
134 help='number of tidy instances to be run in parallel.')
135 parser.add_argument('files', nargs='*', default=['.*'],
136 help='files to be processed (regex on path)')
137 parser.add_argument('-fix', action='store_true', help='apply fix-its')
138 parser.add_argument('-format', action='store_true', help='Reformat code '
139 'after applying fixes')
140 parser.add_argument('-p', dest='build_path',
141 help='Path used to read a compile command database.')
142 parser.add_argument('-extra-arg', dest='extra_arg',
143 action='append', default=[],
144 help='Additional argument to append to the compiler '
145 'command line.')
146 parser.add_argument('-extra-arg-before', dest='extra_arg_before',
147 action='append', default=[],
148 help='Additional argument to prepend to the compiler '
149 'command line.')
150 parser.add_argument('-quiet', action='store_true',
151 help='Run clang-tidy in quiet mode')
152 args = parser.parse_args()
153
154 db_path = 'compile_commands.json'
155
156 if args.build_path is not None:
157 build_path = args.build_path
158 else:
159 # Find our database
160 build_path = find_compilation_database(db_path)
161
162 try:
163 invocation = [args.clang_tidy_binary, '-list-checks']
164 invocation.append('-p=' + build_path)
165 if args.checks:
166 invocation.append('-checks=' + args.checks)
167 invocation.append('-')
168 print(subprocess.check_output(invocation))
169 except:
170 print("Unable to run clang-tidy.", file=sys.stderr)
171 sys.exit(1)
172
173 # Load the database and extract all files.
174 database = json.load(open(os.path.join(build_path, db_path)))
175 files = [entry['file'] for entry in database]
176
177 max_task = args.j
178 if max_task == 0:
179 max_task = multiprocessing.cpu_count()
180
181 tmpdir = None
182 if args.fix:
183 tmpdir = tempfile.mkdtemp()
184
185 # Build up a big regexy filter from all command line arguments.
186 file_name_re = re.compile('|'.join(args.files))
187
188 try:
189 # Spin up a bunch of tidy-launching threads.
190 this_queue = queue.Queue(max_task)
191 for _ in range(max_task):
192 t = threading.Thread(target=run_tidy,
193 args=(args, tmpdir, build_path, this_queue))
194 t.daemon = True
195 t.start()
196
197 # Fill the queue with files.
198 for name in files:
199 if file_name_re.search(name):
200 this_queue.put(name)
201
202 # Wait for all threads to be done.
203 this_queue.join()
204
205 except KeyboardInterrupt:
206 # This is a sad hack. Unfortunately subprocess goes
207 # bonkers with ctrl-c and we start forking merrily.
208 print('\nCtrl-C detected, goodbye.')
209 if args.fix:
210 shutil.rmtree(tmpdir)
211 os.kill(0, 9)
212
213 if args.fix:
214 print('Applying fixes ...')
215 apply_fixes(args, tmpdir)
216
217if __name__ == '__main__':
218 main()