From cd5d4ed3f9fbd082dce2d583b4f4f091449acc94 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mattias=20Engdeg=C3=A5rd?= Date: Sat, 4 May 2024 10:08:19 +0200 Subject: [PATCH] Only issue lexical cookie warning for elisp files * src/lread.c (string_suffix_p): New. (warn_missing_cookie): Suppress warning for files not ending in ".el", except ".emacs". * etc/NEWS: Update accordingly, and mention how the warning can be suppressed. (cherry picked from commit 1121f17d7c4bc3b71edcd0799b894f50aa3a715e) --- etc/NEWS | 5 +++-- src/lread.c | 31 ++++++++++++++++++++++--------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 5489532b76c..875a7d2cdb1 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -2576,8 +2576,9 @@ Its warning name is 'docstrings-wide'. Emacs now emits a run-time warning if an Elisp source file being loaded lacks the '-*- lexical-binding: ... -*-' cookie on the first line. See the lexical-binding compiler warning described above for how to make -the warning go away. The user's init file (whose name is the value of -'user-init-file') and early-init file are exempt from this warning. +the warning go away by adding a declaration to the file. You can also +suppress the warning by adding an entry for the warning type +'lexical-warning' to 'warning-suppress-types'. --- ** New user option 'native-comp-async-warnings-errors-kind'. diff --git a/src/lread.c b/src/lread.c index a8ea52a888d..ba890cb673d 100644 --- a/src/lread.c +++ b/src/lread.c @@ -1342,20 +1342,33 @@ close_file_unwind_android_fd (void *ptr) #endif +static bool +string_suffix_p (const char *string, ptrdiff_t string_len, + const char *suffix, ptrdiff_t suffix_len) +{ + return string_len >= suffix_len && memcmp (string + string_len - suffix_len, + suffix, suffix_len) == 0; +} + static void warn_missing_cookie (Lisp_Object file) { - Lisp_Object msg; - - /* The user init file should not be subject to these warnings, as - Emacs doesn't insert cookies into generated init files. */ - if (!NILP (Fequal (file, Vuser_init_file))) + /* Only warn for files whose name end in .el, to suppress loading of + data-as-code. ".emacs" is an exception, since it does tend to contain + actual hand-written code. */ + if (!STRINGP (file)) + return; + const char *name = SSDATA (file); + ptrdiff_t nb = SBYTES (file); + if (!(string_suffix_p (name, nb, ".el", 3) + || (string_suffix_p (name, nb, ".emacs", 6) + && (nb == 6 || SREF (file, nb - 7) == '/')))) return; - msg = CALLN (Fformat, - build_string ("File %s lacks `lexical-binding'" - " directive on its first line"), - file); + Lisp_Object msg = CALLN (Fformat, + build_string ("File %s lacks `lexical-binding'" + " directive on its first line"), + file); Vdelayed_warnings_list = Fcons (list2 (Qlexical_binding, msg), Vdelayed_warnings_list); } -- 2.39.5