(elint-add-required-env): Revert to not using temp-buffers (2009-09-12).
[bpt/emacs.git] / lisp / emacs-lisp / elint.el
1 ;;; elint.el --- Lint Emacs Lisp
2
3 ;; Copyright (C) 1997, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 ;; 2009 Free Software Foundation, Inc.
5
6 ;; Author: Peter Liljenberg <petli@lysator.liu.se>
7 ;; Created: May 1997
8 ;; Keywords: lisp
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; This is a linter for Emacs Lisp. Currently, it mainly catches
28 ;; misspellings and undefined variables, although it can also catch
29 ;; function calls with the wrong number of arguments.
30
31 ;; To use, call elint-current-buffer or elint-defun to lint a buffer
32 ;; or defun. The first call runs `elint-initialize' to set up some
33 ;; argument data, which may take a while.
34
35 ;; The linter will try to "include" any require'd libraries to find
36 ;; the variables defined in those. There is a fair amount of voodoo
37 ;; involved in this, but it seems to work in normal situations.
38
39 ;;; To do:
40
41 ;; * List of variables and functions defined in dumped lisp files.
42 ;; * Adding type checking. (Stop that sniggering!)
43 ;; * Handle eval-when-compile (eg for requires, being sensitive to the
44 ;; difference between funcs and macros).
45 ;; * Requires within function bodies.
46
47 ;;; Code:
48
49 (defvar elint-log-buffer "*Elint*"
50 "*The buffer to insert lint messages in.")
51
52 ;;;
53 ;;; Data
54 ;;;
55
56
57 ;; FIXME does this serve any useful purpose now elint-builtin-variables exists?
58 (defconst elint-standard-variables '(local-write-file-hooks vc-mode)
59 "Standard buffer local variables, excluding `elint-builtin-variables'.")
60
61 (defvar elint-builtin-variables nil
62 "List of built-in variables. Set by `elint-initialize'.")
63
64 (defvar elint-autoloaded-variables nil
65 "List of `loaddefs.el' variables. Set by `elint-initialize'.")
66
67 ;; FIXME dumped variables and functions.
68
69 (defconst elint-unknown-builtin-args nil
70 "Those built-ins for which we can't find arguments, if any.")
71
72 (defconst elint-extra-errors '(file-locked file-supersession ftp-error)
73 "Errors without error-message or error-confitions properties.")
74
75 ;;;
76 ;;; ADT: top-form
77 ;;;
78
79 (defsubst elint-make-top-form (form pos)
80 "Create a top form.
81 FORM is the form, and POS is the point where it starts in the buffer."
82 (cons form pos))
83
84 (defsubst elint-top-form-form (top-form)
85 "Extract the form from a TOP-FORM."
86 (car top-form))
87
88 (defsubst elint-top-form-pos (top-form)
89 "Extract the position from a TOP-FORM."
90 (cdr top-form))
91
92 ;;;
93 ;;; ADT: env
94 ;;;
95
96 (defsubst elint-make-env ()
97 "Create an empty environment."
98 (list (list nil) nil nil))
99
100 (defsubst elint-env-add-env (env newenv)
101 "Augment ENV with NEWENV.
102 None of them is modified, and the new env is returned."
103 (list (append (car env) (car newenv))
104 (append (cadr env) (cadr newenv))
105 (append (car (cdr (cdr env))) (car (cdr (cdr newenv))))))
106
107 (defsubst elint-env-add-var (env var)
108 "Augment ENV with the variable VAR.
109 The new environment is returned, the old is unmodified."
110 (cons (cons (list var) (car env)) (cdr env)))
111
112 (defsubst elint-env-add-global-var (env var)
113 "Augment ENV with the variable VAR.
114 ENV is modified so VAR is seen everywhere.
115 ENV is returned."
116 (nconc (car env) (list (list var)))
117 env)
118
119 (defsubst elint-env-find-var (env var)
120 "Non-nil if ENV contains the variable VAR.
121 Actually, a list with VAR as a single element is returned."
122 (assq var (car env)))
123
124 (defsubst elint-env-add-func (env func args)
125 "Augment ENV with the function FUNC, which has the arguments ARGS.
126 The new environment is returned, the old is unmodified."
127 (list (car env)
128 (cons (list func args) (cadr env))
129 (car (cdr (cdr env)))))
130
131 (defsubst elint-env-find-func (env func)
132 "Non-nil if ENV contains the function FUNC.
133 Actually, a list of (FUNC ARGS) is returned."
134 (assq func (cadr env)))
135
136 (defsubst elint-env-add-macro (env macro def)
137 "Augment ENV with the macro named MACRO.
138 DEF is the macro definition (a lambda expression or similar).
139 The new environment is returned, the old is unmodified."
140 (list (car env)
141 (cadr env)
142 (cons (cons macro def) (car (cdr (cdr env))))))
143
144 (defsubst elint-env-macro-env (env)
145 "Return the macro environment of ENV.
146 This environment can be passed to `macroexpand'."
147 (car (cdr (cdr env))))
148
149 (defsubst elint-env-macrop (env macro)
150 "Non-nil if ENV contains MACRO."
151 (assq macro (elint-env-macro-env env)))
152
153 ;;;
154 ;;; User interface
155 ;;;
156
157 ;;;###autoload
158 (defun elint-file (file)
159 "Lint the file FILE."
160 (interactive "fElint file: ")
161 (setq file (expand-file-name file))
162 (or elint-builtin-variables
163 (elint-initialize))
164 (let ((dir (file-name-directory file)))
165 (let ((default-directory dir))
166 (elint-display-log))
167 (elint-set-mode-line t)
168 (with-current-buffer elint-log-buffer
169 (unless (string-equal default-directory dir)
170 (elint-log-message (format "\f\nLeaving directory `%s'"
171 default-directory) t)
172 (elint-log-message (format "Entering directory `%s'" dir) t)
173 (setq default-directory dir))))
174 (let ((str (format "Linting file %s" file)))
175 (message "%s..." str)
176 (or noninteractive
177 (elint-log-message (format "\f\n%s at %s" str (current-time-string)) t))
178 ;; elint-current-buffer clears log.
179 (with-temp-buffer
180 (insert-file-contents file)
181 (let ((buffer-file-name file)
182 (max-lisp-eval-depth (max 1000 max-lisp-eval-depth)))
183 (with-syntax-table emacs-lisp-mode-syntax-table
184 (mapc 'elint-top-form (elint-update-env)))))
185 (elint-set-mode-line)
186 (message "%s...done" str)))
187
188 ;; cf byte-recompile-directory.
189 ;;;###autoload
190 (defun elint-directory (directory)
191 "Lint all the .el files in DIRECTORY."
192 (interactive "DElint directory: ")
193 (let ((elint-running t))
194 (dolist (file (directory-files directory t))
195 ;; Bytecomp has emacs-lisp-file-regexp.
196 (when (and (string-match "\\.el\\'" file)
197 (file-readable-p file)
198 (not (auto-save-file-name-p file)))
199 (elint-file file))))
200 (elint-set-mode-line))
201
202 ;;;###autoload
203 (defun elint-current-buffer ()
204 "Lint the current buffer.
205 If necessary, this first calls `elint-initalize'."
206 (interactive)
207 (or elint-builtin-variables
208 (elint-initialize))
209 (elint-clear-log (format "Linting %s" (or (buffer-file-name)
210 (buffer-name))))
211 (elint-display-log)
212 (elint-set-mode-line t)
213 (mapc 'elint-top-form (elint-update-env))
214 ;; Tell the user we're finished. This is terribly klugy: we set
215 ;; elint-top-form-logged so elint-log-message doesn't print the
216 ;; ** top form ** header...
217 (elint-set-mode-line)
218 (elint-log-message "\nLinting finished.\n" t))
219
220
221 ;;;###autoload
222 (defun elint-defun ()
223 "Lint the function at point.
224 If necessary, this first calls `elint-initalize'."
225 (interactive)
226 (or elint-builtin-variables
227 (elint-initialize))
228 (save-excursion
229 (or (beginning-of-defun) (error "Lint what?"))
230 (let ((pos (point))
231 (def (read (current-buffer))))
232 (elint-display-log)
233 (elint-update-env)
234 (elint-top-form (elint-make-top-form def pos)))))
235
236 ;;;
237 ;;; Top form functions
238 ;;;
239
240 (defvar elint-buffer-env nil
241 "The environment of a elisp buffer.
242 Will be local in linted buffers.")
243
244 (defvar elint-buffer-forms nil
245 "The top forms in a buffer.
246 Will be local in linted buffers.")
247
248 (defvar elint-last-env-time nil
249 "The last time the buffers env was updated.
250 Is measured in buffer-modified-ticks and is local in linted buffers.")
251
252 (defun elint-update-env ()
253 "Update the elint environment in the current buffer.
254 Don't do anything if the buffer hasn't been changed since this
255 function was called the last time.
256 Returns the forms."
257 (if (and (local-variable-p 'elint-buffer-env (current-buffer))
258 (local-variable-p 'elint-buffer-forms (current-buffer))
259 (local-variable-p 'elint-last-env-time (current-buffer))
260 (= (buffer-modified-tick) elint-last-env-time))
261 ;; Env is up to date
262 elint-buffer-forms
263 ;; Remake env
264 (set (make-local-variable 'elint-buffer-forms) (elint-get-top-forms))
265 (set (make-local-variable 'elint-buffer-env)
266 (elint-init-env elint-buffer-forms))
267 (set (make-local-variable 'elint-last-env-time) (buffer-modified-tick))
268 elint-buffer-forms))
269
270 (defun elint-get-top-forms ()
271 "Collect all the top forms in the current buffer."
272 (save-excursion
273 (let ((tops nil))
274 (goto-char (point-min))
275 (while (elint-find-next-top-form)
276 (let ((pos (point)))
277 (condition-case nil
278 (setq tops (cons
279 (elint-make-top-form (read (current-buffer)) pos)
280 tops))
281 (end-of-file
282 (goto-char pos)
283 (error "Missing ')' in top form: %s"
284 (buffer-substring pos (line-end-position)))))))
285 (nreverse tops))))
286
287 (defun elint-find-next-top-form ()
288 "Find the next top form from point.
289 Return nil if there are no more forms, t otherwise."
290 (parse-partial-sexp (point) (point-max) nil t)
291 (not (eobp)))
292
293 (defun elint-init-env (forms)
294 "Initialize the environment from FORMS."
295 (let ((env (elint-make-env))
296 form)
297 (while forms
298 (setq form (elint-top-form-form (car forms))
299 forms (cdr forms))
300 (cond
301 ;; Eg nnmaildir seems to use [] as a form of comment syntax.
302 ((not (listp form))
303 (elint-error "Skipping non-list form `%s'" form))
304 ;; Add defined variable
305 ((memq (car form) '(defvar defconst defcustom))
306 (setq env (elint-env-add-var env (cadr form))))
307 ;; Add function
308 ((memq (car form) '(defun defsubst))
309 (setq env (elint-env-add-func env (cadr form) (nth 2 form))))
310 ((eq (car form) 'define-derived-mode)
311 (setq env (elint-env-add-func env (cadr form) ())
312 env (elint-env-add-var env (cadr form))))
313 ;; FIXME it would be nice to check the autoloads are correct.
314 ((eq (car form) 'autoload)
315 (setq env (elint-env-add-func env (cadr (cadr form)) 'unknown)))
316 ((eq (car form) 'declare-function)
317 (setq env (elint-env-add-func env (cadr form)
318 (if (or (< (length form) 4)
319 (eq (nth 3 form) t))
320 'unknown
321 (nth 3 form)))))
322 ((and (eq (car form) 'defalias) (listp (nth 2 form)))
323 ;; If the alias points to something already in the environment,
324 ;; add the alias to the environment with the same arguments.
325 (let ((def (elint-env-find-func env (cadr (nth 2 form)))))
326 ;; FIXME warn if the alias target is unknown.
327 (setq env (elint-env-add-func env (cadr (cadr form))
328 (if def (cadr def) 'unknown)))))
329 ;; Add macro, both as a macro and as a function
330 ((eq (car form) 'defmacro)
331 (setq env (elint-env-add-macro env (cadr form)
332 (cons 'lambda (cddr form)))
333 env (elint-env-add-func env (cadr form) (nth 2 form))))
334 ;; Import variable definitions
335 ((eq (car form) 'require)
336 (let ((name (eval (cadr form)))
337 (file (eval (nth 2 form))))
338 (setq env (elint-add-required-env env name file))))))
339 env))
340
341 (defun elint-add-required-env (env name file)
342 "Augment ENV with the variables defined by feature NAME in FILE."
343 (condition-case nil
344 (let* ((libname (if (stringp file)
345 file
346 (symbol-name name)))
347
348 ;; First try to find .el files, then the raw name
349 (lib1 (locate-library (concat libname ".el") t))
350 (lib (or lib1 (locate-library libname t))))
351 ;; Clear the messages :-/
352 (message nil)
353 (if lib
354 (save-excursion
355 ;; FIXME this doesn't use a temp buffer, because it
356 ;; stores the result in buffer-local variables so that
357 ;; it can be reused.
358 (set-buffer (find-file-noselect lib))
359 (elint-update-env)
360 (setq env (elint-env-add-env env elint-buffer-env)))
361 ;;; (with-temp-buffer
362 ;;; (insert-file-contents lib)
363 ;;; (with-syntax-table emacs-lisp-mode-syntax-table
364 ;;; (elint-update-env))
365 ;;; (setq env (elint-env-add-env env elint-buffer-env))))
366 ;;(message "Elint processed (require '%s)" name))
367 (error "Unable to find require'd library %s" name)))
368 (error
369 (message "Can't get variables from require'd library %s" name)))
370 env)
371
372 (defvar elint-top-form nil
373 "The currently linted top form, or nil.")
374
375 (defvar elint-top-form-logged nil
376 "T if the currently linted top form has been mentioned in the log buffer.")
377
378 (defun elint-top-form (form)
379 "Lint a top FORM."
380 (let ((elint-top-form form)
381 (elint-top-form-logged nil)
382 (elint-current-pos (elint-top-form-pos form)))
383 (elint-form (elint-top-form-form form) elint-buffer-env)))
384
385 ;;;
386 ;;; General form linting functions
387 ;;;
388
389 (defconst elint-special-forms
390 '((let . elint-check-let-form)
391 (let* . elint-check-let-form)
392 (setq . elint-check-setq-form)
393 (quote . elint-check-quote-form)
394 (cond . elint-check-cond-form)
395 (lambda . elint-check-defun-form)
396 (function . elint-check-function-form)
397 (setq-default . elint-check-setq-form)
398 (defun . elint-check-defun-form)
399 (defsubst . elint-check-defun-form)
400 (defmacro . elint-check-defun-form)
401 (defvar . elint-check-defvar-form)
402 (defconst . elint-check-defvar-form)
403 (defcustom . elint-check-defcustom-form)
404 (macro . elint-check-macro-form)
405 (condition-case . elint-check-condition-case-form))
406 "Functions to call when some special form should be linted.")
407
408 (defun elint-form (form env)
409 "Lint FORM in the environment ENV.
410 The environment created by the form is returned."
411 (cond
412 ((consp form)
413 (let ((func (cdr (assq (car form) elint-special-forms))))
414 (if func
415 ;; Special form
416 (funcall func form env)
417
418 (let* ((func (car form))
419 (args (elint-get-args func env))
420 (argsok t))
421 (cond
422 ((eq args 'undefined)
423 (setq argsok nil)
424 (elint-error "Call to undefined function: %s" func))
425
426 ((eq args 'unknown) nil)
427
428 (t (setq argsok (elint-match-args form args))))
429
430 ;; Is this a macro?
431 (if (elint-env-macrop env func)
432 ;; Macro defined in buffer, expand it
433 (if argsok
434 ;; FIXME error if macro uses macro, eg bytecomp.el.
435 (condition-case nil
436 (elint-form
437 (macroexpand form (elint-env-macro-env env)) env)
438 (error
439 (elint-error "Elint failed to expand macro: %s" func)
440 env))
441 env)
442
443 (let ((fcode (if (symbolp func)
444 (if (fboundp func)
445 (indirect-function func))
446 func)))
447 (if (and (listp fcode) (eq (car fcode) 'macro))
448 ;; Macro defined outside buffer
449 (if argsok
450 (elint-form (macroexpand form) env)
451 env)
452 ;; Function, lint its parameters
453 (elint-forms (cdr form) env))))))))
454 ((symbolp form)
455 ;; :foo variables are quoted
456 (if (and (/= (aref (symbol-name form) 0) ?:)
457 (elint-unbound-variable form env))
458 (elint-warning "Reference to unbound symbol: %s" form))
459 env)
460
461 (t env)))
462
463 (defun elint-forms (forms env)
464 "Lint the FORMS, accumulating an environment, starting with ENV."
465 ;; grumblegrumbletailrecursiongrumblegrumble
466 (if (listp forms)
467 (dolist (f forms env)
468 (setq env (elint-form f env)))
469 ;; Loop macro?
470 (elint-error "Elint failed to parse form: %s" forms)
471 env))
472
473 (defun elint-unbound-variable (var env)
474 "T if VAR is unbound in ENV."
475 (not (or (memq var '(nil t))
476 (elint-env-find-var env var)
477 (memq var elint-builtin-variables)
478 (memq var elint-autoloaded-variables)
479 (memq var elint-standard-variables))))
480
481 ;;;
482 ;;; Function argument checking
483 ;;;
484
485 (defun elint-match-args (arglist argpattern)
486 "Match ARGLIST against ARGPATTERN."
487 (let ((state 'all)
488 (al (cdr arglist))
489 (ap argpattern)
490 (ok t))
491 (while
492 (cond
493 ((and (null al) (null ap)) nil)
494 ((eq (car ap) '&optional)
495 (setq state 'optional)
496 (setq ap (cdr ap))
497 t)
498 ((eq (car ap) '&rest)
499 nil)
500 ((or (and (eq state 'all) (or (null al) (null ap)))
501 (and (eq state 'optional) (and al (null ap))))
502 (elint-error "Wrong number of args: %s, %s" arglist argpattern)
503 (setq ok nil)
504 nil)
505 ((and (eq state 'optional) (null al))
506 nil)
507 (t (setq al (cdr al)
508 ap (cdr ap))
509 t)))
510 ok))
511
512 (defun elint-get-args (func env)
513 "Find the args of FUNC in ENV.
514 Returns `unknown' if we couldn't find arguments."
515 (let ((f (elint-env-find-func env func)))
516 (if f
517 (cadr f)
518 (if (symbolp func)
519 (if (fboundp func)
520 (let ((fcode (indirect-function func)))
521 (if (subrp fcode)
522 ;; FIXME builtins with no args have args = nil.
523 (or (get func 'elint-args) 'unknown)
524 (elint-find-args-in-code fcode)))
525 'undefined)
526 (elint-find-args-in-code func)))))
527
528 (defun elint-find-args-in-code (code)
529 "Extract the arguments from CODE.
530 CODE can be a lambda expression, a macro, or byte-compiled code."
531 (cond
532 ((byte-code-function-p code)
533 (aref code 0))
534 ((and (listp code) (eq (car code) 'lambda))
535 (car (cdr code)))
536 ((and (listp code) (eq (car code) 'macro))
537 (elint-find-args-in-code (cdr code)))
538 (t 'unknown)))
539
540 ;;;
541 ;;; Functions to check some special forms
542 ;;;
543
544 (defun elint-check-cond-form (form env)
545 "Lint a cond FORM in ENV."
546 (dolist (f (cdr form) env)
547 (if (consp f)
548 (elint-forms f env)
549 (elint-error "cond clause should be a list: %s" f))))
550
551 (defun elint-check-defun-form (form env)
552 "Lint a defun/defmacro/lambda FORM in ENV."
553 (setq form (if (eq (car form) 'lambda) (cdr form) (cddr form)))
554 (mapc (lambda (p)
555 (or (memq p '(&optional &rest))
556 (setq env (elint-env-add-var env p))))
557 (car form))
558 (elint-forms (cdr form) env))
559
560 (defun elint-check-let-form (form env)
561 "Lint the let/let* FORM in ENV."
562 (let ((varlist (cadr form)))
563 (if (not varlist)
564 (progn
565 (elint-error "Missing varlist in let: %s" form)
566 env)
567 ;; Check for (let (a (car b)) ...) type of error
568 (if (and (= (length varlist) 2)
569 (symbolp (car varlist))
570 (listp (car (cdr varlist)))
571 (fboundp (car (car (cdr varlist)))))
572 (elint-warning "Suspect varlist: %s" form))
573 ;; Add variables to environment, and check the init values
574 (let ((newenv env))
575 (mapc (lambda (s)
576 (cond
577 ((symbolp s)
578 (setq newenv (elint-env-add-var newenv s)))
579 ((and (consp s) (<= (length s) 2))
580 (elint-form (cadr s)
581 (if (eq (car form) 'let)
582 env
583 newenv))
584 (setq newenv
585 (elint-env-add-var newenv (car s))))
586 (t (elint-error
587 "Malformed `let' declaration: %s" s))))
588 varlist)
589
590 ;; Lint the body forms
591 (elint-forms (cddr form) newenv)))))
592
593 (defun elint-check-setq-form (form env)
594 "Lint the setq FORM in ENV."
595 (or (= (mod (length form) 2) 1)
596 (elint-error "Missing value in setq: %s" form))
597 (let ((newenv env)
598 sym val)
599 (setq form (cdr form))
600 (while form
601 (setq sym (car form)
602 val (car (cdr form))
603 form (cdr (cdr form)))
604 (if (symbolp sym)
605 (if (elint-unbound-variable sym newenv)
606 (elint-warning "Setting previously unbound symbol: %s" sym))
607 (elint-error "Setting non-symbol in setq: %s" sym))
608 (elint-form val newenv)
609 (if (symbolp sym)
610 (setq newenv (elint-env-add-var newenv sym))))
611 newenv))
612
613 (defun elint-check-defvar-form (form env)
614 "Lint the defvar/defconst FORM in ENV."
615 (if (or (= (length form) 2)
616 (= (length form) 3)
617 (and (= (length form) 4) (stringp (nth 3 form))))
618 (elint-env-add-global-var (elint-form (nth 2 form) env)
619 (car (cdr form)))
620 (elint-error "Malformed variable declaration: %s" form)
621 env))
622
623 (defun elint-check-defcustom-form (form env)
624 "Lint the defcustom FORM in ENV."
625 (if (and (> (length form) 3)
626 ;; even no. of keyword/value args ?
627 (zerop (logand (length form) 1)))
628 (elint-env-add-global-var (elint-form (nth 2 form) env)
629 (car (cdr form)))
630 (elint-error "Malformed variable declaration: %s" form)
631 env))
632
633 (defun elint-check-function-form (form env)
634 "Lint the function FORM in ENV."
635 (let ((func (car (cdr-safe form))))
636 (cond
637 ((symbolp func)
638 (or (elint-env-find-func env func)
639 (fboundp func)
640 (elint-warning "Reference to undefined function: %s" form))
641 env)
642 ((and (consp func) (memq (car func) '(lambda macro)))
643 (elint-form func env))
644 ((stringp func) env)
645 (t (elint-error "Not a function object: %s" form)
646 env))))
647
648 (defun elint-check-quote-form (form env)
649 "Lint the quote FORM in ENV."
650 env)
651
652 (defun elint-check-macro-form (form env)
653 "Check the macro FORM in ENV."
654 (elint-check-function-form (list (car form) (cdr form)) env))
655
656 (defun elint-check-condition-case-form (form env)
657 "Check the `condition-case' FORM in ENV."
658 (let ((resenv env))
659 (if (< (length form) 3)
660 (elint-error "Malformed condition-case: %s" form)
661 (or (symbolp (cadr form))
662 (elint-warning "First parameter should be a symbol: %s" form))
663 (setq resenv (elint-form (nth 2 form) env))
664 (let ((newenv (elint-env-add-var env (cadr form)))
665 errlist)
666 (dolist (err (nthcdr 3 form))
667 (setq errlist (car err))
668 (mapc (lambda (s)
669 (or (get s 'error-conditions)
670 (get s 'error-message)
671 (memq s elint-extra-errors)
672 (elint-warning
673 "Not an error symbol in error handler: %s" s)))
674 (cond
675 ((symbolp errlist) (list errlist))
676 ((listp errlist) errlist)
677 (t (elint-error "Bad error list in error handler: %s"
678 errlist)
679 nil)))
680 (elint-forms (cdr err) newenv))))
681 resenv))
682
683 ;;;
684 ;;; Message functions
685 ;;;
686
687 (defvar elint-current-pos) ; dynamically bound in elint-top-form
688
689 (defun elint-log (type string args)
690 (elint-log-message (format "%s:%d:%s: %s"
691 (let ((f (buffer-file-name)))
692 (if f
693 (file-name-nondirectory f)
694 (buffer-name)))
695 (if (boundp 'elint-current-pos)
696 (save-excursion
697 (goto-char elint-current-pos)
698 (1+ (count-lines (point-min)
699 (line-beginning-position))))
700 0) ; unknown position
701 type
702 (apply 'format string args))))
703
704 (defun elint-error (string &rest args)
705 "Report a linting error.
706 STRING and ARGS are thrown on `format' to get the message."
707 (elint-log "Error" string args))
708
709 (defun elint-warning (string &rest args)
710 "Report a linting warning.
711 See `elint-error'."
712 (elint-log "Warning" string args))
713
714 (defun elint-output (string)
715 "Print or insert STRING, depending on value of `noninteractive'."
716 (if noninteractive
717 (message "%s" string)
718 (insert string "\n")))
719
720 (defun elint-log-message (errstr &optional top)
721 "Insert ERRSTR last in the lint log buffer.
722 Optional argument TOP non-nil means pretend `elint-top-form-logged' is non-nil."
723 (with-current-buffer (elint-get-log-buffer)
724 (goto-char (point-max))
725 (let ((inhibit-read-only t))
726 (or (bolp) (newline))
727 ;; Do we have to say where we are?
728 (unless (or elint-top-form-logged top)
729 (let* ((form (elint-top-form-form elint-top-form))
730 (top (car form)))
731 (elint-output (cond
732 ((memq top '(defun defsubst))
733 (format "\nIn function %s:" (cadr form)))
734 ((eq top 'defmacro)
735 (format "\nIn macro %s:" (cadr form)))
736 ((memq top '(defvar defconst))
737 (format "\nIn variable %s:" (cadr form)))
738 (t "\nIn top level expression:"))))
739 (setq elint-top-form-logged t))
740 (elint-output errstr))))
741
742 (defun elint-clear-log (&optional header)
743 "Clear the lint log buffer.
744 Insert HEADER followed by a blank line if non-nil."
745 (let ((dir default-directory))
746 (with-current-buffer (elint-get-log-buffer)
747 (setq default-directory dir)
748 (let ((inhibit-read-only t))
749 (erase-buffer)
750 (if header (insert header "\n"))))))
751
752 (defun elint-display-log ()
753 "Display the lint log buffer."
754 (let ((pop-up-windows t))
755 (display-buffer (elint-get-log-buffer))
756 (sit-for 0)))
757
758 (defvar elint-running)
759
760 (defun elint-set-mode-line (&optional on)
761 "Set the mode-line-process of the Elint log buffer."
762 (with-current-buffer (elint-get-log-buffer)
763 (and (eq major-mode 'compilation-mode)
764 (setq mode-line-process
765 (list (if (or on (bound-and-true-p elint-running))
766 (propertize ":run" 'face 'compilation-warning)
767 (propertize ":finished" 'face 'compilation-info)))))))
768
769 (defun elint-get-log-buffer ()
770 "Return a log buffer for elint."
771 (or (get-buffer elint-log-buffer)
772 (with-current-buffer (get-buffer-create elint-log-buffer)
773 (or (eq major-mode 'compilation-mode)
774 (compilation-mode))
775 (setq buffer-undo-list t)
776 (current-buffer))))
777
778 ;;;
779 ;;; Initializing code
780 ;;;
781
782 ;;;###autoload
783 (defun elint-initialize (&optional reinit)
784 "Initialize elint.
785 If elint is already initialized, this does nothing, unless
786 optional prefix argument REINIT is non-nil."
787 (interactive "P")
788 (if (and elint-builtin-variables (not reinit))
789 (message "Elint is already initialized")
790 (message "Initializing elint...")
791 (setq elint-builtin-variables (elint-find-builtin-variables)
792 elint-autoloaded-variables (elint-find-autoloaded-variables))
793 (mapc (lambda (x) (or (not (symbolp (car x)))
794 (eq (cdr x) 'unknown)
795 (put (car x) 'elint-args (cdr x))))
796 (elint-find-builtin-args))
797 (if elint-unknown-builtin-args
798 (mapc (lambda (x) (put (car x) 'elint-args (cdr x)))
799 elint-unknown-builtin-args))
800 (message "Initializing elint...done")))
801
802
803 (defun elint-find-builtin-variables ()
804 "Return a list of all built-in variables."
805 ;; Cribbed from help-fns.el.
806 (let ((docbuf " *DOC*")
807 vars var)
808 (save-excursion
809 (if (get-buffer docbuf)
810 (progn
811 (set-buffer docbuf)
812 (goto-char (point-min)))
813 (set-buffer (get-buffer-create docbuf))
814 (insert-file-contents-literally
815 (expand-file-name internal-doc-file-name doc-directory)))
816 (while (search-forward "\1fV" nil t)
817 (and (setq var (intern-soft
818 (buffer-substring (point) (line-end-position))))
819 (boundp var)
820 (setq vars (cons var vars))))
821 vars)))
822
823 (defun elint-find-autoloaded-variables ()
824 "Return a list of all autoloaded variables."
825 (let (var vars)
826 (with-temp-buffer
827 (insert-file-contents (locate-library "loaddefs.el"))
828 (while (re-search-forward "^(defvar \\([[:alnum:]_-]+\\)" nil t)
829 (and (setq var (intern-soft (match-string 1)))
830 (boundp var)
831 (setq vars (cons var vars)))))
832 vars))
833
834 (defun elint-find-builtins ()
835 "Return a list of all built-in functions."
836 (let (subrs)
837 (mapatoms (lambda (s) (and (fboundp s) (subrp (symbol-function s))
838 (setq subrs (cons s subrs)))))
839 subrs))
840
841 (defun elint-find-builtin-args (&optional list)
842 "Return a list of the built-in functions and their arguments.
843 If LIST is nil, call `elint-find-builtins' to get a list of all built-in
844 functions, otherwise use LIST.
845
846 Each function is represented by a cons cell:
847 \(function-symbol . args)
848 If no documentation could be found args will be `unknown'."
849 (mapcar (lambda (f)
850 (let ((doc (documentation f t)))
851 (or (and doc
852 (string-match "\n\n(fn\\(.*)\\)\\'" doc)
853 (ignore-errors
854 ;; "BODY...)" -> "&rest BODY)".
855 (read (replace-regexp-in-string
856 "\\([^ ]+\\)\\.\\.\\.)\\'" "&rest \\1)"
857 (format "(%s %s" f (match-string 1 doc)) t))))
858 (cons f 'unknown))))
859 (or list (elint-find-builtins))))
860
861 (provide 'elint)
862
863 ;; arch-tag: b2f061e2-af84-4ddc-8e39-f5e969ac228f
864 ;;; elint.el ends here