/ contrib / devtools / clang-format-diff.py
clang-format-diff.py
  1  #!/usr/bin/env python3
  2  #
  3  # ===- clang-format-diff.py - ClangFormat Diff Reformatter ----*- python -*--===#
  4  #
  5  # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  6  # See https://llvm.org/LICENSE.txt for license information.
  7  # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  8  #
  9  # ===------------------------------------------------------------------------===#
 10  
 11  """
 12  This script reads input from a unified diff and reformats all the changed
 13  lines. This is useful to reformat all the lines touched by a specific patch.
 14  Example usage for git/svn users:
 15  
 16    git diff -U0 --no-color --relative HEAD^ | {clang_format_diff} -p1 -i
 17    svn diff --diff-cmd=diff -x-U0 | {clang_format_diff} -i
 18  
 19  It should be noted that the filename contained in the diff is used unmodified
 20  to determine the source file to update. Users calling this script directly
 21  should be careful to ensure that the path in the diff is correct relative to the
 22  current working directory.
 23  """
 24  from __future__ import absolute_import, division, print_function
 25  
 26  import argparse
 27  import difflib
 28  import re
 29  import subprocess
 30  import sys
 31  
 32  from io import StringIO
 33  
 34  
 35  def main():
 36      parser = argparse.ArgumentParser(
 37          description=__doc__.format(clang_format_diff="%(prog)s"),
 38          formatter_class=argparse.RawDescriptionHelpFormatter,
 39      )
 40      parser.add_argument(
 41          "-i",
 42          action="store_true",
 43          default=False,
 44          help="apply edits to files instead of displaying a diff",
 45      )
 46      parser.add_argument(
 47          "-p",
 48          metavar="NUM",
 49          default=0,
 50          help="strip the smallest prefix containing P slashes",
 51      )
 52      parser.add_argument(
 53          "-regex",
 54          metavar="PATTERN",
 55          default=None,
 56          help="custom pattern selecting file paths to reformat "
 57          "(case sensitive, overrides -iregex)",
 58      )
 59      parser.add_argument(
 60          "-iregex",
 61          metavar="PATTERN",
 62          default=r".*\.(?:cpp|cc|c\+\+|cxx|cppm|ccm|cxxm|c\+\+m|c|cl|h|hh|hpp"
 63          r"|hxx|m|mm|inc|js|ts|proto|protodevel|java|cs|json|s?vh?)",
 64          help="custom pattern selecting file paths to reformat "
 65          "(case insensitive, overridden by -regex)",
 66      )
 67      parser.add_argument(
 68          "-sort-includes",
 69          action="store_true",
 70          default=False,
 71          help="let clang-format sort include blocks",
 72      )
 73      parser.add_argument(
 74          "-v",
 75          "--verbose",
 76          action="store_true",
 77          help="be more verbose, ineffective without -i",
 78      )
 79      parser.add_argument(
 80          "-style",
 81          help="formatting style to apply (LLVM, GNU, Google, Chromium, "
 82          "Microsoft, Mozilla, WebKit)",
 83      )
 84      parser.add_argument(
 85          "-fallback-style",
 86          help="The name of the predefined style used as a"
 87          "fallback in case clang-format is invoked with"
 88          "-style=file, but can not find the .clang-format"
 89          "file to use.",
 90      )
 91      parser.add_argument(
 92          "-binary",
 93          default="clang-format",
 94          help="location of binary to use for clang-format",
 95      )
 96      args = parser.parse_args()
 97  
 98      # Extract changed lines for each file.
 99      filename = None
100      lines_by_file = {}
101      for line in sys.stdin:
102          match = re.search(r"^\+\+\+\ (.*?/){%s}(\S*)" % args.p, line)
103          if match:
104              filename = match.group(2)
105          if filename is None:
106              continue
107  
108          if args.regex is not None:
109              if not re.match("^%s$" % args.regex, filename):
110                  continue
111          else:
112              if not re.match("^%s$" % args.iregex, filename, re.IGNORECASE):
113                  continue
114  
115          match = re.search(r"^@@.*\+(\d+)(?:,(\d+))?", line)
116          if match:
117              start_line = int(match.group(1))
118              line_count = 1
119              if match.group(2):
120                  line_count = int(match.group(2))
121                  # The input is something like
122                  #
123                  # @@ -1, +0,0 @@
124                  #
125                  # which means no lines were added.
126                  if line_count == 0:
127                      continue
128              # Also format lines range if line_count is 0 in case of deleting
129              # surrounding statements.
130              end_line = start_line
131              if line_count != 0:
132                  end_line += line_count - 1
133              lines_by_file.setdefault(filename, []).extend(
134                  ["-lines", str(start_line) + ":" + str(end_line)]
135              )
136  
137      # Reformat files containing changes in place.
138      for filename, lines in lines_by_file.items():
139          if args.i and args.verbose:
140              print("Formatting {}".format(filename))
141          command = [args.binary, filename]
142          if args.i:
143              command.append("-i")
144          if args.sort_includes:
145              command.append("-sort-includes")
146          command.extend(lines)
147          if args.style:
148              command.extend(["-style", args.style])
149          if args.fallback_style:
150              command.extend(["-fallback-style", args.fallback_style])
151  
152          try:
153              p = subprocess.Popen(
154                  command,
155                  stdout=subprocess.PIPE,
156                  stderr=None,
157                  stdin=subprocess.PIPE,
158                  universal_newlines=True,
159              )
160          except OSError as e:
161              # Give the user more context when clang-format isn't
162              # found/isn't executable, etc.
163              raise RuntimeError(
164                  'Failed to run "%s" - %s"' % (" ".join(command), e.strerror)
165              )
166  
167          stdout, _stderr = p.communicate()
168          if p.returncode != 0:
169              sys.exit(p.returncode)
170  
171          if not args.i:
172              with open(filename) as f:
173                  code = f.readlines()
174              formatted_code = StringIO(stdout).readlines()
175              diff = difflib.unified_diff(
176                  code,
177                  formatted_code,
178                  filename,
179                  filename,
180                  "(before formatting)",
181                  "(after formatting)",
182              )
183              diff_string = "".join(diff)
184              if len(diff_string) > 0:
185                  sys.stdout.write(diff_string)
186                  sys.exit(1)
187  
188  
189  if __name__ == "__main__":
190      main()