1 ;;; elint.el --- Lint Emacs Lisp
3 ;; Copyright (C) 1997, 2001-2012 Free Software Foundation, Inc.
5 ;; Author: Peter Liljenberg <petli@lysator.liu.se>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This is a linter for Emacs Lisp. Currently, it mainly catches
27 ;; misspellings and undefined variables, although it can also catch
28 ;; function calls with the wrong number of arguments.
30 ;; To use, call elint-current-buffer or elint-defun to lint a buffer
31 ;; or defun. The first call runs `elint-initialize' to set up some
32 ;; argument data, which may take a while.
34 ;; The linter will try to "include" any require'd libraries to find
35 ;; the variables defined in those. There is a fair amount of voodoo
36 ;; involved in this, but it seems to work in normal situations.
40 ;; * Adding type checking. (Stop that sniggering!)
41 ;; * Make eval-when-compile be sensitive to the difference between
43 ;; * Requires within function bodies.
44 ;; * Handle defstruct.
45 ;; * Prevent recursive requires.
50 "Linting for Emacs Lisp."
54 (defcustom elint-log-buffer
"*Elint*"
55 "The buffer in which to log lint messages."
60 (defcustom elint-scan-preloaded t
61 "Non-nil means to scan `preloaded-file-list' when initializing.
62 Otherwise, just scan the DOC file for functions and variables.
63 This is faster, but less accurate, since it misses undocumented features.
64 This may result in spurious warnings about unknown functions, etc."
70 (defcustom elint-ignored-warnings nil
71 "If non-nil, a list of issue types that Elint should ignore.
72 This is useful if Elint has trouble understanding your code and
73 you need to suppress lots of spurious warnings. The valid list elements
74 are as follows, and suppress messages about the indicated features:
75 undefined-functions - calls to unknown functions
76 unbound-reference - reference to unknown variables
77 unbound-assignment - assignment to unknown variables
78 macro-expansions - failure to expand macros
79 empty-let - let-bindings with empty variable lists"
80 :type
'(choice (const :tag
"Don't suppress any warnings" nil
)
81 (repeat :tag
"List of issues to ignore"
82 (choice (const undefined-functions
83 :tag
"Calls to unknown functions")
84 (const unbound-reference
85 :tag
"Reference to unknown variables")
86 (const unbound-assignment
87 :tag
"Assignment to unknown variables")
88 (const macro-expansion
89 :tag
"Failure to expand macros")
91 :tag
"Let-binding with empty varlist"))))
92 :safe
(lambda (value) (or (null value
)
108 (defcustom elint-directory-skip-re
"\\(ldefs-boot\\|loaddefs\\)\\.el\\'"
109 "If nil, a regexp matching files to skip when linting a directory."
110 :type
'(choice (const :tag
"Lint all files" nil
)
111 (regexp :tag
"Regexp to skip"))
112 :safe
'string-or-null-p
120 (defconst elint-standard-variables
121 ;; Most of these are defined in C with no documentation.
122 ;; FIXME I don't see why they shouldn't just get doc-strings.
123 '(vc-mode local-write-file-hooks activate-menubar-hook buffer-name-history
124 coding-system-history extended-command-history
126 "Standard variables, excluding `elint-builtin-variables'.
127 These are variables that we cannot detect automatically for some reason.")
129 (defvar elint-builtin-variables nil
130 "List of built-in variables. Set by `elint-initialize'.
131 This is actually all those documented in the DOC file, which includes
132 built-in variables and those from dumped Lisp files.")
134 (defvar elint-autoloaded-variables nil
135 "List of `loaddefs.el' variables. Set by `elint-initialize'.")
137 (defvar elint-preloaded-env nil
138 "Environment defined by the preloaded (dumped) Lisp files.
139 Set by `elint-initialize', if `elint-scan-preloaded' is non-nil.")
141 (defconst elint-unknown-builtin-args
142 ;; encode-time allows extra arguments for use with decode-time.
143 ;; For some reason, some people seem to like to use them in other cases.
144 '((encode-time second minute hour day month year
&rest zone
))
145 "Those built-ins for which we can't find arguments, if any.")
147 (defvar elint-extra-errors
'(file-locked file-supersession ftp-error
)
148 "Errors without `error-message' or `error-conditions' properties.")
150 (defconst elint-preloaded-skip-re
151 (regexp-opt '("loaddefs.el" "loadup.el" "cus-start" "language/"
152 "eucjp-ms" "mule-conf" "/characters" "/charprop"
154 "Regexp matching elements of `preloaded-file-list' to ignore.
155 We ignore them because they contain no definitions of use to Elint.")
161 (defsubst elint-make-top-form
(form pos
)
163 FORM is the form, and POS is the point where it starts in the buffer."
166 (defsubst elint-top-form-form
(top-form)
167 "Extract the form from a TOP-FORM."
170 (defsubst elint-top-form-pos
(top-form)
171 "Extract the position from a TOP-FORM."
178 (defsubst elint-make-env
()
179 "Create an empty environment."
180 (list (list nil
) nil nil
))
182 (defsubst elint-env-add-env
(env newenv
)
183 "Augment ENV with NEWENV.
184 None of them is modified, and the new env is returned."
185 (list (append (car env
) (car newenv
))
186 (append (cadr env
) (cadr newenv
))
187 (append (car (cdr (cdr env
))) (car (cdr (cdr newenv
))))))
189 (defsubst elint-env-add-var
(env var
)
190 "Augment ENV with the variable VAR.
191 The new environment is returned, the old is unmodified."
192 (cons (cons (list var
) (car env
)) (cdr env
)))
194 (defsubst elint-env-add-global-var
(env var
)
195 "Augment ENV with the variable VAR.
196 ENV is modified so VAR is seen everywhere.
198 (nconc (car env
) (list (list var
)))
201 (defsubst elint-env-find-var
(env var
)
202 "Non-nil if ENV contains the variable VAR.
203 Actually, a list with VAR as a single element is returned."
204 (assq var
(car env
)))
206 (defsubst elint-env-add-func
(env func args
)
207 "Augment ENV with the function FUNC, which has the arguments ARGS.
208 The new environment is returned, the old is unmodified."
210 (cons (list func args
) (cadr env
))
211 (car (cdr (cdr env
)))))
213 (defsubst elint-env-find-func
(env func
)
214 "Non-nil if ENV contains the function FUNC.
215 Actually, a list of (FUNC ARGS) is returned."
216 (assq func
(cadr env
)))
218 (defsubst elint-env-add-macro
(env macro def
)
219 "Augment ENV with the macro named MACRO.
220 DEF is the macro definition (a lambda expression or similar).
221 The new environment is returned, the old is unmodified."
224 (cons (cons macro def
) (car (cdr (cdr env
))))))
226 (defsubst elint-env-macro-env
(env)
227 "Return the macro environment of ENV.
228 This environment can be passed to `macroexpand'."
229 (car (cdr (cdr env
))))
231 (defsubst elint-env-macrop
(env macro
)
232 "Non-nil if ENV contains MACRO."
233 (assq macro
(elint-env-macro-env env
)))
240 (defun elint-file (file)
241 "Lint the file FILE."
242 (interactive "fElint file: ")
243 (setq file
(expand-file-name file
))
244 (or elint-builtin-variables
246 (let ((dir (file-name-directory file
)))
247 (let ((default-directory dir
))
249 (elint-set-mode-line t
)
250 (with-current-buffer elint-log-buffer
251 (unless (string-equal default-directory dir
)
252 (elint-log-message (format "\f\nLeaving directory `%s'"
253 default-directory
) t
)
254 (elint-log-message (format "Entering directory `%s'" dir
) t
)
255 (setq default-directory dir
))))
256 (let ((str (format "Linting file %s" file
)))
257 (message "%s..." str
)
259 (elint-log-message (format "\f\n%s at %s" str
(current-time-string)) t
))
260 ;; elint-current-buffer clears log.
262 (insert-file-contents file
)
263 (let ((buffer-file-name file
)
264 (max-lisp-eval-depth (max 1000 max-lisp-eval-depth
)))
265 (with-syntax-table emacs-lisp-mode-syntax-table
266 (mapc 'elint-top-form
(elint-update-env)))))
267 (elint-set-mode-line)
268 (message "%s...done" str
)))
270 ;; cf byte-recompile-directory.
272 (defun elint-directory (directory)
273 "Lint all the .el files in DIRECTORY.
274 A complicated directory may require a lot of memory."
275 (interactive "DElint directory: ")
276 (let ((elint-running t
))
277 (dolist (file (directory-files directory t
))
278 ;; Bytecomp has emacs-lisp-file-regexp.
279 (when (and (string-match "\\.el\\'" file
)
280 (file-readable-p file
)
281 (not (auto-save-file-name-p file
)))
282 (if (string-match elint-directory-skip-re file
)
283 (message "Skipping file %s" file
)
284 (elint-file file
)))))
285 (elint-set-mode-line))
288 (defun elint-current-buffer ()
289 "Lint the current buffer.
290 If necessary, this first calls `elint-initialize'."
292 (or elint-builtin-variables
294 (elint-clear-log (format "Linting %s" (or (buffer-file-name)
297 (elint-set-mode-line t
)
298 (mapc 'elint-top-form
(elint-update-env))
299 ;; Tell the user we're finished. This is terribly kludgy: we set
300 ;; elint-top-form-logged so elint-log-message doesn't print the
301 ;; ** top form ** header...
302 (elint-set-mode-line)
303 (elint-log-message "\nLinting finished.\n" t
))
307 (defun elint-defun ()
308 "Lint the function at point.
309 If necessary, this first calls `elint-initialize'."
311 (or elint-builtin-variables
314 (or (beginning-of-defun) (error "Lint what?"))
316 (def (read (current-buffer))))
319 (elint-top-form (elint-make-top-form def pos
)))))
322 ;;; Top form functions
325 (defvar elint-buffer-env nil
326 "The environment of an elisp buffer.
327 Will be local in linted buffers.")
329 (defvar elint-buffer-forms nil
330 "The top forms in a buffer.
331 Will be local in linted buffers.")
333 (defvar elint-last-env-time nil
334 "The last time the buffers env was updated.
335 Is measured in buffer-modified-ticks and is local in linted buffers.")
337 ;; This is a minor optimization. It is local to every buffer, and so
338 ;; does not prevent recursive requires. It does not list the requires
340 (defvar elint-features nil
341 "List of all libraries this buffer has required, or that have been provided.")
343 (defun elint-update-env ()
344 "Update the elint environment in the current buffer.
345 Don't do anything if the buffer hasn't been changed since this
346 function was called the last time.
348 (if (and (local-variable-p 'elint-buffer-env
(current-buffer))
349 (local-variable-p 'elint-buffer-forms
(current-buffer))
350 (local-variable-p 'elint-last-env-time
(current-buffer))
351 (= (buffer-modified-tick) elint-last-env-time
))
355 (set (make-local-variable 'elint-buffer-forms
) (elint-get-top-forms))
356 (set (make-local-variable 'elint-features
) nil
)
357 (set (make-local-variable 'elint-buffer-env
)
358 (elint-init-env elint-buffer-forms
))
359 (if elint-preloaded-env
360 ;; FIXME: This doesn't do anything! Should we setq the result to
362 (elint-env-add-env elint-preloaded-env elint-buffer-env
))
363 (set (make-local-variable 'elint-last-env-time
) (buffer-modified-tick))
366 (defun elint-get-top-forms ()
367 "Collect all the top forms in the current buffer."
370 (goto-char (point-min))
371 (while (elint-find-next-top-form)
372 (let ((elint-current-pos (point)))
373 ;; non-list check could be here too. errors may be out of seq.
374 ;; quoted check cannot be elsewhere, since quotes skipped.
375 (if (looking-back "'")
376 ;; Eg cust-print.el uses ' as a comment syntax.
377 (elint-warning "Skipping quoted form `'%.20s...'"
378 (read (current-buffer)))
381 (elint-make-top-form (read (current-buffer))
385 (goto-char elint-current-pos
)
386 (error "Missing ')' in top form: %s"
387 (buffer-substring elint-current-pos
388 (line-end-position))))))))
391 (defun elint-find-next-top-form ()
392 "Find the next top form from point.
393 Return nil if there are no more forms, t otherwise."
394 (parse-partial-sexp (point) (point-max) nil t
)
397 (defvar elint-env
) ; from elint-init-env
399 (defun elint-init-form (form)
400 "Process FORM, adding to ELINT-ENV if recognized."
402 ;; Eg nnmaildir seems to use [] as a form of comment syntax.
404 (elint-warning "Skipping non-list form `%s'" form
))
405 ;; Add defined variable
406 ((memq (car form
) '(defvar defconst defcustom
))
407 (setq elint-env
(elint-env-add-var elint-env
(cadr form
))))
409 ((memq (car form
) '(defun defsubst))
410 (setq elint-env
(elint-env-add-func elint-env
(cadr form
) (nth 2 form
))))
411 ;; FIXME needs a handler to say second arg is not a variable when we come
413 ((eq (car form
) 'define-derived-mode
)
414 (setq elint-env
(elint-env-add-func elint-env
(cadr form
) ())
415 elint-env
(elint-env-add-var elint-env
(cadr form
))
416 elint-env
(elint-env-add-var elint-env
417 (intern (format "%s-map" (cadr form
))))))
418 ((eq (car form
) 'define-minor-mode
)
419 (setq elint-env
(elint-env-add-func elint-env
(cadr form
) '(&optional arg
))
421 elint-env
(elint-env-add-var elint-env
(cadr form
))))
422 ((and (eq (car form
) 'easy-menu-define
)
424 (setq elint-env
(elint-env-add-func elint-env
(cadr form
) '(event))
425 elint-env
(elint-env-add-var elint-env
(cadr form
))))
426 ;; FIXME it would be nice to check the autoloads are correct.
427 ((eq (car form
) 'autoload
)
428 (setq elint-env
(elint-env-add-func elint-env
(cadr (cadr form
)) 'unknown
)))
429 ((eq (car form
) 'declare-function
)
430 (setq elint-env
(elint-env-add-func
431 elint-env
(cadr form
)
432 (if (or (< (length form
) 4)
434 (unless (stringp (nth 2 form
))
435 (elint-error "Malformed declaration for `%s'"
440 ((and (eq (car form
) 'defalias
) (listp (nth 2 form
)))
441 ;; If the alias points to something already in the environment,
442 ;; add the alias to the environment with the same arguments.
443 ;; FIXME symbol-function, eg backquote.el?
444 (let ((def (elint-env-find-func elint-env
(cadr (nth 2 form
)))))
445 (setq elint-env
(elint-env-add-func elint-env
(cadr (cadr form
))
446 (if def
(cadr def
) 'unknown
)))))
447 ;; Add macro, both as a macro and as a function
448 ((eq (car form
) 'defmacro
)
449 (setq elint-env
(elint-env-add-macro elint-env
(cadr form
)
450 (cons 'lambda
(cddr form
)))
451 elint-env
(elint-env-add-func elint-env
(cadr form
) (nth 2 form
))))
452 ((and (eq (car form
) 'put
)
454 (eq (car-safe (cadr form
)) 'quote
)
455 (equal (nth 2 form
) '(quote error-conditions
)))
456 (set (make-local-variable 'elint-extra-errors
)
457 (cons (cadr (cadr form
)) elint-extra-errors
)))
458 ((eq (car form
) 'provide
)
459 (add-to-list 'elint-features
(eval (cadr form
))))
460 ;; Import variable definitions
461 ((memq (car form
) '(require cc-require cc-require-when-compile
))
462 (let ((name (eval (cadr form
)))
463 (file (eval (nth 2 form
)))
464 (elint-doing-cl (bound-and-true-p elint-doing-cl
)))
465 (unless (memq name elint-features
)
466 (add-to-list 'elint-features name
)
467 ;; cl loads cl-macs in an opaque manner.
468 ;; Since cl-macs requires cl, we can just process cl-macs.
469 (and (eq name
'cl
) (not elint-doing-cl
)
470 ;; We need cl if elint-form is to be able to expand cl macros.
474 elint-doing-cl t
)) ; blech
475 (setq elint-env
(elint-add-required-env elint-env name file
))))))
478 (defun elint-init-env (forms)
479 "Initialize the environment from FORMS."
480 (let ((elint-env (elint-make-env))
483 (setq form
(elint-top-form-form (car forms
))
485 ;; FIXME eval-when-compile should be treated differently (macros).
486 ;; Could bind something that makes elint-init-form only check
488 (if (memq (car-safe form
)
489 '(eval-and-compile eval-when-compile progn prog1 prog2
491 (mapc 'elint-init-form
(cdr form
))
492 (elint-init-form form
)))
495 (defun elint-add-required-env (env name file
)
496 "Augment ENV with the variables defined by feature NAME in FILE."
498 (let* ((libname (if (stringp file
)
502 ;; First try to find .el files, then the raw name
503 (lib1 (locate-library (concat libname
".el") t
))
504 (lib (or lib1
(locate-library libname t
))))
505 ;; Clear the messages :-/
506 ;; (Messes up the "Initializing elint..." message.)
509 (with-current-buffer (find-file-noselect lib
)
510 ;; FIXME this doesn't use a temp buffer, because it
511 ;; stores the result in buffer-local variables so that
514 (setq env
(elint-env-add-env env elint-buffer-env
)))
515 ;;; (with-temp-buffer
516 ;;; (insert-file-contents lib)
517 ;;; (with-syntax-table emacs-lisp-mode-syntax-table
518 ;;; (elint-update-env))
519 ;;; (setq env (elint-env-add-env env elint-buffer-env))))
520 ;;(message "Elint processed (require '%s)" name))
521 (error "%s.el not found in load-path" libname
)))
523 (message "Can't get variables from require'd library %s: %s"
524 name
(error-message-string err
))))
527 (defvar elint-top-form nil
528 "The currently linted top form, or nil.")
530 (defvar elint-top-form-logged nil
531 "The value t if the currently linted top form has been mentioned in the log buffer.")
533 (defun elint-top-form (form)
535 (let ((elint-top-form form
)
536 (elint-top-form-logged nil
)
537 (elint-current-pos (elint-top-form-pos form
)))
538 (elint-form (elint-top-form-form form
) elint-buffer-env
)))
541 ;;; General form linting functions
544 (defconst elint-special-forms
545 '((let . elint-check-let-form
)
546 (let* . elint-check-let-form
)
547 (setq . elint-check-setq-form
)
548 (quote . elint-check-quote-form
)
549 (function . elint-check-quote-form
)
550 (cond . elint-check-cond-form
)
551 (lambda . elint-check-defun-form
)
552 (function . elint-check-function-form
)
553 (setq-default . elint-check-setq-form
)
554 (defalias . elint-check-defalias-form
)
555 (defun . elint-check-defun-form
)
556 (defsubst . elint-check-defun-form
)
557 (defmacro . elint-check-defun-form
)
558 (defvar . elint-check-defvar-form
)
559 (defconst . elint-check-defvar-form
)
560 (defcustom . elint-check-defcustom-form
)
561 (macro . elint-check-macro-form
)
562 (condition-case . elint-check-condition-case-form
)
563 (if . elint-check-conditional-form
)
564 (when . elint-check-conditional-form
)
565 (unless . elint-check-conditional-form
)
566 (and . elint-check-conditional-form
)
567 (or . elint-check-conditional-form
))
568 "Functions to call when some special form should be linted.")
570 (defun elint-form (form env
&optional nohandler
)
571 "Lint FORM in the environment ENV.
572 Optional argument NOHANDLER non-nil means ignore `elint-special-forms'.
573 Returns the environment created by the form."
576 (let ((func (cdr (assq (car form
) elint-special-forms
))))
577 (if (and func
(not nohandler
))
579 (funcall func form env
)
581 (let* ((func (car form
))
582 (args (elint-get-args func env
))
585 ((eq args
'undefined
)
587 (or (memq 'undefined-functions elint-ignored-warnings
)
588 (elint-error "Call to undefined function: %s" func
)))
590 ((eq args
'unknown
) nil
)
592 (t (setq argsok
(elint-match-args form args
))))
595 (if (elint-env-macrop env func
)
596 ;; Macro defined in buffer, expand it
598 ;; FIXME error if macro uses macro, eg bytecomp.el.
601 (macroexpand form
(elint-env-macro-env env
)) env
)
603 (or (memq 'macro-expansion elint-ignored-warnings
)
604 (elint-error "Elint failed to expand macro: %s" func
))
608 (let ((fcode (if (symbolp func
)
610 (indirect-function func
))
612 (if (and (listp fcode
) (eq (car fcode
) 'macro
))
613 ;; Macro defined outside buffer
615 (elint-form (macroexpand form
) env
)
617 ;; Function, lint its parameters
618 (elint-forms (cdr form
) env
))))))))
620 ;; :foo variables are quoted
621 (and (/= (aref (symbol-name form
) 0) ?
:)
622 (not (memq 'unbound-reference elint-ignored-warnings
))
623 (elint-unbound-variable form env
)
624 (elint-warning "Reference to unbound symbol: %s" form
))
629 (defun elint-forms (forms env
)
630 "Lint the FORMS, accumulating an environment, starting with ENV."
631 ;; grumblegrumbletailrecursiongrumblegrumble
633 (dolist (f forms env
)
634 (setq env
(elint-form f env
)))
636 (elint-error "Elint failed to parse form: %s" forms
)
639 (defvar elint-bound-variable nil
640 "Name of a temporarily bound symbol.")
642 (defun elint-unbound-variable (var env
)
643 "Return t if VAR is unbound in ENV."
644 ;; #1063 suggests adding (symbol-file var) here, but I don't think
645 ;; this is right, because it depends on what files you happen to have
646 ;; loaded at the time, which might not be the same when the code runs.
647 ;; It also suggests adding:
648 ;; (numberp (get var 'variable-documentation))
649 ;; (numberp (cdr-safe (get var 'variable-documentation)))
650 ;; but this is not needed now elint-scan-doc-file exists.
651 (not (or (memq var
'(nil t
))
652 (eq var elint-bound-variable
)
653 (elint-env-find-var env var
)
654 (memq var elint-builtin-variables
)
655 (memq var elint-autoloaded-variables
)
656 (memq var elint-standard-variables
))))
659 ;;; Function argument checking
662 (defun elint-match-args (arglist argpattern
)
663 "Match ARGLIST against ARGPATTERN."
670 ((and (null al
) (null ap
)) nil
)
671 ((eq (car ap
) '&optional
)
672 (setq state
'optional
)
675 ((eq (car ap
) '&rest
)
677 ((or (and (eq state
'all
) (or (null al
) (null ap
)))
678 (and (eq state
'optional
) (and al
(null ap
))))
679 (elint-error "Wrong number of args: %s, %s" arglist argpattern
)
682 ((and (eq state
'optional
) (null al
))
689 (defvar elint-bound-function nil
690 "Name of a temporarily bound function symbol.")
692 (defun elint-get-args (func env
)
693 "Find the args of FUNC in ENV.
694 Returns `unknown' if we couldn't find arguments."
695 (let ((f (elint-env-find-func env func
)))
699 (if (eq func elint-bound-function
)
702 (let ((fcode (indirect-function func
)))
704 ;; FIXME builtins with no args have args = nil.
705 (or (get func
'elint-args
) 'unknown
)
706 (elint-find-args-in-code fcode
)))
708 (elint-find-args-in-code func
)))))
710 (defun elint-find-args-in-code (code)
711 "Extract the arguments from CODE.
712 CODE can be a lambda expression, a macro, or byte-compiled code."
714 ((byte-code-function-p code
)
716 ((and (listp code
) (eq (car code
) 'lambda
))
718 ((and (listp code
) (eq (car code
) 'macro
))
719 (elint-find-args-in-code (cdr code
)))
723 ;;; Functions to check some special forms
726 (defun elint-check-cond-form (form env
)
727 "Lint a cond FORM in ENV."
728 (dolist (f (cdr form
))
730 (let ((test (car f
)))
731 (cond ((equal test
'(featurep (quote xemacs
))))
732 ((equal test
'(not (featurep (quote emacs
)))))
733 ;; FIXME (and (boundp 'foo)
734 ((and (eq (car-safe test
) 'fboundp
)
736 (eq (car-safe (cadr test
)) 'quote
))
737 (let ((elint-bound-function (cadr (cadr test
))))
738 (elint-forms f env
)))
739 ((and (eq (car-safe test
) 'boundp
)
741 (eq (car-safe (cadr test
)) 'quote
))
742 (let ((elint-bound-variable (cadr (cadr test
))))
743 (elint-forms f env
)))
744 (t (elint-forms f env
))))
745 (elint-error "cond clause should be a list: %s" f
)))
748 (defun elint-check-defun-form (form env
)
749 "Lint a defun/defmacro/lambda FORM in ENV."
750 (setq form
(if (eq (car form
) 'lambda
) (cdr form
) (cddr form
)))
752 (or (memq p
'(&optional
&rest
))
753 (setq env
(elint-env-add-var env p
))))
755 (elint-forms (cdr form
) env
))
757 (defun elint-check-defalias-form (form env
)
758 "Lint a defalias FORM in ENV."
759 (let ((alias (cadr form
))
760 (target (nth 2 form
)))
761 (and (eq (car-safe alias
) 'quote
)
762 (eq (car-safe target
) 'quote
)
763 (eq (elint-get-args (cadr target
) env
) 'undefined
)
764 (elint-warning "Alias `%s' has unknown target `%s'"
765 (cadr alias
) (cadr target
))))
766 (elint-form form env t
))
768 (defun elint-check-let-form (form env
)
769 "Lint the let/let* FORM in ENV."
770 (let ((varlist (cadr form
)))
772 (if (> (length form
) 2)
773 ;; An empty varlist is not really an error. Eg some cl macros
774 ;; can expand to such a form.
776 (or (memq 'empty-let elint-ignored-warnings
)
777 (elint-warning "Empty varlist in let: %s" form
))
778 ;; Lint the body forms
779 (elint-forms (cddr form
) env
))
780 (elint-error "Malformed let: %s" form
)
782 ;; Check for (let (a (car b)) ...) type of error
783 (if (and (= (length varlist
) 2)
784 (symbolp (car varlist
))
785 (listp (car (cdr varlist
)))
786 (fboundp (car (car (cdr varlist
)))))
787 (elint-warning "Suspect varlist: %s" form
))
788 ;; Add variables to environment, and check the init values
793 (setq newenv
(elint-env-add-var newenv s
)))
794 ((and (consp s
) (<= (length s
) 2))
796 (if (eq (car form
) 'let
)
800 (elint-env-add-var newenv
(car s
))))
802 "Malformed `let' declaration: %s" s
))))
805 ;; Lint the body forms
806 (elint-forms (cddr form
) newenv
)))))
808 (defun elint-check-setq-form (form env
)
809 "Lint the setq FORM in ENV."
810 (or (= (mod (length form
) 2) 1)
811 ;; (setq foo) is valid and equivalent to (setq foo nil).
812 (elint-warning "Missing value in setq: %s" form
))
815 (setq form
(cdr form
))
819 form
(cdr (cdr form
)))
821 (and (not (memq 'unbound-assignment elint-ignored-warnings
))
822 (elint-unbound-variable sym newenv
)
823 (elint-warning "Setting previously unbound symbol: %s" sym
))
824 (elint-error "Setting non-symbol in setq: %s" sym
))
825 (elint-form val newenv
)
827 (setq newenv
(elint-env-add-var newenv sym
))))
830 (defun elint-check-defvar-form (form env
)
831 "Lint the defvar/defconst FORM in ENV."
832 (if (or (= (length form
) 2)
834 ;; Eg the defcalcmodevar macro can expand with a nil doc-string.
835 (and (= (length form
) 4) (string-or-null-p (nth 3 form
))))
836 (elint-env-add-global-var (elint-form (nth 2 form
) env
)
838 (elint-error "Malformed variable declaration: %s" form
)
841 (defun elint-check-defcustom-form (form env
)
842 "Lint the defcustom FORM in ENV."
843 (if (and (> (length form
) 3)
844 ;; even no. of keyword/value args ?
845 (zerop (logand (length form
) 1)))
846 (elint-env-add-global-var (elint-form (nth 2 form
) env
)
848 (elint-error "Malformed variable declaration: %s" form
)
851 (defun elint-check-function-form (form env
)
852 "Lint the function FORM in ENV."
853 (let ((func (car (cdr-safe form
))))
856 (or (elint-env-find-func env func
)
857 ;; FIXME potentially bogus, since it uses the current
858 ;; environment rather than a clean one.
860 (elint-warning "Reference to undefined function: %s" form
))
862 ((and (consp func
) (memq (car func
) '(lambda macro
)))
863 (elint-form func env
))
865 (t (elint-error "Not a function object: %s" form
)
868 (defun elint-check-quote-form (form env
)
869 "Lint the quote FORM in ENV."
872 (defun elint-check-macro-form (form env
)
873 "Check the macro FORM in ENV."
874 (elint-check-function-form (list (car form
) (cdr form
)) env
))
876 (defun elint-check-condition-case-form (form env
)
877 "Check the `condition-case' FORM in ENV."
879 (if (< (length form
) 3)
880 (elint-error "Malformed condition-case: %s" form
)
881 (or (symbolp (cadr form
))
882 (elint-warning "First parameter should be a symbol: %s" form
))
883 (setq resenv
(elint-form (nth 2 form
) env
))
884 (let ((newenv (elint-env-add-var env
(cadr form
)))
886 (dolist (err (nthcdr 3 form
))
887 (setq errlist
(car err
))
889 (or (get s
'error-conditions
)
890 (get s
'error-message
)
891 (memq s elint-extra-errors
)
893 "Not an error symbol in error handler: %s" s
)))
895 ((symbolp errlist
) (list errlist
))
896 ((listp errlist
) errlist
)
897 (t (elint-error "Bad error list in error handler: %s"
900 (elint-forms (cdr err
) newenv
))))
903 ;; For the featurep parts, an alternative is to have
904 ;; elint-get-top-forms skip the irrelevant branches.
905 (defun elint-check-conditional-form (form env
)
906 "Check the when/unless/and/or FORM in ENV.
907 Does basic handling of `featurep' tests."
908 (let ((func (car form
))
911 ;; Misses things like (and t (featurep 'xemacs))
912 ;; Check byte-compile-maybe-guarded.
913 (cond ((and (memq func
'(when and
))
914 (eq (car-safe test
) 'boundp
)
916 (eq (car-safe (cadr test
)) 'quote
))
917 ;; Cf elint-check-let-form, which modifies the whole ENV.
918 (let ((elint-bound-variable (cadr (cadr test
))))
919 (elint-form form env t
)))
920 ((and (memq func
'(when and
))
921 (eq (car-safe test
) 'fboundp
)
923 (eq (car-safe (cadr test
)) 'quote
))
924 (let ((elint-bound-function (cadr (cadr test
))))
925 (elint-form form env t
)))
926 ;; Let's not worry about (if (not (boundp...
928 (eq (car-safe test
) 'boundp
)
930 (eq (car-safe (cadr test
)) 'quote
))
931 (let ((elint-bound-variable (cadr (cadr test
))))
932 (elint-form (nth 2 form
) env
))
933 (dolist (f (nthcdr 3 form
))
936 (eq (car-safe test
) 'fboundp
)
938 (eq (car-safe (cadr test
)) 'quote
))
939 (let ((elint-bound-function (cadr (cadr test
))))
940 (elint-form (nth 2 form
) env
))
941 (dolist (f (nthcdr 3 form
))
943 ((and (memq func
'(when and
)) ; skip all
945 (member test
'((featurep (quote xemacs
))
946 (not (featurep (quote emacs
)))))
947 (and (eq (car-safe test
) 'and
)
948 (equal (car-safe (cdr test
))
949 '(featurep (quote xemacs
)))))))
950 ((and (memq func
'(unless or
))
951 (equal test
'(featurep (quote emacs
)))))
953 (or (null test
) ; eg custom-browse-insert-prefix
954 (member test
'((featurep (quote xemacs
))
955 (not (featurep (quote emacs
)))))
956 (and (eq (car-safe test
) 'and
)
957 (equal (car-safe (cdr test
))
958 '(featurep (quote xemacs
))))))
959 (dolist (f (nthcdr 3 form
))
960 (elint-form f env
))) ; lint the else branch
962 (equal test
'(featurep (quote emacs
))))
963 (elint-form (nth 2 form
) env
)) ; lint the if branch
964 ;; Process conditional as normal, without handler.
966 (elint-form form env t
))))
970 ;;; Message functions
973 (defvar elint-current-pos
) ; dynamically bound in elint-top-form
975 (defun elint-log (type string args
)
976 (elint-log-message (format "%s:%d:%s: %s"
977 (let ((f (buffer-file-name)))
979 (file-name-nondirectory f
)
981 (if (boundp 'elint-current-pos
)
983 (goto-char elint-current-pos
)
984 (1+ (count-lines (point-min)
985 (line-beginning-position))))
986 0) ; unknown position
988 (apply 'format string args
))))
990 (defun elint-error (string &rest args
)
991 "Report a linting error.
992 STRING and ARGS are thrown on `format' to get the message."
993 (elint-log "Error" string args
))
995 (defun elint-warning (string &rest args
)
996 "Report a linting warning.
998 (elint-log "Warning" string args
))
1000 (defun elint-output (string)
1001 "Print or insert STRING, depending on value of `noninteractive'."
1003 (message "%s" string
)
1004 (insert string
"\n")))
1006 (defun elint-log-message (errstr &optional top
)
1007 "Insert ERRSTR last in the lint log buffer.
1008 Optional argument TOP non-nil means pretend `elint-top-form-logged' is non-nil."
1009 (with-current-buffer (elint-get-log-buffer)
1010 (goto-char (point-max))
1011 (let ((inhibit-read-only t
))
1012 (or (bolp) (newline))
1013 ;; Do we have to say where we are?
1014 (unless (or elint-top-form-logged top
)
1015 (let* ((form (elint-top-form-form elint-top-form
))
1018 ((memq top
'(defun defsubst))
1019 (format "\nIn function %s:" (cadr form
)))
1021 (format "\nIn macro %s:" (cadr form
)))
1022 ((memq top
'(defvar defconst
))
1023 (format "\nIn variable %s:" (cadr form
)))
1024 (t "\nIn top level expression:"))))
1025 (setq elint-top-form-logged t
))
1026 (elint-output errstr
))))
1028 (defun elint-clear-log (&optional header
)
1029 "Clear the lint log buffer.
1030 Insert HEADER followed by a blank line if non-nil."
1031 (let ((dir default-directory
))
1032 (with-current-buffer (elint-get-log-buffer)
1033 (setq default-directory dir
)
1034 (let ((inhibit-read-only t
))
1036 (if header
(insert header
"\n"))))))
1038 (defun elint-display-log ()
1039 "Display the lint log buffer."
1040 (let ((pop-up-windows t
))
1041 (display-buffer (elint-get-log-buffer))
1044 (defvar elint-running
)
1046 (defun elint-set-mode-line (&optional on
)
1047 "Set the mode-line-process of the Elint log buffer."
1048 (with-current-buffer (elint-get-log-buffer)
1049 (and (eq major-mode
'compilation-mode
)
1050 (setq mode-line-process
1051 (list (if (or on
(bound-and-true-p elint-running
))
1052 (propertize ":run" 'face
'compilation-warning
)
1053 (propertize ":finished" 'face
'compilation-info
)))))))
1055 (defun elint-get-log-buffer ()
1056 "Return a log buffer for elint."
1057 (or (get-buffer elint-log-buffer
)
1058 (with-current-buffer (get-buffer-create elint-log-buffer
)
1059 (or (eq major-mode
'compilation-mode
)
1061 (setq buffer-undo-list t
)
1065 ;;; Initializing code
1068 (defun elint-put-function-args (func args
)
1069 "Mark function FUNC as having argument list ARGS."
1072 (not (eq args
'unknown
))
1073 (put func
'elint-args args
)))
1076 (defun elint-initialize (&optional reinit
)
1078 If elint is already initialized, this does nothing, unless
1079 optional prefix argument REINIT is non-nil."
1081 (if (and elint-builtin-variables
(not reinit
))
1082 (message "Elint is already initialized")
1083 (message "Initializing elint...")
1084 (setq elint-builtin-variables
(elint-scan-doc-file)
1085 elint-autoloaded-variables
(elint-find-autoloaded-variables))
1086 (mapc (lambda (x) (elint-put-function-args (car x
) (cdr x
)))
1087 (elint-find-builtin-args))
1088 (if elint-unknown-builtin-args
1089 (mapc (lambda (x) (elint-put-function-args (car x
) (cdr x
)))
1090 elint-unknown-builtin-args
))
1091 (when elint-scan-preloaded
1092 (dolist (lib preloaded-file-list
)
1093 ;; Skip files that contain nothing of use to us.
1094 (unless (string-match elint-preloaded-skip-re lib
)
1095 (setq elint-preloaded-env
1096 (elint-add-required-env elint-preloaded-env nil lib
)))))
1097 (message "Initializing elint...done")))
1100 ;; This includes all the built-in and dumped things with documentation.
1101 (defun elint-scan-doc-file ()
1102 "Scan the DOC file for function and variables.
1103 Marks the function with their arguments, and returns a list of variables."
1104 ;; Cribbed from help-fns.el.
1105 (let ((docbuf " *DOC*")
1108 (if (get-buffer docbuf
)
1111 (goto-char (point-min)))
1112 (set-buffer (get-buffer-create docbuf
))
1113 (insert-file-contents-literally
1114 (expand-file-name internal-doc-file-name doc-directory
)))
1115 (while (re-search-forward "\x1f\\([VF]\\)" nil t
)
1116 (when (setq sym
(intern-soft (buffer-substring (point)
1117 (line-end-position))))
1118 (if (string-equal (match-string 1) "V")
1119 ;; Excludes platform-specific stuff not relevant to the
1120 ;; running platform.
1121 (if (boundp sym
) (setq vars
(cons sym vars
)))
1124 (when (re-search-forward "\\(^(fn.*)\\)?\x1f" nil t
)
1126 ;; FIXME distinguish no args from not found.
1127 (and (setq args
(match-string 1))
1131 (replace-regexp-in-string "^(fn ?" "(" args
))))
1132 (elint-put-function-args sym args
))))))))
1135 (defun elint-find-autoloaded-variables ()
1136 "Return a list of all autoloaded variables."
1139 (insert-file-contents (locate-library "loaddefs.el"))
1140 (while (re-search-forward "^(defvar \\([[:alnum:]_-]+\\)" nil t
)
1141 (and (setq var
(intern-soft (match-string 1)))
1143 (setq vars
(cons var vars
)))))
1146 (defun elint-find-builtins ()
1147 "Return a list of all built-in functions."
1149 (mapatoms (lambda (s) (and (fboundp s
) (subrp (symbol-function s
))
1150 (setq subrs
(cons s subrs
)))))
1153 (defun elint-find-builtin-args (&optional list
)
1154 "Return a list of the built-in functions and their arguments.
1155 If LIST is nil, call `elint-find-builtins' to get a list of all built-in
1156 functions, otherwise use LIST.
1158 Each function is represented by a cons cell:
1159 \(function-symbol . args)
1160 If no documentation could be found args will be `unknown'."
1162 (let ((doc (documentation f t
)))
1164 (string-match "\n\n(fn\\(.*)\\)\\'" doc
)
1166 ;; "BODY...)" -> "&rest BODY)".
1167 (read (replace-regexp-in-string
1168 "\\([^ ]+\\)\\.\\.\\.)\\'" "&rest \\1)"
1169 (format "(%s %s" f
(match-string 1 doc
)) t
))))
1170 (cons f
'unknown
))))
1171 (or list
(elint-find-builtins))))
1175 ;;; elint.el ends here