1 ;;; elint.el --- Lint Emacs Lisp
3 ;; Copyright (C) 1997 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 2, or (at your option)
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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; This is a linter for Emacs Lisp. Currently, it mainly catches
29 ;; mispellings and undefined variables, although it can also catch
30 ;; function calls with the wrong number of arguments.
32 ;; Before using, call `elint-initialize' to set up som argument
33 ;; data. This takes a while. Then call elint-current-buffer or
34 ;; elint-defun to lint a buffer or a defun.
36 ;; The linter will try to "include" any require'd libraries to find
37 ;; the variables defined in those. There is a fair amount of voodoo
38 ;; involved in this, but it seems to work in normal situations.
44 ;; * A list of all standard Emacs variables would be nice to have...
45 ;; * Adding type checking. (Stop that sniggering!)
49 (defvar elint-log-buffer
"*Elint*"
50 "*The buffer to insert lint messages in.")
56 (defsubst elint-make-top-form
(form pos
)
58 FORM is the form, and POS is the point where it starts in the buffer."
61 (defsubst elint-top-form-form
(top-form)
62 "Extract the form from a TOP-FORM."
65 (defsubst elint-top-form-pos
(top-form)
66 "Extract the position from a TOP-FORM."
73 (defsubst elint-make-env
()
74 "Create an empty environment."
75 (list (list nil
) nil nil
))
77 (defsubst elint-env-add-env
(env newenv
)
78 "Augment ENV with NEWENV.
79 None of them is modified, and the new env is returned."
80 (list (append (car env
) (car newenv
))
81 (append (car (cdr env
)) (car (cdr newenv
)))
82 (append (car (cdr (cdr env
))) (car (cdr (cdr newenv
))))))
84 (defsubst elint-env-add-var
(env var
)
85 "Augment ENV with the variable VAR.
86 The new environment is returned, the old is unmodified."
87 (cons (cons (list var
) (car env
)) (cdr env
)))
89 (defsubst elint-env-add-global-var
(env var
)
90 "Augment ENV with the variable VAR.
91 ENV is modified so VAR is seen everywhere.
93 (nconc (car env
) (list (list var
)))
96 (defsubst elint-env-find-var
(env var
)
97 "Non-nil if ENV contains the variable VAR.
98 Actually, a list with VAR as a single element is returned."
101 (defsubst elint-env-add-func
(env func args
)
102 "Augment ENV with the function FUNC, which has the arguments ARGS.
103 The new environment is returned, the old is unmodified."
105 (cons (list func args
) (car (cdr env
)))
106 (car (cdr (cdr env
)))))
108 (defsubst elint-env-find-func
(env func
)
109 "Non-nil if ENV contains the function FUNC.
110 Actually, a list of (FUNC ARGS) is returned."
111 (assq func
(car (cdr env
))))
113 (defsubst elint-env-add-macro
(env macro def
)
114 "Augment ENV with the macro named MACRO.
115 DEF is the macro definition (a lambda expression or similar).
116 The new environment is returned, the old is unmodified."
119 (cons (cons macro def
) (car (cdr (cdr env
))))))
121 (defsubst elint-env-macro-env
(env)
122 "Return the macro environment of ENV.
123 This environment can be passed to `macroexpand'."
124 (car (cdr (cdr env
))))
126 (defsubst elint-env-macrop
(env macro
)
127 "Non-nil if ENV contains MACRO."
128 (assq macro
(elint-env-macro-env env
)))
134 (defun elint-current-buffer ()
135 "Lint the current buffer."
137 (elint-clear-log (format "Linting %s" (if (buffer-file-name)
141 (mapcar 'elint-top-form
(elint-update-env))
143 ;; Tell the user we're finished. This is terribly klugy: we set
144 ;; elint-top-form-logged so elint-log-message doesn't print the
145 ;; ** top form ** header...
146 (let ((elint-top-form-logged t
))
147 (elint-log-message "\nLinting complete.\n")))
149 (defun elint-defun ()
150 "Lint the function at point."
153 (if (not (beginning-of-defun))
154 (error "Lint what?"))
157 (def (read (current-buffer))))
161 (elint-top-form (elint-make-top-form def pos
)))))
164 ;;; Top form functions
167 (defvar elint-buffer-env nil
168 "The environment of a elisp buffer.
169 Will be local in linted buffers.")
171 (defvar elint-buffer-forms nil
172 "The top forms in a buffer.
173 Will be local in linted buffers.")
175 (defvar elint-last-env-time nil
176 "The last time the buffers env was updated.
177 Is measured in buffer-modified-ticks and is local in linted buffers.")
179 (defun elint-update-env ()
180 "Update the elint environment in the current buffer.
181 Don't do anything if the buffer hasn't been changed since this
182 function was called the last time.
184 (if (and (local-variable-p 'elint-buffer-env
(current-buffer))
185 (local-variable-p 'elint-buffer-forms
(current-buffer))
186 (local-variable-p 'elint-last-env-time
(current-buffer))
187 (= (buffer-modified-tick) elint-last-env-time
))
191 (set (make-local-variable 'elint-buffer-forms
) (elint-get-top-forms))
192 (set (make-local-variable 'elint-buffer-env
)
193 (elint-init-env elint-buffer-forms
))
194 (set (make-local-variable 'elint-last-env-time
) (buffer-modified-tick))
197 (defun elint-get-top-forms ()
198 "Collect all the top forms in the current buffer."
201 (goto-char (point-min))
202 (while (elint-find-next-top-form)
206 (elint-make-top-form (read (current-buffer)) pos
)
211 (error "Missing ')' in top form: %s" (buffer-substring pos
(point)))))
215 (defun elint-find-next-top-form ()
216 "Find the next top form from point.
217 Return nil if there are no more forms, t otherwise."
218 (parse-partial-sexp (point) (point-max) nil t
)
221 (defun elint-init-env (forms)
222 "Initialise the environment from FORMS."
223 (let ((env (elint-make-env))
226 (setq form
(elint-top-form-form (car forms
))
229 ;; Add defined variable
230 ((memq (car form
) '(defvar defconst defcustom
))
231 (setq env
(elint-env-add-var env
(car (cdr form
)))))
233 ((memq (car form
) '(defun defsubst))
234 (setq env
(elint-env-add-func env
(car (cdr form
))
235 (car (cdr (cdr form
))))))
236 ;; Add macro, both as a macro and as a function
237 ((eq (car form
) 'defmacro
)
238 (setq env
(elint-env-add-macro env
(car (cdr form
))
241 env
(elint-env-add-func env
(car (cdr form
))
242 (car (cdr (cdr form
))))))
244 ;; Import variable definitions
245 ((eq (car form
) 'require
)
246 (let ((name (eval (car (cdr form
))))
247 (file (eval (car (cdr (cdr form
))))))
248 (setq env
(elint-add-required-env env name file
))))
252 (defun elint-add-required-env (env name file
)
253 "Augment ENV with the variables definied by feature NAME in FILE."
255 (let* ((libname (if (stringp file
)
259 ;; First try to find .el files, then the raw name
260 (lib1 (locate-library (concat libname
".el") t
))
261 (lib (if lib1 lib1
(locate-library libname t
))))
262 ;; Clear the messages :-/
266 (set-buffer (find-file-noselect lib
))
268 (setq env
(elint-env-add-env env elint-buffer-env
)))
269 (error "dummy error...")))
272 (message "Can't get variables from require'd library %s" name
)))
275 (defun regexp-assoc (regexp alist
)
276 "Search for a key matching REGEXP in ALIST."
278 (while (and alist
(not res
))
279 (if (and (stringp (car (car alist
)))
280 (string-match regexp
(car (car alist
))))
281 (setq res
(car alist
))
282 (setq alist
(cdr alist
))))
285 (defvar elint-top-form nil
286 "The currently linted top form, or nil.")
288 (defvar elint-top-form-logged nil
289 "T if the currently linted top form has been mentioned in the log buffer.")
291 (defun elint-top-form (form)
293 (let ((elint-top-form form
)
294 (elint-top-form-logged nil
))
295 (elint-form (elint-top-form-form form
) elint-buffer-env
)))
298 ;;; General form linting functions
301 (defconst elint-special-forms
302 '((let . elint-check-let-form
)
303 (let* . elint-check-let-form
)
304 (setq . elint-check-setq-form
)
305 (quote . elint-check-quote-form
)
306 (cond . elint-check-cond-form
)
307 (lambda . elint-check-defun-form
)
308 (function . elint-check-function-form
)
309 (setq-default . elint-check-setq-form
)
310 (defun . elint-check-defun-form
)
311 (defsubst . elint-check-defun-form
)
312 (defmacro . elint-check-defun-form
)
313 (defvar . elint-check-defvar-form
)
314 (defconst . elint-check-defvar-form
)
315 (defcustom . elint-check-defcustom-form
)
316 (macro . elint-check-macro-form
)
317 (condition-case . elint-check-condition-case-form
))
318 "Functions to call when some special form should be linted.")
320 (defun elint-form (form env
)
321 "Lint FORM in the environment ENV.
322 The environment created by the form is returned."
325 (let ((func (cdr (assq (car form
) elint-special-forms
))))
328 (funcall func form env
)
330 (let* ((func (car form
))
331 (args (elint-get-args func env
))
334 ((eq args
'undefined
)
336 (elint-error "Call to undefined function: %s" form
))
338 ((eq args
'unknown
) nil
)
340 (t (setq argsok
(elint-match-args form args
))))
343 (if (elint-env-macrop env func
)
344 ;; Macro defined in buffer, expand it
346 (elint-form (macroexpand form
(elint-env-macro-env env
)) env
)
349 (let ((fcode (if (symbolp func
)
351 (indirect-function func
)
354 (if (and (listp fcode
) (eq (car fcode
) 'macro
))
355 ;; Macro defined outside buffer
357 (elint-form (macroexpand form
) env
)
359 ;; Function, lint its parameters
360 (elint-forms (cdr form
) env
))))
364 ;; :foo variables are quoted
365 (if (and (/= (aref (symbol-name form
) 0) ?
:)
366 (elint-unbound-variable form env
))
367 (elint-warning "Reference to unbound symbol: %s" form
))
373 (defun elint-forms (forms env
)
374 "Lint the FORMS, accumulating an environment, starting with ENV."
375 ;; grumblegrumbletailrecursiongrumblegrumble
377 (setq env
(elint-form (car forms
) env
)
381 (defun elint-unbound-variable (var env
)
382 "T if VAR is unbound in ENV."
383 (not (or (eq var nil
)
385 (elint-env-find-var env var
)
386 (memq var elint-standard-variables
))))
389 ;;; Function argument checking
392 (defun elint-match-args (arglist argpattern
)
393 "Match ARGLIST against ARGPATTERN."
401 ((and (null al
) (null ap
)) nil
)
402 ((eq (car ap
) '&optional
)
403 (setq state
'optional
)
406 ((eq (car ap
) '&rest
)
408 ((or (and (eq state
'all
) (or (null al
) (null ap
)))
409 (and (eq state
'optional
) (and al
(null ap
))))
410 (elint-error "Wrong number of args: %s, %s" arglist argpattern
)
413 ((and (eq state
'optional
) (null al
))
420 (defun elint-get-args (func env
)
421 "Find the args of FUNC in ENV.
422 Returns `unknown' if we couldn't find arguments."
423 (let ((f (elint-env-find-func env func
)))
428 (let ((fcode (indirect-function func
)))
430 (let ((args (get func
'elint-args
)))
431 (if args args
'unknown
))
432 (elint-find-args-in-code fcode
)))
434 (elint-find-args-in-code func
)))))
436 (defun elint-find-args-in-code (code)
437 "Extract the arguments from CODE.
438 CODE can be a lambda expression, a macro, or byte-compiled code."
440 ((byte-code-function-p code
)
442 ((and (listp code
) (eq (car code
) 'lambda
))
444 ((and (listp code
) (eq (car code
) 'macro
))
445 (elint-find-args-in-code (cdr code
)))
449 ;;; Functions to check some special forms
452 (defun elint-check-cond-form (form env
)
453 "Lint a cond FORM in ENV."
454 (setq form
(cdr form
))
456 (if (consp (car form
))
457 (elint-forms (car form
) env
)
458 (elint-error "cond clause should be a list: %s" (car form
)))
459 (setq form
(cdr form
)))
462 (defun elint-check-defun-form (form env
)
463 "Lint a defun/defmacro/lambda FORM in ENV."
464 (setq form
(if (eq (car form
) 'lambda
) (cdr form
) (cdr (cdr form
))))
465 (mapcar (function (lambda (p)
466 (or (memq p
'(&optional
&rest
))
467 (setq env
(elint-env-add-var env p
)))
470 (elint-forms (cdr form
) env
))
472 (defun elint-check-let-form (form env
)
473 "Lint the let/let* FORM in ENV."
474 (let ((varlist (car (cdr form
))))
477 (elint-error "Missing varlist in let: %s" form
)
480 ;; Check for (let (a (car b)) ...) type of error
481 (if (and (= (length varlist
) 2)
482 (symbolp (car varlist
))
483 (listp (car (cdr varlist
)))
484 (fboundp (car (car (cdr varlist
)))))
485 (elint-warning "Suspect varlist: %s" form
))
487 ;; Add variables to environment, and check the init values
489 (mapcar (function (lambda (s)
492 (setq newenv
(elint-env-add-var newenv s
)))
493 ((and (consp s
) (<= (length s
) 2))
494 (elint-form (car (cdr s
))
495 (if (eq (car form
) 'let
)
499 (elint-env-add-var newenv
(car s
))))
501 "Malformed `let' declaration: %s" s
))
505 ;; Lint the body forms
506 (elint-forms (cdr (cdr form
)) newenv
)
509 (defun elint-check-setq-form (form env
)
510 "Lint the setq FORM in ENV."
511 (or (= (mod (length form
) 2) 1)
512 (elint-error "Missing value in setq: %s" form
))
516 (setq form
(cdr form
))
520 form
(cdr (cdr form
)))
522 (if (elint-unbound-variable sym newenv
)
523 (elint-warning "Setting previously unbound symbol: %s" sym
))
524 (elint-error "Setting non-symbol in setq: %s" sym
))
525 (elint-form val newenv
)
527 (setq newenv
(elint-env-add-var newenv sym
))))
530 (defun elint-check-defvar-form (form env
)
531 "Lint the defvar/defconst FORM in ENV."
532 (if (or (= (length form
) 2)
534 (and (= (length form
) 4) (stringp (nth 3 form
))))
535 (elint-env-add-global-var (elint-form (nth 2 form
) env
)
537 (elint-error "Malformed variable declaration: %s" form
)
540 (defun elint-check-defcustom-form (form env
)
541 "Lint the defcustom FORM in ENV."
542 (if (and (> (length form
) 3)
543 (evenp (length form
))) ; even no. of keyword/value args
544 (elint-env-add-global-var (elint-form (nth 2 form
) env
)
546 (elint-error "Malformed variable declaration: %s" form
)
549 (defun elint-check-function-form (form env
)
550 "Lint the function FORM in ENV."
551 (let ((func (car (cdr-safe form
))))
554 (or (elint-env-find-func env func
)
556 (elint-warning "Reference to undefined function: %s" form
))
558 ((and (consp func
) (memq (car func
) '(lambda macro
)))
559 (elint-form func env
))
561 (t (elint-error "Not a function object: %s" form
)
565 (defun elint-check-quote-form (form env
)
566 "Lint the quote FORM in ENV."
569 (defun elint-check-macro-form (form env
)
570 "Check the macro FORM in ENV."
571 (elint-check-function-form (list (car form
) (cdr form
)) env
))
573 (defun elint-check-condition-case-form (form env
)
574 "Check the condition-case FORM in ENV."
576 (if (< (length form
) 3)
577 (elint-error "Malformed condition-case: %s" form
)
578 (or (symbolp (car (cdr form
)))
579 (elint-warning "First parameter should be a symbol: %s" form
))
580 (setq resenv
(elint-form (nth 2 form
) env
))
582 (let ((newenv (elint-env-add-var env
(car (cdr form
))))
583 (errforms (nthcdr 3 form
))
586 (setq errlist
(car (car errforms
)))
587 (mapcar (function (lambda (s)
588 (or (get s
'error-conditions
)
589 (get s
'error-message
)
591 "Not an error symbol in error handler: %s" s
))))
593 ((symbolp errlist
) (list errlist
))
594 ((listp errlist
) errlist
)
595 (t (elint-error "Bad error list in error handler: %s"
599 (elint-forms (cdr (car errforms
)) newenv
)
600 (setq errforms
(cdr errforms
))
605 ;;; Message functions
608 ;; elint-error and elint-warning are identical, but they might change
609 ;; to reflect different seriousness of linting errors
611 (defun elint-error (string &rest args
)
612 "Report an linting error.
613 STRING and ARGS are thrown on `format' to get the message."
614 (let ((errstr (apply 'format string args
)))
615 (elint-log-message errstr
)
618 (defun elint-warning (string &rest args
)
619 "Report an linting warning.
620 STRING and ARGS are thrown on `format' to get the message."
621 (let ((errstr (apply 'format string args
)))
622 (elint-log-message errstr
)
625 (defun elint-log-message (errstr)
626 "Insert ERRSTR last in the lint log buffer."
628 (set-buffer (elint-get-log-buffer))
629 (goto-char (point-max))
630 (or (bolp) (newline))
632 ;; Do we have to say where we are?
633 (if elint-top-form-logged
636 (let* ((form (elint-top-form-form elint-top-form
))
639 ((memq top
'(defun defsubst))
640 (format "\n** function %s **\n" (car (cdr form
))))
642 (format "\n** macro %s **\n" (car (cdr form
))))
643 ((memq top
'(defvar defconst
))
644 (format "\n** variable %s **\n" (car (cdr form
))))
645 (t "\n** top level expression **\n"))))
646 (setq elint-top-form-logged t
))
651 (defun elint-clear-log (&optional header
)
652 "Clear the lint log buffer.
653 Insert HEADER followed by a blank line if non-nil."
655 (set-buffer (elint-get-log-buffer))
663 (defun elint-display-log ()
664 "Display the lint log buffer."
665 (let ((pop-up-windows t
))
666 (display-buffer (elint-get-log-buffer))
669 (defun elint-get-log-buffer ()
670 "Return a log buffer for elint."
671 (let ((buf (get-buffer elint-log-buffer
)))
674 (let ((oldbuf (current-buffer)))
676 (set-buffer (get-buffer-create elint-log-buffer
))
677 (setq truncate-lines t
)
678 (set-buffer oldbuf
)))
682 ;;; Initializing code
686 (defun elint-initialize ()
689 (mapcar (function (lambda (x)
690 (or (not (symbolp (car x
)))
691 (eq (cdr x
) 'unknown
)
692 (put (car x
) 'elint-args
(cdr x
)))))
693 (elint-find-builtin-args))
694 (mapcar (function (lambda (x)
695 (put (car x
) 'elint-args
(cdr x
))))
696 elint-unknown-builtin-args
))
699 (defun elint-find-builtins ()
700 "Returns a list of all built-in functions."
702 (mapatoms (lambda (s) (if (and (fboundp s
) (subrp (symbol-function s
)))
703 (setq subrs
(cons s subrs
)))))
707 (defun elint-find-builtin-args (&optional list
)
708 "Returns a list of the built-in functions and their arguments.
710 If LIST is nil, call `elint-find-builtins' to get a list of all built-in
711 functions, otherwise use LIST.
713 Each functions is represented by a cons cell:
714 \(function-symbol . args)
715 If no documentation could be found args will be `unknown'."
717 (mapcar (function (lambda (f)
718 (let ((doc (documentation f t
)))
719 (if (and doc
(string-match "\n\n\\((.*)\\)" doc
))
720 (read (match-string 1 doc
))
724 (elint-find-builtins))))
730 (defconst elint-standard-variables
731 '(abbrev-mode auto-fill-function buffer-auto-save-file-name
732 buffer-backed-up buffer-display-table buffer-file-format
733 buffer-file-name buffer-file-number buffer-file-truename
734 buffer-file-type buffer-invisibility-spec buffer-offer-save
735 buffer-read-only buffer-saved-size buffer-undo-list
736 cache-long-line-scans case-fold-search ctl-arrow comment-column
737 default-directory defun-prompt-regexp fill-column goal-column
738 left-margin local-abbrev-table local-write-file-hooks major-mode
739 mark-active mark-ring minor-modes mode-line-buffer-identification
740 mode-line-format mode-line-modified mode-line-process mode-name
741 overwrite-mode paragraph-separate paragraph-start
742 point-before-scroll require-final-newline selective-display
743 selective-display-ellipses tab-width truncate-lines vc-mode
)
744 "Standard buffer local vars.")
746 (defconst elint-unknown-builtin-args
747 '((while test
&rest forms
)
748 (insert-before-markers-and-inherit &rest text
)
749 (catch tag
&rest body
)
751 (funcall func
&rest args
)
754 (run-hook-with-args hook
&rest args
)
755 (message-or-box string
&rest args
)
756 (save-window-excursion &rest body
)
760 (insert-and-inherit &rest args
)
761 (message-box string
&rest args
)
762 (prog2 x y
&rest body
)
763 (prog1 first
&rest body
)
764 (insert-before-markers &rest args
)
765 (call-process-region start end program
&optional delete
766 destination display
&rest args
)
769 (run-hook-with-args-until-success hook
&rest args
)
770 (track-mouse &rest body
)
771 (unwind-protect bodyform
&rest unwindforms
)
772 (save-restriction &rest body
)
774 (make-byte-code &rest args
)
777 (start-process name buffer program
&rest args
)
778 (run-hook-with-args-until-failure hook
&rest args
)
779 (if cond then
&rest else
)
780 (apply function
&rest args
)
781 (format string
&rest args
)
782 (encode-time second minute hour day month year zone
&rest args
)
788 (message string
&rest args
)
789 (defvar symbol init doc
)
790 (call-process program
&optional infile destination display
&rest args
)
791 (with-output-to-temp-buffer bufname
&rest body
)
793 (save-excursion &rest body
)
794 (run-hooks &rest hooks
)
799 (interactive &optional args
))
800 "Those built-ins for which we can't find arguments.")
804 ;;; elint.el ends here