blob: 7c141785a3256137f257653297c624845f4f7910 [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
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
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000034def main():
35 parser = argparse.ArgumentParser(description=
Alexander Kornienko4e65c982013-09-02 16:39:23 +000036 'Reformat changed lines in diff.')
Daniel Jasper01970ef2013-05-30 11:50:20 +000037 parser.add_argument('-p', default=0,
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000038 help='strip the smallest prefix containing P slashes')
Alexander Kornienko4e65c982013-09-02 16:39:23 +000039 parser.add_argument(
40 '-style',
41 help=
42 'formatting style to apply (LLVM, Google, Chromium, Mozilla, WebKit)')
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000043 args = parser.parse_args()
44
Daniel Jasper2e118a02013-09-18 12:14:09 +000045 # Extract changed lines for each file.
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000046 filename = None
Daniel Jasper2e118a02013-09-18 12:14:09 +000047 lines_by_file = {}
Daniel Jasper7c4a9a02013-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
Daniel Jasper2e118a02013-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
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000060 match = re.search('^@@.*\+(\d+)(,(\d+))?', line)
61 if match:
Daniel Jasper2e118a02013-09-18 12:14:09 +000062 start_line = int(match.group(1))
63 end_line = start_line
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000064 if match.group(3):
Daniel Jasper2e118a02013-09-18 12:14:09 +000065 end_line = start_line + int(match.group(3))
66 lines_by_file.setdefault(filename, []).extend(
67 ['-lines', str(start_line) + ':' + str(end_line)])
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000068
Daniel Jasper2e118a02013-09-18 12:14:09 +000069 # Reformat files containing changes in place.
70 for filename, lines in lines_by_file.iteritems():
71 command = [binary, '-i', filename]
72 command.extend(lines)
73 if args.style:
Daniel Jasper68455be2013-09-21 10:05:02 +000074 command.extend(['-style', args.style])
Daniel Jasper2e118a02013-09-18 12:14:09 +000075 p = subprocess.Popen(command, stdout=subprocess.PIPE,
76 stderr=subprocess.PIPE,
77 stdin=subprocess.PIPE)
78 stdout, stderr = p.communicate()
79 if stderr:
80 print stderr
81 return
Daniel Jasper7c4a9a02013-03-20 09:53:23 +000082
83
84if __name__ == '__main__':
85 main()