[clang-tools-extra] r295988 - Make clang-include-fixer--insert-line work when the difference is on an empty line

Manuel Klimek via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 23 08:02:53 PST 2017


Author: klimek
Date: Thu Feb 23 10:02:53 2017
New Revision: 295988

URL: http://llvm.org/viewvc/llvm-project?rev=295988&view=rev
Log:
Make clang-include-fixer--insert-line work when the difference is on an empty line

`clang-include-fixer--insert-line` has an off-by-one error because it
uses `(goto-char (point-min)) (forward-char chars)`, which is (goto-char
(1+ chars))`. Because of this, when the first difference was on an empty
line (i.e. an include was appended to the block of includes), the
pointer in the `to` buffer would be on the next line.

Also wrapped calls inside another process sentinel inside `with-local-quit`.

Patch by Torsten Marek.

Differential Revision: https://reviews.llvm.org/D30292

Modified:
    clang-tools-extra/trunk/include-fixer/tool/clang-include-fixer-test.el
    clang-tools-extra/trunk/include-fixer/tool/clang-include-fixer.el

Modified: clang-tools-extra/trunk/include-fixer/tool/clang-include-fixer-test.el
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/include-fixer/tool/clang-include-fixer-test.el?rev=295988&r1=295987&r2=295988&view=diff
==============================================================================
--- clang-tools-extra/trunk/include-fixer/tool/clang-include-fixer-test.el (original)
+++ clang-tools-extra/trunk/include-fixer/tool/clang-include-fixer-test.el Thu Feb 23 10:02:53 2017
@@ -23,6 +23,18 @@
           (should (equal (buffer-string) "aa\nab\nac\nad\n")))))
     (should (equal (buffer-string) "aa\nab\nac\nad\n"))))
 
+(ert-deftest clang-include-fixer--insert-line-diff-on-empty-line ()
+  "Unit test for `clang-include-fixer--insert-line'."
+  (with-temp-buffer
+    (insert "aa\nab\n\nac\nad\n")
+    (let ((from (current-buffer)))
+      (with-temp-buffer
+        (insert "aa\n\nac\nad\n")
+        (let ((to (current-buffer)))
+          (should (clang-include-fixer--insert-line from to))
+          (should (equal (buffer-string) "aa\nab\n\nac\nad\n")))))
+    (should (equal (buffer-string) "aa\nab\n\nac\nad\n"))))
+
 (ert-deftest clang-include-fixer--symbol-at-point ()
   "Unit test for `clang-include-fixer--symbol-at-point'."
   (with-temp-buffer

Modified: clang-tools-extra/trunk/include-fixer/tool/clang-include-fixer.el
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/include-fixer/tool/clang-include-fixer.el?rev=295988&r1=295987&r2=295988&view=diff
==============================================================================
--- clang-tools-extra/trunk/include-fixer/tool/clang-include-fixer.el (original)
+++ clang-tools-extra/trunk/include-fixer/tool/clang-include-fixer.el Thu Feb 23 10:02:53 2017
@@ -213,16 +213,14 @@ return nil.  Buffer restrictions are ign
                 (if (zerop chars)
                     ;; Buffer contents are equal, nothing to do.
                     t
-                  (goto-char (point-min))
-                  (forward-char chars)
+                  (goto-char chars)
                   ;; We might have ended up in the middle of a line if the
                   ;; current line partially matches.  In this case we would
                   ;; have to insert more than a line.  Move to the beginning of
                   ;; the line to avoid this situation.
                   (beginning-of-line)
                   (with-current-buffer from
-                    (goto-char (point-min))
-                    (forward-char chars)
+                    (goto-char chars)
                     (beginning-of-line)
                     (let ((from-begin (point))
                           (from-end (progn (forward-line) (point)))
@@ -268,9 +266,10 @@ clang-include-fixer to insert the select
                (clang-include-fixer--replace-buffer stdout)
                (let-alist context
                  (let-alist (car .HeaderInfos)
-                   (run-hook-with-args 'clang-include-fixer-add-include-hook
-                                       (substring .Header 1 -1)
-                                       (string= (substring .Header 0 1) "<"))))))
+                   (with-local-quit
+                     (run-hook-with-args 'clang-include-fixer-add-include-hook
+                                         (substring .Header 1 -1)
+                                         (string= (substring .Header 0 1) "<")))))))
            (format "-insert-header=%s"
                    (clang-include-fixer--encode-json context))))))))
   nil)




More information about the cfe-commits mailing list