Alexander Kornienko | e04dd25 | 2016-01-19 16:10:39 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
Alexander Kornienko | 1de35e7 | 2014-06-25 14:09:52 +0000 | [diff] [blame] | 2 | # |
| 3 | #===- clang-tidy-diff.py - ClangTidy Diff Checker ------------*- 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 | |
| 12 | r""" |
| 13 | ClangTidy Diff Checker |
| 14 | ====================== |
| 15 | |
| 16 | This script reads input from a unified diff, runs clang-tidy on all changed |
| 17 | files and outputs clang-tidy warnings in changed lines only. This is useful to |
| 18 | detect clang-tidy regressions in the lines touched by a specific patch. |
| 19 | Example usage for git/svn users: |
| 20 | |
| 21 | git diff -U0 HEAD^ | clang-tidy-diff.py -p1 |
| 22 | svn diff --diff-cmd=diff -x-U0 | \ |
Alexander Kornienko | 1395253 | 2015-09-15 13:13:48 +0000 | [diff] [blame] | 23 | clang-tidy-diff.py -fix -checks=-*,modernize-use-override |
Alexander Kornienko | 1de35e7 | 2014-06-25 14:09:52 +0000 | [diff] [blame] | 24 | |
| 25 | """ |
| 26 | |
| 27 | import argparse |
| 28 | import json |
| 29 | import re |
| 30 | import subprocess |
| 31 | import sys |
| 32 | |
| 33 | |
| 34 | def main(): |
| 35 | parser = argparse.ArgumentParser(description= |
Alexander Kornienko | fb90b51 | 2016-06-08 14:27:43 +0000 | [diff] [blame] | 36 | 'Run clang-tidy against changed files, and ' |
| 37 | 'output diagnostics only for modified ' |
| 38 | 'lines.') |
Alexander Kornienko | 1de35e7 | 2014-06-25 14:09:52 +0000 | [diff] [blame] | 39 | parser.add_argument('-clang-tidy-binary', metavar='PATH', |
| 40 | default='clang-tidy', |
| 41 | help='path to clang-tidy binary') |
| 42 | parser.add_argument('-p', metavar='NUM', default=0, |
| 43 | help='strip the smallest prefix containing P slashes') |
| 44 | parser.add_argument('-regex', metavar='PATTERN', default=None, |
Alexander Kornienko | fb90b51 | 2016-06-08 14:27:43 +0000 | [diff] [blame] | 45 | help='custom pattern selecting file paths to check ' |
Alexander Kornienko | 1de35e7 | 2014-06-25 14:09:52 +0000 | [diff] [blame] | 46 | '(case sensitive, overrides -iregex)') |
| 47 | parser.add_argument('-iregex', metavar='PATTERN', default= |
| 48 | r'.*\.(cpp|cc|c\+\+|cxx|c|cl|h|hpp|m|mm|inc)', |
Alexander Kornienko | fb90b51 | 2016-06-08 14:27:43 +0000 | [diff] [blame] | 49 | help='custom pattern selecting file paths to check ' |
Alexander Kornienko | 1de35e7 | 2014-06-25 14:09:52 +0000 | [diff] [blame] | 50 | '(case insensitive, overridden by -regex)') |
| 51 | |
| 52 | parser.add_argument('-fix', action='store_true', default=False, |
| 53 | help='apply suggested fixes') |
| 54 | parser.add_argument('-checks', |
| 55 | help='checks filter, when not specified, use clang-tidy ' |
| 56 | 'default', |
| 57 | default='') |
Ehsan Akhgari | 3bceebb | 2017-02-08 17:50:24 +0000 | [diff] [blame] | 58 | parser.add_argument('-extra-arg', dest='extra_arg', |
| 59 | action='append', default=[], |
| 60 | help='Additional argument to append to the compiler ' |
| 61 | 'command line.') |
| 62 | parser.add_argument('-extra-arg-before', dest='extra_arg_before', |
| 63 | action='append', default=[], |
| 64 | help='Additional argument to prepend to the compiler ' |
| 65 | 'command line.') |
Alexander Kornienko | 1de35e7 | 2014-06-25 14:09:52 +0000 | [diff] [blame] | 66 | clang_tidy_args = [] |
| 67 | argv = sys.argv[1:] |
| 68 | if '--' in argv: |
| 69 | clang_tidy_args.extend(argv[argv.index('--'):]) |
| 70 | argv = argv[:argv.index('--')] |
| 71 | |
| 72 | args = parser.parse_args(argv) |
| 73 | |
| 74 | # Extract changed lines for each file. |
| 75 | filename = None |
| 76 | lines_by_file = {} |
| 77 | for line in sys.stdin: |
Yaron Keren | 95e6d9e | 2015-09-01 19:08:17 +0000 | [diff] [blame] | 78 | match = re.search('^\+\+\+\ \"?(.*?/){%s}([^ \t\n\"]*)' % args.p, line) |
Alexander Kornienko | 1de35e7 | 2014-06-25 14:09:52 +0000 | [diff] [blame] | 79 | if match: |
| 80 | filename = match.group(2) |
| 81 | if filename == None: |
| 82 | continue |
| 83 | |
| 84 | if args.regex is not None: |
| 85 | if not re.match('^%s$' % args.regex, filename): |
| 86 | continue |
| 87 | else: |
| 88 | if not re.match('^%s$' % args.iregex, filename, re.IGNORECASE): |
| 89 | continue |
| 90 | |
| 91 | match = re.search('^@@.*\+(\d+)(,(\d+))?', line) |
| 92 | if match: |
| 93 | start_line = int(match.group(1)) |
| 94 | line_count = 1 |
| 95 | if match.group(3): |
| 96 | line_count = int(match.group(3)) |
| 97 | if line_count == 0: |
| 98 | continue |
| 99 | end_line = start_line + line_count - 1; |
| 100 | lines_by_file.setdefault(filename, []).append([start_line, end_line]) |
| 101 | |
| 102 | if len(lines_by_file) == 0: |
NAKAMURA Takumi | 360096e | 2014-06-27 01:10:18 +0000 | [diff] [blame] | 103 | print("No relevant changes found.") |
Alexander Kornienko | 1de35e7 | 2014-06-25 14:09:52 +0000 | [diff] [blame] | 104 | sys.exit(0) |
| 105 | |
| 106 | line_filter_json = json.dumps( |
| 107 | [{"name" : name, "lines" : lines_by_file[name]} for name in lines_by_file], |
| 108 | separators = (',', ':')) |
| 109 | |
NAKAMURA Takumi | ae6b329 | 2015-08-20 15:04:46 +0000 | [diff] [blame] | 110 | quote = ""; |
| 111 | if sys.platform == 'win32': |
| 112 | line_filter_json=re.sub(r'"', r'"""', line_filter_json) |
| 113 | else: |
| 114 | quote = "'"; |
| 115 | |
Alexander Kornienko | 1de35e7 | 2014-06-25 14:09:52 +0000 | [diff] [blame] | 116 | # Run clang-tidy on files containing changes. |
| 117 | command = [args.clang_tidy_binary] |
NAKAMURA Takumi | ae6b329 | 2015-08-20 15:04:46 +0000 | [diff] [blame] | 118 | command.append('-line-filter=' + quote + line_filter_json + quote) |
Alexander Kornienko | 1de35e7 | 2014-06-25 14:09:52 +0000 | [diff] [blame] | 119 | if args.fix: |
| 120 | command.append('-fix') |
| 121 | if args.checks != '': |
NAKAMURA Takumi | ae6b329 | 2015-08-20 15:04:46 +0000 | [diff] [blame] | 122 | command.append('-checks=' + quote + args.checks + quote) |
Alexander Kornienko | 1de35e7 | 2014-06-25 14:09:52 +0000 | [diff] [blame] | 123 | command.extend(lines_by_file.keys()) |
Ehsan Akhgari | 3bceebb | 2017-02-08 17:50:24 +0000 | [diff] [blame] | 124 | for arg in args.extra_arg: |
| 125 | command.append('-extra-arg=%s' % arg) |
| 126 | for arg in args.extra_arg_before: |
| 127 | command.append('-extra-arg-before=%s' % arg) |
Alexander Kornienko | 1de35e7 | 2014-06-25 14:09:52 +0000 | [diff] [blame] | 128 | command.extend(clang_tidy_args) |
| 129 | |
| 130 | sys.exit(subprocess.call(' '.join(command), shell=True)) |
| 131 | |
| 132 | if __name__ == '__main__': |
| 133 | main() |