blob: 0f2f55be23664898f95445b62efc0db120eda2e9 [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')
108 invocation.append(tmpdir)
109 subprocess.call(invocation)
Benjamin Kramera9d9a4d2014-09-08 14:01:31 +0000110
111
112def run_tidy(args, tmpdir, build_path, queue):
113 """Takes filenames out of queue and runs clang-tidy on them."""
114 while True:
115 name = queue.get()
Benjamin Kramer815dbad2014-09-08 14:56:40 +0000116 invocation = get_tidy_invocation(name, args.clang_tidy_binary, args.checks,
Ehsan Akhgari221ab772017-01-18 17:49:35 +0000117 tmpdir, build_path, args.header_filter,
Ehsan Akhgarib7418d32017-02-09 18:32:02 +0000118 args.extra_arg, args.extra_arg_before,
119 args.quiet)
Benjamin Kramera9d9a4d2014-09-08 14:01:31 +0000120 sys.stdout.write(' '.join(invocation) + '\n')
121 subprocess.call(invocation)
122 queue.task_done()
123
124
125def main():
126 parser = argparse.ArgumentParser(description='Runs clang-tidy over all files '
127 'in a compilation database. Requires '
128 'clang-tidy and clang-apply-replacements in '
129 '$PATH.')
Benjamin Kramer815dbad2014-09-08 14:56:40 +0000130 parser.add_argument('-clang-tidy-binary', metavar='PATH',
131 default='clang-tidy',
132 help='path to clang-tidy binary')
133 parser.add_argument('-clang-apply-replacements-binary', metavar='PATH',
134 default='clang-apply-replacements',
135 help='path to clang-apply-replacements binary')
Benjamin Kramera9d9a4d2014-09-08 14:01:31 +0000136 parser.add_argument('-checks', default=None,
137 help='checks filter, when not specified, use clang-tidy '
138 'default')
139 parser.add_argument('-header-filter', default=None,
140 help='regular expression matching the names of the '
141 'headers to output diagnostics from. Diagnostics from '
142 'the main file of each translation unit are always '
143 'displayed.')
144 parser.add_argument('-j', type=int, default=0,
145 help='number of tidy instances to be run in parallel.')
146 parser.add_argument('files', nargs='*', default=['.*'],
147 help='files to be processed (regex on path)')
148 parser.add_argument('-fix', action='store_true', help='apply fix-its')
149 parser.add_argument('-format', action='store_true', help='Reformat code '
150 'after applying fixes')
Guillaume Papin68b59102015-09-28 17:53:04 +0000151 parser.add_argument('-p', dest='build_path',
152 help='Path used to read a compile command database.')
Ehsan Akhgari221ab772017-01-18 17:49:35 +0000153 parser.add_argument('-extra-arg', dest='extra_arg',
154 action='append', default=[],
155 help='Additional argument to append to the compiler '
156 'command line.')
157 parser.add_argument('-extra-arg-before', dest='extra_arg_before',
158 action='append', default=[],
159 help='Additional argument to prepend to the compiler '
160 'command line.')
Ehsan Akhgarib7418d32017-02-09 18:32:02 +0000161 parser.add_argument('-quiet', action='store_true',
162 help='Run clang-tidy in quiet mode')
Benjamin Kramera9d9a4d2014-09-08 14:01:31 +0000163 args = parser.parse_args()
164
Guillaume Papin68b59102015-09-28 17:53:04 +0000165 db_path = 'compile_commands.json'
166
167 if args.build_path is not None:
168 build_path = args.build_path
169 else:
170 # Find our database
171 build_path = find_compilation_database(db_path)
172
Benjamin Kramer815dbad2014-09-08 14:56:40 +0000173 try:
Alexander Kornienkof73fe3a2014-09-22 00:07:07 +0000174 invocation = [args.clang_tidy_binary, '-list-checks']
Guillaume Papin68b59102015-09-28 17:53:04 +0000175 invocation.append('-p=' + build_path)
Alexander Kornienkof73fe3a2014-09-22 00:07:07 +0000176 if args.checks:
Alexander Kornienko3f115382015-09-08 10:31:36 +0000177 invocation.append('-checks=' + args.checks)
Alexander Kornienkof73fe3a2014-09-22 00:07:07 +0000178 invocation.append('-')
Jakub Kuderskia7410fd2017-04-25 22:38:39 +0000179 print(subprocess.check_output(invocation))
Benjamin Kramer815dbad2014-09-08 14:56:40 +0000180 except:
Jakub Kuderskia7410fd2017-04-25 22:38:39 +0000181 print("Unable to run clang-tidy.", file=sys.stderr)
Benjamin Kramer815dbad2014-09-08 14:56:40 +0000182 sys.exit(1)
183
Benjamin Kramera9d9a4d2014-09-08 14:01:31 +0000184 # Load the database and extract all files.
185 database = json.load(open(os.path.join(build_path, db_path)))
186 files = [entry['file'] for entry in database]
187
188 max_task = args.j
189 if max_task == 0:
190 max_task = multiprocessing.cpu_count()
191
192 tmpdir = None
193 if args.fix:
Jakub Kuderskia7410fd2017-04-25 22:38:39 +0000194 check_clang_apply_replacements_binary(args)
Benjamin Kramera9d9a4d2014-09-08 14:01:31 +0000195 tmpdir = tempfile.mkdtemp()
196
197 # Build up a big regexy filter from all command line arguments.
Alexander Kornienkoeaada5c2017-03-23 16:29:39 +0000198 file_name_re = re.compile('|'.join(args.files))
Benjamin Kramera9d9a4d2014-09-08 14:01:31 +0000199
200 try:
201 # Spin up a bunch of tidy-launching threads.
202 queue = Queue.Queue(max_task)
203 for _ in range(max_task):
204 t = threading.Thread(target=run_tidy,
205 args=(args, tmpdir, build_path, queue))
206 t.daemon = True
207 t.start()
208
209 # Fill the queue with files.
210 for name in files:
211 if file_name_re.search(name):
212 queue.put(name)
213
214 # Wait for all threads to be done.
215 queue.join()
216
217 except KeyboardInterrupt:
218 # This is a sad hack. Unfortunately subprocess goes
219 # bonkers with ctrl-c and we start forking merrily.
Jakub Kuderskia7410fd2017-04-25 22:38:39 +0000220 print('\nCtrl-C detected, goodbye.')
Benjamin Kramera9d9a4d2014-09-08 14:01:31 +0000221 if args.fix:
222 shutil.rmtree(tmpdir)
223 os.kill(0, 9)
224
225 if args.fix:
Jakub Kuderskia7410fd2017-04-25 22:38:39 +0000226 print('Applying fixes ...')
227 successfully_applied = False
228
229 try:
230 apply_fixes(args, tmpdir)
231 successfully_applied = True
232 except:
233 print('Error applying fixes.\n', file=sys.stderr)
234 traceback.print_exc()
235
236 shutil.rmtree(tmpdir)
237 if not successfully_applied:
238 sys.exit(1)
Benjamin Kramera9d9a4d2014-09-08 14:01:31 +0000239
240if __name__ == '__main__':
241 main()