lisp/emacs-lisp/package.el (tar-get-file-descriptor, tar--extract): Declare.
[emacs.git] / lisp / emacs-lisp / testcover.el
bloba5619583145b5613c45132d3a814cf7c7f4911a1
1 ;;;; testcover.el -- Visual code-coverage tool
3 ;; Copyright (C) 2002-2013 Free Software Foundation, Inc.
5 ;; Author: Jonathan Yavner <jyavner@member.fsf.org>
6 ;; Maintainer: Jonathan Yavner <jyavner@member.fsf.org>
7 ;; Keywords: lisp utility
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; * Use `testcover-start' to instrument a Lisp file for coverage testing.
28 ;; * Use `testcover-mark-all' to add overlay "splotches" to the Lisp file's
29 ;; buffer to show where coverage is lacking. Normally, a red splotch
30 ;; indicates the form was never evaluated; a brown splotch means it always
31 ;; evaluated to the same value.
32 ;; * Use `testcover-next-mark' (bind it to a key!) to jump to the next spot
33 ;; that has a splotch.
35 ;; * Basic algorithm: use `edebug' to mark up the function text with
36 ;; instrumentation callbacks, then replace edebug's callbacks with ours.
37 ;; * To show good coverage, we want to see two values for every form, except
38 ;; functions that always return the same value and `defconst' variables
39 ;; need show only one value for good coverage. To avoid the brown
40 ;; splotch, the definitions for constants and 1-valued functions must
41 ;; precede the references.
42 ;; * Use the macro `1value' in your Lisp code to mark spots where the local
43 ;; code environment causes a function or variable to always have the same
44 ;; value, but the function or variable is not intrinsically 1-valued.
45 ;; * Use the macro `noreturn' in your Lisp code to mark function calls that
46 ;; never return, because of the local code environment, even though the
47 ;; function being called is capable of returning in other cases.
49 ;; Problems:
50 ;; * To detect different values, we store the form's result in a vector and
51 ;; compare the next result using `equal'. We don't copy the form's
52 ;; result, so if caller alters it (`setcar', etc.) we'll think the next
53 ;; call has the same value! Also, equal thinks two strings are the same
54 ;; if they differ only in properties.
55 ;; * Because we have only a "1value" class and no "always nil" class, we have
56 ;; to treat as potentially 1-valued any `and' whose last term is 1-valued,
57 ;; in case the last term is always nil. Example:
58 ;; (and (< (point) 1000) (forward-char 10))
59 ;; This form always returns nil. Similarly, `or', `if', and `cond' are
60 ;; treated as potentially 1-valued if all clauses are, in case those
61 ;; values are always nil. Unlike truly 1-valued functions, it is not an
62 ;; error if these "potentially" 1-valued forms actually return differing
63 ;; values.
65 (require 'edebug)
66 (provide 'testcover)
69 ;;;==========================================================================
70 ;;; User options
71 ;;;==========================================================================
73 (defgroup testcover nil
74 "Code-coverage tester."
75 :group 'lisp
76 :prefix "testcover-"
77 :version "21.1")
79 (defcustom testcover-constants
80 '(nil t emacs-build-time emacs-version emacs-major-version
81 emacs-minor-version)
82 "Variables whose values never change. No brown splotch is shown for
83 these. This list is quite incomplete!"
84 :group 'testcover
85 :type '(repeat variable))
87 (defcustom testcover-1value-functions
88 '(backward-char barf-if-buffer-read-only beginning-of-line
89 buffer-disable-undo buffer-enable-undo current-global-map
90 deactivate-mark delete-backward-char delete-char delete-region ding
91 forward-char function* insert insert-and-inherit kill-all-local-variables
92 kill-line kill-paragraph kill-region kill-sexp lambda
93 minibuffer-complete-and-exit narrow-to-region next-line push-mark
94 put-text-property run-hooks set-match-data signal
95 substitute-key-definition suppress-keymap undo use-local-map while widen
96 yank)
97 "Functions that always return the same value. No brown splotch is shown
98 for these. This list is quite incomplete! Notes: Nobody ever changes the
99 current global map. The macro `lambda' is self-evaluating, hence always
100 returns the same value (the function it defines may return varying values
101 when called)."
102 :group 'testcover
103 :type '(repeat symbol))
105 (defcustom testcover-noreturn-functions
106 '(error noreturn throw signal)
107 "Subset of `testcover-1value-functions' -- these never return. We mark
108 them as having returned nil just before calling them."
109 :group 'testcover
110 :type '(repeat symbol))
112 (defcustom testcover-compose-functions
113 '(+ - * / = append length list make-keymap make-sparse-keymap
114 mapcar message propertize replace-regexp-in-string
115 run-with-idle-timer set-buffer-modified-p)
116 "Functions that are 1-valued if all their args are either constants or
117 calls to one of the `testcover-1value-functions', so if that's true then no
118 brown splotch is shown for these. This list is quite incomplete! Most
119 side-effect-free functions should be here."
120 :group 'testcover
121 :type '(repeat symbol))
123 (defcustom testcover-progn-functions
124 '(define-key fset function goto-char mapc overlay-put progn
125 save-current-buffer save-excursion save-match-data
126 save-restriction save-selected-window save-window-excursion
127 set set-default set-marker-insertion-type setq setq-default
128 with-current-buffer with-output-to-temp-buffer with-syntax-table
129 with-temp-buffer with-temp-file with-temp-message with-timeout)
130 "Functions whose return value is the same as their last argument. No
131 brown splotch is shown for these if the last argument is a constant or a
132 call to one of the `testcover-1value-functions'. This list is probably
133 incomplete!"
134 :group 'testcover
135 :type '(repeat symbol))
137 (defcustom testcover-prog1-functions
138 '(prog1 unwind-protect)
139 "Functions whose return value is the same as their first argument. No
140 brown splotch is shown for these if the first argument is a constant or a
141 call to one of the `testcover-1value-functions'."
142 :group 'testcover
143 :type '(repeat symbol))
145 (defcustom testcover-potentially-1value-functions
146 '(add-hook and beep or remove-hook unless when)
147 "Functions that are potentially 1-valued. No brown splotch if actually
148 1-valued, no error if actually multi-valued."
149 :group 'testcover)
151 (defface testcover-nohits
152 '((t (:background "DeepPink2")))
153 "Face for forms that had no hits during coverage test"
154 :group 'testcover)
156 (defface testcover-1value
157 '((t (:background "Wheat2")))
158 "Face for forms that always produced the same value during coverage test"
159 :group 'testcover)
162 ;;;=========================================================================
163 ;;; Other variables
164 ;;;=========================================================================
166 (defvar testcover-module-constants nil
167 "Symbols declared with defconst in the last file processed by
168 `testcover-start'.")
170 (defvar testcover-module-1value-functions nil
171 "Symbols declared with defun in the last file processed by
172 `testcover-start', whose functions should always return the same value.")
174 (defvar testcover-module-potentially-1value-functions nil
175 "Symbols declared with defun in the last file processed by
176 `testcover-start', whose functions might always return the same value.")
178 (defvar testcover-vector nil
179 "Locally bound to coverage vector for function in progress.")
182 ;;;=========================================================================
183 ;;; Add instrumentation to your module
184 ;;;=========================================================================
186 (defun testcover-start (filename &optional byte-compile)
187 "Uses edebug to instrument all macros and functions in FILENAME, then
188 changes the instrumentation from edebug to testcover--much faster, no
189 problems with type-ahead or post-command-hook, etc. If BYTE-COMPILE is
190 non-nil, byte-compiles each function after instrumenting."
191 (interactive "fStart covering file: ")
192 (let ((buf (find-file filename))
193 (load-read-function 'testcover-read)
194 (edebug-all-defs t))
195 (setq edebug-form-data nil
196 testcover-module-constants nil
197 testcover-module-1value-functions nil)
198 (eval-buffer buf))
199 (when byte-compile
200 (dolist (x (reverse edebug-form-data))
201 (when (fboundp (car x))
202 (message "Compiling %s..." (car x))
203 (byte-compile (car x))))))
205 ;;;###autoload
206 (defun testcover-this-defun ()
207 "Start coverage on function under point."
208 (interactive)
209 (let* ((edebug-all-defs t)
210 (x (symbol-function (eval-defun nil))))
211 (testcover-reinstrument x)
214 (defun testcover-read (&optional stream)
215 "Read a form using edebug, changing edebug callbacks to testcover callbacks."
216 (let ((x (edebug-read stream)))
217 (testcover-reinstrument x)
220 (defun testcover-reinstrument (form)
221 "Reinstruments FORM to use testcover instead of edebug. This
222 function modifies the list that FORM points to. Result is nil if
223 FORM should return multiple values, t if should always return same
224 value, 'maybe if either is acceptable."
225 (let ((fun (car-safe form))
226 id val)
227 (cond
228 ((not fun) ;Atom
229 (when (or (not (symbolp form))
230 (memq form testcover-constants)
231 (memq form testcover-module-constants))
233 ((consp fun) ;Embedded list
234 (testcover-reinstrument fun)
235 (testcover-reinstrument-list (cdr form))
236 nil)
237 ((or (memq fun testcover-1value-functions)
238 (memq fun testcover-module-1value-functions))
239 ;;Should always return same value
240 (testcover-reinstrument-list (cdr form))
242 ((or (memq fun testcover-potentially-1value-functions)
243 (memq fun testcover-module-potentially-1value-functions))
244 ;;Might always return same value
245 (testcover-reinstrument-list (cdr form))
246 'maybe)
247 ((memq fun testcover-progn-functions)
248 ;;1-valued if last argument is
249 (testcover-reinstrument-list (cdr form)))
250 ((memq fun testcover-prog1-functions)
251 ;;1-valued if first argument is
252 (testcover-reinstrument-list (cddr form))
253 (testcover-reinstrument (cadr form)))
254 ((memq fun testcover-compose-functions)
255 ;;1-valued if all arguments are. Potentially 1-valued if all
256 ;;arguments are either definitely or potentially.
257 (testcover-reinstrument-compose (cdr form) 'testcover-reinstrument))
258 ((eq fun 'edebug-enter)
259 ;;(edebug-enter 'SYM ARGS #'(lambda nil FORMS))
260 ;; => (testcover-enter 'SYM #'(lambda nil FORMS))
261 (setcar form 'testcover-enter)
262 (setcdr (nthcdr 1 form) (nthcdr 3 form))
263 (let ((testcover-vector (get (cadr (cadr form)) 'edebug-coverage)))
264 (testcover-reinstrument-list (nthcdr 2 (cadr (nth 2 form))))))
265 ((eq fun 'edebug-after)
266 ;;(edebug-after (edebug-before XXX) YYY FORM)
267 ;; => (testcover-after YYY FORM), mark XXX as ok-coverage
268 (unless (eq (cadr form) 0)
269 (aset testcover-vector (cadr (cadr form)) 'ok-coverage))
270 (setq id (nth 2 form))
271 (setcdr form (nthcdr 2 form))
272 (setq val (testcover-reinstrument (nth 2 form)))
273 (setcar form (if (eq val t)
274 'testcover-1value
275 'testcover-after))
276 (when val
277 ;;1-valued or potentially 1-valued
278 (aset testcover-vector id '1value))
279 (cond
280 ((memq (car-safe (nth 2 form)) testcover-noreturn-functions)
281 ;;This function won't return, so set the value in advance
282 ;;(edebug-after (edebug-before XXX) YYY FORM)
283 ;; => (progn (edebug-after YYY nil) FORM)
284 (setcar (cdr form) `(,(car form) ,id nil))
285 (setcar form 'progn)
286 (aset testcover-vector id '1value)
287 (setq val t))
288 ((eq (car-safe (nth 2 form)) '1value)
289 ;;This function is always supposed to return the same value
290 (setq val t)
291 (aset testcover-vector id '1value)
292 (setcar form 'testcover-1value)))
293 val)
294 ((eq fun 'defun)
295 (setq val (testcover-reinstrument-list (nthcdr 3 form)))
296 (when (eq val t)
297 (push (cadr form) testcover-module-1value-functions))
298 (when (eq val 'maybe)
299 (push (cadr form) testcover-module-potentially-1value-functions)))
300 ((memq fun '(defconst defcustom))
301 ;;Define this symbol as 1-valued
302 (push (cadr form) testcover-module-constants)
303 (testcover-reinstrument-list (cddr form)))
304 ((memq fun '(dotimes dolist))
305 ;;Always returns third value from SPEC
306 (testcover-reinstrument-list (cddr form))
307 (setq val (testcover-reinstrument-list (cadr form)))
308 (if (nth 2 (cadr form))
310 ;;No third value, always returns nil
312 ((memq fun '(let let*))
313 ;;Special parsing for second argument
314 (mapc 'testcover-reinstrument-list (cadr form))
315 (testcover-reinstrument-list (cddr form)))
316 ((eq fun 'if)
317 ;;Potentially 1-valued if both THEN and ELSE clauses are
318 (testcover-reinstrument (cadr form))
319 (let ((then (testcover-reinstrument (nth 2 form)))
320 (else (testcover-reinstrument-list (nthcdr 3 form))))
321 (and then else 'maybe)))
322 ((eq fun 'cond)
323 ;;Potentially 1-valued if all clauses are
324 (when (testcover-reinstrument-compose (cdr form)
325 'testcover-reinstrument-list)
326 'maybe))
327 ((eq fun 'condition-case)
328 ;;Potentially 1-valued if BODYFORM is and all HANDLERS are
329 (let ((body (testcover-reinstrument (nth 2 form)))
330 (errs (testcover-reinstrument-compose
331 (mapcar #'cdr (nthcdr 3 form))
332 'testcover-reinstrument-list)))
333 (and body errs 'maybe)))
334 ((eq fun 'quote)
335 ;;Don't reinstrument what's inside!
336 ;;This doesn't apply within a backquote
338 ((eq fun '\`)
339 ;;Quotes are not special within backquotes
340 (let ((testcover-1value-functions
341 (cons 'quote testcover-1value-functions)))
342 (testcover-reinstrument (cadr form))))
343 ((eq fun '\,)
344 ;;In commas inside backquotes, quotes are special again
345 (let ((testcover-1value-functions
346 (remq 'quote testcover-1value-functions)))
347 (testcover-reinstrument (cadr form))))
348 ((eq fun '1value)
349 ;;Hack - pretend the arg is 1-valued here
350 (cond
351 ((symbolp (cadr form))
352 ;;A pseudoconstant variable
354 ((and (eq (car (cadr form)) 'edebug-after)
355 (symbolp (nth 3 (cadr form))))
356 ;;Reference to pseudoconstant
357 (aset testcover-vector (nth 2 (cadr form)) '1value)
358 (setcar (cdr form) `(testcover-1value ,(nth 2 (cadr form))
359 ,(nth 3 (cadr form))))
362 (setq id (car (if (eq (car (cadr form)) 'edebug-after)
363 (nth 3 (cadr form))
364 (cadr form))))
365 (let ((testcover-1value-functions
366 (cons id testcover-1value-functions)))
367 (testcover-reinstrument (cadr form))))))
368 ((eq fun 'noreturn)
369 ;;Hack - pretend the arg has no return
370 (cond
371 ((symbolp (cadr form))
372 ;;A pseudoconstant variable
373 'maybe)
374 ((and (eq (car (cadr form)) 'edebug-after)
375 (symbolp (nth 3 (cadr form))))
376 ;;Reference to pseudoconstant
377 (aset testcover-vector (nth 2 (cadr form)) '1value)
378 (setcar (cdr form) `(progn (testcover-after ,(nth 2 (cadr form)) nil)
379 ,(nth 3 (cadr form))))
380 'maybe)
382 (setq id (car (if (eq (car (cadr form)) 'edebug-after)
383 (nth 3 (cadr form))
384 (cadr form))))
385 (let ((testcover-noreturn-functions
386 (cons id testcover-noreturn-functions)))
387 (testcover-reinstrument (cadr form))))))
388 ((and (eq fun 'apply)
389 (eq (car-safe (cadr form)) 'quote)
390 (symbolp (cadr (cadr form))))
391 ;;Apply of a constant symbol. Process as 1value or noreturn
392 ;;depending on symbol.
393 (setq fun (cons (cadr (cadr form)) (cddr form))
394 val (testcover-reinstrument fun))
395 (setcdr (cdr form) (cdr fun))
396 val)
397 (t ;Some other function or weird thing
398 (testcover-reinstrument-list (cdr form))
399 nil))))
401 (defun testcover-reinstrument-list (list)
402 "Reinstruments each form in LIST to use testcover instead of edebug.
403 This function modifies the forms in LIST. Result is `testcover-reinstrument's
404 value for the last form in LIST. If the LIST is empty, its evaluation will
405 always be nil, so we return t for 1-valued."
406 (let ((result t))
407 (while (consp list)
408 (setq result (testcover-reinstrument (pop list))))
409 result))
411 (defun testcover-reinstrument-compose (list fun)
412 "For a compositional function, the result is 1-valued if all
413 arguments are, potentially 1-valued if all arguments are either
414 definitely or potentially 1-valued, and multi-valued otherwise.
415 FUN should be `testcover-reinstrument' for compositional functions,
416 `testcover-reinstrument-list' for clauses in a `cond'."
417 (let ((result t))
418 (mapc #'(lambda (x)
419 (setq x (funcall fun x))
420 (cond
421 ((eq result t)
422 (setq result x))
423 ((eq result 'maybe)
424 (when (not x)
425 (setq result nil)))))
426 list)
427 result))
429 (defun testcover-end (filename)
430 "Turn off instrumentation of all macros and functions in FILENAME."
431 (interactive "fStop covering file: ")
432 (let ((buf (find-file-noselect filename)))
433 (eval-buffer buf)))
436 ;;;=========================================================================
437 ;;; Accumulate coverage data
438 ;;;=========================================================================
440 (defun testcover-enter (testcover-sym testcover-fun)
441 "Internal function for coverage testing. Invokes TESTCOVER-FUN while
442 binding `testcover-vector' to the code-coverage vector for TESTCOVER-SYM
443 \(the name of the current function)."
444 (let ((testcover-vector (get testcover-sym 'edebug-coverage)))
445 (funcall testcover-fun)))
447 (defun testcover-after (idx val)
448 "Internal function for coverage testing. Returns VAL after installing it in
449 `testcover-vector' at offset IDX."
450 (declare (gv-expander (lambda (do)
451 (gv-letplace (getter setter) val
452 (funcall do getter
453 (lambda (store)
454 `(progn (testcover-after ,idx ,getter)
455 ,(funcall setter store))))))))
456 (cond
457 ((eq (aref testcover-vector idx) 'unknown)
458 (aset testcover-vector idx val))
459 ((not (equal (aref testcover-vector idx) val))
460 (aset testcover-vector idx 'ok-coverage)))
461 val)
463 (defun testcover-1value (idx val)
464 "Internal function for coverage testing. Returns VAL after installing it in
465 `testcover-vector' at offset IDX. Error if FORM does not always return the
466 same value during coverage testing."
467 (cond
468 ((eq (aref testcover-vector idx) '1value)
469 (aset testcover-vector idx (cons '1value val)))
470 ((not (and (eq (car-safe (aref testcover-vector idx)) '1value)
471 (equal (cdr (aref testcover-vector idx)) val)))
472 (error "Value of form marked with `1value' does vary: %s" val)))
473 val)
477 ;;;=========================================================================
478 ;;; Display the coverage data as color splotches on your code.
479 ;;;=========================================================================
481 (defun testcover-mark (def)
482 "Marks one DEF (a function or macro symbol) to highlight its contained forms
483 that did not get completely tested during coverage tests.
484 A marking with the face `testcover-nohits' (default = red) indicates that the
485 form was never evaluated. A marking using the `testcover-1value' face
486 \(default = tan) indicates that the form always evaluated to the same value.
487 The forms throw, error, and signal are not marked. They do not return and
488 would always get a red mark. Some forms that always return the same
489 value (e.g., setq of a constant), always get a tan mark that can't be
490 eliminated by adding more test cases."
491 (let* ((data (get def 'edebug))
492 (def-mark (car data))
493 (points (nth 2 data))
494 (len (length points))
495 (changed (buffer-modified-p))
496 (coverage (get def 'edebug-coverage))
497 ov j item)
498 (or (and def-mark points coverage)
499 (error "Missing edebug data for function %s" def))
500 (when (> len 0)
501 (set-buffer (marker-buffer def-mark))
502 (mapc 'delete-overlay
503 (overlays-in def-mark (+ def-mark (aref points (1- len)) 1)))
504 (while (> len 0)
505 (setq len (1- len)
506 data (aref coverage len))
507 (when (and (not (eq data 'ok-coverage))
508 (not (eq (car-safe data) '1value))
509 (setq j (+ def-mark (aref points len))))
510 (setq ov (make-overlay (1- j) j))
511 (overlay-put ov 'face
512 (if (memq data '(unknown 1value))
513 'testcover-nohits
514 'testcover-1value))))
515 (set-buffer-modified-p changed))))
517 (defun testcover-mark-all (&optional buffer)
518 "Mark all forms in BUFFER that did not get completely tested during
519 coverage tests. This function creates many overlays."
520 (interactive "bMark forms in buffer: ")
521 (if buffer
522 (switch-to-buffer buffer))
523 (goto-char 1)
524 (dolist (x edebug-form-data)
525 (if (get (car x) 'edebug)
526 (testcover-mark (car x)))))
528 (defun testcover-unmark-all (buffer)
529 "Remove all overlays from FILENAME."
530 (interactive "bUnmark forms in buffer: ")
531 (condition-case nil
532 (progn
533 (set-buffer buffer)
534 (mapc 'delete-overlay (overlays-in 1 (buffer-size))))
535 (error nil))) ;Ignore "No such buffer" errors
537 (defun testcover-next-mark ()
538 "Moves point to next line in current buffer that has a splotch."
539 (interactive)
540 (goto-char (next-overlay-change (point)))
541 (end-of-line))
543 ;; testcover.el ends here.