check-declare.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. ;;; check-declare.el --- Check declare-function statements
  2. ;; Copyright (C) 2007-2012 Free Software Foundation, Inc.
  3. ;; Author: Glenn Morris <rgm@gnu.org>
  4. ;; Keywords: lisp, tools, maint
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; The byte-compiler often warns about undefined functions that you
  18. ;; know will actually be defined when it matters. The `declare-function'
  19. ;; statement allows you to suppress these warnings. This package
  20. ;; checks that all such statements in a file or directory are accurate.
  21. ;; The entry points are `check-declare-file' and `check-declare-directory'.
  22. ;; For more information, see Info node `(elisp)Declaring Functions'.
  23. ;;; TODO:
  24. ;; 1. Warn about functions marked as obsolete, eg
  25. ;; password-read-and-add in smime.el.
  26. ;; 2. defmethod, defclass argument checking.
  27. ;; 3. defclass also defines -p and -child-p.
  28. ;;; Code:
  29. (defconst check-declare-warning-buffer "*Check Declarations Warnings*"
  30. "Name of buffer used to display any `check-declare' warnings.")
  31. (defun check-declare-locate (file basefile)
  32. "Return the full path of FILE.
  33. Expands files with a \".c\" or \".m\" extension relative to the Emacs
  34. \"src/\" directory. Otherwise, `locate-library' searches for FILE.
  35. If that fails, expands FILE relative to BASEFILE's directory part.
  36. The returned file might not exist. If FILE has an \"ext:\" prefix, so does
  37. the result."
  38. (let ((ext (string-match "^ext:" file))
  39. tfile)
  40. (if ext
  41. (setq file (substring file 4)))
  42. (setq file
  43. (if (member (file-name-extension file) '("c" "m"))
  44. (expand-file-name file (expand-file-name "src" source-directory))
  45. (if (setq tfile (locate-library file))
  46. (progn
  47. (setq tfile
  48. (replace-regexp-in-string "\\.elc\\'" ".el" tfile))
  49. (if (and (not (file-exists-p tfile))
  50. (file-exists-p (concat tfile ".gz")))
  51. (concat tfile ".gz")
  52. tfile))
  53. (setq tfile (expand-file-name file
  54. (file-name-directory basefile)))
  55. (if (or (file-exists-p tfile)
  56. (string-match "\\.el\\'" tfile))
  57. tfile
  58. (concat tfile ".el")))))
  59. (if ext (concat "ext:" file)
  60. file)))
  61. (defun check-declare-scan (file)
  62. "Scan FILE for `declare-function' calls.
  63. Return a list with elements of the form (FNFILE FN ARGLIST FILEONLY),
  64. where only the first two elements need be present. This claims that FNFILE
  65. defines FN, with ARGLIST. FILEONLY non-nil means only check that FNFILE
  66. exists, not that it defines FN. This is for function definitions that we
  67. don't know how to recognize (e.g. some macros)."
  68. (let ((m (format "Scanning %s..." file))
  69. alist form len fn fnfile arglist fileonly)
  70. (message "%s" m)
  71. (with-temp-buffer
  72. (insert-file-contents file)
  73. ;; FIXME we could theoretically be inside a string.
  74. (while (re-search-forward "^[ \t]*\\((declare-function\\)[ \t\n]" nil t)
  75. (goto-char (match-beginning 1))
  76. (if (and (setq form (ignore-errors (read (current-buffer))))
  77. ;; Exclude element of byte-compile-initial-macro-environment.
  78. (or (listp (cdr form)) (setq form nil))
  79. (> (setq len (length form)) 2)
  80. (< len 6)
  81. (symbolp (setq fn (cadr form)))
  82. (setq fn (symbol-name fn)) ; later we use as a search string
  83. (stringp (setq fnfile (nth 2 form)))
  84. (setq fnfile (check-declare-locate fnfile
  85. (expand-file-name file)))
  86. ;; Use `t' to distinguish unspecified arglist from empty one.
  87. (or (eq t (setq arglist (if (> len 3)
  88. (nth 3 form)
  89. t)))
  90. (listp arglist))
  91. (symbolp (setq fileonly (nth 4 form))))
  92. (setq alist (cons (list fnfile fn arglist fileonly) alist))
  93. ;; FIXME make this more noticeable.
  94. (if form (message "Malformed declaration for `%s'" (cadr form))))))
  95. (message "%sdone" m)
  96. alist))
  97. (defun check-declare-errmsg (errlist &optional full)
  98. "Return a string with the number of errors in ERRLIST, if any.
  99. Normally just counts the number of elements in ERRLIST.
  100. With optional argument FULL, sums the number of elements in each element."
  101. (if errlist
  102. (let ((l (length errlist)))
  103. (when full
  104. (setq l 0)
  105. (dolist (e errlist)
  106. (setq l (+ l (1- (length e))))))
  107. (format "%d problem%s found" l (if (= l 1) "" "s")))
  108. "OK"))
  109. (autoload 'byte-compile-arglist-signature "bytecomp")
  110. (defun check-declare-verify (fnfile fnlist)
  111. "Check that FNFILE contains function definitions matching FNLIST.
  112. Each element of FNLIST has the form (FILE FN ARGLIST FILEONLY), where
  113. only the first two elements need be present. This means FILE claimed FN
  114. was defined in FNFILE with the specified ARGLIST. FILEONLY non-nil means
  115. to only check that FNFILE exists, not that it actually defines FN.
  116. Returns nil if all claims are found to be true, otherwise a list
  117. of errors with elements of the form \(FILE FN TYPE), where TYPE
  118. is a string giving details of the error."
  119. (let ((m (format "Checking %s..." fnfile))
  120. (cflag (member (file-name-extension fnfile) '("c" "m")))
  121. (ext (string-match "^ext:" fnfile))
  122. re fn sig siglist arglist type errlist minargs maxargs)
  123. (message "%s" m)
  124. (if ext
  125. (setq fnfile (substring fnfile 4)))
  126. (if (file-regular-p fnfile)
  127. (with-temp-buffer
  128. (insert-file-contents fnfile)
  129. ;; defsubst's don't _have_ to be known at compile time.
  130. (setq re (format (if cflag
  131. "^[ \t]*\\(DEFUN\\)[ \t]*([ \t]*\"%s\""
  132. "^[ \t]*(\\(fset[ \t]+'\\|\
  133. def\\(?:un\\|subst\\|foo\\|method\\|class\\|\
  134. ine-\\(?:derived\\|generic\\|\\(?:global\\(?:ized\\)?-\\)?minor\\)-mode\\|\
  135. \\(?:ine-obsolete-function-\\)?alias[ \t]+'\\|\
  136. ine-overloadable-function\\)\\)\
  137. \[ \t]*%s\\([ \t;]+\\|$\\)")
  138. (regexp-opt (mapcar 'cadr fnlist) t)))
  139. (while (re-search-forward re nil t)
  140. (skip-chars-forward " \t\n")
  141. (setq fn (match-string 2)
  142. type (match-string 1)
  143. ;; (min . max) for a fixed number of arguments, or
  144. ;; arglists with optional elements.
  145. ;; (min) for arglists with &rest.
  146. ;; sig = 'err means we could not find an arglist.
  147. sig (cond (cflag
  148. (or
  149. (when (search-forward "," nil t 3)
  150. (skip-chars-forward " \t\n")
  151. ;; Assuming minargs and maxargs on same line.
  152. (when (looking-at "\\([0-9]+\\)[ \t]*,[ \t]*\
  153. \\([0-9]+\\|MANY\\|UNEVALLED\\)")
  154. (setq minargs (string-to-number
  155. (match-string 1))
  156. maxargs (match-string 2))
  157. (cons minargs (unless (string-match "[^0-9]"
  158. maxargs)
  159. (string-to-number
  160. maxargs)))))
  161. 'err))
  162. ((string-match
  163. "\\`define-\\(derived\\|generic\\)-mode\\'"
  164. type)
  165. '(0 . 0))
  166. ((string-match
  167. "\\`define\\(-global\\(ized\\)?\\)?-minor-mode\\'"
  168. type)
  169. '(0 . 1))
  170. ;; Prompt to update.
  171. ((string-match
  172. "\\`define-obsolete-function-alias\\>"
  173. type)
  174. 'obsolete)
  175. ;; Can't easily check arguments in these cases.
  176. ((string-match "\\`\\(def\\(alias\\|\
  177. method\\|class\\)\\|fset\\)\\>" type)
  178. t)
  179. ((looking-at "\\((\\|nil\\)")
  180. (byte-compile-arglist-signature
  181. (read (current-buffer))))
  182. (t
  183. 'err))
  184. ;; alist of functions and arglist signatures.
  185. siglist (cons (cons fn sig) siglist)))))
  186. (dolist (e fnlist)
  187. (setq arglist (nth 2 e)
  188. type
  189. (if (not re)
  190. "file not found"
  191. (if (not (setq sig (assoc (cadr e) siglist)))
  192. (unless (nth 3 e) ; fileonly
  193. "function not found")
  194. (setq sig (cdr sig))
  195. (cond ((eq sig 'obsolete) ; check even when no arglist specified
  196. "obsolete alias")
  197. ;; arglist t means no arglist specified, as
  198. ;; opposed to an empty arglist.
  199. ((eq arglist t) nil)
  200. ((eq sig t) nil) ; eg defalias - can't check arguments
  201. ((eq sig 'err)
  202. "arglist not found") ; internal error
  203. ((not (equal (byte-compile-arglist-signature
  204. arglist)
  205. sig))
  206. "arglist mismatch")))))
  207. (when type
  208. (setq errlist (cons (list (car e) (cadr e) type) errlist))))
  209. (message "%s%s" m
  210. (if (or re (not ext))
  211. (check-declare-errmsg errlist)
  212. (progn
  213. (setq errlist nil)
  214. "skipping external file")))
  215. errlist))
  216. (defun check-declare-sort (alist)
  217. "Sort a list with elements FILE (FNFILE ...).
  218. Returned list has elements FNFILE (FILE ...)."
  219. (let (file fnfile rest sort a)
  220. (dolist (e alist)
  221. (setq file (car e))
  222. (dolist (f (cdr e))
  223. (setq fnfile (car f)
  224. rest (cdr f))
  225. (if (setq a (assoc fnfile sort))
  226. (setcdr a (append (cdr a) (list (cons file rest))))
  227. (setq sort (cons (list fnfile (cons file rest)) sort)))))
  228. sort))
  229. (defun check-declare-warn (file fn fnfile type)
  230. "Warn that FILE made a false claim about FN in FNFILE.
  231. TYPE is a string giving the nature of the error. Warning is displayed in
  232. `check-declare-warning-buffer'."
  233. (display-warning 'check-declare
  234. (format "%s said `%s' was defined in %s: %s"
  235. (file-name-nondirectory file) fn
  236. (file-name-nondirectory fnfile)
  237. type)
  238. nil check-declare-warning-buffer))
  239. (defun check-declare-files (&rest files)
  240. "Check veracity of all `declare-function' statements in FILES.
  241. Return a list of any errors found."
  242. (let (alist err errlist)
  243. (dolist (file files)
  244. (setq alist (cons (cons file (check-declare-scan file)) alist)))
  245. ;; Sort so that things are ordered by the files supposed to
  246. ;; contain the defuns.
  247. (dolist (e (check-declare-sort alist))
  248. (if (setq err (check-declare-verify (car e) (cdr e)))
  249. (setq errlist (cons (cons (car e) err) errlist))))
  250. (if (get-buffer check-declare-warning-buffer)
  251. (kill-buffer check-declare-warning-buffer))
  252. ;; Sort back again so that errors are ordered by the files
  253. ;; containing the declare-function statements.
  254. (dolist (e (check-declare-sort errlist))
  255. (dolist (f (cdr e))
  256. (check-declare-warn (car e) (cadr f) (car f) (nth 2 f))))
  257. errlist))
  258. ;;;###autoload
  259. (defun check-declare-file (file)
  260. "Check veracity of all `declare-function' statements in FILE.
  261. See `check-declare-directory' for more information."
  262. (interactive "fFile to check: ")
  263. (or (file-exists-p file)
  264. (error "File `%s' not found" file))
  265. (let ((m (format "Checking %s..." file))
  266. errlist)
  267. (message "%s" m)
  268. (setq errlist (check-declare-files file))
  269. (message "%s%s" m (check-declare-errmsg errlist))
  270. errlist))
  271. ;;;###autoload
  272. (defun check-declare-directory (root)
  273. "Check veracity of all `declare-function' statements under directory ROOT.
  274. Returns non-nil if any false statements are found."
  275. (interactive "DDirectory to check: ")
  276. (or (file-directory-p (setq root (expand-file-name root)))
  277. (error "Directory `%s' not found" root))
  278. (let ((m "Checking `declare-function' statements...")
  279. (m2 "Finding files with declarations...")
  280. errlist files)
  281. (message "%s" m)
  282. (message "%s" m2)
  283. (setq files (process-lines find-program root
  284. "-name" "*.el"
  285. "-exec" grep-program
  286. "-l" "^[ \t]*(declare-function" "{}" ";"))
  287. (message "%s%d found" m2 (length files))
  288. (when files
  289. (setq errlist (apply 'check-declare-files files))
  290. (message "%s%s" m (check-declare-errmsg errlist t))
  291. errlist)))
  292. (provide 'check-declare)
  293. ;;; check-declare.el ends here.