blob: a7f4bfcef50af8e80c33e03258dace8e30fb592a [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
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, checks, tmpdir, build_path, header_filter):
62 """Gets a command line for clang-tidy."""
63 start = ['clang-tidy']
64 if header_filter is not None:
65 start.append('-header-filter=' + header_filter)
66 else:
67 # Show warnings in all in-project headers by default.
68 start.append('-header-filter=^' + build_path + '/.*')
69 if checks:
70 start.append('-checks=-*,' + checks)
71 if tmpdir is not None:
72 start.append('-export-fixes')
73 # Get a temporary file. We immediately close the handle so clang-tidy can
74 # overwrite it.
75 (handle, name) = tempfile.mkstemp(suffix='.yaml', dir=tmpdir)
76 handle.close()
77 start.append(name)
78 start.append('-p=' + build_path)
79 start.append(f)
80 return start
81
82
83def apply_fixes(tmpdir, format_enabled):
84 """Calls clang-apply-fixes on a given directory. Deletes the dir when done."""
85 invocation = ['clang-apply-replacements']
86 if format_enabled:
87 invocation.append('-format')
88 invocation.append(tmpdir)
89 subprocess.call(invocation)
90 shutil.rmtree(tmpdir)
91
92
93def run_tidy(args, tmpdir, build_path, queue):
94 """Takes filenames out of queue and runs clang-tidy on them."""
95 while True:
96 name = queue.get()
97 invocation = get_tidy_invocation(name, args.checks, tmpdir,
98 build_path, args.header_filter)
99 sys.stdout.write(' '.join(invocation) + '\n')
100 subprocess.call(invocation)
101 queue.task_done()
102
103
104def main():
105 parser = argparse.ArgumentParser(description='Runs clang-tidy over all files '
106 'in a compilation database. Requires '
107 'clang-tidy and clang-apply-replacements in '
108 '$PATH.')
109 parser.add_argument('-checks', default=None,
110 help='checks filter, when not specified, use clang-tidy '
111 'default')
112 parser.add_argument('-header-filter', default=None,
113 help='regular expression matching the names of the '
114 'headers to output diagnostics from. Diagnostics from '
115 'the main file of each translation unit are always '
116 'displayed.')
117 parser.add_argument('-j', type=int, default=0,
118 help='number of tidy instances to be run in parallel.')
119 parser.add_argument('files', nargs='*', default=['.*'],
120 help='files to be processed (regex on path)')
121 parser.add_argument('-fix', action='store_true', help='apply fix-its')
122 parser.add_argument('-format', action='store_true', help='Reformat code '
123 'after applying fixes')
124 args = parser.parse_args()
125
126 # Find our database.
127 db_path = 'compile_commands.json'
128 build_path = find_compilation_database(db_path)
129
130 # Load the database and extract all files.
131 database = json.load(open(os.path.join(build_path, db_path)))
132 files = [entry['file'] for entry in database]
133
134 max_task = args.j
135 if max_task == 0:
136 max_task = multiprocessing.cpu_count()
137
138 tmpdir = None
139 if args.fix:
140 tmpdir = tempfile.mkdtemp()
141
142 # Build up a big regexy filter from all command line arguments.
143 file_name_re = re.compile('(' + ')|('.join(args.files) + ')')
144
145 try:
146 # Spin up a bunch of tidy-launching threads.
147 queue = Queue.Queue(max_task)
148 for _ in range(max_task):
149 t = threading.Thread(target=run_tidy,
150 args=(args, tmpdir, build_path, queue))
151 t.daemon = True
152 t.start()
153
154 # Fill the queue with files.
155 for name in files:
156 if file_name_re.search(name):
157 queue.put(name)
158
159 # Wait for all threads to be done.
160 queue.join()
161
162 except KeyboardInterrupt:
163 # This is a sad hack. Unfortunately subprocess goes
164 # bonkers with ctrl-c and we start forking merrily.
165 print '\nCtrl-C detected, goodbye.'
166 if args.fix:
167 shutil.rmtree(tmpdir)
168 os.kill(0, 9)
169
170 if args.fix:
171 print 'Applying fixes ...'
172 apply_fixes(tmpdir, args.format)
173
174if __name__ == '__main__':
175 main()