blob: fa6f5284d915446c7819c41e9e263580e17a0b1f [file] [log] [blame]
Alexander Kornienkoe04dd252016-01-19 16:10:39 +00001#!/usr/bin/env python
Alexander Kornienko1de35e72014-06-25 14:09:52 +00002#
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
12r"""
13ClangTidy Diff Checker
14======================
15
16This script reads input from a unified diff, runs clang-tidy on all changed
17files and outputs clang-tidy warnings in changed lines only. This is useful to
18detect clang-tidy regressions in the lines touched by a specific patch.
19Example 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 Kornienko13952532015-09-15 13:13:48 +000023 clang-tidy-diff.py -fix -checks=-*,modernize-use-override
Alexander Kornienko1de35e72014-06-25 14:09:52 +000024
25"""
26
27import argparse
28import json
29import re
30import subprocess
31import sys
32
33
34def main():
35 parser = argparse.ArgumentParser(description=
Alexander Kornienkofb90b512016-06-08 14:27:43 +000036 'Run clang-tidy against changed files, and '
37 'output diagnostics only for modified '
38 'lines.')
Alexander Kornienko1de35e72014-06-25 14:09:52 +000039 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 Kornienkofb90b512016-06-08 14:27:43 +000045 help='custom pattern selecting file paths to check '
Alexander Kornienko1de35e72014-06-25 14:09:52 +000046 '(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 Kornienkofb90b512016-06-08 14:27:43 +000049 help='custom pattern selecting file paths to check '
Alexander Kornienko1de35e72014-06-25 14:09:52 +000050 '(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 Akhgari3bceebb2017-02-08 17:50:24 +000058 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.')
Ehsan Akhgarib7418d32017-02-09 18:32:02 +000066 parser.add_argument('-quiet', action='store_true', default=False,
67 help='Run clang-tidy in quiet mode')
Alexander Kornienko1de35e72014-06-25 14:09:52 +000068 clang_tidy_args = []
69 argv = sys.argv[1:]
70 if '--' in argv:
71 clang_tidy_args.extend(argv[argv.index('--'):])
72 argv = argv[:argv.index('--')]
73
74 args = parser.parse_args(argv)
75
76 # Extract changed lines for each file.
77 filename = None
78 lines_by_file = {}
79 for line in sys.stdin:
Yaron Keren95e6d9e2015-09-01 19:08:17 +000080 match = re.search('^\+\+\+\ \"?(.*?/){%s}([^ \t\n\"]*)' % args.p, line)
Alexander Kornienko1de35e72014-06-25 14:09:52 +000081 if match:
82 filename = match.group(2)
83 if filename == None:
84 continue
85
86 if args.regex is not None:
87 if not re.match('^%s$' % args.regex, filename):
88 continue
89 else:
90 if not re.match('^%s$' % args.iregex, filename, re.IGNORECASE):
91 continue
92
93 match = re.search('^@@.*\+(\d+)(,(\d+))?', line)
94 if match:
95 start_line = int(match.group(1))
96 line_count = 1
97 if match.group(3):
98 line_count = int(match.group(3))
99 if line_count == 0:
100 continue
101 end_line = start_line + line_count - 1;
102 lines_by_file.setdefault(filename, []).append([start_line, end_line])
103
104 if len(lines_by_file) == 0:
NAKAMURA Takumi360096e2014-06-27 01:10:18 +0000105 print("No relevant changes found.")
Alexander Kornienko1de35e72014-06-25 14:09:52 +0000106 sys.exit(0)
107
108 line_filter_json = json.dumps(
109 [{"name" : name, "lines" : lines_by_file[name]} for name in lines_by_file],
110 separators = (',', ':'))
111
NAKAMURA Takumiae6b3292015-08-20 15:04:46 +0000112 quote = "";
113 if sys.platform == 'win32':
114 line_filter_json=re.sub(r'"', r'"""', line_filter_json)
115 else:
116 quote = "'";
117
Alexander Kornienko1de35e72014-06-25 14:09:52 +0000118 # Run clang-tidy on files containing changes.
119 command = [args.clang_tidy_binary]
NAKAMURA Takumiae6b3292015-08-20 15:04:46 +0000120 command.append('-line-filter=' + quote + line_filter_json + quote)
Alexander Kornienko1de35e72014-06-25 14:09:52 +0000121 if args.fix:
122 command.append('-fix')
123 if args.checks != '':
NAKAMURA Takumiae6b3292015-08-20 15:04:46 +0000124 command.append('-checks=' + quote + args.checks + quote)
Ehsan Akhgarib7418d32017-02-09 18:32:02 +0000125 if args.quiet:
126 command.append('-quiet')
Alexander Kornienko1de35e72014-06-25 14:09:52 +0000127 command.extend(lines_by_file.keys())
Ehsan Akhgari3bceebb2017-02-08 17:50:24 +0000128 for arg in args.extra_arg:
129 command.append('-extra-arg=%s' % arg)
130 for arg in args.extra_arg_before:
131 command.append('-extra-arg-before=%s' % arg)
Alexander Kornienko1de35e72014-06-25 14:09:52 +0000132 command.extend(clang_tidy_args)
133
134 sys.exit(subprocess.call(' '.join(command), shell=True))
135
136if __name__ == '__main__':
137 main()