lint.scm 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. ;;; lint --- Preemptive checks for coding errors in Guile Scheme code
  2. ;; Copyright (C) 2002, 2006 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. ;; $ guile-tools lint `guile-tools`
  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 (lint filename)
  102. (let ((module-name (scan-file-for-module-name filename))
  103. (free-vars (uniq (scan-file-for-free-variables filename))))
  104. (let ((module (resolve-module module-name))
  105. (all-resolved? #t))
  106. (format #t "Resolved module: ~S\n" module)
  107. (let loop ((free-vars free-vars))
  108. (or (null? free-vars)
  109. (begin
  110. (catch #t
  111. (lambda ()
  112. (eval (car free-vars) module))
  113. (lambda args
  114. (if all-resolved?
  115. (format #t
  116. "Unresolved free variables in ~A:\n"
  117. filename))
  118. (write-char #\tab)
  119. (write (car free-vars))
  120. (newline)
  121. (set! all-resolved? #f)))
  122. (loop (cdr free-vars)))))
  123. (if all-resolved?
  124. (format #t
  125. "No unresolved free variables in ~A\n"
  126. filename)))))
  127. (define (scan-file-for-module-name filename)
  128. (with-input-from-file filename
  129. (lambda ()
  130. (let loop ((x (read)))
  131. (cond ((eof-object? x) #f)
  132. ((and (pair? x)
  133. (eq? (car x) 'define-module))
  134. (cadr x))
  135. (else (loop (read))))))))
  136. (define (scan-file-for-free-variables filename)
  137. (with-input-from-file filename
  138. (lambda ()
  139. (let loop ((x (read)) (fvlists '()))
  140. (if (eof-object? x)
  141. (apply append fvlists)
  142. (loop (read) (cons (detect-free-variables x '()) fvlists)))))))
  143. ; guile> (detect-free-variables '(let ((a 1)) a) '())
  144. ; ()
  145. ; guile> (detect-free-variables '(let ((a 1)) b) '())
  146. ; (b)
  147. ; guile> (detect-free-variables '(let ((a 1) (b a)) b) '())
  148. ; (a)
  149. ; guile> (detect-free-variables '(let* ((a 1) (b a)) b) '())
  150. ; ()
  151. ; guile> (detect-free-variables '(define a 1) '())
  152. ; ()
  153. ; guile> (detect-free-variables '(define a b) '())
  154. ; (b)
  155. ; guile> (detect-free-variables '(define (a b c) b) '())
  156. ; ()
  157. ; guile> (detect-free-variables '(define (a b c) e) '())
  158. ; (e)
  159. (define (detect-free-variables x locals)
  160. ;; Given an expression @var{x} and a list @var{locals} of local
  161. ;; variables (symbols) that are in scope for @var{x}, return a list
  162. ;; of free variable symbols.
  163. (cond ((symbol? x)
  164. (if (memq x locals) '() (list x)))
  165. ((pair? x)
  166. (case (car x)
  167. ((define-module define-generic quote quasiquote)
  168. ;; No code of interest in these expressions.
  169. '())
  170. ((let letrec)
  171. ;; Check for named let. If there is a name, transform the
  172. ;; expression so that it looks like an unnamed let with
  173. ;; the name as one of the bindings.
  174. (if (symbol? (cadr x))
  175. (set-cdr! x (cons (cons (list (cadr x) #f) (caddr x))
  176. (cdddr x))))
  177. ;; Unnamed let processing.
  178. (let ((letrec? (eq? (car x) 'letrec))
  179. (locals-for-let-body (append locals (map car (cadr x)))))
  180. (append (apply append
  181. (map (lambda (binding)
  182. (detect-free-variables (cadr binding)
  183. (if letrec?
  184. locals-for-let-body
  185. locals)))
  186. (cadr x)))
  187. (apply append
  188. (map (lambda (bodyform)
  189. (detect-free-variables bodyform
  190. locals-for-let-body))
  191. (cddr x))))))
  192. ((let* and-let*)
  193. ;; Handle bindings recursively.
  194. (if (null? (cadr x))
  195. (apply append
  196. (map (lambda (bodyform)
  197. (detect-free-variables bodyform locals))
  198. (cddr x)))
  199. (append (detect-free-variables (cadr (caadr x)) locals)
  200. (detect-free-variables `(let* ,(cdadr x) ,@(cddr x))
  201. (cons (caaadr x) locals)))))
  202. ((define define-public define-macro)
  203. (if (pair? (cadr x))
  204. (begin
  205. (set! locals (cons (caadr x) locals))
  206. (detect-free-variables `(lambda ,(cdadr x) ,@(cddr x))
  207. locals))
  208. (begin
  209. (set! locals (cons (cadr x) locals))
  210. (detect-free-variables (caddr x) locals))))
  211. ((lambda lambda*)
  212. (let ((locals-for-lambda-body (let loop ((locals locals)
  213. (args (cadr x)))
  214. (cond ((null? args) locals)
  215. ((pair? args)
  216. (loop (cons (car args) locals)
  217. (cdr args)))
  218. (else
  219. (cons args locals))))))
  220. (apply append
  221. (map (lambda (bodyform)
  222. (detect-free-variables bodyform
  223. locals-for-lambda-body))
  224. (cddr x)))))
  225. ((receive)
  226. (let ((locals-for-receive-body (append locals (cadr x))))
  227. (apply append
  228. (detect-free-variables (caddr x) locals)
  229. (map (lambda (bodyform)
  230. (detect-free-variables bodyform
  231. locals-for-receive-body))
  232. (cdddr x)))))
  233. ((define-method define*)
  234. (let ((locals-for-method-body (let loop ((locals locals)
  235. (args (cdadr x)))
  236. (cond ((null? args) locals)
  237. ((pair? args)
  238. (loop (cons (if (pair? (car args))
  239. (caar args)
  240. (car args))
  241. locals)
  242. (cdr args)))
  243. (else
  244. (cons args locals))))))
  245. (apply append
  246. (map (lambda (bodyform)
  247. (detect-free-variables bodyform
  248. locals-for-method-body))
  249. (cddr x)))))
  250. ((define-class)
  251. ;; Avoid picking up slot names at the start of slot
  252. ;; definitions.
  253. (apply append
  254. (map (lambda (slot/option)
  255. (detect-free-variables-noncar (if (pair? slot/option)
  256. (cdr slot/option)
  257. slot/option)
  258. locals))
  259. (cdddr x))))
  260. ((case)
  261. (apply append
  262. (detect-free-variables (cadr x) locals)
  263. (map (lambda (case)
  264. (detect-free-variables (cdr case) locals))
  265. (cddr x))))
  266. ((unquote unquote-splicing else =>)
  267. (detect-free-variables-noncar (cdr x) locals))
  268. (else (append (detect-free-variables (car x) locals)
  269. (detect-free-variables-noncar (cdr x) locals)))))
  270. (else '())))
  271. (define (detect-free-variables-noncar x locals)
  272. ;; Given an expression @var{x} and a list @var{locals} of local
  273. ;; variables (symbols) that are in scope for @var{x}, return a list
  274. ;; of free variable symbols.
  275. (cond ((symbol? x)
  276. (if (memq x locals) '() (list x)))
  277. ((pair? x)
  278. (case (car x)
  279. ((=>)
  280. (detect-free-variables-noncar (cdr x) locals))
  281. (else (append (detect-free-variables (car x) locals)
  282. (detect-free-variables-noncar (cdr x) locals)))))
  283. (else '())))
  284. (define (main . files)
  285. (for-each lint files))
  286. ;;; lint ends here