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 some 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 (defconst elint-standard-variables
57 '(abbrev-mode auto-fill-function buffer-auto-save-file-name
58 buffer-backed-up buffer-display-count buffer-display-table buffer-display-time buffer-file-coding-system buffer-file-format
59 buffer-file-name buffer-file-number buffer-file-truename
60 buffer-file-type buffer-invisibility-spec buffer-offer-save
61 buffer-read-only buffer-saved-size buffer-undo-list
62 cache-long-line-scans case-fold-search ctl-arrow cursor-type comment-column
63 default-directory defun-prompt-regexp desktop-save-buffer enable-multibyte-characters fill-column fringes-outside-margins goal-column
64 header-line-format indicate-buffer-boundaries indicate-empty-lines
66 left-margin left-margin-width line-spacing local-abbrev-table local-write-file-hooks major-mode
67 mark-active mark-ring mode-line-buffer-identification
68 mode-line-format mode-line-modified mode-line-process mode-name
70 point-before-scroll right-fringe-width right-margin-width
71 scroll-bar-width scroll-down-aggressively scroll-up-aggressively selective-display
72 selective-display-ellipses tab-width truncate-lines vc-mode vertical-scroll-bar
)
73 "Standard buffer local vars.")
75 (defconst elint-unknown-builtin-args
76 '((while test
&rest forms
)
77 (insert-before-markers-and-inherit &rest text
)
78 (catch tag
&rest body
)
80 (funcall func
&rest args
)
83 (run-hook-with-args hook
&rest args
)
84 (message-or-box string
&rest args
)
85 (save-window-excursion &rest body
)
89 (insert-and-inherit &rest args
)
90 (message-box string
&rest args
)
91 (prog2 x y
&rest body
)
92 (prog1 first
&rest body
)
93 (insert-before-markers &rest args
)
94 (call-process-region start end program
&optional delete
95 destination display
&rest args
)
98 (run-hook-with-args-until-success hook
&rest args
)
99 (track-mouse &rest body
)
100 (unwind-protect bodyform
&rest unwindforms
)
101 (save-restriction &rest body
)
103 (make-byte-code &rest args
)
106 (start-process name buffer program
&rest args
)
107 (run-hook-with-args-until-failure hook
&rest args
)
108 (if cond then
&rest else
)
109 (apply function
&rest args
)
110 (format string
&rest args
)
111 (encode-time second minute hour day month year zone
&rest args
)
117 (message string
&rest args
)
118 (defvar symbol init doc
)
119 (call-process program
&optional infile destination display
&rest args
)
120 (with-output-to-temp-buffer bufname
&rest body
)
122 (save-excursion &rest body
)
123 (run-hooks &rest hooks
)
128 (interactive &optional args
))
129 "Those built-ins for which we can't find arguments.")
135 (defsubst elint-make-top-form
(form pos
)
137 FORM is the form, and POS is the point where it starts in the buffer."
140 (defsubst elint-top-form-form
(top-form)
141 "Extract the form from a TOP-FORM."
144 (defsubst elint-top-form-pos
(top-form)
145 "Extract the position from a TOP-FORM."
152 (defsubst elint-make-env
()
153 "Create an empty environment."
154 (list (list nil
) nil nil
))
156 (defsubst elint-env-add-env
(env newenv
)
157 "Augment ENV with NEWENV.
158 None of them is modified, and the new env is returned."
159 (list (append (car env
) (car newenv
))
160 (append (car (cdr env
)) (car (cdr newenv
)))
161 (append (car (cdr (cdr env
))) (car (cdr (cdr newenv
))))))
163 (defsubst elint-env-add-var
(env var
)
164 "Augment ENV with the variable VAR.
165 The new environment is returned, the old is unmodified."
166 (cons (cons (list var
) (car env
)) (cdr env
)))
168 (defsubst elint-env-add-global-var
(env var
)
169 "Augment ENV with the variable VAR.
170 ENV is modified so VAR is seen everywhere.
172 (nconc (car env
) (list (list var
)))
175 (defsubst elint-env-find-var
(env var
)
176 "Non-nil if ENV contains the variable VAR.
177 Actually, a list with VAR as a single element is returned."
178 (assq var
(car env
)))
180 (defsubst elint-env-add-func
(env func args
)
181 "Augment ENV with the function FUNC, which has the arguments ARGS.
182 The new environment is returned, the old is unmodified."
184 (cons (list func args
) (car (cdr env
)))
185 (car (cdr (cdr env
)))))
187 (defsubst elint-env-find-func
(env func
)
188 "Non-nil if ENV contains the function FUNC.
189 Actually, a list of (FUNC ARGS) is returned."
190 (assq func
(car (cdr env
))))
192 (defsubst elint-env-add-macro
(env macro def
)
193 "Augment ENV with the macro named MACRO.
194 DEF is the macro definition (a lambda expression or similar).
195 The new environment is returned, the old is unmodified."
198 (cons (cons macro def
) (car (cdr (cdr env
))))))
200 (defsubst elint-env-macro-env
(env)
201 "Return the macro environment of ENV.
202 This environment can be passed to `macroexpand'."
203 (car (cdr (cdr env
))))
205 (defsubst elint-env-macrop
(env macro
)
206 "Non-nil if ENV contains MACRO."
207 (assq macro
(elint-env-macro-env env
)))
213 (defun elint-current-buffer ()
214 "Lint the current buffer."
216 (elint-clear-log (format "Linting %s" (if (buffer-file-name)
220 (mapcar 'elint-top-form
(elint-update-env))
222 ;; Tell the user we're finished. This is terribly klugy: we set
223 ;; elint-top-form-logged so elint-log-message doesn't print the
224 ;; ** top form ** header...
225 (let ((elint-top-form-logged t
))
226 (elint-log-message "\nLinting complete.\n")))
228 (defun elint-defun ()
229 "Lint the function at point."
232 (if (not (beginning-of-defun))
233 (error "Lint what?"))
236 (def (read (current-buffer))))
240 (elint-top-form (elint-make-top-form def pos
)))))
243 ;;; Top form functions
246 (defvar elint-buffer-env nil
247 "The environment of a elisp buffer.
248 Will be local in linted buffers.")
250 (defvar elint-buffer-forms nil
251 "The top forms in a buffer.
252 Will be local in linted buffers.")
254 (defvar elint-last-env-time nil
255 "The last time the buffers env was updated.
256 Is measured in buffer-modified-ticks and is local in linted buffers.")
258 (defun elint-update-env ()
259 "Update the elint environment in the current buffer.
260 Don't do anything if the buffer hasn't been changed since this
261 function was called the last time.
263 (if (and (local-variable-p 'elint-buffer-env
(current-buffer))
264 (local-variable-p 'elint-buffer-forms
(current-buffer))
265 (local-variable-p 'elint-last-env-time
(current-buffer))
266 (= (buffer-modified-tick) elint-last-env-time
))
270 (set (make-local-variable 'elint-buffer-forms
) (elint-get-top-forms))
271 (set (make-local-variable 'elint-buffer-env
)
272 (elint-init-env elint-buffer-forms
))
273 (set (make-local-variable 'elint-last-env-time
) (buffer-modified-tick))
276 (defun elint-get-top-forms ()
277 "Collect all the top forms in the current buffer."
280 (goto-char (point-min))
281 (while (elint-find-next-top-form)
285 (elint-make-top-form (read (current-buffer)) pos
)
290 (error "Missing ')' in top form: %s" (buffer-substring pos
(point)))))
294 (defun elint-find-next-top-form ()
295 "Find the next top form from point.
296 Return nil if there are no more forms, t otherwise."
297 (parse-partial-sexp (point) (point-max) nil t
)
300 (defun elint-init-env (forms)
301 "Initialise the environment from FORMS."
302 (let ((env (elint-make-env))
305 (setq form
(elint-top-form-form (car forms
))
308 ;; Add defined variable
309 ((memq (car form
) '(defvar defconst defcustom
))
310 (setq env
(elint-env-add-var env
(car (cdr form
)))))
312 ((memq (car form
) '(defun defsubst))
313 (setq env
(elint-env-add-func env
(car (cdr form
))
314 (car (cdr (cdr form
))))))
315 ;; Add macro, both as a macro and as a function
316 ((eq (car form
) 'defmacro
)
317 (setq env
(elint-env-add-macro env
(car (cdr form
))
320 env
(elint-env-add-func env
(car (cdr form
))
321 (car (cdr (cdr form
))))))
323 ;; Import variable definitions
324 ((eq (car form
) 'require
)
325 (let ((name (eval (car (cdr form
))))
326 (file (eval (car (cdr (cdr form
))))))
327 (setq env
(elint-add-required-env env name file
))))
331 (defun elint-add-required-env (env name file
)
332 "Augment ENV with the variables definied by feature NAME in FILE."
334 (let* ((libname (if (stringp file
)
338 ;; First try to find .el files, then the raw name
339 (lib1 (locate-library (concat libname
".el") t
))
340 (lib (if lib1 lib1
(locate-library libname t
))))
341 ;; Clear the messages :-/
345 (set-buffer (find-file-noselect lib
))
347 (setq env
(elint-env-add-env env elint-buffer-env
)))
348 (error "dummy error...")))
351 (message "Can't get variables from require'd library %s" name
)))
354 (defun regexp-assoc (regexp alist
)
355 "Search for a key matching REGEXP in ALIST."
357 (while (and alist
(not res
))
358 (if (and (stringp (car (car alist
)))
359 (string-match regexp
(car (car alist
))))
360 (setq res
(car alist
))
361 (setq alist
(cdr alist
))))
364 (defvar elint-top-form nil
365 "The currently linted top form, or nil.")
367 (defvar elint-top-form-logged nil
368 "T if the currently linted top form has been mentioned in the log buffer.")
370 (defun elint-top-form (form)
372 (let ((elint-top-form form
)
373 (elint-top-form-logged nil
))
374 (elint-form (elint-top-form-form form
) elint-buffer-env
)))
377 ;;; General form linting functions
380 (defconst elint-special-forms
381 '((let . elint-check-let-form
)
382 (let* . elint-check-let-form
)
383 (setq . elint-check-setq-form
)
384 (quote . elint-check-quote-form
)
385 (cond . elint-check-cond-form
)
386 (lambda . elint-check-defun-form
)
387 (function . elint-check-function-form
)
388 (setq-default . elint-check-setq-form
)
389 (defun . elint-check-defun-form
)
390 (defsubst . elint-check-defun-form
)
391 (defmacro . elint-check-defun-form
)
392 (defvar . elint-check-defvar-form
)
393 (defconst . elint-check-defvar-form
)
394 (defcustom . elint-check-defcustom-form
)
395 (macro . elint-check-macro-form
)
396 (condition-case . elint-check-condition-case-form
))
397 "Functions to call when some special form should be linted.")
399 (defun elint-form (form env
)
400 "Lint FORM in the environment ENV.
401 The environment created by the form is returned."
404 (let ((func (cdr (assq (car form
) elint-special-forms
))))
407 (funcall func form env
)
409 (let* ((func (car form
))
410 (args (elint-get-args func env
))
413 ((eq args
'undefined
)
415 (elint-error "Call to undefined function: %s" form
))
417 ((eq args
'unknown
) nil
)
419 (t (setq argsok
(elint-match-args form args
))))
422 (if (elint-env-macrop env func
)
423 ;; Macro defined in buffer, expand it
425 (elint-form (macroexpand form
(elint-env-macro-env env
)) env
)
428 (let ((fcode (if (symbolp func
)
430 (indirect-function func
)
433 (if (and (listp fcode
) (eq (car fcode
) 'macro
))
434 ;; Macro defined outside buffer
436 (elint-form (macroexpand form
) env
)
438 ;; Function, lint its parameters
439 (elint-forms (cdr form
) env
))))
443 ;; :foo variables are quoted
444 (if (and (/= (aref (symbol-name form
) 0) ?
:)
445 (elint-unbound-variable form env
))
446 (elint-warning "Reference to unbound symbol: %s" form
))
452 (defun elint-forms (forms env
)
453 "Lint the FORMS, accumulating an environment, starting with ENV."
454 ;; grumblegrumbletailrecursiongrumblegrumble
456 (setq env
(elint-form (car forms
) env
)
460 (defun elint-unbound-variable (var env
)
461 "T if VAR is unbound in ENV."
462 (not (or (eq var nil
)
464 (elint-env-find-var env var
)
465 (memq var elint-standard-variables
))))
468 ;;; Function argument checking
471 (defun elint-match-args (arglist argpattern
)
472 "Match ARGLIST against ARGPATTERN."
480 ((and (null al
) (null ap
)) nil
)
481 ((eq (car ap
) '&optional
)
482 (setq state
'optional
)
485 ((eq (car ap
) '&rest
)
487 ((or (and (eq state
'all
) (or (null al
) (null ap
)))
488 (and (eq state
'optional
) (and al
(null ap
))))
489 (elint-error "Wrong number of args: %s, %s" arglist argpattern
)
492 ((and (eq state
'optional
) (null al
))
499 (defun elint-get-args (func env
)
500 "Find the args of FUNC in ENV.
501 Returns `unknown' if we couldn't find arguments."
502 (let ((f (elint-env-find-func env func
)))
507 (let ((fcode (indirect-function func
)))
509 (let ((args (get func
'elint-args
)))
510 (if args args
'unknown
))
511 (elint-find-args-in-code fcode
)))
513 (elint-find-args-in-code func
)))))
515 (defun elint-find-args-in-code (code)
516 "Extract the arguments from CODE.
517 CODE can be a lambda expression, a macro, or byte-compiled code."
519 ((byte-code-function-p code
)
521 ((and (listp code
) (eq (car code
) 'lambda
))
523 ((and (listp code
) (eq (car code
) 'macro
))
524 (elint-find-args-in-code (cdr code
)))
528 ;;; Functions to check some special forms
531 (defun elint-check-cond-form (form env
)
532 "Lint a cond FORM in ENV."
533 (setq form
(cdr form
))
535 (if (consp (car form
))
536 (elint-forms (car form
) env
)
537 (elint-error "cond clause should be a list: %s" (car form
)))
538 (setq form
(cdr form
)))
541 (defun elint-check-defun-form (form env
)
542 "Lint a defun/defmacro/lambda FORM in ENV."
543 (setq form
(if (eq (car form
) 'lambda
) (cdr form
) (cdr (cdr form
))))
544 (mapcar (function (lambda (p)
545 (or (memq p
'(&optional
&rest
))
546 (setq env
(elint-env-add-var env p
)))
549 (elint-forms (cdr form
) env
))
551 (defun elint-check-let-form (form env
)
552 "Lint the let/let* FORM in ENV."
553 (let ((varlist (car (cdr form
))))
556 (elint-error "Missing varlist in let: %s" form
)
559 ;; Check for (let (a (car b)) ...) type of error
560 (if (and (= (length varlist
) 2)
561 (symbolp (car varlist
))
562 (listp (car (cdr varlist
)))
563 (fboundp (car (car (cdr varlist
)))))
564 (elint-warning "Suspect varlist: %s" form
))
566 ;; Add variables to environment, and check the init values
568 (mapcar (function (lambda (s)
571 (setq newenv
(elint-env-add-var newenv s
)))
572 ((and (consp s
) (<= (length s
) 2))
573 (elint-form (car (cdr s
))
574 (if (eq (car form
) 'let
)
578 (elint-env-add-var newenv
(car s
))))
580 "Malformed `let' declaration: %s" s
))
584 ;; Lint the body forms
585 (elint-forms (cdr (cdr form
)) newenv
)
588 (defun elint-check-setq-form (form env
)
589 "Lint the setq FORM in ENV."
590 (or (= (mod (length form
) 2) 1)
591 (elint-error "Missing value in setq: %s" form
))
595 (setq form
(cdr form
))
599 form
(cdr (cdr form
)))
601 (if (elint-unbound-variable sym newenv
)
602 (elint-warning "Setting previously unbound symbol: %s" sym
))
603 (elint-error "Setting non-symbol in setq: %s" sym
))
604 (elint-form val newenv
)
606 (setq newenv
(elint-env-add-var newenv sym
))))
609 (defun elint-check-defvar-form (form env
)
610 "Lint the defvar/defconst FORM in ENV."
611 (if (or (= (length form
) 2)
613 (and (= (length form
) 4) (stringp (nth 3 form
))))
614 (elint-env-add-global-var (elint-form (nth 2 form
) env
)
616 (elint-error "Malformed variable declaration: %s" form
)
619 (defun elint-check-defcustom-form (form env
)
620 "Lint the defcustom FORM in ENV."
621 (if (and (> (length form
) 3)
622 ;; even no. of keyword/value args ?
623 (zerop (logand (length form
) 1)))
624 (elint-env-add-global-var (elint-form (nth 2 form
) env
)
626 (elint-error "Malformed variable declaration: %s" form
)
629 (defun elint-check-function-form (form env
)
630 "Lint the function FORM in ENV."
631 (let ((func (car (cdr-safe form
))))
634 (or (elint-env-find-func env func
)
636 (elint-warning "Reference to undefined function: %s" form
))
638 ((and (consp func
) (memq (car func
) '(lambda macro
)))
639 (elint-form func env
))
641 (t (elint-error "Not a function object: %s" form
)
645 (defun elint-check-quote-form (form env
)
646 "Lint the quote FORM in ENV."
649 (defun elint-check-macro-form (form env
)
650 "Check the macro FORM in ENV."
651 (elint-check-function-form (list (car form
) (cdr form
)) env
))
653 (defun elint-check-condition-case-form (form env
)
654 "Check the condition-case FORM in ENV."
656 (if (< (length form
) 3)
657 (elint-error "Malformed condition-case: %s" form
)
658 (or (symbolp (car (cdr form
)))
659 (elint-warning "First parameter should be a symbol: %s" form
))
660 (setq resenv
(elint-form (nth 2 form
) env
))
662 (let ((newenv (elint-env-add-var env
(car (cdr form
))))
663 (errforms (nthcdr 3 form
))
666 (setq errlist
(car (car errforms
)))
667 (mapcar (function (lambda (s)
668 (or (get s
'error-conditions
)
669 (get s
'error-message
)
671 "Not an error symbol in error handler: %s" s
))))
673 ((symbolp errlist
) (list errlist
))
674 ((listp errlist
) errlist
)
675 (t (elint-error "Bad error list in error handler: %s"
679 (elint-forms (cdr (car errforms
)) newenv
)
680 (setq errforms
(cdr errforms
))
685 ;;; Message functions
688 ;; elint-error and elint-warning are identical, but they might change
689 ;; to reflect different seriousness of linting errors
691 (defun elint-error (string &rest args
)
692 "Report a linting error.
693 STRING and ARGS are thrown on `format' to get the message."
694 (let ((errstr (apply 'format string args
)))
695 (elint-log-message errstr
)
698 (defun elint-warning (string &rest args
)
699 "Report a linting warning.
700 STRING and ARGS are thrown on `format' to get the message."
701 (let ((errstr (apply 'format string args
)))
702 (elint-log-message errstr
)
705 (defun elint-log-message (errstr)
706 "Insert ERRSTR last in the lint log buffer."
708 (set-buffer (elint-get-log-buffer))
709 (goto-char (point-max))
710 (or (bolp) (newline))
712 ;; Do we have to say where we are?
713 (if elint-top-form-logged
716 (let* ((form (elint-top-form-form elint-top-form
))
719 ((memq top
'(defun defsubst))
720 (format "\n** function %s **\n" (car (cdr form
))))
722 (format "\n** macro %s **\n" (car (cdr form
))))
723 ((memq top
'(defvar defconst
))
724 (format "\n** variable %s **\n" (car (cdr form
))))
725 (t "\n** top level expression **\n"))))
726 (setq elint-top-form-logged t
))
731 (defun elint-clear-log (&optional header
)
732 "Clear the lint log buffer.
733 Insert HEADER followed by a blank line if non-nil."
735 (set-buffer (elint-get-log-buffer))
743 (defun elint-display-log ()
744 "Display the lint log buffer."
745 (let ((pop-up-windows t
))
746 (display-buffer (elint-get-log-buffer))
749 (defun elint-get-log-buffer ()
750 "Return a log buffer for elint."
751 (let ((buf (get-buffer elint-log-buffer
)))
754 (let ((oldbuf (current-buffer)))
756 (set-buffer (get-buffer-create elint-log-buffer
))
757 (setq truncate-lines t
)
758 (set-buffer oldbuf
)))
762 ;;; Initializing code
766 (defun elint-initialize ()
769 (mapcar (function (lambda (x)
770 (or (not (symbolp (car x
)))
771 (eq (cdr x
) 'unknown
)
772 (put (car x
) 'elint-args
(cdr x
)))))
773 (elint-find-builtin-args))
774 (mapcar (function (lambda (x)
775 (put (car x
) 'elint-args
(cdr x
))))
776 elint-unknown-builtin-args
))
779 (defun elint-find-builtins ()
780 "Returns a list of all built-in functions."
782 (mapatoms (lambda (s) (if (and (fboundp s
) (subrp (symbol-function s
)))
783 (setq subrs
(cons s subrs
)))))
787 (defun elint-find-builtin-args (&optional list
)
788 "Returns a list of the built-in functions and their arguments.
790 If LIST is nil, call `elint-find-builtins' to get a list of all built-in
791 functions, otherwise use LIST.
793 Each functions is represented by a cons cell:
794 \(function-symbol . args)
795 If no documentation could be found args will be `unknown'."
797 (mapcar (function (lambda (f)
798 (let ((doc (documentation f t
)))
799 (if (and doc
(string-match "\n\n\\((.*)\\)" doc
))
800 (read (match-string 1 doc
))
804 (elint-find-builtins))))
808 ;;; arch-tag: b2f061e2-af84-4ddc-8e39-f5e969ac228f
809 ;;; elint.el ends here