Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 1 | #!/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 | """ |
| 14 | Parallel clang-tidy runner |
| 15 | ========================== |
| 16 | |
| 17 | Runs clang-tidy over all files in a compilation database. Requires clang-tidy |
| 18 | and clang-apply-replacements in $PATH. |
| 19 | |
| 20 | Example 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 | |
| 33 | Compilation database setup: |
| 34 | http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html |
| 35 | """ |
| 36 | |
Jakub Kuderski | a7410fd | 2017-04-25 22:38:39 +0000 | [diff] [blame^] | 37 | from __future__ import print_function |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 38 | import argparse |
| 39 | import json |
| 40 | import multiprocessing |
| 41 | import os |
| 42 | import Queue |
| 43 | import re |
| 44 | import shutil |
| 45 | import subprocess |
| 46 | import sys |
| 47 | import tempfile |
| 48 | import threading |
Jakub Kuderski | a7410fd | 2017-04-25 22:38:39 +0000 | [diff] [blame^] | 49 | import traceback |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 50 | |
| 51 | |
| 52 | def 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 Kuderski | a7410fd | 2017-04-25 22:38:39 +0000 | [diff] [blame^] | 57 | print('Error: could not find compilation database.') |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 58 | sys.exit(1) |
| 59 | result += '../' |
| 60 | return os.path.realpath(result) |
| 61 | |
| 62 | |
Benjamin Kramer | 815dbad | 2014-09-08 14:56:40 +0000 | [diff] [blame] | 63 | def get_tidy_invocation(f, clang_tidy_binary, checks, tmpdir, build_path, |
Ehsan Akhgari | b7418d3 | 2017-02-09 18:32:02 +0000 | [diff] [blame] | 64 | header_filter, extra_arg, extra_arg_before, quiet): |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 65 | """Gets a command line for clang-tidy.""" |
Benjamin Kramer | 815dbad | 2014-09-08 14:56:40 +0000 | [diff] [blame] | 66 | start = [clang_tidy_binary] |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 67 | 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 Kornienko | 3f11538 | 2015-09-08 10:31:36 +0000 | [diff] [blame] | 73 | start.append('-checks=' + checks) |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 74 | 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 Kramer | 815dbad | 2014-09-08 14:56:40 +0000 | [diff] [blame] | 79 | os.close(handle) |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 80 | start.append(name) |
Ehsan Akhgari | 221ab77 | 2017-01-18 17:49:35 +0000 | [diff] [blame] | 81 | 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 Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 85 | start.append('-p=' + build_path) |
Ehsan Akhgari | b7418d3 | 2017-02-09 18:32:02 +0000 | [diff] [blame] | 86 | if quiet: |
| 87 | start.append('-quiet') |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 88 | start.append(f) |
| 89 | return start |
| 90 | |
| 91 | |
Jakub Kuderski | a7410fd | 2017-04-25 22:38:39 +0000 | [diff] [blame^] | 92 | def 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 Kramer | 815dbad | 2014-09-08 14:56:40 +0000 | [diff] [blame] | 103 | def apply_fixes(args, tmpdir): |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 104 | """Calls clang-apply-fixes on a given directory. Deletes the dir when done.""" |
Benjamin Kramer | 815dbad | 2014-09-08 14:56:40 +0000 | [diff] [blame] | 105 | invocation = [args.clang_apply_replacements_binary] |
| 106 | if args.format: |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 107 | invocation.append('-format') |
| 108 | invocation.append(tmpdir) |
| 109 | subprocess.call(invocation) |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 110 | |
| 111 | |
| 112 | def 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 Kramer | 815dbad | 2014-09-08 14:56:40 +0000 | [diff] [blame] | 116 | invocation = get_tidy_invocation(name, args.clang_tidy_binary, args.checks, |
Ehsan Akhgari | 221ab77 | 2017-01-18 17:49:35 +0000 | [diff] [blame] | 117 | tmpdir, build_path, args.header_filter, |
Ehsan Akhgari | b7418d3 | 2017-02-09 18:32:02 +0000 | [diff] [blame] | 118 | args.extra_arg, args.extra_arg_before, |
| 119 | args.quiet) |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 120 | sys.stdout.write(' '.join(invocation) + '\n') |
| 121 | subprocess.call(invocation) |
| 122 | queue.task_done() |
| 123 | |
| 124 | |
| 125 | def 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 Kramer | 815dbad | 2014-09-08 14:56:40 +0000 | [diff] [blame] | 130 | 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 Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 136 | 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 Papin | 68b5910 | 2015-09-28 17:53:04 +0000 | [diff] [blame] | 151 | parser.add_argument('-p', dest='build_path', |
| 152 | help='Path used to read a compile command database.') |
Ehsan Akhgari | 221ab77 | 2017-01-18 17:49:35 +0000 | [diff] [blame] | 153 | 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 Akhgari | b7418d3 | 2017-02-09 18:32:02 +0000 | [diff] [blame] | 161 | parser.add_argument('-quiet', action='store_true', |
| 162 | help='Run clang-tidy in quiet mode') |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 163 | args = parser.parse_args() |
| 164 | |
Guillaume Papin | 68b5910 | 2015-09-28 17:53:04 +0000 | [diff] [blame] | 165 | 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 Kramer | 815dbad | 2014-09-08 14:56:40 +0000 | [diff] [blame] | 173 | try: |
Alexander Kornienko | f73fe3a | 2014-09-22 00:07:07 +0000 | [diff] [blame] | 174 | invocation = [args.clang_tidy_binary, '-list-checks'] |
Guillaume Papin | 68b5910 | 2015-09-28 17:53:04 +0000 | [diff] [blame] | 175 | invocation.append('-p=' + build_path) |
Alexander Kornienko | f73fe3a | 2014-09-22 00:07:07 +0000 | [diff] [blame] | 176 | if args.checks: |
Alexander Kornienko | 3f11538 | 2015-09-08 10:31:36 +0000 | [diff] [blame] | 177 | invocation.append('-checks=' + args.checks) |
Alexander Kornienko | f73fe3a | 2014-09-22 00:07:07 +0000 | [diff] [blame] | 178 | invocation.append('-') |
Jakub Kuderski | a7410fd | 2017-04-25 22:38:39 +0000 | [diff] [blame^] | 179 | print(subprocess.check_output(invocation)) |
Benjamin Kramer | 815dbad | 2014-09-08 14:56:40 +0000 | [diff] [blame] | 180 | except: |
Jakub Kuderski | a7410fd | 2017-04-25 22:38:39 +0000 | [diff] [blame^] | 181 | print("Unable to run clang-tidy.", file=sys.stderr) |
Benjamin Kramer | 815dbad | 2014-09-08 14:56:40 +0000 | [diff] [blame] | 182 | sys.exit(1) |
| 183 | |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 184 | # 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 Kuderski | a7410fd | 2017-04-25 22:38:39 +0000 | [diff] [blame^] | 194 | check_clang_apply_replacements_binary(args) |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 195 | tmpdir = tempfile.mkdtemp() |
| 196 | |
| 197 | # Build up a big regexy filter from all command line arguments. |
Alexander Kornienko | eaada5c | 2017-03-23 16:29:39 +0000 | [diff] [blame] | 198 | file_name_re = re.compile('|'.join(args.files)) |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 199 | |
| 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 Kuderski | a7410fd | 2017-04-25 22:38:39 +0000 | [diff] [blame^] | 220 | print('\nCtrl-C detected, goodbye.') |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 221 | if args.fix: |
| 222 | shutil.rmtree(tmpdir) |
| 223 | os.kill(0, 9) |
| 224 | |
| 225 | if args.fix: |
Jakub Kuderski | a7410fd | 2017-04-25 22:38:39 +0000 | [diff] [blame^] | 226 | 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 Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 239 | |
| 240 | if __name__ == '__main__': |
| 241 | main() |