blob: f8fceeb4fc07c9e2f5a8cd00201f46307715812f [file] [log] [blame]
djasper7f663602013-03-20 09:53:23 +00001#!/usr/bin/python
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
12r"""
13ClangFormat Diff Reformatter
14============================
15
16This script reads input from a unified diff and reformats all the changed
17lines. This is useful to reformat all the lines touched by a specific patch.
18Example usage for git users:
19
alexfhc770b672013-10-11 21:32:01 +000020 git diff -U0 HEAD^ | clang-format-diff.py -p1 -i
djasper7f663602013-03-20 09:53:23 +000021
22"""
23
24import argparse
alexfhc770b672013-10-11 21:32:01 +000025import difflib
djasper7f663602013-03-20 09:53:23 +000026import re
alexfhc770b672013-10-11 21:32:01 +000027import string
djasper7f663602013-03-20 09:53:23 +000028import subprocess
alexfhc770b672013-10-11 21:32:01 +000029import StringIO
djasper7f663602013-03-20 09:53:23 +000030import sys
31
32
33# Change this to the full path if clang-format is not on the path.
34binary = 'clang-format'
35
36
djasper7f663602013-03-20 09:53:23 +000037def main():
38 parser = argparse.ArgumentParser(description=
alexfhc770b672013-10-11 21:32:01 +000039 'Reformat changed lines in diff. Without -i '
alp9b6f1cb2013-12-04 00:48:22 +000040 'option just output the diff that would be '
alexfhc770b672013-10-11 21:32:01 +000041 'introduced.')
42 parser.add_argument('-i', action='store_true', default=False,
43 help='apply edits to files instead of displaying a diff')
alpcfbd31f2013-12-10 13:51:53 +000044 parser.add_argument('-p', metavar='NUM', default=0,
djasper7f663602013-03-20 09:53:23 +000045 help='strip the smallest prefix containing P slashes')
alpcfbd31f2013-12-10 13:51:53 +000046 parser.add_argument('-regex', metavar='PATTERN', default=
47 r'.*\.(cpp|cc|CPP|C|c\+\+|cxx|c|h|hpp|m|mm|inc|js)',
48 help='custom pattern selecting file paths to reformat')
alexfh39293052013-09-02 16:39:23 +000049 parser.add_argument(
50 '-style',
51 help=
52 'formatting style to apply (LLVM, Google, Chromium, Mozilla, WebKit)')
djasper7f663602013-03-20 09:53:23 +000053 args = parser.parse_args()
54
djasper75c32192013-09-18 12:14:09 +000055 # Extract changed lines for each file.
djasper7f663602013-03-20 09:53:23 +000056 filename = None
djasper75c32192013-09-18 12:14:09 +000057 lines_by_file = {}
djasper7f663602013-03-20 09:53:23 +000058 for line in sys.stdin:
59 match = re.search('^\+\+\+\ (.*?/){%s}(\S*)' % args.p, line)
60 if match:
61 filename = match.group(2)
62 if filename == None:
63 continue
64
alpcfbd31f2013-12-10 13:51:53 +000065 if not re.match(args.regex, filename):
djasper75c32192013-09-18 12:14:09 +000066 continue
67
djasper7f663602013-03-20 09:53:23 +000068 match = re.search('^@@.*\+(\d+)(,(\d+))?', line)
69 if match:
djasper75c32192013-09-18 12:14:09 +000070 start_line = int(match.group(1))
djaspere3eb0582013-10-02 13:59:03 +000071 line_count = 1
djasper7f663602013-03-20 09:53:23 +000072 if match.group(3):
djaspere3eb0582013-10-02 13:59:03 +000073 line_count = int(match.group(3))
74 if line_count == 0:
75 continue
76 end_line = start_line + line_count - 1;
djasper75c32192013-09-18 12:14:09 +000077 lines_by_file.setdefault(filename, []).extend(
78 ['-lines', str(start_line) + ':' + str(end_line)])
djasper7f663602013-03-20 09:53:23 +000079
djasper75c32192013-09-18 12:14:09 +000080 # Reformat files containing changes in place.
81 for filename, lines in lines_by_file.iteritems():
alexfhc770b672013-10-11 21:32:01 +000082 command = [binary, filename]
83 if args.i:
84 command.append('-i')
djasper75c32192013-09-18 12:14:09 +000085 command.extend(lines)
86 if args.style:
djasperc49abf92013-09-21 10:05:02 +000087 command.extend(['-style', args.style])
djasper75c32192013-09-18 12:14:09 +000088 p = subprocess.Popen(command, stdout=subprocess.PIPE,
alp367218e2013-12-05 08:14:54 +000089 stderr=None, stdin=subprocess.PIPE)
djasper75c32192013-09-18 12:14:09 +000090 stdout, stderr = p.communicate()
djasper60dad2e2013-10-08 15:54:36 +000091 if p.returncode != 0:
92 sys.exit(p.returncode);
djasper7f663602013-03-20 09:53:23 +000093
alexfhc770b672013-10-11 21:32:01 +000094 if not args.i:
95 with open(filename) as f:
96 code = f.readlines()
97 formatted_code = StringIO.StringIO(stdout).readlines()
98 diff = difflib.unified_diff(code, formatted_code,
99 filename, filename,
100 '(before formatting)', '(after formatting)')
101 diff_string = string.join(diff, '')
102 if len(diff_string) > 0:
alp367218e2013-12-05 08:14:54 +0000103 sys.stdout.write(diff_string)
djasper7f663602013-03-20 09:53:23 +0000104
105if __name__ == '__main__':
106 main()