lint.scm 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. ;;; lint --- Preemptive checks for coding errors in Guile Scheme code
  2. ;; Copyright (C) 2002, 2006, 2011 Free Software Foundation, Inc.
  3. ;;
  4. ;; This program is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU Lesser General Public License
  6. ;; as published by the Free Software Foundation; either version 3, or
  7. ;; (at your option) any later version.
  8. ;;
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Lesser General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Lesser General Public
  15. ;; License along with this software; see the file COPYING.LESSER. If
  16. ;; not, write to the Free Software Foundation, Inc., 51 Franklin
  17. ;; Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. ;;; Author: Neil Jerram
  19. ;;; Commentary:
  20. ;; Usage: lint FILE1 FILE2 ...
  21. ;;
  22. ;; Perform various preemptive checks for coding errors in Guile Scheme
  23. ;; code.
  24. ;;
  25. ;; Right now, there is only one check available, for unresolved free
  26. ;; variables. The intention is that future lint-like checks will be
  27. ;; implemented by adding to this script file.
  28. ;;
  29. ;; Unresolved free variables
  30. ;; -------------------------
  31. ;;
  32. ;; Free variables are those whose definitions come from outside the
  33. ;; module under investigation. In Guile, these definitions are
  34. ;; imported from other modules using `#:use-module' forms.
  35. ;;
  36. ;; This tool scans the specified files for unresolved free variables -
  37. ;; i.e. variables for which you may have forgotten the appropriate
  38. ;; `#:use-module', or for which the module that is supposed to export
  39. ;; them forgot to.
  40. ;;
  41. ;; It isn't guaranteed that the scan will find absolutely all such
  42. ;; errors. Quoted (and quasiquoted) expressions are skipped, since
  43. ;; they are most commonly used to describe constant data, not code, so
  44. ;; code that is explicitly evaluated using `eval' will not be checked.
  45. ;; For example, the `unresolved-var' in `(eval 'unresolved-var
  46. ;; (current-module))' would be missed.
  47. ;;
  48. ;; False positives are also possible. Firstly, the tool doesn't
  49. ;; understand all possible forms of implicit quoting; in particular,
  50. ;; it doesn't detect and expand uses of macros. Secondly, it picks up
  51. ;; explicit compatibility code like `(if (defined? 'x) (define y x))'.
  52. ;; Thirdly, there are occasional oddities like `next-method'.
  53. ;; However, the number of false positives for realistic code is
  54. ;; hopefully small enough that they can be individually considered and
  55. ;; ignored.
  56. ;;
  57. ;; Example
  58. ;; -------
  59. ;;
  60. ;; Note: most of the unresolved variables found in this example are
  61. ;; false positives, as you would hope. => scope for improvement.
  62. ;;
  63. ;; $ guild lint `guild`
  64. ;; No unresolved free variables in PROGRAM
  65. ;; No unresolved free variables in autofrisk
  66. ;; No unresolved free variables in display-commentary
  67. ;; Unresolved free variables in doc-snarf:
  68. ;; doc-snarf-version
  69. ;; No unresolved free variables in frisk
  70. ;; No unresolved free variables in generate-autoload
  71. ;; No unresolved free variables in lint
  72. ;; No unresolved free variables in punify
  73. ;; No unresolved free variables in read-scheme-source
  74. ;; Unresolved free variables in snarf-check-and-output-texi:
  75. ;; name
  76. ;; pos
  77. ;; line
  78. ;; x
  79. ;; rest
  80. ;; ...
  81. ;; do-argpos
  82. ;; do-command
  83. ;; do-args
  84. ;; type
  85. ;; num
  86. ;; file
  87. ;; do-arglist
  88. ;; req
  89. ;; opt
  90. ;; var
  91. ;; command
  92. ;; do-directive
  93. ;; s
  94. ;; ?
  95. ;; No unresolved free variables in use2dot
  96. ;;; Code:
  97. (define-module (scripts lint)
  98. #:use-module (ice-9 common-list)
  99. #:use-module (ice-9 format)
  100. #:export (lint))
  101. (define %include-in-guild-list #f)
  102. (define %summary "Check for bugs and style errors in a Scheme file.")
  103. (define (lint filename)
  104. (let ((module-name (scan-file-for-module-name filename))
  105. (free-vars (uniq (scan-file-for-free-variables filename))))
  106. (let ((module (resolve-module module-name))
  107. (all-resolved? #t))
  108. (format #t "Resolved module: ~S\n" module)
  109. (let loop ((free-vars free-vars))
  110. (or (null? free-vars)
  111. (begin
  112. (catch #t
  113. (lambda ()
  114. (eval (car free-vars) module))
  115. (lambda args
  116. (if all-resolved?
  117. (format #t
  118. "Unresolved free variables in ~A:\n"
  119. filename))
  120. (write-char #\tab)
  121. (write (car free-vars))
  122. (newline)
  123. (set! all-resolved? #f)))
  124. (loop (cdr free-vars)))))
  125. (if all-resolved?
  126. (format #t
  127. "No unresolved free variables in ~A\n"
  128. filename)))))
  129. (define (scan-file-for-module-name filename)
  130. (with-input-from-file filename
  131. (lambda ()
  132. (let loop ((x (read)))
  133. (cond ((eof-object? x) #f)
  134. ((and (pair? x)
  135. (eq? (car x) 'define-module))
  136. (cadr x))
  137. (else (loop (read))))))))
  138. (define (scan-file-for-free-variables filename)
  139. (with-input-from-file filename
  140. (lambda ()
  141. (let loop ((x (read)) (fvlists '()))
  142. (if (eof-object? x)
  143. (apply append fvlists)
  144. (loop (read) (cons (detect-free-variables x '()) fvlists)))))))
  145. ; guile> (detect-free-variables '(let ((a 1)) a) '())
  146. ; ()
  147. ; guile> (detect-free-variables '(let ((a 1)) b) '())
  148. ; (b)
  149. ; guile> (detect-free-variables '(let ((a 1) (b a)) b) '())
  150. ; (a)
  151. ; guile> (detect-free-variables '(let* ((a 1) (b a)) b) '())
  152. ; ()
  153. ; guile> (detect-free-variables '(define a 1) '())
  154. ; ()
  155. ; guile> (detect-free-variables '(define a b) '())
  156. ; (b)
  157. ; guile> (detect-free-variables '(define (a b c) b) '())
  158. ; ()
  159. ; guile> (detect-free-variables '(define (a b c) e) '())
  160. ; (e)
  161. (define (detect-free-variables x locals)
  162. ;; Given an expression @var{x} and a list @var{locals} of local
  163. ;; variables (symbols) that are in scope for @var{x}, return a list
  164. ;; of free variable symbols.
  165. (cond ((symbol? x)
  166. (if (memq x locals) '() (list x)))
  167. ((pair? x)
  168. (case (car x)
  169. ((define-module define-generic quote quasiquote)
  170. ;; No code of interest in these expressions.
  171. '())
  172. ((let letrec)
  173. ;; Check for named let. If there is a name, transform the
  174. ;; expression so that it looks like an unnamed let with
  175. ;; the name as one of the bindings.
  176. (if (symbol? (cadr x))
  177. (set-cdr! x (cons (cons (list (cadr x) #f) (caddr x))
  178. (cdddr x))))
  179. ;; Unnamed let processing.
  180. (let ((letrec? (eq? (car x) 'letrec))
  181. (locals-for-let-body (append locals (map car (cadr x)))))
  182. (append (apply append
  183. (map (lambda (binding)
  184. (detect-free-variables (cadr binding)
  185. (if letrec?
  186. locals-for-let-body
  187. locals)))
  188. (cadr x)))
  189. (apply append
  190. (map (lambda (bodyform)
  191. (detect-free-variables bodyform
  192. locals-for-let-body))
  193. (cddr x))))))
  194. ((let* and-let*)
  195. ;; Handle bindings recursively.
  196. (if (null? (cadr x))
  197. (apply append
  198. (map (lambda (bodyform)
  199. (detect-free-variables bodyform locals))
  200. (cddr x)))
  201. (append (detect-free-variables (cadr (caadr x)) locals)
  202. (detect-free-variables `(let* ,(cdadr x) ,@(cddr x))
  203. (cons (caaadr x) locals)))))
  204. ((define define-public define-macro)
  205. (if (pair? (cadr x))
  206. (begin
  207. (set! locals (cons (caadr x) locals))
  208. (detect-free-variables `(lambda ,(cdadr x) ,@(cddr x))
  209. locals))
  210. (begin
  211. (set! locals (cons (cadr x) locals))
  212. (detect-free-variables (caddr x) locals))))
  213. ((lambda lambda*)
  214. (let ((locals-for-lambda-body (let loop ((locals locals)
  215. (args (cadr x)))
  216. (cond ((null? args) locals)
  217. ((pair? args)
  218. (loop (cons (car args) locals)
  219. (cdr args)))
  220. (else
  221. (cons args locals))))))
  222. (apply append
  223. (map (lambda (bodyform)
  224. (detect-free-variables bodyform
  225. locals-for-lambda-body))
  226. (cddr x)))))
  227. ((receive)
  228. (let ((locals-for-receive-body (append locals (cadr x))))
  229. (apply append
  230. (detect-free-variables (caddr x) locals)
  231. (map (lambda (bodyform)
  232. (detect-free-variables bodyform
  233. locals-for-receive-body))
  234. (cdddr x)))))
  235. ((define-method define*)
  236. (let ((locals-for-method-body (let loop ((locals locals)
  237. (args (cdadr x)))
  238. (cond ((null? args) locals)
  239. ((pair? args)
  240. (loop (cons (if (pair? (car args))
  241. (caar args)
  242. (car args))
  243. locals)
  244. (cdr args)))
  245. (else
  246. (cons args locals))))))
  247. (apply append
  248. (map (lambda (bodyform)
  249. (detect-free-variables bodyform
  250. locals-for-method-body))
  251. (cddr x)))))
  252. ((define-class)
  253. ;; Avoid picking up slot names at the start of slot
  254. ;; definitions.
  255. (apply append
  256. (map (lambda (slot/option)
  257. (detect-free-variables-noncar (if (pair? slot/option)
  258. (cdr slot/option)
  259. slot/option)
  260. locals))
  261. (cdddr x))))
  262. ((case)
  263. (apply append
  264. (detect-free-variables (cadr x) locals)
  265. (map (lambda (case)
  266. (detect-free-variables (cdr case) locals))
  267. (cddr x))))
  268. ((unquote unquote-splicing else =>)
  269. (detect-free-variables-noncar (cdr x) locals))
  270. (else (append (detect-free-variables (car x) locals)
  271. (detect-free-variables-noncar (cdr x) locals)))))
  272. (else '())))
  273. (define (detect-free-variables-noncar x locals)
  274. ;; Given an expression @var{x} and a list @var{locals} of local
  275. ;; variables (symbols) that are in scope for @var{x}, return a list
  276. ;; of free variable symbols.
  277. (cond ((symbol? x)
  278. (if (memq x locals) '() (list x)))
  279. ((pair? x)
  280. (case (car x)
  281. ((=>)
  282. (detect-free-variables-noncar (cdr x) locals))
  283. (else (append (detect-free-variables (car x) locals)
  284. (detect-free-variables-noncar (cdr x) locals)))))
  285. (else '())))
  286. (define (main . files)
  287. (for-each lint files))
  288. ;;; lint ends here