blob: 60b8fb730dcb83c8b32b2088d8c6aa9bd8490bc9 [file] [log] [blame]
Daniel Jasper7c4a9a02013-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
Alexander Kornienkocf207a62013-10-11 21:32:01 +000020 git diff -U0 HEAD^ | clang-format-diff.py -p1 -i
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000021
22"""
23
24import argparse
Alexander Kornienkocf207a62013-10-11 21:32:01 +000025import difflib
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000026import re
Alexander Kornienkocf207a62013-10-11 21:32:01 +000027import string
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000028import subprocess
Alexander Kornienkocf207a62013-10-11 21:32:01 +000029import StringIO
Daniel Jasper7c4a9a02013-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
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000037def main():
38 parser = argparse.ArgumentParser(description=
Alexander Kornienkocf207a62013-10-11 21:32:01 +000039 'Reformat changed lines in diff. Without -i '
40 'option just output the diff that would be'
41 'introduced.')
42 parser.add_argument('-i', action='store_true', default=False,
43 help='apply edits to files instead of displaying a diff')
Daniel Jasper01970ef2013-05-30 11:50:20 +000044 parser.add_argument('-p', default=0,
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000045 help='strip the smallest prefix containing P slashes')
Alexander Kornienko4e65c982013-09-02 16:39:23 +000046 parser.add_argument(
47 '-style',
48 help=
49 'formatting style to apply (LLVM, Google, Chromium, Mozilla, WebKit)')
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000050 args = parser.parse_args()
51
Daniel Jasper2e118a02013-09-18 12:14:09 +000052 # Extract changed lines for each file.
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000053 filename = None
Daniel Jasper2e118a02013-09-18 12:14:09 +000054 lines_by_file = {}
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000055 for line in sys.stdin:
56 match = re.search('^\+\+\+\ (.*?/){%s}(\S*)' % args.p, line)
57 if match:
58 filename = match.group(2)
59 if filename == None:
60 continue
61
Daniel Jasper2e118a02013-09-18 12:14:09 +000062 # FIXME: Add other types containing C++/ObjC code.
63 if not (filename.endswith(".cpp") or filename.endswith(".cc") or
64 filename.endswith(".h")):
65 continue
66
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000067 match = re.search('^@@.*\+(\d+)(,(\d+))?', line)
68 if match:
Daniel Jasper2e118a02013-09-18 12:14:09 +000069 start_line = int(match.group(1))
Daniel Jasper4a966d32013-10-02 13:59:03 +000070 line_count = 1
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000071 if match.group(3):
Daniel Jasper4a966d32013-10-02 13:59:03 +000072 line_count = int(match.group(3))
73 if line_count == 0:
74 continue
75 end_line = start_line + line_count - 1;
Daniel Jasper2e118a02013-09-18 12:14:09 +000076 lines_by_file.setdefault(filename, []).extend(
77 ['-lines', str(start_line) + ':' + str(end_line)])
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000078
Daniel Jasper2e118a02013-09-18 12:14:09 +000079 # Reformat files containing changes in place.
80 for filename, lines in lines_by_file.iteritems():
Alexander Kornienkocf207a62013-10-11 21:32:01 +000081 command = [binary, filename]
82 if args.i:
83 command.append('-i')
Daniel Jasper2e118a02013-09-18 12:14:09 +000084 command.extend(lines)
85 if args.style:
Daniel Jasper68455be2013-09-21 10:05:02 +000086 command.extend(['-style', args.style])
Daniel Jasper2e118a02013-09-18 12:14:09 +000087 p = subprocess.Popen(command, stdout=subprocess.PIPE,
88 stderr=subprocess.PIPE,
89 stdin=subprocess.PIPE)
90 stdout, stderr = p.communicate()
91 if stderr:
92 print stderr
Daniel Jasperf7d9e632013-10-08 15:54:36 +000093 if p.returncode != 0:
94 sys.exit(p.returncode);
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000095
Alexander Kornienkocf207a62013-10-11 21:32:01 +000096 if not args.i:
97 with open(filename) as f:
98 code = f.readlines()
99 formatted_code = StringIO.StringIO(stdout).readlines()
100 diff = difflib.unified_diff(code, formatted_code,
101 filename, filename,
102 '(before formatting)', '(after formatting)')
103 diff_string = string.join(diff, '')
104 if len(diff_string) > 0:
105 print diff_string
Daniel Jasper7c4a9a02013-03-20 09:53:23 +0000106
107if __name__ == '__main__':
108 main()