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