include-fixer: Don't overwrite buffer changes
authorManuel Klimek <klimek@google.com>
Tue, 25 Oct 2016 11:31:22 +0000 (11:31 +0000)
committerManuel Klimek <klimek@google.com>
Tue, 25 Oct 2016 11:31:22 +0000 (11:31 +0000)
Raise a signal if the buffer has been modified before replacing it, to
avoid overwriting users' changes.

Patch by Philipp Stephani.

llvm-svn: 285060

clang-tools-extra/include-fixer/tool/clang-include-fixer.el

index ab7a59c..194f4b1 100644 (file)
@@ -244,7 +244,13 @@ clang-include-fixer to insert the selected header."
         (clang-include-fixer--select-header context)
         ;; Call clang-include-fixer again to insert the selected header.
         (clang-include-fixer--start
-         #'clang-include-fixer--replace-buffer
+         (let ((old-tick (buffer-chars-modified-tick)))
+           (lambda (stdout)
+             (when (/= old-tick (buffer-chars-modified-tick))
+               ;; Replacing the buffer now would undo the user’s changes.
+               (user-error (concat "The buffer has been changed "
+                                   "before the header could be inserted")))
+             (clang-include-fixer--replace-buffer stdout)))
          (format "-insert-header=%s"
                  (clang-include-fixer--encode-json context)))))))
   nil)