[PATCH] D37903: Fix assume-filename handling in clang-format.el

Micah Werbitt via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 15 07:39:29 PDT 2017


werbitt created this revision.

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', to specify an assume-filename that overrides 'buffer-file-name'.


https://reviews.llvm.org/D37903

Files:
  tools/clang-format/clang-format.el


Index: tools/clang-format/clang-format.el
===================================================================
--- tools/clang-format/clang-format.el
+++ tools/clang-format/clang-format.el
@@ -119,7 +119,7 @@
       (byte-to-position (1+ byte)))))
 
 ;;;###autoload
-(defun clang-format-region (start end &optional style)
+(defun clang-format-region (start end &optional style assume-file)
   "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'."
@@ -131,6 +131,9 @@
   (unless style
     (setq style clang-format-style))
 
+  (unless assume-file
+    (setq assume-file 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 +147,16 @@
         ;; 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
+                             (append `(nil nil ,clang-format-executable
+                                           nil (,temp-buffer ,temp-file) nil)
+                                     '("-output-replacements-xml")
+                                     (if assume-file
+                                         `("-assume-filename" ,assume-file) nil)
+                                     `("-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 +184,10 @@
       (when (buffer-name temp-buffer) (kill-buffer temp-buffer)))))
 
 ;;;###autoload
-(defun clang-format-buffer (&optional style)
+(defun clang-format-buffer (&optional style assume-file)
   "Use clang-format to format the current buffer according to STYLE."
   (interactive)
-  (clang-format-region (point-min) (point-max) style))
+  (clang-format-region (point-min) (point-max) style assume-file))
 
 ;;;###autoload
 (defalias 'clang-format 'clang-format-region)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D37903.115405.patch
Type: text/x-patch
Size: 3067 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170915/8d13ca78/attachment.bin>


More information about the cfe-commits mailing list