summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorPhilipp Stephani <phst@google.com>2017-12-02 21:18:14 +0000
committerPhilipp Stephani <phst@google.com>2017-12-02 21:18:14 +0000
commit5a0ab09ed86ed4bc944b76d1bd4f2f784a94464f (patch)
tree6ad099693d76f2ba1b509ad283387989bb95fe2c /tools
parentbf691ab9cdc04546974fa94ec6dd5d7cf2bd0fa6 (diff)
Fix assume-filename handling in clang-format.el
Summary: When 'buffer-file-name' is nil 'call-process-region' returned a segmentation fault error. This was a problem when using clang-format-buffer on an orgmode source code editing buffer. I fixed this problem by excluding the '-assume-filename' argument when 'buffer-file-name' is nil. To make it a bit more flexible I also added an optional argument, 'assume-file-name', to specify an assume-filename that overrides 'buffer-file-name'. Reviewers: klimek, djasper, phst, phi Reviewed By: phst, phi Subscribers: phi, jholewinski, mgorny, javed.absar, eraman, cfe-commits Differential Revision: https://reviews.llvm.org/D37903 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@319621 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools')
-rw-r--r--tools/clang-format/clang-format.el45
1 files changed, 29 insertions, 16 deletions
diff --git a/tools/clang-format/clang-format.el b/tools/clang-format/clang-format.el
index aa9c3ff4ca..6c626e0b83 100644
--- a/tools/clang-format/clang-format.el
+++ b/tools/clang-format/clang-format.el
@@ -119,10 +119,12 @@ is a zero-based file offset, assuming ‘utf-8-unix’ coding."
(byte-to-position (1+ byte)))))
;;;###autoload
-(defun clang-format-region (start end &optional style)
+(defun clang-format-region (start end &optional style assume-file-name)
"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'."
+If called interactively uses the region or the current statement if there is no
+no active region. If no STYLE is given uses `clang-format-style'. Use
+ASSUME-FILE-NAME to locate a style config file, if no ASSUME-FILE-NAME is given
+uses the function `buffer-file-name'."
(interactive
(if (use-region-p)
(list (region-beginning) (region-end))
@@ -131,6 +133,9 @@ is no active region. If no style is given uses `clang-format-style'."
(unless style
(setq style clang-format-style))
+ (unless assume-file-name
+ (setq assume-file-name buffer-file-name))
+
(let ((file-start (clang-format--bufferpos-to-filepos start 'approximate
'utf-8-unix))
(file-end (clang-format--bufferpos-to-filepos end 'approximate
@@ -144,16 +149,21 @@ is no active region. If no style is given uses `clang-format-style'."
;; always use ‘utf-8-unix’ and ignore the buffer coding system.
(default-process-coding-system '(utf-8-unix . utf-8-unix)))
(unwind-protect
- (let ((status (call-process-region
- nil nil clang-format-executable
- nil `(,temp-buffer ,temp-file) nil
-
- "-output-replacements-xml"
- "-assume-filename" (or (buffer-file-name) "")
- "-style" style
- "-offset" (number-to-string file-start)
- "-length" (number-to-string (- file-end file-start))
- "-cursor" (number-to-string cursor)))
+ (let ((status (apply #'call-process-region
+ nil nil clang-format-executable
+ nil `(,temp-buffer ,temp-file) nil
+ `("-output-replacements-xml"
+ ;; Gaurd against a nil assume-file-name.
+ ;; If the clang-format option -assume-filename
+ ;; is given a blank string it will crash as per
+ ;; the following bug report
+ ;; https://bugs.llvm.org/show_bug.cgi?id=34667
+ ,@(and assume-file-name
+ (list "-assume-filename" assume-file-name))
+ "-style" ,style
+ "-offset" ,(number-to-string file-start)
+ "-length" ,(number-to-string (- file-end file-start))
+ "-cursor" ,(number-to-string cursor))))
(stderr (with-temp-buffer
(unless (zerop (cadr (insert-file-contents temp-file)))
(insert ": "))
@@ -181,10 +191,13 @@ is no active region. If no style is given uses `clang-format-style'."
(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."
+(defun clang-format-buffer (&optional style assume-file-name)
+ "Use clang-format to format the current buffer according to STYLE.
+If no STYLE is given uses `clang-format-style'. Use ASSUME-FILE-NAME
+to locate a style config file. If no ASSUME-FILE-NAME is given uses
+the function `buffer-file-name'."
(interactive)
- (clang-format-region (point-min) (point-max) style))
+ (clang-format-region (point-min) (point-max) style assume-file-name))
;;;###autoload
(defalias 'clang-format 'clang-format-region)