unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#15122: 24.3.50; [PATCH] byte-compiler warnings about destructive functions
@ 2013-08-18  5:26 Drew Adams
  2013-08-18 18:38 ` Richard Stallman
  2013-08-19  2:59 ` Stefan Monnier
  0 siblings, 2 replies; 11+ messages in thread
From: Drew Adams @ 2013-08-18  5:26 UTC (permalink / raw)
  To: 15122

[-- Attachment #1: Type: text/plain, Size: 1638 bytes --]

This StackOverflow entry suggested that the Emacs-Lisp byte compiler be
able to warn about the use of functions that are destructive, i.e., can
modify data structures in place:

http://stackoverflow.com/questions/17610046/elisp-destructive-operation-warning

Attached is a patch that provides this, at least a start.

It covers the basic functions that can perform in-place data structure 
modification: lists, strings, vectors and other arrays, symbol properties.
These are generally the functions identified as destructive or modifying
in the Elisp manual.

A comment in the code identifies some other possible candidates that
occurred to me while browsing the manual.  Modify the list of functions
as you see fit (variable `byte-compile-destructive-functions').

I'm no expert on some of the data structure implementations, and the
manual is sometimes not so clear about whether destructive modification
occurs.  And perhaps there are cases where although the modification is
destructive at an implementation level the user need not be concerned
about that level, so such functions should not be included.  Dunno.

Perhaps also something could be done in the future wrt recognition of
particular cases for `setf' modification.

In GNU Emacs 24.3.50.1 (i686-pc-mingw32)
 of 2013-08-07 on ODIEONE
Bzr revision: 113750 lekktu@gmail.com-20130808011911-0jzpc9xuncegg6x9
Windowing system distributor `Microsoft Corp.', version 6.1.7601
Configured using:
 `configure --prefix=/c/Devel/emacs/binary --enable-checking=yes,glyphs
 CFLAGS=-O0 -g3 LDFLAGS=-Lc:/Devel/emacs/lib
 CPPFLAGS=-Ic:/Devel/emacs/include'

[-- Attachment #2: bytecomp-2013-08-17a.patch --]
[-- Type: application/octet-stream, Size: 3045 bytes --]

diff -c bytecomp.el bytecomp-patched.el
*** bytecomp.el	Sat Aug 17 21:35:05 2013
--- bytecomp-patched.el	Sat Aug 17 22:11:45 2013
***************
*** 274,281 ****
  (defconst byte-compile-warning-types
    '(redefine callargs free-vars unresolved
  	     obsolete noruntime cl-functions interactive-only
! 	     make-local mapcar constants suspicious lexical)
    "The list of warning types used when `byte-compile-warnings' is t.")
  (defcustom byte-compile-warnings t
    "List of warnings that the byte-compiler should issue (t for all).
  
--- 274,282 ----
  (defconst byte-compile-warning-types
    '(redefine callargs free-vars unresolved
  	     obsolete noruntime cl-functions interactive-only
! 	     make-local mapcar constants suspicious lexical destructive)
    "The list of warning types used when `byte-compile-warnings' is t.")
+ 
  (defcustom byte-compile-warnings t
    "List of warnings that the byte-compiler should issue (t for all).
  
***************
*** 298,303 ****
--- 299,305 ----
    mapcar      mapcar called for effect.
    constants   let-binding of, or assignment to, constants/nonvariables.
    suspicious  constructs that usually don't do what the coder wanted.
+   destructive functions that can modify data structure in place.
  
  If the list begins with `not', then the remaining elements specify warnings to
  suppress.  For example, (not mapcar) will suppress warnings about mapcar."
***************
*** 353,358 ****
--- 355,370 ----
                    (t
                     (append byte-compile-warnings (list warning)))))))
  
+ ;; Possible candidates? `clrhash', `match-data' (with REUSE), `put', `puthash',
+ ;; `remhash', `set-char-table-extra-slot', `set-char-table-parent',
+ ;; `set-char-table-range', `setplist', `unintern', `write-region' (merging annotations)?
+ ;; What to do about `setf' with various generalized vars?
+ ;;
+ (defvar byte-compile-destructive-functions
+   '(aset clear-string delete delete-dups delq fillarray nbutlast nconc nreverse plist-put
+     ring-insert ring-insert-at-beginning ring-remove setcar setcdr sort store-substring)
+   "List of functions that can modify data structures in place.")
+ 
  (defvar byte-compile-interactive-only-functions
    '(beginning-of-buffer end-of-buffer replace-string replace-regexp
      insert-file insert-buffer insert-file-literally previous-line next-line
***************
*** 2925,2930 ****
--- 2937,2946 ----
               (handler (get fn 'byte-compile)))
          (when (macroexp--const-symbol-p fn)
            (byte-compile-warn "`%s' called as a function" fn))
+         (and (byte-compile-warning-enabled-p 'destructive)
+              (memq fn byte-compile-destructive-functions)
+              (byte-compile-warn "`%s' can modify data structure in place \
+ \(destructive)" fn))
          (and (byte-compile-warning-enabled-p 'interactive-only)
               (memq fn byte-compile-interactive-only-functions)
               (byte-compile-warn "`%s' used from Lisp code\n\

Diff finished.  Sat Aug 17 22:12:04 2013

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2013-08-21 16:54 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-08-18  5:26 bug#15122: 24.3.50; [PATCH] byte-compiler warnings about destructive functions Drew Adams
2013-08-18 18:38 ` Richard Stallman
2013-08-19  2:59 ` Stefan Monnier
2013-08-19  5:07   ` Drew Adams
2013-08-20  5:07     ` Stefan Monnier
2013-08-20 14:07       ` Drew Adams
2013-08-20 14:39         ` Stefan Monnier
2013-08-20 15:15           ` Drew Adams
2013-08-21  1:00           ` Leo Liu
2013-08-21  4:24             ` Stefan Monnier
2013-08-21 16:54               ` Richard Stallman

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).