comparison lisp/bytecomp.el @ 4719:bd51ab22afa8

Make it possible to silence warnings issued when #'mapcar's result is discarded. lisp/ChangeLog addition: 2009-10-19 Aidan Kehoe <kehoea@parhasard.net> * bytecomp.el (byte-compile-default-warnings): Add two new warning types, discarded-consing (basically use of mapcar instead of mapc where its result is discarded) and quoted-lambda (use of a lambda expression quoted as data in a function context). (byte-compile-warnings): Document the new warnings. (byte-compile-fset, byte-compile-funarg): Implement the quoted-lambda warning option. (byte-compile-mapcar): Renamed to byte-compile-maybe-mapc. (byte-compile-maybe-mapc, byte-compile-maplist): Implement the discarded-consing warning option. Add more functions that should be compiled using byte-compile-funarg, notably mapvector, mapc-internal, map-char-table. * cl-macs.el (mapcar*): If we know at compile time that there are no CL options being used, use the mapcar subr, not the byte-coded function.
author Aidan Kehoe <kehoea@parhasard.net>
date Mon, 19 Oct 2009 12:47:21 +0100
parents dca5bb2adff1
children 776bbf454f3a
comparison
equal deleted inserted replaced
4718:a27de91ae83c 4719:bd51ab22afa8
115 ;;; a macro to a lambda or vice versa, 115 ;;; a macro to a lambda or vice versa,
116 ;;; or redefined to take other args) 116 ;;; or redefined to take other args)
117 ;;; 'obsolete (obsolete variables and functions) 117 ;;; 'obsolete (obsolete variables and functions)
118 ;;; 'pedantic (references to Emacs-compatible 118 ;;; 'pedantic (references to Emacs-compatible
119 ;;; symbols) 119 ;;; symbols)
120 ;;; 'discarded-consing (use of mapcar instead of
121 ;;; mapc, and similar)
122 ;;; 'quoted-lambda (quoting a lambda expression
123 ;;; as data, not as a function,
124 ;;; and using it in a function
125 ;;; context )
120 ;;; byte-compile-emacs19-compatibility Whether the compiler should 126 ;;; byte-compile-emacs19-compatibility Whether the compiler should
121 ;;; generate .elc files which can be loaded into 127 ;;; generate .elc files which can be loaded into
122 ;;; generic emacs 19. 128 ;;; generic emacs 19.
123 ;;; emacs-lisp-file-regexp Regexp for the extension of source-files; 129 ;;; emacs-lisp-file-regexp Regexp for the extension of source-files;
124 ;;; see also the function `byte-compile-dest-file'. 130 ;;; see also the function `byte-compile-dest-file'.
359 (defvar byte-compile-error-on-warn nil 365 (defvar byte-compile-error-on-warn nil
360 "*If true, the byte-compiler reports warnings with `error'.") 366 "*If true, the byte-compiler reports warnings with `error'.")
361 367
362 ;; byte-compile-warning-types in FSF. 368 ;; byte-compile-warning-types in FSF.
363 (defvar byte-compile-default-warnings 369 (defvar byte-compile-default-warnings
364 '(redefine callargs subr-callargs free-vars unresolved unused-vars obsolete) 370 '(redefine callargs subr-callargs free-vars unresolved unused-vars obsolete
371 discarded-consing quoted-lambda)
365 "*The warnings used when byte-compile-warnings is t.") 372 "*The warnings used when byte-compile-warnings is t.")
366 373
367 (defvar byte-compile-warnings t 374 (defvar byte-compile-warnings t
368 "*List of warnings that the compiler should issue (t for the default set). 375 "*List of warnings that the compiler should issue (t for the default set).
369 Elements of the list may be: 376 Elements of the list may be:
375 subr-callargs calls to subrs with args that don't match the definition. 382 subr-callargs calls to subrs with args that don't match the definition.
376 redefine function cell redefined from a macro to a lambda or vice 383 redefine function cell redefined from a macro to a lambda or vice
377 versa, or redefined to take a different number of arguments. 384 versa, or redefined to take a different number of arguments.
378 obsolete use of an obsolete function or variable. 385 obsolete use of an obsolete function or variable.
379 pedantic warn of use of compatible symbols. 386 pedantic warn of use of compatible symbols.
387 discarded-consing
388 calls to (some) functions that allocate memory, where that
389 memory is immediately discarded; canonically, the use of
390 mapcar instead of mapc
391 quoted-lambda passing a lambda expression not quoted as a function, as a
392 function argument
380 393
381 The default set is specified by `byte-compile-default-warnings' and 394 The default set is specified by `byte-compile-default-warnings' and
382 normally encompasses all possible warnings. 395 normally encompasses all possible warnings.
383 396
384 See also the macro `byte-compiler-options'.") 397 See also the macro `byte-compiler-options'.")
1071 (eq val 'emacs19)) 1084 (eq val 'emacs19))
1072 (delete-errors byte-compile-delete-errors (t nil) val) 1085 (delete-errors byte-compile-delete-errors (t nil) val)
1073 (verbose byte-compile-verbose (t nil) val) 1086 (verbose byte-compile-verbose (t nil) val)
1074 (new-bytecodes byte-compile-new-bytecodes (t nil) val) 1087 (new-bytecodes byte-compile-new-bytecodes (t nil) val)
1075 (warnings byte-compile-warnings 1088 (warnings byte-compile-warnings
1076 ((callargs subr-callargs redefine free-vars unused-vars unresolved)) 1089 ((callargs subr-callargs redefine free-vars unused-vars
1090 unresolved discarded-consing quoted-lambda))
1077 val))) 1091 val)))
1078 1092
1079 ;; XEmacs addition 1093 ;; XEmacs addition
1080 (defconst byte-compiler-obsolete-options 1094 (defconst byte-compiler-obsolete-options
1081 '((new-bytecodes t))) 1095 '((new-bytecodes t)))
3500 (not (eq (car-safe (cdr-safe (nth 1 form))) 'make-byte-code))) 3514 (not (eq (car-safe (cdr-safe (nth 1 form))) 'make-byte-code)))
3501 (setq body (cdr (cdr fn))) 3515 (setq body (cdr (cdr fn)))
3502 (if (stringp (car body)) (setq body (cdr body))) 3516 (if (stringp (car body)) (setq body (cdr body)))
3503 (if (eq 'interactive (car-safe (car body))) (setq body (cdr body))) 3517 (if (eq 'interactive (car-safe (car body))) (setq body (cdr body)))
3504 (if (and (consp (car body)) 3518 (if (and (consp (car body))
3505 (not (eq 'byte-code (car (car body))))) 3519 (not (eq 'byte-code (car (car body))))
3520 (memq 'quoted-lambda byte-compile-warnings))
3506 (byte-compile-warn 3521 (byte-compile-warn
3507 "A quoted lambda form is the second argument of fset. This is probably 3522 "A quoted lambda form is the second argument of fset. This is probably
3508 not what you want, as that lambda cannot be compiled. Consider using 3523 not what you want, as that lambda cannot be compiled. Consider using
3509 the syntax (function (lambda (...) ...)) instead.")))) 3524 the syntax (function (lambda (...) ...)) instead."))))
3510 (byte-compile-two-args form)) 3525 (byte-compile-two-args form))
3513 ;; (mapcar '(lambda (x) ..) ..) ==> (mapcar (function (lambda (x) ..)) ..) 3528 ;; (mapcar '(lambda (x) ..) ..) ==> (mapcar (function (lambda (x) ..)) ..)
3514 ;; for cases where it's guaranteed that first arg will be used as a lambda. 3529 ;; for cases where it's guaranteed that first arg will be used as a lambda.
3515 (byte-compile-normal-call 3530 (byte-compile-normal-call
3516 (let ((fn (nth 1 form))) 3531 (let ((fn (nth 1 form)))
3517 (if (and (eq (car-safe fn) 'quote) 3532 (if (and (eq (car-safe fn) 'quote)
3518 (eq (car-safe (nth 1 fn)) 'lambda)) 3533 (eq (car-safe (nth 1 fn)) 'lambda)
3534 (or
3535 (null (memq 'quoted-lambda byte-compile-warnings))
3536 (byte-compile-warn
3537 "Passing a quoted lambda to #'%s, forcing function quoting"
3538 (car form))))
3519 (cons (car form) 3539 (cons (car form)
3520 (cons (cons 'function (cdr fn)) 3540 (cons (cons 'function (cdr fn))
3521 (cdr (cdr form)))) 3541 (cdr (cdr form))))
3522 form)))) 3542 form))))
3523 3543
3524 ;; XEmacs change; don't cons up the list if it's going to be immediately 3544 ;; XEmacs change; don't cons up the list if it's going to be immediately
3525 ;; discarded. 3545 ;; discarded.
3526 (defun byte-compile-mapcar (form) 3546 (defun byte-compile-maybe-mapc (form)
3527 (and for-effect (setq form (cons 'mapc-internal (cdr form))) 3547 (and for-effect
3528 (byte-compile-warn 3548 (or (null (memq 'discarded-consing byte-compile-warnings))
3529 "Discarding the result of #'mapcar; maybe you meant #'mapc?")) 3549 (byte-compile-warn
3550 "Discarding the result of #'%s; maybe you meant #'mapc?"
3551 (car form)))
3552 (setq form (cons 'mapc-internal (cdr form))))
3530 (byte-compile-funarg form)) 3553 (byte-compile-funarg form))
3531 3554
3532 (defun byte-compile-maplist (form) 3555 (defun byte-compile-maplist (form)
3533 (and for-effect (setq form (cons 'mapl (cdr form))) 3556 (and for-effect
3534 (byte-compile-warn 3557 (or (null (memq 'discarded-consing byte-compile-warnings))
3535 "Discarding the result of #'maplist; maybe you meant #'mapl?")) 3558 (byte-compile-warn
3559 "Discarding the result of #'maplist; maybe you meant #'mapl?"))
3560 (setq form (cons 'mapl (cdr form))))
3536 (byte-compile-funarg form)) 3561 (byte-compile-funarg form))
3537 3562
3538 ;; (function foo) must compile like 'foo, not like (symbol-function 'foo). 3563 ;; (function foo) must compile like 'foo, not like (symbol-function 'foo).
3539 ;; Otherwise it will be incompatible with the interpreter, 3564 ;; Otherwise it will be incompatible with the interpreter,
3540 ;; and (funcall (function foo)) will lose with autoloads. 3565 ;; and (funcall (function foo)) will lose with autoloads.
3710 (byte-defop-compiler-1 and) 3735 (byte-defop-compiler-1 and)
3711 (byte-defop-compiler-1 or) 3736 (byte-defop-compiler-1 or)
3712 (byte-defop-compiler-1 while) 3737 (byte-defop-compiler-1 while)
3713 (byte-defop-compiler-1 funcall) 3738 (byte-defop-compiler-1 funcall)
3714 (byte-defop-compiler-1 apply byte-compile-funarg) 3739 (byte-defop-compiler-1 apply byte-compile-funarg)
3715 (byte-defop-compiler-1 mapcar byte-compile-mapcar) 3740 (byte-defop-compiler-1 mapcar byte-compile-maybe-mapc)
3741 (byte-defop-compiler-1 mapvector byte-compile-maybe-mapc)
3742 (byte-defop-compiler-1 mapc byte-compile-funarg)
3743 (byte-defop-compiler-1 mapc-internal byte-compile-funarg)
3716 (byte-defop-compiler-1 mapatoms byte-compile-funarg) 3744 (byte-defop-compiler-1 mapatoms byte-compile-funarg)
3717 (byte-defop-compiler-1 mapconcat byte-compile-funarg) 3745 (byte-defop-compiler-1 mapconcat byte-compile-funarg)
3718 (byte-defop-compiler-1 map byte-compile-funarg) 3746 (byte-defop-compiler-1 map byte-compile-funarg)
3719 (byte-defop-compiler-1 maplist byte-compile-maplist) 3747 (byte-defop-compiler-1 maplist byte-compile-maplist)
3720 (byte-defop-compiler-1 mapl byte-compile-funarg) 3748 (byte-defop-compiler-1 mapl byte-compile-funarg)
3721 (byte-defop-compiler-1 mapcan byte-compile-funarg) 3749 (byte-defop-compiler-1 mapcan byte-compile-funarg)
3722 (byte-defop-compiler-1 mapcon byte-compile-funarg) 3750 (byte-defop-compiler-1 mapcon byte-compile-funarg)
3751 (byte-defop-compiler-1 map-char-table byte-compile-funarg)
3752 (byte-defop-compiler-1 map-database byte-compile-funarg)
3753 (byte-defop-compiler-1 map-extent-children byte-compile-funarg)
3754 (byte-defop-compiler-1 map-extents byte-compile-funarg)
3755 (byte-defop-compiler-1 map-plist byte-compile-funarg)
3756 (byte-defop-compiler-1 map-range-table byte-compile-funarg)
3757 (byte-defop-compiler-1 map-syntax-table byte-compile-funarg)
3758 (byte-defop-compiler-1 mapcar-extents byte-compile-funarg)
3759 (byte-defop-compiler-1 mapcar* byte-compile-funarg)
3760 (byte-defop-compiler-1 maphash byte-compile-funarg)
3723 (byte-defop-compiler-1 let) 3761 (byte-defop-compiler-1 let)
3724 (byte-defop-compiler-1 let*) 3762 (byte-defop-compiler-1 let*)
3725 3763
3726 (defun byte-compile-progn (form) 3764 (defun byte-compile-progn (form)
3727 (byte-compile-body-do-effect (cdr form))) 3765 (byte-compile-body-do-effect (cdr form)))