Hi.

At r249567, '-sort-includes' option is set to clang-format-region
command in clang-format.el. This option can be used only with
development version clang-format. and users get error if they use old
version clang-format. And I suppose clang-format options should not be
hard corded and should be customizable.

This attached patch adds customize variable about clang-format options
and its default value is '("-sort-includes") if users use clang-format
3.8 or higher, while it is nil if not.

Regards.

See also
  - https://github.com/syl20bnr/spacemacs/issues/3408 (older
clang-format user's issue)

-- 
Syohei YOSHIDA(syo...@gmail.com)
Index: clang-format.el
===================================================================
--- clang-format.el	(revision 250381)
+++ clang-format.el	(working copy)
@@ -56,6 +56,20 @@
   :safe #'stringp)
 (make-variable-buffer-local 'clang-format-style)

+(defun clang-format--default-options ()
+  (with-temp-buffer
+    (when (and (executable-find clang-format-executable)
+               (= (process-file clang-format-executable nil t nil "--version") 0))
+      (goto-char (point-min))
+      (when (re-search-forward "clang-format version \\([0-9.]+\\)" nil t)
+        (when (version<= "3.8.0" (match-string-no-properties 1))
+          '("-sort-includes"))))))
+
+(defcustom clang-format-options (clang-format--default-options)
+  "Optional arguments to pass to clang-format"
+  :group 'clang-format
+  :type '(repeat string))
+
 (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))
@@ -100,6 +114,16 @@
     (when text
       (insert text))))

+(defun clang-format--construct-options (start end cursor style)
+  (append (list
+           "-output-replacements-xml"
+           "-assume-filename" (or (buffer-file-name) "")
+           "-style" style
+           "-offset" (number-to-string start)
+           "-length" (number-to-string (- end start))
+           "-cursor" (number-to-string cursor))
+          clang-format-options))
+
 ;;;###autoload
 (defun clang-format-region (char-start char-end &optional style)
   "Use clang-format to format the code between START and END according to STYLE.
@@ -121,17 +145,10 @@
     (unwind-protect
         (let (status stderr operations)
           (setq status
-                (call-process-region
+                (apply #'call-process-region
                  (point-min) (point-max) clang-format-executable
                  nil `(,temp-buffer ,temp-file) nil
-
-                 "-output-replacements-xml"
-                 "-sort-includes"
-                 "-assume-filename" (or (buffer-file-name) "")
-                 "-style" style
-                 "-offset" (number-to-string start)
-                 "-length" (number-to-string (- end start))
-                 "-cursor" (number-to-string cursor)))
+                 (clang-format--construct-options start end cursor style)))
           (setq stderr
                 (with-temp-buffer
                   (insert-file-contents temp-file)
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to