blob: a62cae8ad51b44a52ee33db41af739f765111419 [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')
djasperbe05ebc2013-05-30 11:50:20 +000044 parser.add_argument('-p', default=0,
djasper7f663602013-03-20 09:53:23 +000045 help='strip the smallest prefix containing P slashes')
alexfh39293052013-09-02 16:39:23 +000046 parser.add_argument(
47 '-style',
48 help=
49 'formatting style to apply (LLVM, Google, Chromium, Mozilla, WebKit)')
djasper7f663602013-03-20 09:53:23 +000050 args = parser.parse_args()
51
djasper75c32192013-09-18 12:14:09 +000052 # Extract changed lines for each file.
djasper7f663602013-03-20 09:53:23 +000053 filename = None
djasper75c32192013-09-18 12:14:09 +000054 lines_by_file = {}
djasper7f663602013-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
djasper75c32192013-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
djasper7f663602013-03-20 09:53:23 +000067 match = re.search('^@@.*\+(\d+)(,(\d+))?', line)
68 if match:
djasper75c32192013-09-18 12:14:09 +000069 start_line = int(match.group(1))
djaspere3eb0582013-10-02 13:59:03 +000070 line_count = 1
djasper7f663602013-03-20 09:53:23 +000071 if match.group(3):
djaspere3eb0582013-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;
djasper75c32192013-09-18 12:14:09 +000076 lines_by_file.setdefault(filename, []).extend(
77 ['-lines', str(start_line) + ':' + str(end_line)])
djasper7f663602013-03-20 09:53:23 +000078
djasper75c32192013-09-18 12:14:09 +000079 # Reformat files containing changes in place.
80 for filename, lines in lines_by_file.iteritems():
alexfhc770b672013-10-11 21:32:01 +000081 command = [binary, filename]
82 if args.i:
83 command.append('-i')
djasper75c32192013-09-18 12:14:09 +000084 command.extend(lines)
85 if args.style:
djasperc49abf92013-09-21 10:05:02 +000086 command.extend(['-style', args.style])
djasper75c32192013-09-18 12:14:09 +000087 p = subprocess.Popen(command, stdout=subprocess.PIPE,
alp367218e2013-12-05 08:14:54 +000088 stderr=None, stdin=subprocess.PIPE)
djasper75c32192013-09-18 12:14:09 +000089 stdout, stderr = p.communicate()
djasper60dad2e2013-10-08 15:54:36 +000090 if p.returncode != 0:
91 sys.exit(p.returncode);
djasper7f663602013-03-20 09:53:23 +000092
alexfhc770b672013-10-11 21:32:01 +000093 if not args.i:
94 with open(filename) as f:
95 code = f.readlines()
96 formatted_code = StringIO.StringIO(stdout).readlines()
97 diff = difflib.unified_diff(code, formatted_code,
98 filename, filename,
99 '(before formatting)', '(after formatting)')
100 diff_string = string.join(diff, '')
101 if len(diff_string) > 0:
alp367218e2013-12-05 08:14:54 +0000102 sys.stdout.write(diff_string)
djasper7f663602013-03-20 09:53:23 +0000103
104if __name__ == '__main__':
105 main()