comparison lisp/emacs-lisp/bytecomp.el @ 105806:83e7d269fc49

(byte-compile-warning-types, byte-compile-warnings): Add `constants' as an option. (byte-compile-callargs-warn, byte-compile-arglist-warn) (display-call-tree): Update for byte-compile-fdefinition possibly returning `(macro lambda ...)'. (Bug#4778) (byte-compile-variable-ref, byte-compile-setq-default): Respect `constants' member of byte-compile-warnings.
author Glenn Morris <rgm@gnu.org>
date Sat, 31 Oct 2009 02:10:43 +0000
parents 338d102432df
children 56392d7b0ff4
comparison
equal deleted inserted replaced
105805:d29fa94d860d 105806:83e7d269fc49
64 ;; - various syntax errors; 64 ;; - various syntax errors;
65 ;; + correct compilation of nested defuns, defmacros, defvars and defsubsts; 65 ;; + correct compilation of nested defuns, defmacros, defvars and defsubsts;
66 ;; + correct compilation of top-level uses of macros; 66 ;; + correct compilation of top-level uses of macros;
67 ;; + the ability to generate a histogram of functions called. 67 ;; + the ability to generate a histogram of functions called.
68 68
69 ;; User customization variables: 69 ;; User customization variables: M-x customize-group bytecomp
70 ;;
71 ;; byte-compile-verbose Whether to report the function currently being
72 ;; compiled in the echo area;
73 ;; byte-optimize Whether to do optimizations; this may be
74 ;; t, nil, 'source, or 'byte;
75 ;; byte-optimize-log Whether to report (in excruciating detail)
76 ;; exactly which optimizations have been made.
77 ;; This may be t, nil, 'source, or 'byte;
78 ;; byte-compile-error-on-warn Whether to stop compilation when a warning is
79 ;; produced;
80 ;; byte-compile-delete-errors Whether the optimizer may delete calls or
81 ;; variable references that are side-effect-free
82 ;; except that they may return an error.
83 ;; byte-compile-generate-call-tree Whether to generate a histogram of
84 ;; function calls. This can be useful for
85 ;; finding unused functions, as well as simple
86 ;; performance metering.
87 ;; byte-compile-warnings List of warnings to issue, or t. May contain
88 ;; `free-vars' (references to variables not in the
89 ;; current lexical scope)
90 ;; `unresolved' (calls to unknown functions)
91 ;; `callargs' (lambda calls with args that don't
92 ;; match the lambda's definition)
93 ;; `redefine' (function cell redefined from
94 ;; a macro to a lambda or vice versa,
95 ;; or redefined to take other args)
96 ;; `obsolete' (obsolete variables and functions)
97 ;; `noruntime' (calls to functions only defined
98 ;; within `eval-when-compile')
99 ;; `cl-functions' (calls to CL functions)
100 ;; `interactive-only' (calls to commands that are
101 ;; not good to call from Lisp)
102 ;; `make-local' (dubious calls to
103 ;; `make-variable-buffer-local')
104 ;; `mapcar' (mapcar called for effect)
105 ;; byte-compile-compatibility Whether the compiler should
106 ;; generate .elc files which can be loaded into
107 ;; generic emacs 18.
108 ;; emacs-lisp-file-regexp Regexp for the extension of source-files;
109 ;; see also the function byte-compile-dest-file.
110 70
111 ;; New Features: 71 ;; New Features:
112 ;; 72 ;;
113 ;; o The form `defsubst' is just like `defun', except that the function 73 ;; o The form `defsubst' is just like `defun', except that the function
114 ;; generated will be open-coded in compiled code which uses it. This 74 ;; generated will be open-coded in compiled code which uses it. This
347 :type 'boolean) 307 :type 'boolean)
348 308
349 (defconst byte-compile-warning-types 309 (defconst byte-compile-warning-types
350 '(redefine callargs free-vars unresolved 310 '(redefine callargs free-vars unresolved
351 obsolete noruntime cl-functions interactive-only 311 obsolete noruntime cl-functions interactive-only
352 make-local mapcar) 312 make-local mapcar constants)
353 "The list of warning types used when `byte-compile-warnings' is t.") 313 "The list of warning types used when `byte-compile-warnings' is t.")
354 (defcustom byte-compile-warnings t 314 (defcustom byte-compile-warnings t
355 "List of warnings that the byte-compiler should issue (t for all). 315 "List of warnings that the byte-compiler should issue (t for all).
356 316
357 Elements of the list may be: 317 Elements of the list may be:
368 distinguished from macros and aliases). 328 distinguished from macros and aliases).
369 interactive-only 329 interactive-only
370 commands that normally shouldn't be called from Lisp code. 330 commands that normally shouldn't be called from Lisp code.
371 make-local calls to make-variable-buffer-local that may be incorrect. 331 make-local calls to make-variable-buffer-local that may be incorrect.
372 mapcar mapcar called for effect. 332 mapcar mapcar called for effect.
333 constants let-binding of, or assignment to, constants/nonvariables.
373 334
374 If the list begins with `not', then the remaining elements specify warnings to 335 If the list begins with `not', then the remaining elements specify warnings to
375 suppress. For example, (not mapcar) will suppress warnings about mapcar." 336 suppress. For example, (not mapcar) will suppress warnings about mapcar."
376 :group 'bytecomp 337 :group 'bytecomp
377 :type `(choice (const :tag "All" t) 338 :type `(choice (const :tag "All" t)
378 (set :menu-tag "Some" 339 (set :menu-tag "Some"
379 (const free-vars) (const unresolved) 340 (const free-vars) (const unresolved)
380 (const callargs) (const redefine) 341 (const callargs) (const redefine)
381 (const obsolete) (const noruntime) 342 (const obsolete) (const noruntime)
382 (const cl-functions) (const interactive-only) 343 (const cl-functions) (const interactive-only)
383 (const make-local) (const mapcar)))) 344 (const make-local) (const mapcar) (const constants))))
384 ;;;###autoload(put 'byte-compile-warnings 'safe-local-variable 'byte-compile-warnings-safe-p) 345 ;;;###autoload(put 'byte-compile-warnings 'safe-local-variable 'byte-compile-warnings-safe-p)
385 346
386 ;;;###autoload 347 ;;;###autoload
387 (defun byte-compile-warnings-safe-p (x) 348 (defun byte-compile-warnings-safe-p (x)
388 "Return non-nil if X is valid as a value of `byte-compile-warnings'." 349 "Return non-nil if X is valid as a value of `byte-compile-warnings'."
1304 ;; Warn if the form is calling a function with the wrong number of arguments. 1265 ;; Warn if the form is calling a function with the wrong number of arguments.
1305 (defun byte-compile-callargs-warn (form) 1266 (defun byte-compile-callargs-warn (form)
1306 (let* ((def (or (byte-compile-fdefinition (car form) nil) 1267 (let* ((def (or (byte-compile-fdefinition (car form) nil)
1307 (byte-compile-fdefinition (car form) t))) 1268 (byte-compile-fdefinition (car form) t)))
1308 (sig (if (and def (not (eq def t))) 1269 (sig (if (and def (not (eq def t)))
1309 (byte-compile-arglist-signature 1270 (progn
1310 (if (memq (car-safe def) '(declared lambda)) 1271 (and (eq (car-safe def) 'macro)
1311 (nth 1 def) 1272 (eq (car-safe (cdr-safe def)) 'lambda)
1312 (if (byte-code-function-p def) 1273 (setq def (cdr def)))
1313 (aref def 0) 1274 (byte-compile-arglist-signature
1314 '(&rest def)))) 1275 (if (memq (car-safe def) '(declared lambda))
1276 (nth 1 def)
1277 (if (byte-code-function-p def)
1278 (aref def 0)
1279 '(&rest def)))))
1315 (if (and (fboundp (car form)) 1280 (if (and (fboundp (car form))
1316 (subrp (symbol-function (car form)))) 1281 (subrp (symbol-function (car form))))
1317 (subr-arity (symbol-function (car form)))))) 1282 (subr-arity (symbol-function (car form))))))
1318 (ncall (length (cdr form)))) 1283 (ncall (length (cdr form))))
1319 ;; Check many or unevalled from subr-arity. 1284 ;; Check many or unevalled from subr-arity.
1404 ;; Warn if the function or macro is being redefined with a different 1369 ;; Warn if the function or macro is being redefined with a different
1405 ;; number of arguments. 1370 ;; number of arguments.
1406 (defun byte-compile-arglist-warn (form macrop) 1371 (defun byte-compile-arglist-warn (form macrop)
1407 (let ((old (byte-compile-fdefinition (nth 1 form) macrop))) 1372 (let ((old (byte-compile-fdefinition (nth 1 form) macrop)))
1408 (if (and old (not (eq old t))) 1373 (if (and old (not (eq old t)))
1409 (let ((sig1 (byte-compile-arglist-signature 1374 (progn
1410 (if (eq 'lambda (car-safe old)) 1375 (and (eq 'macro (car-safe old))
1411 (nth 1 old) 1376 (eq 'lambda (car-safe (cdr-safe old)))
1412 (if (byte-code-function-p old) 1377 (setq old (cdr old)))
1413 (aref old 0) 1378 (let ((sig1 (byte-compile-arglist-signature
1414 '(&rest def))))) 1379 (if (eq 'lambda (car-safe old))
1415 (sig2 (byte-compile-arglist-signature (nth 2 form)))) 1380 (nth 1 old)
1416 (unless (byte-compile-arglist-signatures-congruent-p sig1 sig2) 1381 (if (byte-code-function-p old)
1417 (byte-compile-set-symbol-position (nth 1 form)) 1382 (aref old 0)
1418 (byte-compile-warn 1383 '(&rest def)))))
1419 "%s %s used to take %s %s, now takes %s" 1384 (sig2 (byte-compile-arglist-signature (nth 2 form))))
1420 (if (eq (car form) 'defun) "function" "macro") 1385 (unless (byte-compile-arglist-signatures-congruent-p sig1 sig2)
1421 (nth 1 form) 1386 (byte-compile-set-symbol-position (nth 1 form))
1422 (byte-compile-arglist-signature-string sig1) 1387 (byte-compile-warn
1423 (if (equal sig1 '(1 . 1)) "argument" "arguments") 1388 "%s %s used to take %s %s, now takes %s"
1424 (byte-compile-arglist-signature-string sig2)))) 1389 (if (eq (car form) 'defun) "function" "macro")
1390 (nth 1 form)
1391 (byte-compile-arglist-signature-string sig1)
1392 (if (equal sig1 '(1 . 1)) "argument" "arguments")
1393 (byte-compile-arglist-signature-string sig2)))))
1425 ;; This is the first definition. See if previous calls are compatible. 1394 ;; This is the first definition. See if previous calls are compatible.
1426 (let ((calls (assq (nth 1 form) byte-compile-unresolved-functions)) 1395 (let ((calls (assq (nth 1 form) byte-compile-unresolved-functions))
1427 nums sig min max) 1396 nums sig min max)
1428 (if calls 1397 (if calls
1429 (progn 1398 (progn
3044 (when (symbolp bytecomp-var) 3013 (when (symbolp bytecomp-var)
3045 (byte-compile-set-symbol-position bytecomp-var)) 3014 (byte-compile-set-symbol-position bytecomp-var))
3046 (if (or (not (symbolp bytecomp-var)) 3015 (if (or (not (symbolp bytecomp-var))
3047 (byte-compile-const-symbol-p bytecomp-var 3016 (byte-compile-const-symbol-p bytecomp-var
3048 (not (eq base-op 'byte-varref)))) 3017 (not (eq base-op 'byte-varref))))
3049 (byte-compile-warn 3018 (if (byte-compile-warning-enabled-p 'constants)
3050 (cond ((eq base-op 'byte-varbind) "attempt to let-bind %s `%s'") 3019 (byte-compile-warn
3051 ((eq base-op 'byte-varset) "variable assignment to %s `%s'") 3020 (cond ((eq base-op 'byte-varbind) "attempt to let-bind %s `%s'")
3052 (t "variable reference to %s `%s'")) 3021 ((eq base-op 'byte-varset) "variable assignment to %s `%s'")
3053 (if (symbolp bytecomp-var) "constant" "nonvariable") 3022 (t "variable reference to %s `%s'"))
3054 (prin1-to-string bytecomp-var)) 3023 (if (symbolp bytecomp-var) "constant" "nonvariable")
3024 (prin1-to-string bytecomp-var)))
3055 (and (get bytecomp-var 'byte-obsolete-variable) 3025 (and (get bytecomp-var 'byte-obsolete-variable)
3056 (not (memq bytecomp-var byte-compile-not-obsolete-vars)) 3026 (not (memq bytecomp-var byte-compile-not-obsolete-vars))
3057 (byte-compile-warn-obsolete bytecomp-var)) 3027 (byte-compile-warn-obsolete bytecomp-var))
3058 (if (byte-compile-warning-enabled-p 'free-vars) 3028 (if (byte-compile-warning-enabled-p 'free-vars)
3059 (if (eq base-op 'byte-varbind) 3029 (if (eq base-op 'byte-varbind)
3580 (defun byte-compile-setq-default (form) 3550 (defun byte-compile-setq-default (form)
3581 (let ((bytecomp-args (cdr form)) 3551 (let ((bytecomp-args (cdr form))
3582 setters) 3552 setters)
3583 (while bytecomp-args 3553 (while bytecomp-args
3584 (let ((var (car bytecomp-args))) 3554 (let ((var (car bytecomp-args)))
3585 (if (or (not (symbolp var)) 3555 (and (or (not (symbolp var))
3586 (byte-compile-const-symbol-p var t)) 3556 (byte-compile-const-symbol-p var t))
3587 (byte-compile-warn 3557 (byte-compile-warning-enabled-p 'constants)
3588 "variable assignment to %s `%s'" 3558 (byte-compile-warn
3589 (if (symbolp var) "constant" "nonvariable") 3559 "variable assignment to %s `%s'"
3590 (prin1-to-string var))) 3560 (if (symbolp var) "constant" "nonvariable")
3561 (prin1-to-string var)))
3591 (push (list 'set-default (list 'quote var) (car (cdr bytecomp-args))) 3562 (push (list 'set-default (list 'quote var) (car (cdr bytecomp-args)))
3592 setters)) 3563 setters))
3593 (setq bytecomp-args (cdr (cdr bytecomp-args)))) 3564 (setq bytecomp-args (cdr (cdr bytecomp-args))))
3594 (byte-compile-form (cons 'progn (nreverse setters))))) 3565 (byte-compile-form (cons 'progn (nreverse setters)))))
3595 3566
4327 (insert "\n")))) 4298 (insert "\n"))))
4328 (setq rest (cdr rest))) 4299 (setq rest (cdr rest)))
4329 4300
4330 (message "Generating call tree...(finding uncalled functions...)") 4301 (message "Generating call tree...(finding uncalled functions...)")
4331 (setq rest byte-compile-call-tree) 4302 (setq rest byte-compile-call-tree)
4332 (let ((uncalled nil)) 4303 (let (uncalled def)
4333 (while rest 4304 (while rest
4334 (or (nth 1 (car rest)) 4305 (or (nth 1 (car rest))
4335 (null (setq f (car (car rest)))) 4306 (null (setq f (caar rest)))
4336 (functionp (byte-compile-fdefinition f t)) 4307 (progn
4337 (commandp (byte-compile-fdefinition f nil)) 4308 (setq def (byte-compile-fdefinition f t))
4309 (and (eq (car-safe def) 'macro)
4310 (eq (car-safe (cdr-safe def)) 'lambda)
4311 (setq def (cdr def)))
4312 (functionp def))
4313 (progn
4314 (setq def (byte-compile-fdefinition f nil))
4315 (and (eq (car-safe def) 'macro)
4316 (eq (car-safe (cdr-safe def)) 'lambda)
4317 (setq def (cdr def)))
4318 (commandp def))
4338 (setq uncalled (cons f uncalled))) 4319 (setq uncalled (cons f uncalled)))
4339 (setq rest (cdr rest))) 4320 (setq rest (cdr rest)))
4340 (if uncalled 4321 (if uncalled
4341 (let ((fill-prefix " ")) 4322 (let ((fill-prefix " "))
4342 (insert "Noninteractive functions not known to be called:\n ") 4323 (insert "Noninteractive functions not known to be called:\n ")
4343 (setq p (point)) 4324 (setq p (point))
4344 (insert (mapconcat 'symbol-name (nreverse uncalled) ", ")) 4325 (insert (mapconcat 'symbol-name (nreverse uncalled) ", "))
4345 (fill-region-as-paragraph p (point))))) 4326 (fill-region-as-paragraph p (point))))))
4346 ) 4327 (message "Generating call tree...done.")))
4347 (message "Generating call tree...done.")
4348 ))
4349 4328
4350 4329
4351 ;;;###autoload 4330 ;;;###autoload
4352 (defun batch-byte-compile-if-not-done () 4331 (defun batch-byte-compile-if-not-done ()
4353 "Like `byte-compile-file' but doesn't recompile if already up to date. 4332 "Like `byte-compile-file' but doesn't recompile if already up to date.