diff options
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | DEPS | 34 | ||||
-rw-r--r-- | third_party/clang_format/README.chromium | 15 | ||||
-rw-r--r-- | third_party/clang_format/README.txt | 28 | ||||
-rw-r--r-- | third_party/clang_format/bin/linux/clang-format.sha1 | 1 | ||||
-rw-r--r-- | third_party/clang_format/bin/mac/clang-format.sha1 | 1 | ||||
-rw-r--r-- | third_party/clang_format/bin/win/clang-format.exe.sha1 | 1 | ||||
-rw-r--r-- | third_party/clang_format/scripts/clang-format-bbedit.applescript | 27 | ||||
-rwxr-xr-x | third_party/clang_format/scripts/clang-format-diff.py | 114 | ||||
-rw-r--r-- | third_party/clang_format/scripts/clang-format-sublime.py | 58 | ||||
-rw-r--r-- | third_party/clang_format/scripts/clang-format.el | 56 | ||||
-rw-r--r-- | third_party/clang_format/scripts/clang-format.py | 79 | ||||
-rwxr-xr-x | tools/checklicenses/checklicenses.py | 5 |
13 files changed, 421 insertions, 0 deletions
@@ -198,6 +198,8 @@ v8.log /third_party/chromeos_login_manager /third_party/chromeos_text_input /third_party/chromite +/third_party/clang_format/bin/*/* +!/third_party/clang_format/bin/*/*.sha1 /third_party/cld_2/src /third_party/cros /third_party/cros_dbus_cplusplus/source @@ -706,6 +706,40 @@ hooks = [ "-s", "src/tools/gn/bin/linux/gn32.sha1", ], }, + # Pull clang-format binaries using checked-in hashes. + { + "name": "clang_format_win", + "pattern": "src/third_party/clang_format/bin/win/clang-format.exe.sha1", + "action": [ "download_from_google_storage", + "--no_resume", + "--platform=win32", + "--no_auth", + "--bucket", "chromium-clang-format", + "-s", "src/third_party/clang_format/bin/win/clang-format.exe.sha1", + ], + }, + { + "name": "clang_format_mac", + "pattern": "src/third_party/clang_format/bin/mac/clang-format.sha1", + "action": [ "download_from_google_storage", + "--no_resume", + "--platform=darwin", + "--no_auth", + "--bucket", "chromium-clang-format", + "-s", "src/third_party/clang_format/bin/mac/clang-format.sha1", + ], + }, + { + "name": "clang_format_linux", + "pattern": "src/third_party/clang_format/bin/linux/clang-format.sha1", + "action": [ "download_from_google_storage", + "--no_resume", + "--platform=linux*", + "--no_auth", + "--bucket", "chromium-clang-format", + "-s", "src/third_party/clang_format/bin/linux/clang-format.sha1", + ], + }, { # A change to a .gyp, .gypi, or to GYP itself should run the generator. "name": "gyp", diff --git a/third_party/clang_format/README.chromium b/third_party/clang_format/README.chromium new file mode 100644 index 0000000..103b5ea --- /dev/null +++ b/third_party/clang_format/README.chromium @@ -0,0 +1,15 @@ +Name: clang-format +Short Name: clang-format +URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-format/ +Version: 3.5 +Date: 8 January 2014 +Revision: 198831 +License: University of Illinois/NCSA Open Source License +License File: NOT_SHIPPED +Security Critical: No + +Description: +A tool for formatting C++ code to style. + +Local Modifications: +None diff --git a/third_party/clang_format/README.txt b/third_party/clang_format/README.txt new file mode 100644 index 0000000..b042487 --- /dev/null +++ b/third_party/clang_format/README.txt @@ -0,0 +1,28 @@ +This folder contains clang-format binaries. The binaries will be automatically +downloaded from Google Storage by gclient runhooks for the current platform. + +For a walkthrough on how to maintain these binaries: + https://code.google.com/p/chromium/wiki/UpdatingClangFormatBinaries + +To upload a file: + python ~/depot_tools/upload_to_google_storage.py -b chromium-clang-format <FILENAME> + +To download a file given a .sha1 file: + python ~/depot_tools/download_from_google_storage.py -b chromium-clang-format -s <FILENAME>.sha1 + +List the contents of GN's Google Storage bucket: + python ~/depot_tools/third_party/gsutil/gsutil ls gs://chromium-clang-format/ + +To initialize gsutil's credentials: + python ~/depot_tools/third_party/gsutil/gsutil config + + That will give a URL which you should log into with your web browser. The + username should be the one that is on the ACL for the "chromium-clang-format" + bucket (probably your @google.com address). Contact the build team for help + getting access if necessary. + + Copy the code back to the command line util. Ignore the project ID (it's OK + to just leave blank when prompted). + +gsutil documentation: + https://developers.google.com/storage/docs/gsutil diff --git a/third_party/clang_format/bin/linux/clang-format.sha1 b/third_party/clang_format/bin/linux/clang-format.sha1 new file mode 100644 index 0000000..7229213 --- /dev/null +++ b/third_party/clang_format/bin/linux/clang-format.sha1 @@ -0,0 +1 @@ +ee0117f00e70c8cbaa780fc95b55754fc5a3bd06
\ No newline at end of file diff --git a/third_party/clang_format/bin/mac/clang-format.sha1 b/third_party/clang_format/bin/mac/clang-format.sha1 new file mode 100644 index 0000000..743ff4e --- /dev/null +++ b/third_party/clang_format/bin/mac/clang-format.sha1 @@ -0,0 +1 @@ +bc7ce933f91ecf1ef08284421b1ff9617fad5d9a
\ No newline at end of file diff --git a/third_party/clang_format/bin/win/clang-format.exe.sha1 b/third_party/clang_format/bin/win/clang-format.exe.sha1 new file mode 100644 index 0000000..7aab6547 --- /dev/null +++ b/third_party/clang_format/bin/win/clang-format.exe.sha1 @@ -0,0 +1 @@ +ab07bc6cc1f91c920da95603a63cc8fdfc2f33d8
\ No newline at end of file diff --git a/third_party/clang_format/scripts/clang-format-bbedit.applescript b/third_party/clang_format/scripts/clang-format-bbedit.applescript new file mode 100644 index 0000000..fa88fe9 --- /dev/null +++ b/third_party/clang_format/scripts/clang-format-bbedit.applescript @@ -0,0 +1,27 @@ +-- In this file, change "/path/to/" to the path where you installed clang-format +-- and save it to ~/Library/Application Support/BBEdit/Scripts. You can then +-- select the script from the Script menu and clang-format will format the +-- selection. Note that you can rename the menu item by renaming the script, and +-- can assign the menu item a keyboard shortcut in the BBEdit preferences, under +-- Menus & Shortcuts. +on urlToPOSIXPath(theURL) + return do shell script "python -c \"import urllib, urlparse, sys; print urllib.unquote(urlparse.urlparse(sys.argv[1])[2])\" " & quoted form of theURL +end urlToPOSIXPath + +tell application "BBEdit" + set selectionOffset to characterOffset of selection + set selectionLength to length of selection + set fileURL to URL of text document 1 +end tell + +set filePath to urlToPOSIXPath(fileURL) +set newContents to do shell script "/path/to/clang-format -offset=" & selectionOffset & " -length=" & selectionLength & " " & quoted form of filePath + +tell application "BBEdit" + -- "set contents of text document 1 to newContents" scrolls to the bottom while + -- replacing a selection flashes a bit but doesn't affect the scroll position. + set currentLength to length of contents of text document 1 + select characters 1 thru currentLength of text document 1 + set text of selection to newContents + select characters selectionOffset thru (selectionOffset + selectionLength - 1) of text document 1 +end tell diff --git a/third_party/clang_format/scripts/clang-format-diff.py b/third_party/clang_format/scripts/clang-format-diff.py new file mode 100755 index 0000000..d575a8a --- /dev/null +++ b/third_party/clang_format/scripts/clang-format-diff.py @@ -0,0 +1,114 @@ +#!/usr/bin/python +# +#===- clang-format-diff.py - ClangFormat Diff Reformatter ----*- python -*--===# +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +#===------------------------------------------------------------------------===# + +r""" +ClangFormat Diff Reformatter +============================ + +This script reads input from a unified diff and reformats all the changed +lines. This is useful to reformat all the lines touched by a specific patch. +Example usage for git users: + + git diff -U0 HEAD^ | clang-format-diff.py -p1 -i + +""" + +import argparse +import difflib +import re +import string +import subprocess +import StringIO +import sys + + +# Change this to the full path if clang-format is not on the path. +binary = 'clang-format' + + +def main(): + parser = argparse.ArgumentParser(description= + 'Reformat changed lines in diff. Without -i ' + 'option just output the diff that would be ' + 'introduced.') + parser.add_argument('-i', action='store_true', default=False, + help='apply edits to files instead of displaying a diff') + parser.add_argument('-p', metavar='NUM', default=0, + help='strip the smallest prefix containing P slashes') + parser.add_argument('-regex', metavar='PATTERN', default=None, + help='custom pattern selecting file paths to reformat ' + '(case sensitive, overrides -iregex)') + parser.add_argument('-iregex', metavar='PATTERN', default= + r'.*\.(cpp|cc|c\+\+|cxx|c|cl|h|hpp|m|mm|inc|js)', + help='custom pattern selecting file paths to reformat ' + '(case insensitive, overridden by -regex)') + parser.add_argument( + '-style', + help= + 'formatting style to apply (LLVM, Google, Chromium, Mozilla, WebKit)') + args = parser.parse_args() + + # Extract changed lines for each file. + filename = None + lines_by_file = {} + for line in sys.stdin: + match = re.search('^\+\+\+\ (.*?/){%s}(\S*)' % args.p, line) + if match: + filename = match.group(2) + if filename == None: + continue + + if args.regex is not None: + if not re.match('^%s$' % args.regex, filename): + continue + else: + if not re.match('^%s$' % args.iregex, filename, re.IGNORECASE): + continue + + match = re.search('^@@.*\+(\d+)(,(\d+))?', line) + if match: + start_line = int(match.group(1)) + line_count = 1 + if match.group(3): + line_count = int(match.group(3)) + if line_count == 0: + continue + end_line = start_line + line_count - 1; + lines_by_file.setdefault(filename, []).extend( + ['-lines', str(start_line) + ':' + str(end_line)]) + + # Reformat files containing changes in place. + for filename, lines in lines_by_file.iteritems(): + command = [binary, filename] + if args.i: + command.append('-i') + command.extend(lines) + if args.style: + command.extend(['-style', args.style]) + p = subprocess.Popen(command, stdout=subprocess.PIPE, + stderr=None, stdin=subprocess.PIPE) + stdout, stderr = p.communicate() + if p.returncode != 0: + sys.exit(p.returncode); + + if not args.i: + with open(filename) as f: + code = f.readlines() + formatted_code = StringIO.StringIO(stdout).readlines() + diff = difflib.unified_diff(code, formatted_code, + filename, filename, + '(before formatting)', '(after formatting)') + diff_string = string.join(diff, '') + if len(diff_string) > 0: + sys.stdout.write(diff_string) + +if __name__ == '__main__': + main() diff --git a/third_party/clang_format/scripts/clang-format-sublime.py b/third_party/clang_format/scripts/clang-format-sublime.py new file mode 100644 index 0000000..16ff56e --- /dev/null +++ b/third_party/clang_format/scripts/clang-format-sublime.py @@ -0,0 +1,58 @@ +# This file is a minimal clang-format sublime-integration. To install: +# - Change 'binary' if clang-format is not on the path (see below). +# - Put this file into your sublime Packages directory, e.g. on Linux: +# ~/.config/sublime-text-2/Packages/User/clang-format-sublime.py +# - Add a key binding: +# { "keys": ["ctrl+shift+c"], "command": "clang_format" }, +# +# With this integration you can press the bound key and clang-format will +# format the current lines and selections for all cursor positions. The lines +# or regions are extended to the next bigger syntactic entities. +# +# It operates on the current, potentially unsaved buffer and does not create +# or save any files. To revert a formatting, just undo. + +from __future__ import print_function +import sublime +import sublime_plugin +import subprocess + +# Change this to the full path if clang-format is not on the path. +binary = 'clang-format' + +# Change this to format according to other formatting styles. See the output of +# 'clang-format --help' for a list of supported styles. The default looks for +# a '.clang-format' or '_clang-format' file to indicate the style that should be +# used. +style = 'file' + +class ClangFormatCommand(sublime_plugin.TextCommand): + def run(self, edit): + encoding = self.view.encoding() + if encoding == 'Undefined': + encoding = 'utf-8' + regions = [] + command = [binary, '-style', style] + for region in self.view.sel(): + regions.append(region) + region_offset = min(region.a, region.b) + region_length = abs(region.b - region.a) + command.extend(['-offset', str(region_offset), + '-length', str(region_length), + '-assume-filename', str(self.view.file_name())]) + old_viewport_position = self.view.viewport_position() + buf = self.view.substr(sublime.Region(0, self.view.size())) + p = subprocess.Popen(command, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, stdin=subprocess.PIPE) + output, error = p.communicate(buf.encode(encoding)) + if error: + print(error) + self.view.replace( + edit, sublime.Region(0, self.view.size()), + output.decode(encoding)) + self.view.sel().clear() + for region in regions: + self.view.sel().add(region) + # FIXME: Without the 10ms delay, the viewport sometimes jumps. + sublime.set_timeout(lambda: self.view.set_viewport_position( + old_viewport_position, False), 10) diff --git a/third_party/clang_format/scripts/clang-format.el b/third_party/clang_format/scripts/clang-format.el new file mode 100644 index 0000000..520a3e2 --- /dev/null +++ b/third_party/clang_format/scripts/clang-format.el @@ -0,0 +1,56 @@ +;;; Clang-format emacs integration for use with C/Objective-C/C++. + +;; This defines a function clang-format-region that you can bind to a key. +;; A minimal .emacs would contain: +;; +;; (load "<path-to-clang>/tools/clang-format/clang-format.el") +;; (global-set-key [C-M-tab] 'clang-format-region) +;; +;; Depending on your configuration and coding style, you might need to modify +;; 'style' in clang-format, below. + +(require 'json) + +;; *Location of the clang-format binary. If it is on your PATH, a full path name +;; need not be specified. +(defvar clang-format-binary "clang-format") + +(defun clang-format-region () + "Use clang-format to format the currently active region." + (interactive) + (let ((beg (if mark-active + (region-beginning) + (min (line-beginning-position) (1- (point-max))))) + (end (if mark-active + (region-end) + (line-end-position)))) + (clang-format beg end))) + +(defun clang-format-buffer () + "Use clang-format to format the current buffer." + (interactive) + (clang-format (point-min) (point-max))) + +(defun clang-format (begin end) + "Use clang-format to format the code between BEGIN and END." + (let* ((orig-windows (get-buffer-window-list (current-buffer))) + (orig-window-starts (mapcar #'window-start orig-windows)) + (orig-point (point)) + (style "file")) + (unwind-protect + (call-process-region (point-min) (point-max) clang-format-binary + t (list t nil) nil + "-offset" (number-to-string (1- begin)) + "-length" (number-to-string (- end begin)) + "-cursor" (number-to-string (1- (point))) + "-assume-filename" (buffer-file-name) + "-style" style) + (goto-char (point-min)) + (let ((json-output (json-read-from-string + (buffer-substring-no-properties + (point-min) (line-beginning-position 2))))) + (delete-region (point-min) (line-beginning-position 2)) + (goto-char (1+ (cdr (assoc 'Cursor json-output)))) + (dotimes (index (length orig-windows)) + (set-window-start (nth index orig-windows) + (nth index orig-window-starts))))))) diff --git a/third_party/clang_format/scripts/clang-format.py b/third_party/clang_format/scripts/clang-format.py new file mode 100644 index 0000000..f5a5756 --- /dev/null +++ b/third_party/clang_format/scripts/clang-format.py @@ -0,0 +1,79 @@ +# This file is a minimal clang-format vim-integration. To install: +# - Change 'binary' if clang-format is not on the path (see below). +# - Add to your .vimrc: +# +# map <C-I> :pyf <path-to-this-file>/clang-format.py<CR> +# imap <C-I> <ESC>:pyf <path-to-this-file>/clang-format.py<CR>i +# +# The first line enables clang-format for NORMAL and VISUAL mode, the second +# line adds support for INSERT mode. Change "C-I" to another binding if you +# need clang-format on a different key (C-I stands for Ctrl+i). +# +# With this integration you can press the bound key and clang-format will +# format the current line in NORMAL and INSERT mode or the selected region in +# VISUAL mode. The line or region is extended to the next bigger syntactic +# entity. +# +# It operates on the current, potentially unsaved buffer and does not create +# or save any files. To revert a formatting, just undo. + +import difflib +import json +import subprocess +import sys +import vim + +# Change this to the full path if clang-format is not on the path. +binary = 'clang-format' + +# Change this to format according to other formatting styles. See the output of +# 'clang-format --help' for a list of supported styles. The default looks for +# a '.clang-format' or '_clang-format' file to indicate the style that should be +# used. +style = 'file' + +# Get the current text. +buf = vim.current.buffer +text = '\n'.join(buf) + +# Determine range to format. +cursor = int(vim.eval('line2byte(line("."))+col(".")')) - 2 +lines = '%s:%s' % (vim.current.range.start + 1, vim.current.range.end + 1) + +# Avoid flashing an ugly, ugly cmd prompt on Windows when invoking clang-format. +startupinfo = None +if sys.platform.startswith('win32'): + startupinfo = subprocess.STARTUPINFO() + startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW + startupinfo.wShowWindow = subprocess.SW_HIDE + +# Call formatter. +command = [binary, '-lines', lines, '-style', style, '-cursor', str(cursor)] +if vim.current.buffer.name: + command.extend(['-assume-filename', vim.current.buffer.name]) +p = subprocess.Popen(command, + stdout=subprocess.PIPE, stderr=subprocess.PIPE, + stdin=subprocess.PIPE, startupinfo=startupinfo) +stdout, stderr = p.communicate(input=text) + +# If successful, replace buffer contents. +if stderr: + message = stderr.splitlines()[0] + parts = message.split(' ', 2) + if len(parts) > 2: + message = parts[2] + print 'Formatting failed: %s (total %d warnings, %d errors)' % ( + message, stderr.count('warning:'), stderr.count('error:')) + +if not stdout: + print ('No output from clang-format (crashed?).\n' + + 'Please report to bugs.llvm.org.') +else: + lines = stdout.split('\n') + output = json.loads(lines[0]) + lines = lines[1:] + sequence = difflib.SequenceMatcher(None, vim.current.buffer, lines) + for op in reversed(sequence.get_opcodes()): + if op[0] is not 'equal': + vim.current.buffer[op[1]:op[2]] = lines[op[3]:op[4]] + vim.command('goto %d' % (output['Cursor'] + 1)) diff --git a/tools/checklicenses/checklicenses.py b/tools/checklicenses/checklicenses.py index 16fc1ac..d3918ac7 100755 --- a/tools/checklicenses/checklicenses.py +++ b/tools/checklicenses/checklicenses.py @@ -165,6 +165,11 @@ PATH_SPECIFIC_WHITELISTED_LICENSES = { 'UNKNOWN', ], + # http://crbug.com/333508 + 'third_party/clang_format/scripts': [ + 'UNKNOWN', + ], + # Not used. http://crbug.com/156020 # Using third_party/cros_dbus_cplusplus/cros_dbus_cplusplus.gyp instead. 'third_party/cros_dbus_cplusplus/source/autogen.sh': [ |