[clang] [emacs][clang-format] Add elisp API for clang-format on git diffs (PR #112792)

Campbell Barton via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 4 16:52:36 PST 2024


================
@@ -312,15 +335,35 @@ diffs from HEAD in the buffer. If no STYLE is given uses
 file. If no ASSUME-FILE-NAME is given uses the function
 ‘buffer-file-name’."
   (interactive)
-  (let ((diff-lines (clang-format--vc-diff-get-diff-lines)))
-    ;; If we have any diffs, format them.
-    (when diff-lines
-      (clang-format--region-impl
-       (point-min)
-       (point-max)
-       style
-       assume-file-name
-       diff-lines))))
+  (let ((tmpfile-vc-head nil)
+        (tmpfile-curbuf nil))
+    (unwind-protect
----------------
ideasman42 wrote:

\*picky\* I find it often works better to wrap `unwind-protect` in a macro.

Example usage 
```
(clang-format--with-delete-files-guard files-to-delete
  ... snip ...
    (push (setq tmpfile-curbuf (make-temp-file "clang-format-vc-tmp")) temp-files)
  (push tmpfile-curbuf temp-files))
```k

Here is a macro that does this:
```
(defmacro clang-format--with-advice (bind-files-to-delete &rest body)
  "Execute BODY which may add temp files to BIND-FILES-TO-DELETE."
  (declare (indent 1))
  `(let ((,bind-files-to-delete nil))
     (unwind-protect
         (progn
           , at body)
       (while ,bind-files-to-delete
         (with-demoted-errors "failed to remove file: %S"
           (delete-file (pop ,bind-files-to-delete)))))))
```


https://github.com/llvm/llvm-project/pull/112792


More information about the cfe-commits mailing list