Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 1 | #!/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 | |
| 12 | r""" |
| 13 | ClangFormat Diff Reformatter |
| 14 | ============================ |
| 15 | |
| 16 | This script reads input from a unified diff and reformats all the changed |
| 17 | lines. This is useful to reformat all the lines touched by a specific patch. |
| 18 | Example usage for git users: |
| 19 | |
| 20 | git diff -U0 HEAD^ | clang-format-diff.py -p1 |
| 21 | |
| 22 | """ |
| 23 | |
| 24 | import argparse |
| 25 | import re |
| 26 | import subprocess |
| 27 | import sys |
| 28 | |
| 29 | |
| 30 | # Change this to the full path if clang-format is not on the path. |
| 31 | binary = 'clang-format' |
| 32 | |
| 33 | |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 34 | def main(): |
| 35 | parser = argparse.ArgumentParser(description= |
Alexander Kornienko | 4e65c98 | 2013-09-02 16:39:23 +0000 | [diff] [blame] | 36 | 'Reformat changed lines in diff.') |
Daniel Jasper | 01970ef | 2013-05-30 11:50:20 +0000 | [diff] [blame] | 37 | parser.add_argument('-p', default=0, |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 38 | help='strip the smallest prefix containing P slashes') |
Alexander Kornienko | 4e65c98 | 2013-09-02 16:39:23 +0000 | [diff] [blame] | 39 | parser.add_argument( |
| 40 | '-style', |
| 41 | help= |
| 42 | 'formatting style to apply (LLVM, Google, Chromium, Mozilla, WebKit)') |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 43 | args = parser.parse_args() |
| 44 | |
Daniel Jasper | 2e118a0 | 2013-09-18 12:14:09 +0000 | [diff] [blame^] | 45 | # Extract changed lines for each file. |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 46 | filename = None |
Daniel Jasper | 2e118a0 | 2013-09-18 12:14:09 +0000 | [diff] [blame^] | 47 | lines_by_file = {} |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 48 | 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 Jasper | 2e118a0 | 2013-09-18 12:14:09 +0000 | [diff] [blame^] | 55 | # 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 Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 60 | match = re.search('^@@.*\+(\d+)(,(\d+))?', line) |
| 61 | if match: |
Daniel Jasper | 2e118a0 | 2013-09-18 12:14:09 +0000 | [diff] [blame^] | 62 | start_line = int(match.group(1)) |
| 63 | end_line = start_line |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 64 | if match.group(3): |
Daniel Jasper | 2e118a0 | 2013-09-18 12:14:09 +0000 | [diff] [blame^] | 65 | end_line = start_line + int(match.group(3)) |
| 66 | lines_by_file.setdefault(filename, []).extend( |
| 67 | ['-lines', str(start_line) + ':' + str(end_line)]) |
Daniel Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 68 | |
Daniel Jasper | 2e118a0 | 2013-09-18 12:14:09 +0000 | [diff] [blame^] | 69 | # 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: |
| 74 | command.extend(['-style', style]) |
| 75 | 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 Jasper | 7c4a9a0 | 2013-03-20 09:53:23 +0000 | [diff] [blame] | 82 | |
| 83 | |
| 84 | if __name__ == '__main__': |
| 85 | main() |