Ed Schouten | bf041d9 | 2014-09-02 20:59:13 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 2 | # |
| 3 | #===- clang-format-diff.py - ClangFormat Diff Reformatter ----*- 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 | ClangFormat Diff Reformatter |
| 14 | ============================ |
| 15 | |
| 16 | This script reads input from a unified diff and reformats all the changed |
| 17 | lines. This is useful to reformat all the lines touched by a specific patch. |
Daniel Jasper | 2e8600c | 2014-05-14 09:36:11 +0000 | [diff] [blame] | 18 | Example usage for git/svn users: |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 19 | |
Sylvestre Ledru | f421403 | 2016-12-03 23:22:45 +0000 | [diff] [blame] | 20 | git diff -U0 --no-color HEAD^ | clang-format-diff.py -p1 -i |
Daniel Jasper | 2e8600c | 2014-05-14 09:36:11 +0000 | [diff] [blame] | 21 | svn diff --diff-cmd=diff -x-U0 | clang-format-diff.py -i |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 22 | |
| 23 | """ |
Serge Guelton | b748c0e | 2018-12-18 16:07:37 +0000 | [diff] [blame] | 24 | from __future__ import absolute_import, division, print_function |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 25 | |
| 26 | import argparse |
Alexander Kornienko | 95c009a | 2013-10-11 21:32:01 +0000 | [diff] [blame] | 27 | import difflib |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 28 | import re |
| 29 | import subprocess |
| 30 | import sys |
Serge Guelton | f886c03 | 2019-01-03 14:26:56 +0000 | [diff] [blame^] | 31 | |
| 32 | if sys.version_info.major >= 3: |
| 33 | from io import StringIO |
| 34 | else: |
| 35 | from io import BytesIO as StringIO |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 36 | |
| 37 | |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 38 | def main(): |
| 39 | parser = argparse.ArgumentParser(description= |
Alexander Kornienko | 95c009a | 2013-10-11 21:32:01 +0000 | [diff] [blame] | 40 | 'Reformat changed lines in diff. Without -i ' |
Alp Toker | 0e7f6da | 2013-12-04 00:48:22 +0000 | [diff] [blame] | 41 | 'option just output the diff that would be ' |
Alexander Kornienko | 95c009a | 2013-10-11 21:32:01 +0000 | [diff] [blame] | 42 | 'introduced.') |
| 43 | parser.add_argument('-i', action='store_true', default=False, |
| 44 | help='apply edits to files instead of displaying a diff') |
Alp Toker | 8c7cbdf | 2013-12-10 13:51:53 +0000 | [diff] [blame] | 45 | parser.add_argument('-p', metavar='NUM', default=0, |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 46 | help='strip the smallest prefix containing P slashes') |
Alp Toker | 679bf01 | 2013-12-18 21:34:07 +0000 | [diff] [blame] | 47 | parser.add_argument('-regex', metavar='PATTERN', default=None, |
Alexander Kornienko | 25c6838 | 2013-12-16 10:57:30 +0000 | [diff] [blame] | 48 | help='custom pattern selecting file paths to reformat ' |
Daniel Jasper | db7933a | 2013-12-19 10:21:37 +0000 | [diff] [blame] | 49 | '(case sensitive, overrides -iregex)') |
Alexander Kornienko | 25c6838 | 2013-12-16 10:57:30 +0000 | [diff] [blame] | 50 | parser.add_argument('-iregex', metavar='PATTERN', default= |
Daniel Jasper | 8c68a64 | 2015-03-11 14:58:38 +0000 | [diff] [blame] | 51 | r'.*\.(cpp|cc|c\+\+|cxx|c|cl|h|hpp|m|mm|inc|js|ts|proto' |
Daniel Jasper | 7c627a0 | 2014-12-08 19:39:03 +0000 | [diff] [blame] | 52 | r'|protodevel|java)', |
Alexander Kornienko | 25c6838 | 2013-12-16 10:57:30 +0000 | [diff] [blame] | 53 | help='custom pattern selecting file paths to reformat ' |
Daniel Jasper | db7933a | 2013-12-19 10:21:37 +0000 | [diff] [blame] | 54 | '(case insensitive, overridden by -regex)') |
Daniel Jasper | db125cb | 2015-10-07 17:00:20 +0000 | [diff] [blame] | 55 | parser.add_argument('-sort-includes', action='store_true', default=False, |
| 56 | help='let clang-format sort include blocks') |
Daniel Jasper | c670688 | 2014-11-14 13:27:28 +0000 | [diff] [blame] | 57 | parser.add_argument('-v', '--verbose', action='store_true', |
| 58 | help='be more verbose, ineffective without -i') |
Daniel Jasper | 18b1de3 | 2016-01-20 18:55:57 +0000 | [diff] [blame] | 59 | parser.add_argument('-style', |
| 60 | help='formatting style to apply (LLVM, Google, Chromium, ' |
| 61 | 'Mozilla, WebKit)') |
| 62 | parser.add_argument('-binary', default='clang-format', |
| 63 | help='location of binary to use for clang-format') |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 64 | args = parser.parse_args() |
| 65 | |
Daniel Jasper | dcab7fb | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 66 | # Extract changed lines for each file. |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 67 | filename = None |
Daniel Jasper | dcab7fb | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 68 | lines_by_file = {} |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 69 | for line in sys.stdin: |
| 70 | match = re.search('^\+\+\+\ (.*?/){%s}(\S*)' % args.p, line) |
| 71 | if match: |
| 72 | filename = match.group(2) |
| 73 | if filename == None: |
| 74 | continue |
| 75 | |
Alp Toker | 679bf01 | 2013-12-18 21:34:07 +0000 | [diff] [blame] | 76 | if args.regex is not None: |
| 77 | if not re.match('^%s$' % args.regex, filename): |
Alexander Kornienko | 25c6838 | 2013-12-16 10:57:30 +0000 | [diff] [blame] | 78 | continue |
| 79 | else: |
Alp Toker | 679bf01 | 2013-12-18 21:34:07 +0000 | [diff] [blame] | 80 | if not re.match('^%s$' % args.iregex, filename, re.IGNORECASE): |
Alexander Kornienko | 25c6838 | 2013-12-16 10:57:30 +0000 | [diff] [blame] | 81 | continue |
Daniel Jasper | dcab7fb | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 82 | |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 83 | match = re.search('^@@.*\+(\d+)(,(\d+))?', line) |
| 84 | if match: |
Daniel Jasper | dcab7fb | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 85 | start_line = int(match.group(1)) |
Daniel Jasper | 164c8e1 | 2013-10-02 13:59:03 +0000 | [diff] [blame] | 86 | line_count = 1 |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 87 | if match.group(3): |
Daniel Jasper | 164c8e1 | 2013-10-02 13:59:03 +0000 | [diff] [blame] | 88 | line_count = int(match.group(3)) |
| 89 | if line_count == 0: |
| 90 | continue |
Krasimir Georgiev | cb4dfae | 2018-08-03 10:04:58 +0000 | [diff] [blame] | 91 | end_line = start_line + line_count - 1 |
Daniel Jasper | dcab7fb | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 92 | lines_by_file.setdefault(filename, []).extend( |
| 93 | ['-lines', str(start_line) + ':' + str(end_line)]) |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 94 | |
Daniel Jasper | dcab7fb | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 95 | # Reformat files containing changes in place. |
Krasimir Georgiev | cb4dfae | 2018-08-03 10:04:58 +0000 | [diff] [blame] | 96 | for filename, lines in lines_by_file.items(): |
Daniel Jasper | c670688 | 2014-11-14 13:27:28 +0000 | [diff] [blame] | 97 | if args.i and args.verbose: |
Krasimir Georgiev | cb4dfae | 2018-08-03 10:04:58 +0000 | [diff] [blame] | 98 | print('Formatting {}'.format(filename)) |
Daniel Jasper | 18b1de3 | 2016-01-20 18:55:57 +0000 | [diff] [blame] | 99 | command = [args.binary, filename] |
Alexander Kornienko | 95c009a | 2013-10-11 21:32:01 +0000 | [diff] [blame] | 100 | if args.i: |
| 101 | command.append('-i') |
Daniel Jasper | db125cb | 2015-10-07 17:00:20 +0000 | [diff] [blame] | 102 | if args.sort_includes: |
| 103 | command.append('-sort-includes') |
Daniel Jasper | dcab7fb | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 104 | command.extend(lines) |
| 105 | if args.style: |
Daniel Jasper | cf627f0 | 2013-09-21 10:05:02 +0000 | [diff] [blame] | 106 | command.extend(['-style', args.style]) |
Krasimir Georgiev | cb4dfae | 2018-08-03 10:04:58 +0000 | [diff] [blame] | 107 | p = subprocess.Popen(command, |
| 108 | stdout=subprocess.PIPE, |
| 109 | stderr=None, |
| 110 | stdin=subprocess.PIPE, |
| 111 | universal_newlines=True) |
Daniel Jasper | dcab7fb | 2013-09-18 12:14:09 +0000 | [diff] [blame] | 112 | stdout, stderr = p.communicate() |
Daniel Jasper | e8845ad | 2013-10-08 15:54:36 +0000 | [diff] [blame] | 113 | if p.returncode != 0: |
Krasimir Georgiev | cb4dfae | 2018-08-03 10:04:58 +0000 | [diff] [blame] | 114 | sys.exit(p.returncode) |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 115 | |
Alexander Kornienko | 95c009a | 2013-10-11 21:32:01 +0000 | [diff] [blame] | 116 | if not args.i: |
| 117 | with open(filename) as f: |
| 118 | code = f.readlines() |
Krasimir Georgiev | cb4dfae | 2018-08-03 10:04:58 +0000 | [diff] [blame] | 119 | formatted_code = StringIO(stdout).readlines() |
Alexander Kornienko | 95c009a | 2013-10-11 21:32:01 +0000 | [diff] [blame] | 120 | diff = difflib.unified_diff(code, formatted_code, |
| 121 | filename, filename, |
| 122 | '(before formatting)', '(after formatting)') |
Krasimir Georgiev | cb4dfae | 2018-08-03 10:04:58 +0000 | [diff] [blame] | 123 | diff_string = ''.join(diff) |
Alexander Kornienko | 95c009a | 2013-10-11 21:32:01 +0000 | [diff] [blame] | 124 | if len(diff_string) > 0: |
Alp Toker | fcf3032 | 2013-12-05 08:14:54 +0000 | [diff] [blame] | 125 | sys.stdout.write(diff_string) |
Daniel Jasper | 9be2c5c | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 126 | |
| 127 | if __name__ == '__main__': |
| 128 | main() |