testcover.el 21 KB

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