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 |
Alexander Kornienko | 16300e1 | 2017-07-21 10:31:26 +0000 | [diff] [blame^] | 39 | import glob |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 40 | import json |
| 41 | import multiprocessing |
| 42 | import os |
| 43 | import Queue |
| 44 | import re |
| 45 | import shutil |
| 46 | import subprocess |
| 47 | import sys |
| 48 | import tempfile |
| 49 | import threading |
Jakub Kuderski | a7410fd | 2017-04-25 22:38:39 +0000 | [diff] [blame] | 50 | import traceback |
Alexander Kornienko | 16300e1 | 2017-07-21 10:31:26 +0000 | [diff] [blame^] | 51 | import yaml |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 52 | |
| 53 | |
| 54 | def find_compilation_database(path): |
| 55 | """Adjusts the directory until a compilation database is found.""" |
| 56 | result = './' |
| 57 | while not os.path.isfile(os.path.join(result, path)): |
| 58 | if os.path.realpath(result) == '/': |
Jakub Kuderski | a7410fd | 2017-04-25 22:38:39 +0000 | [diff] [blame] | 59 | print('Error: could not find compilation database.') |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 60 | sys.exit(1) |
| 61 | result += '../' |
| 62 | return os.path.realpath(result) |
| 63 | |
| 64 | |
Benjamin Kramer | 815dbad | 2014-09-08 14:56:40 +0000 | [diff] [blame] | 65 | def get_tidy_invocation(f, clang_tidy_binary, checks, tmpdir, build_path, |
Ehsan Akhgari | b7418d3 | 2017-02-09 18:32:02 +0000 | [diff] [blame] | 66 | header_filter, extra_arg, extra_arg_before, quiet): |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 67 | """Gets a command line for clang-tidy.""" |
Benjamin Kramer | 815dbad | 2014-09-08 14:56:40 +0000 | [diff] [blame] | 68 | start = [clang_tidy_binary] |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 69 | if header_filter is not None: |
| 70 | start.append('-header-filter=' + header_filter) |
| 71 | else: |
| 72 | # Show warnings in all in-project headers by default. |
| 73 | start.append('-header-filter=^' + build_path + '/.*') |
| 74 | if checks: |
Alexander Kornienko | 3f11538 | 2015-09-08 10:31:36 +0000 | [diff] [blame] | 75 | start.append('-checks=' + checks) |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 76 | if tmpdir is not None: |
| 77 | start.append('-export-fixes') |
| 78 | # Get a temporary file. We immediately close the handle so clang-tidy can |
| 79 | # overwrite it. |
| 80 | (handle, name) = tempfile.mkstemp(suffix='.yaml', dir=tmpdir) |
Benjamin Kramer | 815dbad | 2014-09-08 14:56:40 +0000 | [diff] [blame] | 81 | os.close(handle) |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 82 | start.append(name) |
Ehsan Akhgari | 221ab77 | 2017-01-18 17:49:35 +0000 | [diff] [blame] | 83 | for arg in extra_arg: |
| 84 | start.append('-extra-arg=%s' % arg) |
| 85 | for arg in extra_arg_before: |
| 86 | start.append('-extra-arg-before=%s' % arg) |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 87 | start.append('-p=' + build_path) |
Ehsan Akhgari | b7418d3 | 2017-02-09 18:32:02 +0000 | [diff] [blame] | 88 | if quiet: |
| 89 | start.append('-quiet') |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 90 | start.append(f) |
| 91 | return start |
| 92 | |
| 93 | |
Alexander Kornienko | 16300e1 | 2017-07-21 10:31:26 +0000 | [diff] [blame^] | 94 | def merge_replacement_files(tmpdir, mergefile): |
| 95 | """Merge all replacement files in a directory into a single file""" |
| 96 | # The fixes suggested by clang-tidy >= 4.0.0 are given under |
| 97 | # the top level key 'Diagnostics' in the output yaml files |
| 98 | mergekey="Diagnostics" |
| 99 | merged=[] |
| 100 | for replacefile in glob.iglob(os.path.join(tmpdir, '*.yaml')): |
| 101 | content = yaml.safe_load(open(replacefile, 'r')) |
| 102 | if not content: |
| 103 | continue # Skip empty files. |
| 104 | merged.extend(content.get(mergekey, [])) |
| 105 | |
| 106 | if merged: |
| 107 | # MainSourceFile: The key is required by the definition inside |
| 108 | # include/clang/Tooling/ReplacementsYaml.h, but the value |
| 109 | # is actually never used inside clang-apply-replacements, |
| 110 | # so we set it to '' here. |
| 111 | output = { 'MainSourceFile': '', mergekey: merged } |
| 112 | with open(mergefile, 'w') as out: |
| 113 | yaml.safe_dump(output, out) |
| 114 | else: |
| 115 | # Empty the file: |
| 116 | open(mergefile, 'w').close() |
| 117 | |
| 118 | |
Jakub Kuderski | a7410fd | 2017-04-25 22:38:39 +0000 | [diff] [blame] | 119 | def check_clang_apply_replacements_binary(args): |
| 120 | """Checks if invoking supplied clang-apply-replacements binary works.""" |
| 121 | try: |
| 122 | subprocess.check_call([args.clang_apply_replacements_binary, '--version']) |
| 123 | except: |
| 124 | print('Unable to run clang-apply-replacements. Is clang-apply-replacements ' |
| 125 | 'binary correctly specified?', file=sys.stderr) |
| 126 | traceback.print_exc() |
| 127 | sys.exit(1) |
| 128 | |
| 129 | |
Benjamin Kramer | 815dbad | 2014-09-08 14:56:40 +0000 | [diff] [blame] | 130 | def apply_fixes(args, tmpdir): |
Alexander Kornienko | 16300e1 | 2017-07-21 10:31:26 +0000 | [diff] [blame^] | 131 | """Calls clang-apply-fixes on a given directory.""" |
Benjamin Kramer | 815dbad | 2014-09-08 14:56:40 +0000 | [diff] [blame] | 132 | invocation = [args.clang_apply_replacements_binary] |
| 133 | if args.format: |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 134 | invocation.append('-format') |
Vassil Vassilev | c4c33ce | 2017-06-09 22:23:03 +0000 | [diff] [blame] | 135 | if args.style: |
| 136 | invocation.append('-style=' + args.style) |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 137 | invocation.append(tmpdir) |
| 138 | subprocess.call(invocation) |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 139 | |
| 140 | |
| 141 | def run_tidy(args, tmpdir, build_path, queue): |
| 142 | """Takes filenames out of queue and runs clang-tidy on them.""" |
| 143 | while True: |
| 144 | name = queue.get() |
Benjamin Kramer | 815dbad | 2014-09-08 14:56:40 +0000 | [diff] [blame] | 145 | invocation = get_tidy_invocation(name, args.clang_tidy_binary, args.checks, |
Ehsan Akhgari | 221ab77 | 2017-01-18 17:49:35 +0000 | [diff] [blame] | 146 | tmpdir, build_path, args.header_filter, |
Ehsan Akhgari | b7418d3 | 2017-02-09 18:32:02 +0000 | [diff] [blame] | 147 | args.extra_arg, args.extra_arg_before, |
| 148 | args.quiet) |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 149 | sys.stdout.write(' '.join(invocation) + '\n') |
| 150 | subprocess.call(invocation) |
| 151 | queue.task_done() |
| 152 | |
| 153 | |
| 154 | def main(): |
| 155 | parser = argparse.ArgumentParser(description='Runs clang-tidy over all files ' |
| 156 | 'in a compilation database. Requires ' |
| 157 | 'clang-tidy and clang-apply-replacements in ' |
| 158 | '$PATH.') |
Benjamin Kramer | 815dbad | 2014-09-08 14:56:40 +0000 | [diff] [blame] | 159 | parser.add_argument('-clang-tidy-binary', metavar='PATH', |
| 160 | default='clang-tidy', |
| 161 | help='path to clang-tidy binary') |
| 162 | parser.add_argument('-clang-apply-replacements-binary', metavar='PATH', |
| 163 | default='clang-apply-replacements', |
| 164 | help='path to clang-apply-replacements binary') |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 165 | parser.add_argument('-checks', default=None, |
| 166 | help='checks filter, when not specified, use clang-tidy ' |
| 167 | 'default') |
| 168 | parser.add_argument('-header-filter', default=None, |
| 169 | help='regular expression matching the names of the ' |
| 170 | 'headers to output diagnostics from. Diagnostics from ' |
| 171 | 'the main file of each translation unit are always ' |
| 172 | 'displayed.') |
Alexander Kornienko | 16300e1 | 2017-07-21 10:31:26 +0000 | [diff] [blame^] | 173 | parser.add_argument('-export-fixes', metavar='filename', dest='export_fixes', |
| 174 | help='Create a yaml file to store suggested fixes in, ' |
| 175 | 'which can be applied with clang-apply-replacements.') |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 176 | parser.add_argument('-j', type=int, default=0, |
| 177 | help='number of tidy instances to be run in parallel.') |
| 178 | parser.add_argument('files', nargs='*', default=['.*'], |
| 179 | help='files to be processed (regex on path)') |
| 180 | parser.add_argument('-fix', action='store_true', help='apply fix-its') |
| 181 | parser.add_argument('-format', action='store_true', help='Reformat code ' |
| 182 | 'after applying fixes') |
Vassil Vassilev | c4c33ce | 2017-06-09 22:23:03 +0000 | [diff] [blame] | 183 | parser.add_argument('-style', default='file', help='The style of reformat ' |
| 184 | 'code after applying fixes') |
Guillaume Papin | 68b5910 | 2015-09-28 17:53:04 +0000 | [diff] [blame] | 185 | parser.add_argument('-p', dest='build_path', |
| 186 | help='Path used to read a compile command database.') |
Ehsan Akhgari | 221ab77 | 2017-01-18 17:49:35 +0000 | [diff] [blame] | 187 | parser.add_argument('-extra-arg', dest='extra_arg', |
| 188 | action='append', default=[], |
| 189 | help='Additional argument to append to the compiler ' |
| 190 | 'command line.') |
| 191 | parser.add_argument('-extra-arg-before', dest='extra_arg_before', |
| 192 | action='append', default=[], |
| 193 | help='Additional argument to prepend to the compiler ' |
| 194 | 'command line.') |
Ehsan Akhgari | b7418d3 | 2017-02-09 18:32:02 +0000 | [diff] [blame] | 195 | parser.add_argument('-quiet', action='store_true', |
| 196 | help='Run clang-tidy in quiet mode') |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 197 | args = parser.parse_args() |
| 198 | |
Guillaume Papin | 68b5910 | 2015-09-28 17:53:04 +0000 | [diff] [blame] | 199 | db_path = 'compile_commands.json' |
| 200 | |
| 201 | if args.build_path is not None: |
| 202 | build_path = args.build_path |
| 203 | else: |
| 204 | # Find our database |
| 205 | build_path = find_compilation_database(db_path) |
| 206 | |
Benjamin Kramer | 815dbad | 2014-09-08 14:56:40 +0000 | [diff] [blame] | 207 | try: |
Alexander Kornienko | f73fe3a | 2014-09-22 00:07:07 +0000 | [diff] [blame] | 208 | invocation = [args.clang_tidy_binary, '-list-checks'] |
Guillaume Papin | 68b5910 | 2015-09-28 17:53:04 +0000 | [diff] [blame] | 209 | invocation.append('-p=' + build_path) |
Alexander Kornienko | f73fe3a | 2014-09-22 00:07:07 +0000 | [diff] [blame] | 210 | if args.checks: |
Alexander Kornienko | 3f11538 | 2015-09-08 10:31:36 +0000 | [diff] [blame] | 211 | invocation.append('-checks=' + args.checks) |
Alexander Kornienko | f73fe3a | 2014-09-22 00:07:07 +0000 | [diff] [blame] | 212 | invocation.append('-') |
Jakub Kuderski | a7410fd | 2017-04-25 22:38:39 +0000 | [diff] [blame] | 213 | print(subprocess.check_output(invocation)) |
Benjamin Kramer | 815dbad | 2014-09-08 14:56:40 +0000 | [diff] [blame] | 214 | except: |
Jakub Kuderski | a7410fd | 2017-04-25 22:38:39 +0000 | [diff] [blame] | 215 | print("Unable to run clang-tidy.", file=sys.stderr) |
Benjamin Kramer | 815dbad | 2014-09-08 14:56:40 +0000 | [diff] [blame] | 216 | sys.exit(1) |
| 217 | |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 218 | # Load the database and extract all files. |
| 219 | database = json.load(open(os.path.join(build_path, db_path))) |
| 220 | files = [entry['file'] for entry in database] |
| 221 | |
| 222 | max_task = args.j |
| 223 | if max_task == 0: |
| 224 | max_task = multiprocessing.cpu_count() |
| 225 | |
| 226 | tmpdir = None |
Alexander Kornienko | 16300e1 | 2017-07-21 10:31:26 +0000 | [diff] [blame^] | 227 | if args.fix or args.export_fixes: |
Jakub Kuderski | a7410fd | 2017-04-25 22:38:39 +0000 | [diff] [blame] | 228 | check_clang_apply_replacements_binary(args) |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 229 | tmpdir = tempfile.mkdtemp() |
| 230 | |
| 231 | # Build up a big regexy filter from all command line arguments. |
Alexander Kornienko | eaada5c | 2017-03-23 16:29:39 +0000 | [diff] [blame] | 232 | file_name_re = re.compile('|'.join(args.files)) |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 233 | |
| 234 | try: |
| 235 | # Spin up a bunch of tidy-launching threads. |
| 236 | queue = Queue.Queue(max_task) |
| 237 | for _ in range(max_task): |
| 238 | t = threading.Thread(target=run_tidy, |
| 239 | args=(args, tmpdir, build_path, queue)) |
| 240 | t.daemon = True |
| 241 | t.start() |
| 242 | |
| 243 | # Fill the queue with files. |
| 244 | for name in files: |
| 245 | if file_name_re.search(name): |
| 246 | queue.put(name) |
| 247 | |
| 248 | # Wait for all threads to be done. |
| 249 | queue.join() |
| 250 | |
| 251 | except KeyboardInterrupt: |
| 252 | # This is a sad hack. Unfortunately subprocess goes |
| 253 | # bonkers with ctrl-c and we start forking merrily. |
Jakub Kuderski | a7410fd | 2017-04-25 22:38:39 +0000 | [diff] [blame] | 254 | print('\nCtrl-C detected, goodbye.') |
Alexander Kornienko | 16300e1 | 2017-07-21 10:31:26 +0000 | [diff] [blame^] | 255 | if tmpdir: |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 256 | shutil.rmtree(tmpdir) |
| 257 | os.kill(0, 9) |
| 258 | |
Alexander Kornienko | 16300e1 | 2017-07-21 10:31:26 +0000 | [diff] [blame^] | 259 | return_code = 0 |
| 260 | if args.export_fixes: |
| 261 | print('Writing fixes to ' + args.export_fixes + ' ...') |
| 262 | try: |
| 263 | merge_replacement_files(tmpdir, args.export_fixes) |
| 264 | except: |
| 265 | print('Error exporting fixes.\n', file=sys.stderr) |
| 266 | traceback.print_exc() |
| 267 | return_code=1 |
| 268 | |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 269 | if args.fix: |
Jakub Kuderski | a7410fd | 2017-04-25 22:38:39 +0000 | [diff] [blame] | 270 | print('Applying fixes ...') |
Jakub Kuderski | a7410fd | 2017-04-25 22:38:39 +0000 | [diff] [blame] | 271 | try: |
| 272 | apply_fixes(args, tmpdir) |
Jakub Kuderski | a7410fd | 2017-04-25 22:38:39 +0000 | [diff] [blame] | 273 | except: |
| 274 | print('Error applying fixes.\n', file=sys.stderr) |
| 275 | traceback.print_exc() |
Alexander Kornienko | 16300e1 | 2017-07-21 10:31:26 +0000 | [diff] [blame^] | 276 | return_code=1 |
Jakub Kuderski | a7410fd | 2017-04-25 22:38:39 +0000 | [diff] [blame] | 277 | |
Alexander Kornienko | 16300e1 | 2017-07-21 10:31:26 +0000 | [diff] [blame^] | 278 | if tmpdir: |
Jakub Kuderski | a7410fd | 2017-04-25 22:38:39 +0000 | [diff] [blame] | 279 | shutil.rmtree(tmpdir) |
Alexander Kornienko | 16300e1 | 2017-07-21 10:31:26 +0000 | [diff] [blame^] | 280 | sys.exit(return_code) |
Benjamin Kramer | a9d9a4d | 2014-09-08 14:01:31 +0000 | [diff] [blame] | 281 | |
| 282 | if __name__ == '__main__': |
| 283 | main() |