]> git.eshelyaron.com Git - emacs.git/commitdiff
Make warning about unescaped character literals more helpful.
authorPhilipp Stephani <phst@google.com>
Sat, 2 Jun 2018 09:59:02 +0000 (11:59 +0200)
committerPhilipp Stephani <phst@google.com>
Fri, 19 Apr 2019 17:19:35 +0000 (19:19 +0200)
See Bug#31676.

* lisp/emacs-lisp/byte-run.el
(byte-run--unescaped-character-literals-warning): New defun.

* src/lread.c (load_warn_unescaped_character_literals): Use new defun.
(syms_of_lread): Define symbol for new defun.

* lisp/emacs-lisp/bytecomp.el (byte-compile-from-buffer): Use new
defun.

* test/src/lread-tests.el (lread-tests--unescaped-char-literals):
test/lisp/emacs-lisp/bytecomp-tests.el
(bytecomp-tests--unescaped-char-literals): Adapt unit tests.

lisp/emacs-lisp/byte-run.el
lisp/emacs-lisp/bytecomp.el
src/lread.c
test/lisp/emacs-lisp/bytecomp-tests.el
test/src/lread-tests.el

index b638b56be1feb892c7f5f8c2d2f996953b748b38..7e256f832724d46c041acc123d0b9ceb4ebbbcf4 100644 (file)
@@ -494,6 +494,21 @@ is enabled."
   ;; The implementation for the interpreter is basically trivial.
   (car (last body)))
 
+\f
+(defun byte-run--unescaped-character-literals-warning ()
+  "Return a warning about unescaped character literals.
+If there were any unescaped character literals in the last form
+read, return an appropriate warning message as a string.
+Otherwise, return nil.  For internal use only."
+  ;; This is called from lread.c and therefore needs to be preloaded.
+  (if lread--unescaped-character-literals
+      (let ((sorted (sort lread--unescaped-character-literals #'<)))
+        (format-message "unescaped character literals %s detected, %s expected!"
+                        (mapconcat (lambda (char) (format "`?%c'" char))
+                                   sorted ", ")
+                        (mapconcat (lambda (char) (format "`?\\%c'" char))
+                                   sorted ", ")))))
+
 \f
 ;; I nuked this because it's not a good idea for users to think of using it.
 ;; These options are a matter of installation preference, and have nothing to
index 8bbe6292d9d007bd08d032c7d86885d531223d97..4c61e1a44713324be7407a5c96e11b9b5994555e 100644 (file)
@@ -2082,14 +2082,9 @@ With argument ARG, insert value in current buffer after the form."
                 (not (eobp)))
          (setq byte-compile-read-position (point)
                byte-compile-last-position byte-compile-read-position)
-         (let* ((lread--unescaped-character-literals nil)
-                 (form (read inbuffer)))
-            (when lread--unescaped-character-literals
-              (byte-compile-warn
-               "unescaped character literals %s detected!"
-               (mapconcat (lambda (char) (format "`?%c'" char))
-                          (sort lread--unescaped-character-literals #'<)
-                          ", ")))
+         (let ((form (read inbuffer))
+                (warning (byte-run--unescaped-character-literals-warning)))
+            (when warning (byte-compile-warn "%s" warning))
            (byte-compile-toplevel-file-form form)))
        ;; Compile pending forms at end of file.
        (byte-compile-flush-pending)
index 8cb4b63cc3ab31e48474fb7c0fc1a5467eb05a85..8b38cacde0780bc75afbb6c259450d91c2bf695d 100644 (file)
@@ -1034,18 +1034,12 @@ load_error_old_style_backquotes (void)
 static void
 load_warn_unescaped_character_literals (Lisp_Object file)
 {
-  if (NILP (Vlread_unescaped_character_literals)) return;
-  CHECK_CONS (Vlread_unescaped_character_literals);
-  Lisp_Object format =
-    build_string ("Loading `%s': unescaped character literals %s detected!");
-  Lisp_Object separator = build_string (", ");
-  Lisp_Object inner_format = build_string ("`?%c'");
-  CALLN (Fmessage,
-         format, file,
-         Fmapconcat (list3 (Qlambda, list1 (Qchar),
-                            list3 (Qformat, inner_format, Qchar)),
-                     Fsort (Vlread_unescaped_character_literals, Qlss),
-                     separator));
+  Lisp_Object warning
+    = call0 (Qbyte_run_unescaped_character_literals_warning);
+  if (NILP (warning))
+    return;
+  Lisp_Object format = build_string ("Loading `%s': %s");
+  CALLN (Fmessage, format, file, warning);
 }
 
 DEFUN ("get-load-suffixes", Fget_load_suffixes, Sget_load_suffixes, 0, 0, 0,
@@ -5014,9 +5008,9 @@ For internal use only.  */);
   DEFSYM (Qlread_unescaped_character_literals,
           "lread--unescaped-character-literals");
 
-  DEFSYM (Qlss, "<");
-  DEFSYM (Qchar, "char");
-  DEFSYM (Qformat, "format");
+  /* Defined in lisp/emacs-lisp/byte-run.el.  */
+  DEFSYM (Qbyte_run_unescaped_character_literals_warning,
+          "byte-run--unescaped-character-literals-warning");
 
   DEFVAR_BOOL ("load-prefer-newer", load_prefer_newer,
                doc: /* Non-nil means `load' prefers the newest version of a file.
index f66a06bc1bca95cc176a8a1cb937be788395c264..5fb64ff2881236a330f22c0f55fdc404b1e511ae 100644 (file)
@@ -540,7 +540,9 @@ literals (Bug#20852)."
         (should (equal (cdr err)
                        (list (concat "unescaped character literals "
                                      "`?\"', `?(', `?)', `?;', `?[', `?]' "
-                                     "detected!"))))))))
+                                     "detected, "
+                                     "`?\\\"', `?\\(', `?\\)', `?\\;', `?\\[', "
+                                     "`?\\]' expected!"))))))))
 
 (ert-deftest bytecomp-tests--old-style-backquotes ()
   "Check that byte compiling warns about old-style backquotes."
index ae918f03120841b1fa80980612e64c915a8ae5d4..82b75b195ca360ed399ef6c0662c9c305ab96f54 100644 (file)
@@ -140,7 +140,9 @@ literals (Bug#20852)."
     (should (equal (lread-tests--last-message)
                    (concat (format-message "Loading `%s': " file-name)
                            "unescaped character literals "
-                           "`?\"', `?(', `?)', `?;', `?[', `?]' detected!")))))
+                           "`?\"', `?(', `?)', `?;', `?[', `?]' detected, "
+                           "`?\\\"', `?\\(', `?\\)', `?\\;', `?\\[', `?\\]' "
+                           "expected!")))))
 
 (ert-deftest lread-tests--funny-quote-symbols ()
   "Check that 'smart quotes' or similar trigger errors in symbol names."