comparison lisp/emacs-lisp/elint.el @ 58938:f3ac898990de

(elint-standard-variables, elint-unknown-builtin-args): Move definitions up.
author Richard M. Stallman <rms@gnu.org>
date Mon, 13 Dec 2004 19:34:46 +0000
parents 4ea947ec3dac
children 3da5314608ab fb79180b618d
comparison
equal deleted inserted replaced
58937:48de6e40fcfc 58938:f3ac898990de
46 46
47 ;;; Code: 47 ;;; Code:
48 48
49 (defvar elint-log-buffer "*Elint*" 49 (defvar elint-log-buffer "*Elint*"
50 "*The buffer to insert lint messages in.") 50 "*The buffer to insert lint messages in.")
51
52 ;;;
53 ;;; ADT: top-form
54 ;;;
55
56 (defsubst elint-make-top-form (form pos)
57 "Create a top form.
58 FORM is the form, and POS is the point where it starts in the buffer."
59 (cons form pos))
60
61 (defsubst elint-top-form-form (top-form)
62 "Extract the form from a TOP-FORM."
63 (car top-form))
64
65 (defsubst elint-top-form-pos (top-form)
66 "Extract the position from a TOP-FORM."
67 (cdr top-form))
68
69 ;;;
70 ;;; ADT: env
71 ;;;
72
73 (defsubst elint-make-env ()
74 "Create an empty environment."
75 (list (list nil) nil nil))
76
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))))))
83
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)))
88
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.
92 ENV is returned."
93 (nconc (car env) (list (list var)))
94 env)
95
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."
99 (assq var (car env)))
100
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."
104 (list (car env)
105 (cons (list func args) (car (cdr env)))
106 (car (cdr (cdr env)))))
107
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))))
112
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."
117 (list (car env)
118 (car (cdr env))
119 (cons (cons macro def) (car (cdr (cdr env))))))
120
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))))
125
126 (defsubst elint-env-macrop (env macro)
127 "Non-nil if ENV contains MACRO."
128 (assq macro (elint-env-macro-env env)))
129
130 ;;;
131 ;;; User interface
132 ;;;
133
134 (defun elint-current-buffer ()
135 "Lint the current buffer."
136 (interactive)
137 (elint-clear-log (format "Linting %s" (if (buffer-file-name)
138 (buffer-file-name)
139 (buffer-name))))
140 (elint-display-log)
141 (mapcar 'elint-top-form (elint-update-env))
142
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")))
148
149 (defun elint-defun ()
150 "Lint the function at point."
151 (interactive)
152 (save-excursion
153 (if (not (beginning-of-defun))
154 (error "Lint what?"))
155
156 (let ((pos (point))
157 (def (read (current-buffer))))
158 (elint-display-log)
159
160 (elint-update-env)
161 (elint-top-form (elint-make-top-form def pos)))))
162
163 ;;;
164 ;;; Top form functions
165 ;;;
166
167 (defvar elint-buffer-env nil
168 "The environment of a elisp buffer.
169 Will be local in linted buffers.")
170
171 (defvar elint-buffer-forms nil
172 "The top forms in a buffer.
173 Will be local in linted buffers.")
174
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.")
178
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.
183 Returns the forms."
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))
188 ;; Env is up to date
189 elint-buffer-forms
190 ;; Remake env
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))
195 elint-buffer-forms))
196
197 (defun elint-get-top-forms ()
198 "Collect all the top forms in the current buffer."
199 (save-excursion
200 (let ((tops nil))
201 (goto-char (point-min))
202 (while (elint-find-next-top-form)
203 (let ((pos (point)))
204 (condition-case nil
205 (setq tops (cons
206 (elint-make-top-form (read (current-buffer)) pos)
207 tops))
208 (end-of-file
209 (goto-char pos)
210 (end-of-line)
211 (error "Missing ')' in top form: %s" (buffer-substring pos (point)))))
212 ))
213 (nreverse tops))))
214
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)
219 (not (eobp)))
220
221 (defun elint-init-env (forms)
222 "Initialise the environment from FORMS."
223 (let ((env (elint-make-env))
224 form)
225 (while forms
226 (setq form (elint-top-form-form (car forms))
227 forms (cdr forms))
228 (cond
229 ;; Add defined variable
230 ((memq (car form) '(defvar defconst defcustom))
231 (setq env (elint-env-add-var env (car (cdr form)))))
232 ;; Add function
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))
239 (cons 'lambda
240 (cdr (cdr form))))
241 env (elint-env-add-func env (car (cdr form))
242 (car (cdr (cdr form))))))
243
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))))
249 ))
250 env))
251
252 (defun elint-add-required-env (env name file)
253 "Augment ENV with the variables definied by feature NAME in FILE."
254 (condition-case nil
255 (let* ((libname (if (stringp file)
256 file
257 (symbol-name name)))
258
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 :-/
263 (message nil)
264 (if lib
265 (save-excursion
266 (set-buffer (find-file-noselect lib))
267 (elint-update-env)
268 (setq env (elint-env-add-env env elint-buffer-env)))
269 (error "dummy error...")))
270 (error
271 (ding)
272 (message "Can't get variables from require'd library %s" name)))
273 env)
274
275 (defun regexp-assoc (regexp alist)
276 "Search for a key matching REGEXP in ALIST."
277 (let ((res nil))
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))))
283 res))
284
285 (defvar elint-top-form nil
286 "The currently linted top form, or nil.")
287
288 (defvar elint-top-form-logged nil
289 "T if the currently linted top form has been mentioned in the log buffer.")
290
291 (defun elint-top-form (form)
292 "Lint a top FORM."
293 (let ((elint-top-form form)
294 (elint-top-form-logged nil))
295 (elint-form (elint-top-form-form form) elint-buffer-env)))
296
297 ;;;
298 ;;; General form linting functions
299 ;;;
300
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.")
319
320 (defun elint-form (form env)
321 "Lint FORM in the environment ENV.
322 The environment created by the form is returned."
323 (cond
324 ((consp form)
325 (let ((func (cdr (assq (car form) elint-special-forms))))
326 (if func
327 ;; Special form
328 (funcall func form env)
329
330 (let* ((func (car form))
331 (args (elint-get-args func env))
332 (argsok t))
333 (cond
334 ((eq args 'undefined)
335 (setq argsok nil)
336 (elint-error "Call to undefined function: %s" form))
337
338 ((eq args 'unknown) nil)
339
340 (t (setq argsok (elint-match-args form args))))
341
342 ;; Is this a macro?
343 (if (elint-env-macrop env func)
344 ;; Macro defined in buffer, expand it
345 (if argsok
346 (elint-form (macroexpand form (elint-env-macro-env env)) env)
347 env)
348
349 (let ((fcode (if (symbolp func)
350 (if (fboundp func)
351 (indirect-function func)
352 nil)
353 func)))
354 (if (and (listp fcode) (eq (car fcode) 'macro))
355 ;; Macro defined outside buffer
356 (if argsok
357 (elint-form (macroexpand form) env)
358 env)
359 ;; Function, lint its parameters
360 (elint-forms (cdr form) env))))
361 ))
362 ))
363 ((symbolp form)
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))
368 env)
369
370 (t env)
371 ))
372
373 (defun elint-forms (forms env)
374 "Lint the FORMS, accumulating an environment, starting with ENV."
375 ;; grumblegrumbletailrecursiongrumblegrumble
376 (while forms
377 (setq env (elint-form (car forms) env)
378 forms (cdr forms)))
379 env)
380
381 (defun elint-unbound-variable (var env)
382 "T if VAR is unbound in ENV."
383 (not (or (eq var nil)
384 (eq var t)
385 (elint-env-find-var env var)
386 (memq var elint-standard-variables))))
387
388 ;;;
389 ;;; Function argument checking
390 ;;;
391
392 (defun elint-match-args (arglist argpattern)
393 "Match ARGLIST against ARGPATTERN."
394
395 (let ((state 'all)
396 (al (cdr arglist))
397 (ap argpattern)
398 (ok t))
399 (while
400 (cond
401 ((and (null al) (null ap)) nil)
402 ((eq (car ap) '&optional)
403 (setq state 'optional)
404 (setq ap (cdr ap))
405 t)
406 ((eq (car ap) '&rest)
407 nil)
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)
411 (setq ok nil)
412 nil)
413 ((and (eq state 'optional) (null al))
414 nil)
415 (t (setq al (cdr al)
416 ap (cdr ap))
417 t)))
418 ok))
419
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)))
424 (if f
425 (car (cdr f))
426 (if (symbolp func)
427 (if (fboundp func)
428 (let ((fcode (indirect-function func)))
429 (if (subrp fcode)
430 (let ((args (get func 'elint-args)))
431 (if args args 'unknown))
432 (elint-find-args-in-code fcode)))
433 'undefined)
434 (elint-find-args-in-code func)))))
435
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."
439 (cond
440 ((byte-code-function-p code)
441 (aref code 0))
442 ((and (listp code) (eq (car code) 'lambda))
443 (car (cdr code)))
444 ((and (listp code) (eq (car code) 'macro))
445 (elint-find-args-in-code (cdr code)))
446 (t 'unknown)))
447
448 ;;;
449 ;;; Functions to check some special forms
450 ;;;
451
452 (defun elint-check-cond-form (form env)
453 "Lint a cond FORM in ENV."
454 (setq form (cdr form))
455 (while 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)))
460 env)
461
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)))
468 ))
469 (car form))
470 (elint-forms (cdr form) env))
471
472 (defun elint-check-let-form (form env)
473 "Lint the let/let* FORM in ENV."
474 (let ((varlist (car (cdr form))))
475 (if (not varlist)
476 (progn
477 (elint-error "Missing varlist in let: %s" form)
478 env)
479
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))
486
487 ;; Add variables to environment, and check the init values
488 (let ((newenv env))
489 (mapcar (function (lambda (s)
490 (cond
491 ((symbolp 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)
496 env
497 newenv))
498 (setq newenv
499 (elint-env-add-var newenv (car s))))
500 (t (elint-error
501 "Malformed `let' declaration: %s" s))
502 )))
503 varlist)
504
505 ;; Lint the body forms
506 (elint-forms (cdr (cdr form)) newenv)
507 ))))
508
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))
513
514 (let ((newenv env)
515 sym val)
516 (setq form (cdr form))
517 (while form
518 (setq sym (car form)
519 val (car (cdr form))
520 form (cdr (cdr form)))
521 (if (symbolp sym)
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)
526 (if (symbolp sym)
527 (setq newenv (elint-env-add-var newenv sym))))
528 newenv))
529
530 (defun elint-check-defvar-form (form env)
531 "Lint the defvar/defconst FORM in ENV."
532 (if (or (= (length form) 2)
533 (= (length form) 3)
534 (and (= (length form) 4) (stringp (nth 3 form))))
535 (elint-env-add-global-var (elint-form (nth 2 form) env)
536 (car (cdr form)))
537 (elint-error "Malformed variable declaration: %s" form)
538 env))
539
540 (defun elint-check-defcustom-form (form env)
541 "Lint the defcustom FORM in ENV."
542 (if (and (> (length form) 3)
543 ;; even no. of keyword/value args ?
544 (zerop (logand (length form) 1)))
545 (elint-env-add-global-var (elint-form (nth 2 form) env)
546 (car (cdr form)))
547 (elint-error "Malformed variable declaration: %s" form)
548 env))
549
550 (defun elint-check-function-form (form env)
551 "Lint the function FORM in ENV."
552 (let ((func (car (cdr-safe form))))
553 (cond
554 ((symbolp func)
555 (or (elint-env-find-func env func)
556 (fboundp func)
557 (elint-warning "Reference to undefined function: %s" form))
558 env)
559 ((and (consp func) (memq (car func) '(lambda macro)))
560 (elint-form func env))
561 ((stringp func) env)
562 (t (elint-error "Not a function object: %s" form)
563 env)
564 )))
565
566 (defun elint-check-quote-form (form env)
567 "Lint the quote FORM in ENV."
568 env)
569
570 (defun elint-check-macro-form (form env)
571 "Check the macro FORM in ENV."
572 (elint-check-function-form (list (car form) (cdr form)) env))
573
574 (defun elint-check-condition-case-form (form env)
575 "Check the condition-case FORM in ENV."
576 (let ((resenv env))
577 (if (< (length form) 3)
578 (elint-error "Malformed condition-case: %s" form)
579 (or (symbolp (car (cdr form)))
580 (elint-warning "First parameter should be a symbol: %s" form))
581 (setq resenv (elint-form (nth 2 form) env))
582
583 (let ((newenv (elint-env-add-var env (car (cdr form))))
584 (errforms (nthcdr 3 form))
585 errlist)
586 (while errforms
587 (setq errlist (car (car errforms)))
588 (mapcar (function (lambda (s)
589 (or (get s 'error-conditions)
590 (get s 'error-message)
591 (elint-warning
592 "Not an error symbol in error handler: %s" s))))
593 (cond
594 ((symbolp errlist) (list errlist))
595 ((listp errlist) errlist)
596 (t (elint-error "Bad error list in error handler: %s"
597 errlist)
598 nil))
599 )
600 (elint-forms (cdr (car errforms)) newenv)
601 (setq errforms (cdr errforms))
602 )))
603 resenv))
604
605 ;;;
606 ;;; Message functions
607 ;;;
608
609 ;; elint-error and elint-warning are identical, but they might change
610 ;; to reflect different seriousness of linting errors
611
612 (defun elint-error (string &rest args)
613 "Report a linting error.
614 STRING and ARGS are thrown on `format' to get the message."
615 (let ((errstr (apply 'format string args)))
616 (elint-log-message errstr)
617 ))
618
619 (defun elint-warning (string &rest args)
620 "Report a linting warning.
621 STRING and ARGS are thrown on `format' to get the message."
622 (let ((errstr (apply 'format string args)))
623 (elint-log-message errstr)
624 ))
625
626 (defun elint-log-message (errstr)
627 "Insert ERRSTR last in the lint log buffer."
628 (save-excursion
629 (set-buffer (elint-get-log-buffer))
630 (goto-char (point-max))
631 (or (bolp) (newline))
632
633 ;; Do we have to say where we are?
634 (if elint-top-form-logged
635 nil
636 (insert
637 (let* ((form (elint-top-form-form elint-top-form))
638 (top (car form)))
639 (cond
640 ((memq top '(defun defsubst))
641 (format "\n** function %s **\n" (car (cdr form))))
642 ((eq top 'defmacro)
643 (format "\n** macro %s **\n" (car (cdr form))))
644 ((memq top '(defvar defconst))
645 (format "\n** variable %s **\n" (car (cdr form))))
646 (t "\n** top level expression **\n"))))
647 (setq elint-top-form-logged t))
648
649 (insert errstr)
650 (newline)))
651
652 (defun elint-clear-log (&optional header)
653 "Clear the lint log buffer.
654 Insert HEADER followed by a blank line if non-nil."
655 (save-excursion
656 (set-buffer (elint-get-log-buffer))
657 (erase-buffer)
658 (if header
659 (progn
660 (insert header)
661 (newline))
662 )))
663
664 (defun elint-display-log ()
665 "Display the lint log buffer."
666 (let ((pop-up-windows t))
667 (display-buffer (elint-get-log-buffer))
668 (sit-for 0)))
669
670 (defun elint-get-log-buffer ()
671 "Return a log buffer for elint."
672 (let ((buf (get-buffer elint-log-buffer)))
673 (if buf
674 buf
675 (let ((oldbuf (current-buffer)))
676 (prog1
677 (set-buffer (get-buffer-create elint-log-buffer))
678 (setq truncate-lines t)
679 (set-buffer oldbuf)))
680 )))
681
682 ;;;
683 ;;; Initializing code
684 ;;;
685
686 ;;;###autoload
687 (defun elint-initialize ()
688 "Initialize elint."
689 (interactive)
690 (mapcar (function (lambda (x)
691 (or (not (symbolp (car x)))
692 (eq (cdr x) 'unknown)
693 (put (car x) 'elint-args (cdr x)))))
694 (elint-find-builtin-args))
695 (mapcar (function (lambda (x)
696 (put (car x) 'elint-args (cdr x))))
697 elint-unknown-builtin-args))
698
699
700 (defun elint-find-builtins ()
701 "Returns a list of all built-in functions."
702 (let ((subrs nil))
703 (mapatoms (lambda (s) (if (and (fboundp s) (subrp (symbol-function s)))
704 (setq subrs (cons s subrs)))))
705 subrs
706 ))
707
708 (defun elint-find-builtin-args (&optional list)
709 "Returns a list of the built-in functions and their arguments.
710
711 If LIST is nil, call `elint-find-builtins' to get a list of all built-in
712 functions, otherwise use LIST.
713
714 Each functions is represented by a cons cell:
715 \(function-symbol . args)
716 If no documentation could be found args will be `unknown'."
717
718 (mapcar (function (lambda (f)
719 (let ((doc (documentation f t)))
720 (if (and doc (string-match "\n\n\\((.*)\\)" doc))
721 (read (match-string 1 doc))
722 (cons f 'unknown))
723 )))
724 (if list list
725 (elint-find-builtins))))
726 51
727 ;;; 52 ;;;
728 ;;; Data 53 ;;; Data
729 ;;; 54 ;;;
730 55
801 (+ &rest args) 126 (+ &rest args)
802 (* &rest args) 127 (* &rest args)
803 (interactive &optional args)) 128 (interactive &optional args))
804 "Those built-ins for which we can't find arguments.") 129 "Those built-ins for which we can't find arguments.")
805 130
131 ;;;
132 ;;; ADT: top-form
133 ;;;
134
135 (defsubst elint-make-top-form (form pos)
136 "Create a top form.
137 FORM is the form, and POS is the point where it starts in the buffer."
138 (cons form pos))
139
140 (defsubst elint-top-form-form (top-form)
141 "Extract the form from a TOP-FORM."
142 (car top-form))
143
144 (defsubst elint-top-form-pos (top-form)
145 "Extract the position from a TOP-FORM."
146 (cdr top-form))
147
148 ;;;
149 ;;; ADT: env
150 ;;;
151
152 (defsubst elint-make-env ()
153 "Create an empty environment."
154 (list (list nil) nil nil))
155
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))))))
162
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)))
167
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.
171 ENV is returned."
172 (nconc (car env) (list (list var)))
173 env)
174
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)))
179
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."
183 (list (car env)
184 (cons (list func args) (car (cdr env)))
185 (car (cdr (cdr env)))))
186
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))))
191
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."
196 (list (car env)
197 (car (cdr env))
198 (cons (cons macro def) (car (cdr (cdr env))))))
199
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))))
204
205 (defsubst elint-env-macrop (env macro)
206 "Non-nil if ENV contains MACRO."
207 (assq macro (elint-env-macro-env env)))
208
209 ;;;
210 ;;; User interface
211 ;;;
212
213 (defun elint-current-buffer ()
214 "Lint the current buffer."
215 (interactive)
216 (elint-clear-log (format "Linting %s" (if (buffer-file-name)
217 (buffer-file-name)
218 (buffer-name))))
219 (elint-display-log)
220 (mapcar 'elint-top-form (elint-update-env))
221
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")))
227
228 (defun elint-defun ()
229 "Lint the function at point."
230 (interactive)
231 (save-excursion
232 (if (not (beginning-of-defun))
233 (error "Lint what?"))
234
235 (let ((pos (point))
236 (def (read (current-buffer))))
237 (elint-display-log)
238
239 (elint-update-env)
240 (elint-top-form (elint-make-top-form def pos)))))
241
242 ;;;
243 ;;; Top form functions
244 ;;;
245
246 (defvar elint-buffer-env nil
247 "The environment of a elisp buffer.
248 Will be local in linted buffers.")
249
250 (defvar elint-buffer-forms nil
251 "The top forms in a buffer.
252 Will be local in linted buffers.")
253
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.")
257
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.
262 Returns the forms."
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))
267 ;; Env is up to date
268 elint-buffer-forms
269 ;; Remake env
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))
274 elint-buffer-forms))
275
276 (defun elint-get-top-forms ()
277 "Collect all the top forms in the current buffer."
278 (save-excursion
279 (let ((tops nil))
280 (goto-char (point-min))
281 (while (elint-find-next-top-form)
282 (let ((pos (point)))
283 (condition-case nil
284 (setq tops (cons
285 (elint-make-top-form (read (current-buffer)) pos)
286 tops))
287 (end-of-file
288 (goto-char pos)
289 (end-of-line)
290 (error "Missing ')' in top form: %s" (buffer-substring pos (point)))))
291 ))
292 (nreverse tops))))
293
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)
298 (not (eobp)))
299
300 (defun elint-init-env (forms)
301 "Initialise the environment from FORMS."
302 (let ((env (elint-make-env))
303 form)
304 (while forms
305 (setq form (elint-top-form-form (car forms))
306 forms (cdr forms))
307 (cond
308 ;; Add defined variable
309 ((memq (car form) '(defvar defconst defcustom))
310 (setq env (elint-env-add-var env (car (cdr form)))))
311 ;; Add function
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))
318 (cons 'lambda
319 (cdr (cdr form))))
320 env (elint-env-add-func env (car (cdr form))
321 (car (cdr (cdr form))))))
322
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))))
328 ))
329 env))
330
331 (defun elint-add-required-env (env name file)
332 "Augment ENV with the variables definied by feature NAME in FILE."
333 (condition-case nil
334 (let* ((libname (if (stringp file)
335 file
336 (symbol-name name)))
337
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 :-/
342 (message nil)
343 (if lib
344 (save-excursion
345 (set-buffer (find-file-noselect lib))
346 (elint-update-env)
347 (setq env (elint-env-add-env env elint-buffer-env)))
348 (error "dummy error...")))
349 (error
350 (ding)
351 (message "Can't get variables from require'd library %s" name)))
352 env)
353
354 (defun regexp-assoc (regexp alist)
355 "Search for a key matching REGEXP in ALIST."
356 (let ((res nil))
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))))
362 res))
363
364 (defvar elint-top-form nil
365 "The currently linted top form, or nil.")
366
367 (defvar elint-top-form-logged nil
368 "T if the currently linted top form has been mentioned in the log buffer.")
369
370 (defun elint-top-form (form)
371 "Lint a top FORM."
372 (let ((elint-top-form form)
373 (elint-top-form-logged nil))
374 (elint-form (elint-top-form-form form) elint-buffer-env)))
375
376 ;;;
377 ;;; General form linting functions
378 ;;;
379
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.")
398
399 (defun elint-form (form env)
400 "Lint FORM in the environment ENV.
401 The environment created by the form is returned."
402 (cond
403 ((consp form)
404 (let ((func (cdr (assq (car form) elint-special-forms))))
405 (if func
406 ;; Special form
407 (funcall func form env)
408
409 (let* ((func (car form))
410 (args (elint-get-args func env))
411 (argsok t))
412 (cond
413 ((eq args 'undefined)
414 (setq argsok nil)
415 (elint-error "Call to undefined function: %s" form))
416
417 ((eq args 'unknown) nil)
418
419 (t (setq argsok (elint-match-args form args))))
420
421 ;; Is this a macro?
422 (if (elint-env-macrop env func)
423 ;; Macro defined in buffer, expand it
424 (if argsok
425 (elint-form (macroexpand form (elint-env-macro-env env)) env)
426 env)
427
428 (let ((fcode (if (symbolp func)
429 (if (fboundp func)
430 (indirect-function func)
431 nil)
432 func)))
433 (if (and (listp fcode) (eq (car fcode) 'macro))
434 ;; Macro defined outside buffer
435 (if argsok
436 (elint-form (macroexpand form) env)
437 env)
438 ;; Function, lint its parameters
439 (elint-forms (cdr form) env))))
440 ))
441 ))
442 ((symbolp form)
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))
447 env)
448
449 (t env)
450 ))
451
452 (defun elint-forms (forms env)
453 "Lint the FORMS, accumulating an environment, starting with ENV."
454 ;; grumblegrumbletailrecursiongrumblegrumble
455 (while forms
456 (setq env (elint-form (car forms) env)
457 forms (cdr forms)))
458 env)
459
460 (defun elint-unbound-variable (var env)
461 "T if VAR is unbound in ENV."
462 (not (or (eq var nil)
463 (eq var t)
464 (elint-env-find-var env var)
465 (memq var elint-standard-variables))))
466
467 ;;;
468 ;;; Function argument checking
469 ;;;
470
471 (defun elint-match-args (arglist argpattern)
472 "Match ARGLIST against ARGPATTERN."
473
474 (let ((state 'all)
475 (al (cdr arglist))
476 (ap argpattern)
477 (ok t))
478 (while
479 (cond
480 ((and (null al) (null ap)) nil)
481 ((eq (car ap) '&optional)
482 (setq state 'optional)
483 (setq ap (cdr ap))
484 t)
485 ((eq (car ap) '&rest)
486 nil)
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)
490 (setq ok nil)
491 nil)
492 ((and (eq state 'optional) (null al))
493 nil)
494 (t (setq al (cdr al)
495 ap (cdr ap))
496 t)))
497 ok))
498
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)))
503 (if f
504 (car (cdr f))
505 (if (symbolp func)
506 (if (fboundp func)
507 (let ((fcode (indirect-function func)))
508 (if (subrp fcode)
509 (let ((args (get func 'elint-args)))
510 (if args args 'unknown))
511 (elint-find-args-in-code fcode)))
512 'undefined)
513 (elint-find-args-in-code func)))))
514
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."
518 (cond
519 ((byte-code-function-p code)
520 (aref code 0))
521 ((and (listp code) (eq (car code) 'lambda))
522 (car (cdr code)))
523 ((and (listp code) (eq (car code) 'macro))
524 (elint-find-args-in-code (cdr code)))
525 (t 'unknown)))
526
527 ;;;
528 ;;; Functions to check some special forms
529 ;;;
530
531 (defun elint-check-cond-form (form env)
532 "Lint a cond FORM in ENV."
533 (setq form (cdr form))
534 (while 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)))
539 env)
540
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)))
547 ))
548 (car form))
549 (elint-forms (cdr form) env))
550
551 (defun elint-check-let-form (form env)
552 "Lint the let/let* FORM in ENV."
553 (let ((varlist (car (cdr form))))
554 (if (not varlist)
555 (progn
556 (elint-error "Missing varlist in let: %s" form)
557 env)
558
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))
565
566 ;; Add variables to environment, and check the init values
567 (let ((newenv env))
568 (mapcar (function (lambda (s)
569 (cond
570 ((symbolp 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)
575 env
576 newenv))
577 (setq newenv
578 (elint-env-add-var newenv (car s))))
579 (t (elint-error
580 "Malformed `let' declaration: %s" s))
581 )))
582 varlist)
583
584 ;; Lint the body forms
585 (elint-forms (cdr (cdr form)) newenv)
586 ))))
587
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))
592
593 (let ((newenv env)
594 sym val)
595 (setq form (cdr form))
596 (while form
597 (setq sym (car form)
598 val (car (cdr form))
599 form (cdr (cdr form)))
600 (if (symbolp sym)
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)
605 (if (symbolp sym)
606 (setq newenv (elint-env-add-var newenv sym))))
607 newenv))
608
609 (defun elint-check-defvar-form (form env)
610 "Lint the defvar/defconst FORM in ENV."
611 (if (or (= (length form) 2)
612 (= (length form) 3)
613 (and (= (length form) 4) (stringp (nth 3 form))))
614 (elint-env-add-global-var (elint-form (nth 2 form) env)
615 (car (cdr form)))
616 (elint-error "Malformed variable declaration: %s" form)
617 env))
618
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)
625 (car (cdr form)))
626 (elint-error "Malformed variable declaration: %s" form)
627 env))
628
629 (defun elint-check-function-form (form env)
630 "Lint the function FORM in ENV."
631 (let ((func (car (cdr-safe form))))
632 (cond
633 ((symbolp func)
634 (or (elint-env-find-func env func)
635 (fboundp func)
636 (elint-warning "Reference to undefined function: %s" form))
637 env)
638 ((and (consp func) (memq (car func) '(lambda macro)))
639 (elint-form func env))
640 ((stringp func) env)
641 (t (elint-error "Not a function object: %s" form)
642 env)
643 )))
644
645 (defun elint-check-quote-form (form env)
646 "Lint the quote FORM in ENV."
647 env)
648
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))
652
653 (defun elint-check-condition-case-form (form env)
654 "Check the condition-case FORM in ENV."
655 (let ((resenv 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))
661
662 (let ((newenv (elint-env-add-var env (car (cdr form))))
663 (errforms (nthcdr 3 form))
664 errlist)
665 (while errforms
666 (setq errlist (car (car errforms)))
667 (mapcar (function (lambda (s)
668 (or (get s 'error-conditions)
669 (get s 'error-message)
670 (elint-warning
671 "Not an error symbol in error handler: %s" s))))
672 (cond
673 ((symbolp errlist) (list errlist))
674 ((listp errlist) errlist)
675 (t (elint-error "Bad error list in error handler: %s"
676 errlist)
677 nil))
678 )
679 (elint-forms (cdr (car errforms)) newenv)
680 (setq errforms (cdr errforms))
681 )))
682 resenv))
683
684 ;;;
685 ;;; Message functions
686 ;;;
687
688 ;; elint-error and elint-warning are identical, but they might change
689 ;; to reflect different seriousness of linting errors
690
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)
696 ))
697
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)
703 ))
704
705 (defun elint-log-message (errstr)
706 "Insert ERRSTR last in the lint log buffer."
707 (save-excursion
708 (set-buffer (elint-get-log-buffer))
709 (goto-char (point-max))
710 (or (bolp) (newline))
711
712 ;; Do we have to say where we are?
713 (if elint-top-form-logged
714 nil
715 (insert
716 (let* ((form (elint-top-form-form elint-top-form))
717 (top (car form)))
718 (cond
719 ((memq top '(defun defsubst))
720 (format "\n** function %s **\n" (car (cdr form))))
721 ((eq top 'defmacro)
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))
727
728 (insert errstr)
729 (newline)))
730
731 (defun elint-clear-log (&optional header)
732 "Clear the lint log buffer.
733 Insert HEADER followed by a blank line if non-nil."
734 (save-excursion
735 (set-buffer (elint-get-log-buffer))
736 (erase-buffer)
737 (if header
738 (progn
739 (insert header)
740 (newline))
741 )))
742
743 (defun elint-display-log ()
744 "Display the lint log buffer."
745 (let ((pop-up-windows t))
746 (display-buffer (elint-get-log-buffer))
747 (sit-for 0)))
748
749 (defun elint-get-log-buffer ()
750 "Return a log buffer for elint."
751 (let ((buf (get-buffer elint-log-buffer)))
752 (if buf
753 buf
754 (let ((oldbuf (current-buffer)))
755 (prog1
756 (set-buffer (get-buffer-create elint-log-buffer))
757 (setq truncate-lines t)
758 (set-buffer oldbuf)))
759 )))
760
761 ;;;
762 ;;; Initializing code
763 ;;;
764
765 ;;;###autoload
766 (defun elint-initialize ()
767 "Initialize elint."
768 (interactive)
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))
777
778
779 (defun elint-find-builtins ()
780 "Returns a list of all built-in functions."
781 (let ((subrs nil))
782 (mapatoms (lambda (s) (if (and (fboundp s) (subrp (symbol-function s)))
783 (setq subrs (cons s subrs)))))
784 subrs
785 ))
786
787 (defun elint-find-builtin-args (&optional list)
788 "Returns a list of the built-in functions and their arguments.
789
790 If LIST is nil, call `elint-find-builtins' to get a list of all built-in
791 functions, otherwise use LIST.
792
793 Each functions is represented by a cons cell:
794 \(function-symbol . args)
795 If no documentation could be found args will be `unknown'."
796
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))
801 (cons f 'unknown))
802 )))
803 (if list list
804 (elint-find-builtins))))
805
806 (provide 'elint) 806 (provide 'elint)
807 807
808 ;;; arch-tag: b2f061e2-af84-4ddc-8e39-f5e969ac228f 808 ;;; arch-tag: b2f061e2-af84-4ddc-8e39-f5e969ac228f
809 ;;; elint.el ends here 809 ;;; elint.el ends here