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

via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 4 08:07:11 PST 2024


================
@@ -146,18 +146,115 @@ is a zero-based file offset, assuming ‘utf-8-unix’ coding."
     (lambda (byte &optional _quality _coding-system)
       (byte-to-position (1+ byte)))))
 
-;;;###autoload
-(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
-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))
-     (list (point) (point))))
-
+(defun clang-format--vc-diff-match-diff-line (line)
+  ;; Matching something like:
+  ;; "@@ -80 +80 @@" or "@@ -80,2 +80,2 @@"
+  ;; Return as "<LineStart>:<LineEnd>"
+  (when (string-match "^@@\s-[0-9,]+\s\\+\\([0-9]+\\)\\(,\\([0-9]+\\)\\)?\s@@$" line)
+    ;; If we have multi-line diff
+    (if (match-string 3 line)
+        (concat (match-string 1 line)
+                ":"
+                (number-to-string
+                 (+ (string-to-number (match-string 1 line))
+                    (string-to-number (match-string 3 line)))))
+      (concat (match-string 1 line) ":" (match-string 1 line)))))
+
+(defun clang-format--vc-diff-get-diff-lines (file-orig file-new)
+  "Return all line regions that contain diffs between FILE-ORIG and
+FILE-NEW.  If there is no diff 'nil' is returned. Otherwise the
+return is a 'list' of lines in the format '--lines=<start>:<end>'
+which can be passed directly to 'clang-format'"
+  ;; Temporary buffer for output of diff.
+  (with-temp-buffer
+    (let ((status (call-process
----------------
goldsteinn wrote:

Okay, I'm opposed to changing to use `diff-no-select`. It is notably slower. I think because we go through temporary buffers. I'm going to push the code that uses `diff-no-select`, unless you see a way to speed it up I think we should keep the current implementation.

Benchmark code:
```
(require 'benchmark)
(benchmark-elapse
  (with-current-buffer "ValueTracking.cpp" ;; 10000 line file
    (clang-format-vc-diff)
    (clang-format-vc-diff)
    (clang-format-vc-diff)
    (clang-format-vc-diff)
    (clang-format-vc-diff)
    (clang-format-vc-diff)
    (clang-format-vc-diff)
    (clang-format-vc-diff)
    (clang-format-vc-diff)
    (clang-format-vc-diff)            
    )
  )
```
Bespoke Diff   : 0.1 += 0.05 sec
`diff-no-select`: 0.2 += 0.05 sec

If I scale `ValueTracking.cpp` to 100000 lines:

Bespoke Diff   : 0.45 += 0.1 sec
`diff-no-select`: 1.1 += 0.1 sec

So it seems twice as slow, I imagine because we end up sending the VC version of the file through memory.

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


More information about the cfe-commits mailing list