summaryrefslogtreecommitdiffstats
path: root/tools/clang-format
diff options
context:
space:
mode:
Diffstat (limited to 'tools/clang-format')
-rw-r--r--tools/clang-format/CMakeLists.txt3
-rw-r--r--tools/clang-format/ClangFormat.cpp12
-rw-r--r--tools/clang-format/Makefile4
-rwxr-xr-xtools/clang-format/clang-format-diff.py8
-rw-r--r--tools/clang-format/clang-format.el209
-rw-r--r--tools/clang-format/clang-format.py7
-rwxr-xr-xtools/clang-format/git-clang-format2
7 files changed, 181 insertions, 64 deletions
diff --git a/tools/clang-format/CMakeLists.txt b/tools/clang-format/CMakeLists.txt
index f80a3ec..6a24e13 100644
--- a/tools/clang-format/CMakeLists.txt
+++ b/tools/clang-format/CMakeLists.txt
@@ -7,9 +7,8 @@ add_clang_executable(clang-format
target_link_libraries(clang-format
clangBasic
clangFormat
- clangLex
clangRewrite
- clangTooling
+ clangToolingCore
)
install(TARGETS clang-format RUNTIME DESTINATION bin)
diff --git a/tools/clang-format/ClangFormat.cpp b/tools/clang-format/ClangFormat.cpp
index cebb275..d44d407 100644
--- a/tools/clang-format/ClangFormat.cpp
+++ b/tools/clang-format/ClangFormat.cpp
@@ -19,9 +19,9 @@
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/Version.h"
#include "clang/Format/Format.h"
-#include "clang/Lex/Lexer.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "llvm/ADT/StringMap.h"
+#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Signals.h"
@@ -76,7 +76,7 @@ static cl::opt<std::string>
AssumeFilename("assume-filename",
cl::desc("When reading from stdin, clang-format assumes this\n"
"filename to look for a style config file (with\n"
- "-style=file)."),
+ "-style=file) and to determine the language."),
cl::cat(ClangFormatCategory));
static cl::opt<bool> Inplace("i",
@@ -225,12 +225,14 @@ static bool format(StringRef FileName) {
FormatStyle FormatStyle = getStyle(
Style, (FileName == "-") ? AssumeFilename : FileName, FallbackStyle);
- Lexer Lex(ID, Sources.getBuffer(ID), Sources,
- getFormattingLangOpts(FormatStyle.Standard));
- tooling::Replacements Replaces = reformat(FormatStyle, Lex, Sources, Ranges);
+ tooling::Replacements Replaces = reformat(FormatStyle, Sources, ID, Ranges);
if (OutputXML) {
llvm::outs()
<< "<?xml version='1.0'?>\n<replacements xml:space='preserve'>\n";
+ if (Cursor.getNumOccurrences() != 0)
+ llvm::outs() << "<cursor>"
+ << tooling::shiftedCodePosition(Replaces, Cursor)
+ << "</cursor>\n";
for (tooling::Replacements::const_iterator I = Replaces.begin(),
E = Replaces.end();
I != E; ++I) {
diff --git a/tools/clang-format/Makefile b/tools/clang-format/Makefile
index a26ef59..76e31cc 100644
--- a/tools/clang-format/Makefile
+++ b/tools/clang-format/Makefile
@@ -16,9 +16,7 @@ TOOL_NO_EXPORTS = 1
include $(CLANG_LEVEL)/../../Makefile.config
LINK_COMPONENTS := $(TARGETS_TO_BUILD) asmparser bitreader support mc option
-USEDLIBS = clangFormat.a clangTooling.a clangFrontend.a clangSerialization.a \
- clangDriver.a clangParse.a clangSema.a clangAnalysis.a \
- clangRewriteFrontend.a clangRewrite.a clangEdit.a clangAST.a \
+USEDLIBS = clangFormat.a clangToolingCore.a clangDriver.a clangRewrite.a \
clangLex.a clangBasic.a
include $(CLANG_LEVEL)/Makefile
diff --git a/tools/clang-format/clang-format-diff.py b/tools/clang-format/clang-format-diff.py
index d6d0d44..23adb07 100755
--- a/tools/clang-format/clang-format-diff.py
+++ b/tools/clang-format/clang-format-diff.py
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python
#
#===- clang-format-diff.py - ClangFormat Diff Reformatter ----*- python -*--===#
#
@@ -49,9 +49,11 @@ def main():
'(case sensitive, overrides -iregex)')
parser.add_argument('-iregex', metavar='PATTERN', default=
r'.*\.(cpp|cc|c\+\+|cxx|c|cl|h|hpp|m|mm|inc|js|proto'
- r'|protodevel)',
+ r'|protodevel|java)',
help='custom pattern selecting file paths to reformat '
'(case insensitive, overridden by -regex)')
+ parser.add_argument('-v', '--verbose', action='store_true',
+ help='be more verbose, ineffective without -i')
parser.add_argument(
'-style',
help=
@@ -89,6 +91,8 @@ def main():
# Reformat files containing changes in place.
for filename, lines in lines_by_file.iteritems():
+ if args.i and args.verbose:
+ print 'Formatting', filename
command = [binary, filename]
if args.i:
command.append('-i')
diff --git a/tools/clang-format/clang-format.el b/tools/clang-format/clang-format.el
index 520a3e2..ab0991b 100644
--- a/tools/clang-format/clang-format.el
+++ b/tools/clang-format/clang-format.el
@@ -1,56 +1,167 @@
-;;; Clang-format emacs integration for use with C/Objective-C/C++.
+;;; clang-format.el --- Format code using clang-format
-;; This defines a function clang-format-region that you can bind to a key.
-;; A minimal .emacs would contain:
+;; Keywords: tools, c
+;; Package-Requires: ((cl-lib "0.3"))
+
+;;; Commentary:
+
+;; This package allows to filter code through clang-format to fix its formatting.
+;; clang-format is a tool that formats C/C++/Obj-C code according to a set of
+;; style options, see <http://clang.llvm.org/docs/ClangFormatStyleOptions.html>.
+;; Note that clang-format 3.4 or newer is required.
+
+;; clang-format.el is available via MELPA and can be installed via
;;
-;; (load "<path-to-clang>/tools/clang-format/clang-format.el")
-;; (global-set-key [C-M-tab] 'clang-format-region)
+;; M-x package-install clang-format
+;;
+;; when ("melpa" . "http://melpa.org/packages/") is included in
+;; `package-archives'. Alternatively, ensure the directory of this
+;; file is in your `load-path' and add
+;;
+;; (require 'clang-format)
+;;
+;; to your .emacs configuration.
+
+;; You may also want to bind `clang-format-region' to a key:
;;
-;; Depending on your configuration and coding style, you might need to modify
-;; 'style' in clang-format, below.
+;; (global-set-key [C-M-tab] 'clang-format-region)
-(require 'json)
+;;; Code:
-;; *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")
+(require 'cl-lib)
+(require 'xml)
-(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"))
+(defgroup clang-format nil
+ "Format code using clang-format."
+ :group 'tools)
+
+(defcustom clang-format-executable
+ (or (executable-find "clang-format")
+ "clang-format")
+ "Location of the clang-format executable.
+
+A string containing the name or the full path of the executable."
+ :group 'clang-format
+ :type 'string
+ :risky t)
+
+(defcustom clang-format-style "file"
+ "Style argument to pass to clang-format.
+
+By default clang-format will load the style configuration from
+a file named .clang-format located in one of the parent directories
+of the buffer."
+ :group 'clang-format
+ :type 'string
+ :safe #'stringp)
+(make-variable-buffer-local 'clang-format-style)
+
+(defun clang-format--extract (xml-node)
+ "Extract replacements and cursor information from XML-NODE."
+ (unless (and (listp xml-node) (eq (xml-node-name xml-node) 'replacements))
+ (error "Expected <replacements> node"))
+ (let ((nodes (xml-node-children xml-node))
+ replacements
+ cursor)
+ (dolist (node nodes)
+ (when (listp node)
+ (let* ((children (xml-node-children node))
+ (text (car children)))
+ (cl-case (xml-node-name node)
+ ('replacement
+ (let* ((offset (xml-get-attribute-or-nil node 'offset))
+ (length (xml-get-attribute-or-nil node 'length)))
+ (when (or (null offset) (null length))
+ (error "<replacement> node does not have offset and length attributes"))
+ (when (cdr children)
+ (error "More than one child node in <replacement> node"))
+
+ (setq offset (1+ (string-to-number offset)))
+ (setq length (string-to-number length))
+ (push (list offset length text) replacements)))
+ ('cursor
+ (setq cursor (1+ (string-to-number text))))))))
+
+ ;; Sort by decreasing offset, length.
+ (setq replacements (sort (delq nil replacements)
+ (lambda (a b)
+ (or (> (car a) (car b))
+ (and (= (car a) (car b))
+ (> (cadr a) (cadr b)))))))
+
+ (cons replacements cursor)))
+
+(defun clang-format--replace (offset length &optional text)
+ (goto-char offset)
+ (delete-char length)
+ (when text
+ (insert text)))
+
+;;;###autoload
+(defun clang-format-region (start end &optional style)
+ "Use clang-format to format the code between START and END according to STYLE.
+If called interactively uses the region or the current statement if there
+is no active region. If no style is given uses `clang-format-style'."
+ (interactive
+ (if (use-region-p)
+ (list (region-beginning) (region-end))
+ (list (point) (point))))
+
+ (unless style
+ (setq style clang-format-style))
+
+ (let ((temp-buffer (generate-new-buffer " *clang-format-temp*"))
+ (temp-file (make-temp-file "clang-format")))
(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)))))))
+ (let (status stderr operations)
+ (setq status
+ (call-process-region
+ (point-min) (point-max) clang-format-executable
+ nil `(,temp-buffer ,temp-file) nil
+
+ "-output-replacements-xml"
+ "-assume-filename" (or (buffer-file-name) "")
+ "-style" style
+ "-offset" (number-to-string (1- start))
+ "-length" (number-to-string (- end start))
+ "-cursor" (number-to-string (1- (point)))))
+ (setq stderr
+ (with-temp-buffer
+ (insert-file-contents temp-file)
+ (when (> (point-max) (point-min))
+ (insert ": "))
+ (buffer-substring-no-properties
+ (point-min) (line-end-position))))
+
+ (cond
+ ((stringp status)
+ (error "(clang-format killed by signal %s%s)" status stderr))
+ ((not (equal 0 status))
+ (error "(clang-format failed with code %d%s)" status stderr))
+ (t (message "(clang-format succeeded%s)" stderr)))
+
+ (with-current-buffer temp-buffer
+ (setq operations (clang-format--extract (car (xml-parse-region)))))
+
+ (let ((replacements (car operations))
+ (cursor (cdr operations)))
+ (save-excursion
+ (mapc (lambda (rpl)
+ (apply #'clang-format--replace rpl))
+ replacements))
+ (when cursor
+ (goto-char cursor))))
+ (delete-file temp-file)
+ (when (buffer-name temp-buffer) (kill-buffer temp-buffer)))))
+
+;;;###autoload
+(defun clang-format-buffer (&optional style)
+ "Use clang-format to format the current buffer according to STYLE."
+ (interactive)
+ (clang-format-region (point-min) (point-max) style))
+
+;;;###autoload
+(defalias 'clang-format 'clang-format-region)
+
+(provide 'clang-format)
+;;; clang-format.el ends here
diff --git a/tools/clang-format/clang-format.py b/tools/clang-format/clang-format.py
index 16a1879..a79205a 100644
--- a/tools/clang-format/clang-format.py
+++ b/tools/clang-format/clang-format.py
@@ -2,8 +2,8 @@
# - 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
+# map <C-I> :pyf <path-to-this-file>/clang-format.py<cr>
+# imap <C-I> <c-o>:pyf <path-to-this-file>/clang-format.py<cr>
#
# 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
@@ -23,8 +23,11 @@ import subprocess
import sys
import vim
+# set g:clang_format_path to the path to clang-format if it is not on the path
# Change this to the full path if clang-format is not on the path.
binary = 'clang-format'
+if vim.eval('exists("g:clang_format_path")') == "1":
+ binary = vim.eval('g:clang_format_path')
# 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
diff --git a/tools/clang-format/git-clang-format b/tools/clang-format/git-clang-format
index c40b74d..6a0db27 100755
--- a/tools/clang-format/git-clang-format
+++ b/tools/clang-format/git-clang-format
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python
#
#===- git-clang-format - ClangFormat Git Integration ---------*- python -*--===#
#
OpenPOWER on IntegriCloud