blob: d9d827b01290777d2d21b95a497d3c1e9d26027b [file] [log] [blame]
Benjamin Kramera9d9a4d2014-09-08 14:01:31 +00001#!/usr/bin/env python
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
Jakub Kuderskia7410fd2017-04-25 22:38:39 +000037from __future__ import print_function
Benjamin Kramera9d9a4d2014-09-08 14:01:31 +000038import argparse
39import json
40import multiprocessing
41import os
42import Queue
43import re
44import shutil
45import subprocess
46import sys
47import tempfile
48import threading
Jakub Kuderskia7410fd2017-04-25 22:38:39 +000049import traceback
Benjamin Kramera9d9a4d2014-09-08 14:01:31 +000050
51
52def find_compilation_database(path):
53 """Adjusts the directory until a compilation database is found."""
54 result = './'
55 while not os.path.isfile(os.path.join(result, path)):
56 if os.path.realpath(result) == '/':
Jakub Kuderskia7410fd2017-04-25 22:38:39 +000057 print('Error: could not find compilation database.')
Benjamin Kramera9d9a4d2014-09-08 14:01:31 +000058 sys.exit(1)
59 result += '../'
60 return os.path.realpath(result)
61
62
Benjamin Kramer815dbad2014-09-08 14:56:40 +000063def get_tidy_invocation(f, clang_tidy_binary, checks, tmpdir, build_path,
Ehsan Akhgarib7418d32017-02-09 18:32:02 +000064 header_filter, extra_arg, extra_arg_before, quiet):
Benjamin Kramera9d9a4d2014-09-08 14:01:31 +000065 """Gets a command line for clang-tidy."""
Benjamin Kramer815dbad2014-09-08 14:56:40 +000066 start = [clang_tidy_binary]
Benjamin Kramera9d9a4d2014-09-08 14:01:31 +000067 if header_filter is not None:
68 start.append('-header-filter=' + header_filter)
69 else:
70 # Show warnings in all in-project headers by default.
71 start.append('-header-filter=^' + build_path + '/.*')
72 if checks:
Alexander Kornienko3f115382015-09-08 10:31:36 +000073 start.append('-checks=' + checks)
Benjamin Kramera9d9a4d2014-09-08 14:01:31 +000074 if tmpdir is not None:
75 start.append('-export-fixes')
76 # Get a temporary file. We immediately close the handle so clang-tidy can
77 # overwrite it.
78 (handle, name) = tempfile.mkstemp(suffix='.yaml', dir=tmpdir)
Benjamin Kramer815dbad2014-09-08 14:56:40 +000079 os.close(handle)
Benjamin Kramera9d9a4d2014-09-08 14:01:31 +000080 start.append(name)
Ehsan Akhgari221ab772017-01-18 17:49:35 +000081 for arg in extra_arg:
82 start.append('-extra-arg=%s' % arg)
83 for arg in extra_arg_before:
84 start.append('-extra-arg-before=%s' % arg)
Benjamin Kramera9d9a4d2014-09-08 14:01:31 +000085 start.append('-p=' + build_path)
Ehsan Akhgarib7418d32017-02-09 18:32:02 +000086 if quiet:
87 start.append('-quiet')
Benjamin Kramera9d9a4d2014-09-08 14:01:31 +000088 start.append(f)
89 return start
90
91
Jakub Kuderskia7410fd2017-04-25 22:38:39 +000092def check_clang_apply_replacements_binary(args):
93 """Checks if invoking supplied clang-apply-replacements binary works."""
94 try:
95 subprocess.check_call([args.clang_apply_replacements_binary, '--version'])
96 except:
97 print('Unable to run clang-apply-replacements. Is clang-apply-replacements '
98 'binary correctly specified?', file=sys.stderr)
99 traceback.print_exc()
100 sys.exit(1)
101
102
Benjamin Kramer815dbad2014-09-08 14:56:40 +0000103def apply_fixes(args, tmpdir):
Benjamin Kramera9d9a4d2014-09-08 14:01:31 +0000104 """Calls clang-apply-fixes on a given directory. Deletes the dir when done."""
Benjamin Kramer815dbad2014-09-08 14:56:40 +0000105 invocation = [args.clang_apply_replacements_binary]
106 if args.format:
Benjamin Kramera9d9a4d2014-09-08 14:01:31 +0000107 invocation.append('-format')
Vassil Vassilevc4c33ce2017-06-09 22:23:03 +0000108 if args.style:
109 invocation.append('-style=' + args.style)
Benjamin Kramera9d9a4d2014-09-08 14:01:31 +0000110 invocation.append(tmpdir)
111 subprocess.call(invocation)
Benjamin Kramera9d9a4d2014-09-08 14:01:31 +0000112
113
114def run_tidy(args, tmpdir, build_path, queue):
115 """Takes filenames out of queue and runs clang-tidy on them."""
116 while True:
117 name = queue.get()
Benjamin Kramer815dbad2014-09-08 14:56:40 +0000118 invocation = get_tidy_invocation(name, args.clang_tidy_binary, args.checks,
Ehsan Akhgari221ab772017-01-18 17:49:35 +0000119 tmpdir, build_path, args.header_filter,
Ehsan Akhgarib7418d32017-02-09 18:32:02 +0000120 args.extra_arg, args.extra_arg_before,
121 args.quiet)
Benjamin Kramera9d9a4d2014-09-08 14:01:31 +0000122 sys.stdout.write(' '.join(invocation) + '\n')
123 subprocess.call(invocation)
124 queue.task_done()
125
126
127def main():
128 parser = argparse.ArgumentParser(description='Runs clang-tidy over all files '
129 'in a compilation database. Requires '
130 'clang-tidy and clang-apply-replacements in '
131 '$PATH.')
Benjamin Kramer815dbad2014-09-08 14:56:40 +0000132 parser.add_argument('-clang-tidy-binary', metavar='PATH',
133 default='clang-tidy',
134 help='path to clang-tidy binary')
135 parser.add_argument('-clang-apply-replacements-binary', metavar='PATH',
136 default='clang-apply-replacements',
137 help='path to clang-apply-replacements binary')
Benjamin Kramera9d9a4d2014-09-08 14:01:31 +0000138 parser.add_argument('-checks', default=None,
139 help='checks filter, when not specified, use clang-tidy '
140 'default')
141 parser.add_argument('-header-filter', default=None,
142 help='regular expression matching the names of the '
143 'headers to output diagnostics from. Diagnostics from '
144 'the main file of each translation unit are always '
145 'displayed.')
146 parser.add_argument('-j', type=int, default=0,
147 help='number of tidy instances to be run in parallel.')
148 parser.add_argument('files', nargs='*', default=['.*'],
149 help='files to be processed (regex on path)')
150 parser.add_argument('-fix', action='store_true', help='apply fix-its')
151 parser.add_argument('-format', action='store_true', help='Reformat code '
152 'after applying fixes')
Vassil Vassilevc4c33ce2017-06-09 22:23:03 +0000153 parser.add_argument('-style', default='file', help='The style of reformat '
154 'code after applying fixes')
Guillaume Papin68b59102015-09-28 17:53:04 +0000155 parser.add_argument('-p', dest='build_path',
156 help='Path used to read a compile command database.')
Ehsan Akhgari221ab772017-01-18 17:49:35 +0000157 parser.add_argument('-extra-arg', dest='extra_arg',
158 action='append', default=[],
159 help='Additional argument to append to the compiler '
160 'command line.')
161 parser.add_argument('-extra-arg-before', dest='extra_arg_before',
162 action='append', default=[],
163 help='Additional argument to prepend to the compiler '
164 'command line.')
Ehsan Akhgarib7418d32017-02-09 18:32:02 +0000165 parser.add_argument('-quiet', action='store_true',
166 help='Run clang-tidy in quiet mode')
Benjamin Kramera9d9a4d2014-09-08 14:01:31 +0000167 args = parser.parse_args()
168
Guillaume Papin68b59102015-09-28 17:53:04 +0000169 db_path = 'compile_commands.json'
170
171 if args.build_path is not None:
172 build_path = args.build_path
173 else:
174 # Find our database
175 build_path = find_compilation_database(db_path)
176
Benjamin Kramer815dbad2014-09-08 14:56:40 +0000177 try:
Alexander Kornienkof73fe3a2014-09-22 00:07:07 +0000178 invocation = [args.clang_tidy_binary, '-list-checks']
Guillaume Papin68b59102015-09-28 17:53:04 +0000179 invocation.append('-p=' + build_path)
Alexander Kornienkof73fe3a2014-09-22 00:07:07 +0000180 if args.checks:
Alexander Kornienko3f115382015-09-08 10:31:36 +0000181 invocation.append('-checks=' + args.checks)
Alexander Kornienkof73fe3a2014-09-22 00:07:07 +0000182 invocation.append('-')
Jakub Kuderskia7410fd2017-04-25 22:38:39 +0000183 print(subprocess.check_output(invocation))
Benjamin Kramer815dbad2014-09-08 14:56:40 +0000184 except:
Jakub Kuderskia7410fd2017-04-25 22:38:39 +0000185 print("Unable to run clang-tidy.", file=sys.stderr)
Benjamin Kramer815dbad2014-09-08 14:56:40 +0000186 sys.exit(1)
187
Benjamin Kramera9d9a4d2014-09-08 14:01:31 +0000188 # Load the database and extract all files.
189 database = json.load(open(os.path.join(build_path, db_path)))
190 files = [entry['file'] for entry in database]
191
192 max_task = args.j
193 if max_task == 0:
194 max_task = multiprocessing.cpu_count()
195
196 tmpdir = None
197 if args.fix:
Jakub Kuderskia7410fd2017-04-25 22:38:39 +0000198 check_clang_apply_replacements_binary(args)
Benjamin Kramera9d9a4d2014-09-08 14:01:31 +0000199 tmpdir = tempfile.mkdtemp()
200
201 # Build up a big regexy filter from all command line arguments.
Alexander Kornienkoeaada5c2017-03-23 16:29:39 +0000202 file_name_re = re.compile('|'.join(args.files))
Benjamin Kramera9d9a4d2014-09-08 14:01:31 +0000203
204 try:
205 # Spin up a bunch of tidy-launching threads.
206 queue = Queue.Queue(max_task)
207 for _ in range(max_task):
208 t = threading.Thread(target=run_tidy,
209 args=(args, tmpdir, build_path, queue))
210 t.daemon = True
211 t.start()
212
213 # Fill the queue with files.
214 for name in files:
215 if file_name_re.search(name):
216 queue.put(name)
217
218 # Wait for all threads to be done.
219 queue.join()
220
221 except KeyboardInterrupt:
222 # This is a sad hack. Unfortunately subprocess goes
223 # bonkers with ctrl-c and we start forking merrily.
Jakub Kuderskia7410fd2017-04-25 22:38:39 +0000224 print('\nCtrl-C detected, goodbye.')
Benjamin Kramera9d9a4d2014-09-08 14:01:31 +0000225 if args.fix:
226 shutil.rmtree(tmpdir)
227 os.kill(0, 9)
228
229 if args.fix:
Jakub Kuderskia7410fd2017-04-25 22:38:39 +0000230 print('Applying fixes ...')
231 successfully_applied = False
232
233 try:
234 apply_fixes(args, tmpdir)
235 successfully_applied = True
236 except:
237 print('Error applying fixes.\n', file=sys.stderr)
238 traceback.print_exc()
239
240 shutil.rmtree(tmpdir)
241 if not successfully_applied:
242 sys.exit(1)
Benjamin Kramera9d9a4d2014-09-08 14:01:31 +0000243
244if __name__ == '__main__':
245 main()