Skip to content

Commit 7742bd1

Browse files
committed
Use macro for cleaning files
1 parent 6d49dcf commit 7742bd1

File tree

1 file changed

+37
-29
lines changed

1 file changed

+37
-29
lines changed

clang/tools/clang-format/clang-format.el

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,17 @@ is a zero-based file offset, assuming ‘utf-8-unix’ coding."
146146
(lambda (byte &optional _quality _coding-system)
147147
(byte-to-position (1+ byte)))))
148148

149+
(defmacro clang-format--with-delete-files-guard (bind-files-to-delete &rest body)
150+
"Execute BODY which may add temp files to BIND-FILES-TO-DELETE."
151+
(declare (indent 1))
152+
`(let ((,bind-files-to-delete nil))
153+
(unwind-protect
154+
(progn
155+
,@body)
156+
(while ,bind-files-to-delete
157+
(with-demoted-errors "failed to remove file: %S"
158+
(delete-file (pop ,bind-files-to-delete)))))))
159+
149160
(defun clang-format--vc-diff-get-diff-lines (file-orig file-new)
150161
"Return all line regions that contain diffs between FILE-ORIG and
151162
FILE-NEW. If there is no diff ‘nil’ is returned. Otherwise the
@@ -318,35 +329,32 @@ diffs from HEAD in the buffer. If no STYLE is given uses
318329
file. If no ASSUME-FILE-NAME is given uses the function
319330
‘buffer-file-name’."
320331
(interactive)
321-
(let ((tmpfile-vc-head nil)
322-
(tmpfile-curbuf nil))
323-
(unwind-protect
324-
(progn
325-
(setq tmpfile-vc-head
326-
(make-temp-file "clang-format-vc-tmp-head-content"))
327-
(clang-format--vc-diff-get-vc-head-file tmpfile-vc-head)
328-
;; Move the current buffer to a temporary file to take a
329-
;; diff. Even if current-buffer is backed by a file, we
330-
;; want to diff the buffer contents which might not be
331-
;; saved.
332-
(setq tmpfile-curbuf (make-temp-file "clang-format-vc-tmp"))
333-
(write-region nil nil tmpfile-curbuf nil 'nomessage)
334-
;; Get a list of lines with a diff.
335-
(let ((diff-lines
336-
(clang-format--vc-diff-get-diff-lines
337-
tmpfile-vc-head tmpfile-curbuf)))
338-
;; If we have any diffs, format them.
339-
(when diff-lines
340-
(clang-format--region-impl
341-
(point-min)
342-
(point-max)
343-
style
344-
assume-file-name
345-
diff-lines))))
346-
(progn
347-
;; Cleanup temporary files we created.
348-
(when tmpfile-vc-head (ignore-errors (delete-file tmpfile-vc-head)))
349-
(when tmpfile-curbuf (ignore-errors (delete-file tmpfile-curbuf)))))))
332+
(clang-format--with-delete-files-guard tmp-files
333+
(let ((tmpfile-vc-head nil)
334+
(tmpfile-curbuf nil))
335+
(setq tmpfile-vc-head
336+
(make-temp-file "clang-format-vc-tmp-head-content"))
337+
(push tmpfile-vc-head tmp-files)
338+
(clang-format--vc-diff-get-vc-head-file tmpfile-vc-head)
339+
;; Move the current buffer to a temporary file to take a
340+
;; diff. Even if current-buffer is backed by a file, we
341+
;; want to diff the buffer contents which might not be
342+
;; saved.
343+
(setq tmpfile-curbuf (make-temp-file "clang-format-vc-tmp"))
344+
(push tmpfile-curbuf tmp-files)
345+
(write-region nil nil tmpfile-curbuf nil 'nomessage)
346+
;; Get a list of lines with a diff.
347+
(let ((diff-lines
348+
(clang-format--vc-diff-get-diff-lines
349+
tmpfile-vc-head tmpfile-curbuf)))
350+
;; If we have any diffs, format them.
351+
(when diff-lines
352+
(clang-format--region-impl
353+
(point-min)
354+
(point-max)
355+
style
356+
assume-file-name
357+
diff-lines))))))
350358

351359

352360
;;;###autoload

0 commit comments

Comments
 (0)