run-clang-tidy.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #!/usr/bin/env python
  2. #
  3. #===- run-clang-tidy.py - Parallel clang-tidy runner ---------*- python -*--===#
  4. #
  5. # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  6. # See https://llvm.org/LICENSE.txt for license information.
  7. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  8. #
  9. #===------------------------------------------------------------------------===#
  10. # FIXME: Integrate with clang-tidy-diff.py
  11. """
  12. Parallel clang-tidy runner
  13. ==========================
  14. Runs clang-tidy over all files in a compilation database. Requires clang-tidy
  15. and clang-apply-replacements in $PATH.
  16. Example invocations.
  17. - Run clang-tidy on all files in the current working directory with a default
  18. set of checks and show warnings in the cpp files and all project headers.
  19. run-clang-tidy.py $PWD
  20. - Fix all header guards.
  21. run-clang-tidy.py -fix -checks=-*,llvm-header-guard
  22. - Fix all header guards included from clang-tidy and header guards
  23. for clang-tidy headers.
  24. run-clang-tidy.py -fix -checks=-*,llvm-header-guard extra/clang-tidy \
  25. -header-filter=extra/clang-tidy
  26. Compilation database setup:
  27. http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html
  28. """
  29. from __future__ import print_function
  30. import argparse
  31. import glob
  32. import json
  33. import multiprocessing
  34. import os
  35. import re
  36. import shutil
  37. import subprocess
  38. import sys
  39. import tempfile
  40. import threading
  41. import traceback
  42. try:
  43. import yaml
  44. except ImportError:
  45. yaml = None
  46. is_py2 = sys.version[0] == '2'
  47. if is_py2:
  48. import Queue as queue
  49. else:
  50. import queue as queue
  51. def find_compilation_database(path):
  52. """Adjusts the directory until a compilation database is found."""
  53. result = './'
  54. while not os.path.isfile(os.path.join(result, path)):
  55. if os.path.realpath(result) == '/':
  56. print('Error: could not find compilation database.')
  57. sys.exit(1)
  58. result += '../'
  59. return os.path.realpath(result)
  60. def make_absolute(f, directory):
  61. if os.path.isabs(f):
  62. return f
  63. return os.path.normpath(os.path.join(directory, f))
  64. def get_tidy_invocation(f, clang_tidy_binary, checks, tmpdir, build_path,
  65. header_filter, extra_arg, extra_arg_before, quiet,
  66. config):
  67. """Gets a command line for clang-tidy."""
  68. start = [clang_tidy_binary]
  69. if header_filter is not None:
  70. start.append('-header-filter=' + header_filter)
  71. if checks:
  72. start.append('-checks=' + checks)
  73. if tmpdir is not None:
  74. start.append('-export-fixes')
  75. # Get a temporary file. We immediately close the handle so clang-tidy can
  76. # overwrite it.
  77. (handle, name) = tempfile.mkstemp(suffix='.yaml', dir=tmpdir)
  78. os.close(handle)
  79. start.append(name)
  80. for arg in extra_arg:
  81. start.append('-extra-arg=%s' % arg)
  82. for arg in extra_arg_before:
  83. start.append('-extra-arg-before=%s' % arg)
  84. start.append('-p=' + build_path)
  85. if quiet:
  86. start.append('-quiet')
  87. if config:
  88. start.append('-config=' + config)
  89. start.append(f)
  90. return start
  91. def merge_replacement_files(tmpdir, mergefile):
  92. """Merge all replacement files in a directory into a single file"""
  93. # The fixes suggested by clang-tidy >= 4.0.0 are given under
  94. # the top level key 'Diagnostics' in the output yaml files
  95. mergekey="Diagnostics"
  96. merged=[]
  97. for replacefile in glob.iglob(os.path.join(tmpdir, '*.yaml')):
  98. content = yaml.safe_load(open(replacefile, 'r'))
  99. if not content:
  100. continue # Skip empty files.
  101. merged.extend(content.get(mergekey, []))
  102. if merged:
  103. # MainSourceFile: The key is required by the definition inside
  104. # include/clang/Tooling/ReplacementsYaml.h, but the value
  105. # is actually never used inside clang-apply-replacements,
  106. # so we set it to '' here.
  107. output = { 'MainSourceFile': '', mergekey: merged }
  108. with open(mergefile, 'w') as out:
  109. yaml.safe_dump(output, out)
  110. else:
  111. # Empty the file:
  112. open(mergefile, 'w').close()
  113. def check_clang_apply_replacements_binary(args):
  114. """Checks if invoking supplied clang-apply-replacements binary works."""
  115. try:
  116. subprocess.check_call([args.clang_apply_replacements_binary, '--version'])
  117. except:
  118. print('Unable to run clang-apply-replacements. Is clang-apply-replacements '
  119. 'binary correctly specified?', file=sys.stderr)
  120. traceback.print_exc()
  121. sys.exit(1)
  122. def apply_fixes(args, tmpdir):
  123. """Calls clang-apply-fixes on a given directory."""
  124. invocation = [args.clang_apply_replacements_binary]
  125. if args.format:
  126. invocation.append('-format')
  127. if args.style:
  128. invocation.append('-style=' + args.style)
  129. invocation.append(tmpdir)
  130. subprocess.call(invocation)
  131. def run_tidy(args, tmpdir, build_path, queue, lock, failed_files):
  132. """Takes filenames out of queue and runs clang-tidy on them."""
  133. while True:
  134. name = queue.get()
  135. invocation = get_tidy_invocation(name, args.clang_tidy_binary, args.checks,
  136. tmpdir, build_path, args.header_filter,
  137. args.extra_arg, args.extra_arg_before,
  138. args.quiet, args.config)
  139. proc = subprocess.Popen(invocation)
  140. proc.wait()
  141. if proc.returncode != 0:
  142. failed_files.append(name)
  143. queue.task_done()
  144. def main():
  145. parser = argparse.ArgumentParser(description='Runs clang-tidy over all files '
  146. 'in a compilation database. Requires '
  147. 'clang-tidy and clang-apply-replacements in '
  148. '$PATH.')
  149. parser.add_argument('-clang-tidy-binary', metavar='PATH',
  150. default='clang-tidy',
  151. help='path to clang-tidy binary')
  152. parser.add_argument('-clang-apply-replacements-binary', metavar='PATH',
  153. default='clang-apply-replacements',
  154. help='path to clang-apply-replacements binary')
  155. parser.add_argument('-checks', default=None,
  156. help='checks filter, when not specified, use clang-tidy '
  157. 'default')
  158. parser.add_argument('-config', default=None,
  159. help='Specifies a configuration in YAML/JSON format: '
  160. ' -config="{Checks: \'*\', '
  161. ' CheckOptions: [{key: x, '
  162. ' value: y}]}" '
  163. 'When the value is empty, clang-tidy will '
  164. 'attempt to find a file named .clang-tidy for '
  165. 'each source file in its parent directories.')
  166. parser.add_argument('-header-filter', default=None,
  167. help='regular expression matching the names of the '
  168. 'headers to output diagnostics from. Diagnostics from '
  169. 'the main file of each translation unit are always '
  170. 'displayed.')
  171. if yaml:
  172. parser.add_argument('-export-fixes', metavar='filename', dest='export_fixes',
  173. help='Create a yaml file to store suggested fixes in, '
  174. 'which can be applied with clang-apply-replacements.')
  175. parser.add_argument('-j', type=int, default=0,
  176. help='number of tidy instances to be run in parallel.')
  177. parser.add_argument('files', nargs='*', default=['.*'],
  178. help='files to be processed (regex on path)')
  179. parser.add_argument('-fix', action='store_true', help='apply fix-its')
  180. parser.add_argument('-format', action='store_true', help='Reformat code '
  181. 'after applying fixes')
  182. parser.add_argument('-style', default='file', help='The style of reformat '
  183. 'code after applying fixes')
  184. parser.add_argument('-p', dest='build_path',
  185. help='Path used to read a compile command database.')
  186. parser.add_argument('-extra-arg', dest='extra_arg',
  187. action='append', default=[],
  188. help='Additional argument to append to the compiler '
  189. 'command line.')
  190. parser.add_argument('-extra-arg-before', dest='extra_arg_before',
  191. action='append', default=[],
  192. help='Additional argument to prepend to the compiler '
  193. 'command line.')
  194. parser.add_argument('-quiet', action='store_true',
  195. help='Run clang-tidy in quiet mode')
  196. args = parser.parse_args()
  197. db_path = 'compile_commands.json'
  198. if args.build_path is not None:
  199. build_path = args.build_path
  200. else:
  201. # Find our database
  202. build_path = find_compilation_database(db_path)
  203. try:
  204. invocation = [args.clang_tidy_binary, '-list-checks']
  205. invocation.append('-p=' + build_path)
  206. if args.checks:
  207. invocation.append('-checks=' + args.checks)
  208. invocation.append('-')
  209. if args.quiet:
  210. # Even with -quiet we still want to check if we can call clang-tidy.
  211. with open(os.devnull, 'w') as dev_null:
  212. subprocess.check_call(invocation, stdout=dev_null)
  213. else:
  214. subprocess.check_call(invocation)
  215. except:
  216. print("Unable to run clang-tidy.", file=sys.stderr)
  217. sys.exit(1)
  218. # Load the database and extract all files.
  219. database = json.load(open(os.path.join(build_path, db_path)))
  220. files = [make_absolute(entry['file'], entry['directory'])
  221. for entry in database]
  222. max_task = args.j
  223. if max_task == 0:
  224. max_task = multiprocessing.cpu_count()
  225. tmpdir = None
  226. if args.fix or (yaml and args.export_fixes):
  227. check_clang_apply_replacements_binary(args)
  228. tmpdir = tempfile.mkdtemp()
  229. # Build up a big regexy filter from all command line arguments.
  230. file_name_re = re.compile('|'.join(args.files))
  231. return_code = 0
  232. try:
  233. # Spin up a bunch of tidy-launching threads.
  234. task_queue = queue.Queue(max_task)
  235. # List of files with a non-zero return code.
  236. failed_files = []
  237. lock = threading.Lock()
  238. for _ in range(max_task):
  239. t = threading.Thread(target=run_tidy,
  240. args=(args, tmpdir, build_path, task_queue, lock, failed_files))
  241. t.daemon = True
  242. t.start()
  243. # Fill the queue with files.
  244. for name in files:
  245. if file_name_re.search(name):
  246. task_queue.put(name)
  247. # Wait for all threads to be done.
  248. task_queue.join()
  249. if len(failed_files):
  250. return_code = 1
  251. except KeyboardInterrupt:
  252. # This is a sad hack. Unfortunately subprocess goes
  253. # bonkers with ctrl-c and we start forking merrily.
  254. print('\nCtrl-C detected, goodbye.')
  255. if tmpdir:
  256. shutil.rmtree(tmpdir)
  257. os.kill(0, 9)
  258. if yaml and args.export_fixes:
  259. print('Writing fixes to ' + args.export_fixes + ' ...')
  260. try:
  261. merge_replacement_files(tmpdir, args.export_fixes)
  262. except:
  263. print('Error exporting fixes.\n', file=sys.stderr)
  264. traceback.print_exc()
  265. return_code=1
  266. if args.fix:
  267. print('Applying fixes ...')
  268. try:
  269. apply_fixes(args, tmpdir)
  270. except:
  271. print('Error applying fixes.\n', file=sys.stderr)
  272. traceback.print_exc()
  273. return_code=1
  274. if tmpdir:
  275. shutil.rmtree(tmpdir)
  276. sys.exit(return_code)
  277. if __name__ == '__main__':
  278. main()