blob: ca949b84ef9f46102568b4b4bc8851fabbf2b029 [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
20 git diff -U0 HEAD^ | clang-format-diff.py -p1
21
22"""
23
24import argparse
25import re
26import subprocess
27import sys
28
29
30# Change this to the full path if clang-format is not on the path.
31binary = 'clang-format'
32
33
djasper7f663602013-03-20 09:53:23 +000034def main():
35 parser = argparse.ArgumentParser(description=
alexfh39293052013-09-02 16:39:23 +000036 'Reformat changed lines in diff.')
djasperbe05ebc2013-05-30 11:50:20 +000037 parser.add_argument('-p', default=0,
djasper7f663602013-03-20 09:53:23 +000038 help='strip the smallest prefix containing P slashes')
alexfh39293052013-09-02 16:39:23 +000039 parser.add_argument(
40 '-style',
41 help=
42 'formatting style to apply (LLVM, Google, Chromium, Mozilla, WebKit)')
djasper7f663602013-03-20 09:53:23 +000043 args = parser.parse_args()
44
djasper75c32192013-09-18 12:14:09 +000045 # Extract changed lines for each file.
djasper7f663602013-03-20 09:53:23 +000046 filename = None
djasper75c32192013-09-18 12:14:09 +000047 lines_by_file = {}
djasper7f663602013-03-20 09:53:23 +000048 for line in sys.stdin:
49 match = re.search('^\+\+\+\ (.*?/){%s}(\S*)' % args.p, line)
50 if match:
51 filename = match.group(2)
52 if filename == None:
53 continue
54
djasper75c32192013-09-18 12:14:09 +000055 # FIXME: Add other types containing C++/ObjC code.
56 if not (filename.endswith(".cpp") or filename.endswith(".cc") or
57 filename.endswith(".h")):
58 continue
59
djasper7f663602013-03-20 09:53:23 +000060 match = re.search('^@@.*\+(\d+)(,(\d+))?', line)
61 if match:
djasper75c32192013-09-18 12:14:09 +000062 start_line = int(match.group(1))
djaspere3eb0582013-10-02 13:59:03 +000063 line_count = 1
djasper7f663602013-03-20 09:53:23 +000064 if match.group(3):
djaspere3eb0582013-10-02 13:59:03 +000065 line_count = int(match.group(3))
66 if line_count == 0:
67 continue
68 end_line = start_line + line_count - 1;
djasper75c32192013-09-18 12:14:09 +000069 lines_by_file.setdefault(filename, []).extend(
70 ['-lines', str(start_line) + ':' + str(end_line)])
djasper7f663602013-03-20 09:53:23 +000071
djasper75c32192013-09-18 12:14:09 +000072 # Reformat files containing changes in place.
73 for filename, lines in lines_by_file.iteritems():
74 command = [binary, '-i', filename]
75 command.extend(lines)
76 if args.style:
djasperc49abf92013-09-21 10:05:02 +000077 command.extend(['-style', args.style])
djasper75c32192013-09-18 12:14:09 +000078 p = subprocess.Popen(command, stdout=subprocess.PIPE,
79 stderr=subprocess.PIPE,
80 stdin=subprocess.PIPE)
81 stdout, stderr = p.communicate()
82 if stderr:
83 print stderr
djasper60dad2e2013-10-08 15:54:36 +000084 if p.returncode != 0:
85 sys.exit(p.returncode);
djasper7f663602013-03-20 09:53:23 +000086
87
88if __name__ == '__main__':
89 main()