1 ;;; check-declare.el --- Check declare-function statements
3 ;; Copyright (C) 2007-2015 Free Software Foundation, Inc.
5 ;; Author: Glenn Morris <rgm@gnu.org>
6 ;; Keywords: lisp, tools, maint
8 ;; This file is part of GNU Emacs.
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 of the License, or
13 ;; (at your option) any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; The byte-compiler often warns about undefined functions that you
26 ;; know will actually be defined when it matters. The `declare-function'
27 ;; statement allows you to suppress these warnings. This package
28 ;; checks that all such statements in a file or directory are accurate.
29 ;; The entry points are `check-declare-file' and `check-declare-directory'.
31 ;; For more information, see Info node `(elisp)Declaring Functions'.
35 ;; 1. Warn about functions marked as obsolete, eg
36 ;; password-read-and-add in smime.el.
37 ;; 2. defmethod, defclass argument checking.
38 ;; 3. defclass also defines -p and -child-p.
42 (defconst check-declare-warning-buffer
"*Check Declarations Warnings*"
43 "Name of buffer used to display any `check-declare' warnings.")
45 (defun check-declare-locate (file basefile
)
46 "Return the full path of FILE.
47 Expands files with a \".c\" or \".m\" extension relative to the Emacs
48 \"src/\" directory. Otherwise, `locate-library' searches for FILE.
49 If that fails, expands FILE relative to BASEFILE's directory part.
50 The returned file might not exist. If FILE has an \"ext:\" prefix, so does
52 (let ((ext (string-match "^ext:" file
))
55 (setq file
(substring file
4)))
57 (if (member (file-name-extension file
) '("c" "m"))
58 (expand-file-name file
(expand-file-name "src" source-directory
))
59 (if (setq tfile
(locate-library file
))
62 (replace-regexp-in-string "\\.elc\\'" ".el" tfile
))
63 (if (and (not (file-exists-p tfile
))
64 (file-exists-p (concat tfile
".gz")))
67 (setq tfile
(expand-file-name file
68 (file-name-directory basefile
)))
69 (if (or (file-exists-p tfile
)
70 (string-match "\\.el\\'" tfile
))
72 (concat tfile
".el")))))
73 (if ext
(concat "ext:" file
)
76 (defun check-declare-scan (file)
77 "Scan FILE for `declare-function' calls.
78 Return a list with elements of the form (FNFILE FN ARGLIST FILEONLY),
79 where only the first two elements need be present. This claims that FNFILE
80 defines FN, with ARGLIST. FILEONLY non-nil means only check that FNFILE
81 exists, not that it defines FN. This is for function definitions that we
82 don't know how to recognize (e.g. some macros)."
83 (let ((m (format "Scanning %s..." file
))
84 alist form len fn fnfile arglist fileonly
)
87 (insert-file-contents file
)
88 ;; FIXME we could theoretically be inside a string.
89 (while (re-search-forward "^[ \t]*\\((declare-function\\)[ \t\n]" nil t
)
90 (goto-char (match-beginning 1))
91 (if (and (setq form
(ignore-errors (read (current-buffer))))
92 ;; Exclude element of byte-compile-initial-macro-environment.
93 (or (listp (cdr form
)) (setq form nil
))
94 (> (setq len
(length form
)) 2)
96 (symbolp (setq fn
(cadr form
)))
97 (setq fn
(symbol-name fn
)) ; later we use as a search string
98 (stringp (setq fnfile
(nth 2 form
)))
99 (setq fnfile
(check-declare-locate fnfile
100 (expand-file-name file
)))
101 ;; Use `t' to distinguish unspecified arglist from empty one.
102 (or (eq t
(setq arglist
(if (> len
3)
106 (symbolp (setq fileonly
(nth 4 form
))))
107 (setq alist
(cons (list fnfile fn arglist fileonly
) alist
))
108 ;; FIXME make this more noticeable.
109 (if form
(message "Malformed declaration for `%s'" (cadr form
))))))
113 (defun check-declare-errmsg (errlist &optional full
)
114 "Return a string with the number of errors in ERRLIST, if any.
115 Normally just counts the number of elements in ERRLIST.
116 With optional argument FULL, sums the number of elements in each element."
118 (let ((l (length errlist
)))
122 (setq l
(+ l
(1- (length e
))))))
123 (format "%d problem%s found" l
(if (= l
1) "" "s")))
126 (autoload 'byte-compile-arglist-signature
"bytecomp")
128 (defgroup check-declare nil
129 "Check declare-function statements."
132 (defcustom check-declare-ext-errors nil
133 "When non-nil, warn about functions not found in :ext."
136 (defun check-declare-verify (fnfile fnlist
)
137 "Check that FNFILE contains function definitions matching FNLIST.
138 Each element of FNLIST has the form (FILE FN ARGLIST FILEONLY), where
139 only the first two elements need be present. This means FILE claimed FN
140 was defined in FNFILE with the specified ARGLIST. FILEONLY non-nil means
141 to only check that FNFILE exists, not that it actually defines FN.
143 Returns nil if all claims are found to be true, otherwise a list
144 of errors with elements of the form \(FILE FN TYPE), where TYPE
145 is a string giving details of the error."
146 (let ((m (format "Checking %s..." fnfile
))
147 (cflag (member (file-name-extension fnfile
) '("c" "m")))
148 (ext (string-match "^ext:" fnfile
))
149 re fn sig siglist arglist type errlist minargs maxargs
)
152 (setq fnfile
(substring fnfile
4)))
153 (if (file-regular-p fnfile
)
155 (insert-file-contents fnfile
)
156 ;; defsubst's don't _have_ to be known at compile time.
157 (setq re
(format (if cflag
158 "^[ \t]*\\(DEFUN\\)[ \t]*([ \t]*\"%s\""
159 "^[ \t]*(\\(fset[ \t]+'\\|\
160 def\\(?:un\\|subst\\|foo\\|method\\|class\\|\
161 ine-\\(?:derived\\|generic\\|\\(?:global\\(?:ized\\)?-\\)?minor\\)-mode\\|\
162 \\(?:ine-obsolete-function-\\)?alias[ \t]+'\\|\
163 ine-overloadable-function\\)\\)\
164 \[ \t]*%s\\([ \t;]+\\|$\\)")
165 (regexp-opt (mapcar 'cadr fnlist
) t
)))
166 (while (re-search-forward re nil t
)
167 (skip-chars-forward " \t\n")
168 (setq fn
(match-string 2)
169 type
(match-string 1)
170 ;; (min . max) for a fixed number of arguments, or
171 ;; arglists with optional elements.
172 ;; (min) for arglists with &rest.
173 ;; sig = 'err means we could not find an arglist.
176 (when (search-forward "," nil t
3)
177 (skip-chars-forward " \t\n")
178 ;; Assuming minargs and maxargs on same line.
179 (when (looking-at "\\([0-9]+\\)[ \t]*,[ \t]*\
180 \\([0-9]+\\|MANY\\|UNEVALLED\\)")
181 (setq minargs
(string-to-number
183 maxargs
(match-string 2))
184 (cons minargs
(unless (string-match "[^0-9]"
190 "\\`define-\\(derived\\|generic\\)-mode\\'"
194 "\\`define\\(-global\\(ized\\)?\\)?-minor-mode\\'"
199 "\\`define-obsolete-function-alias\\>"
202 ;; Can't easily check arguments in these cases.
203 ((string-match "\\`\\(def\\(alias\\|\
204 method\\|class\\)\\|fset\\)\\>" type
)
206 ((looking-at "\\((\\|nil\\)")
207 (byte-compile-arglist-signature
208 (read (current-buffer))))
211 ;; alist of functions and arglist signatures.
212 siglist
(cons (cons fn sig
) siglist
)))))
214 (setq arglist
(nth 2 e
)
218 (if (not (setq sig
(assoc (cadr e
) siglist
)))
219 (unless (nth 3 e
) ; fileonly
220 "function not found")
222 (cond ((eq sig
'obsolete
) ; check even when no arglist specified
224 ;; arglist t means no arglist specified, as
225 ;; opposed to an empty arglist.
227 ((eq sig t
) nil
) ; eg defalias - can't check arguments
229 "arglist not found") ; internal error
230 ((not (equal (byte-compile-arglist-signature
233 "arglist mismatch")))))
235 (setq errlist
(cons (list (car e
) (cadr e
) type
) errlist
))))
237 (if (or re
(or check-declare-ext-errors
239 (check-declare-errmsg errlist
)
242 "skipping external file")))
245 (defun check-declare-sort (alist)
246 "Sort a list with elements FILE (FNFILE ...).
247 Returned list has elements FNFILE (FILE ...)."
248 (let (file fnfile rest sort a
)
254 (if (setq a
(assoc fnfile sort
))
255 (setcdr a
(append (cdr a
) (list (cons file rest
))))
256 (setq sort
(cons (list fnfile
(cons file rest
)) sort
)))))
259 (defun check-declare-warn (file fn fnfile type
)
260 "Warn that FILE made a false claim about FN in FNFILE.
261 TYPE is a string giving the nature of the error. Warning is displayed in
262 `check-declare-warning-buffer'."
263 (let ((warning-prefix-function
264 (lambda (level entry
)
268 (with-current-buffer (find-file-noselect file
)
269 (goto-char (point-min))
270 (when (re-search-forward
271 (format "(declare-function[ \t\n]+%s" fn
) nil t
)
272 (goto-char (match-beginning 0))
273 (setq line
(line-number-at-pos))
274 (setq col
(1+ (current-column))))
276 (file-name-nondirectory file
)
279 (warning-fill-prefix " "))
280 (display-warning 'check-declare
281 (format "%s said `%s' was defined in %s: %s"
282 (file-name-nondirectory file
) fn
283 (file-name-nondirectory fnfile
)
285 nil check-declare-warning-buffer
)))
287 (defun check-declare-files (&rest files
)
288 "Check veracity of all `declare-function' statements in FILES.
289 Return a list of any errors found."
290 (let (alist err errlist
)
292 (setq alist
(cons (cons file
(check-declare-scan file
)) alist
)))
293 ;; Sort so that things are ordered by the files supposed to
294 ;; contain the defuns.
295 (dolist (e (check-declare-sort alist
))
296 (if (setq err
(check-declare-verify (car e
) (cdr e
)))
297 (setq errlist
(cons (cons (car e
) err
) errlist
))))
298 (setq errlist
(nreverse errlist
))
299 (if (get-buffer check-declare-warning-buffer
)
300 (kill-buffer check-declare-warning-buffer
))
301 (with-current-buffer (get-buffer-create check-declare-warning-buffer
)
302 (unless (derived-mode-p 'compilation-mode
)
304 (let ((inhibit-read-only t
))
306 (compilation-forget-errors))
307 ;; Sort back again so that errors are ordered by the files
308 ;; containing the declare-function statements.
309 (dolist (e (check-declare-sort errlist
))
311 (check-declare-warn (car e
) (cadr f
) (car f
) (nth 2 f
))))
315 (defun check-declare-file (file)
316 "Check veracity of all `declare-function' statements in FILE.
317 See `check-declare-directory' for more information."
318 (interactive "fFile to check: ")
319 (or (file-exists-p file
)
320 (error "File `%s' not found" file
))
321 (let ((m (format "Checking %s..." file
))
324 (setq errlist
(check-declare-files file
))
325 (message "%s%s" m
(check-declare-errmsg errlist
))
329 (defun check-declare-directory (root)
330 "Check veracity of all `declare-function' statements under directory ROOT.
331 Returns non-nil if any false statements are found."
332 (interactive "DDirectory to check: ")
333 (or (file-directory-p (setq root
(expand-file-name root
)))
334 (error "Directory `%s' not found" root
))
335 (let ((m "Checking `declare-function' statements...")
336 (m2 "Finding files with declarations...")
340 (setq files
(process-lines find-program root
343 "-l" "^[ \t]*(declare-function" "{}" ";"))
344 (message "%s%d found" m2
(length files
))
346 (setq errlist
(apply 'check-declare-files files
))
347 (message "%s%s" m
(check-declare-errmsg errlist t
))
350 (provide 'check-declare
)
352 ;;; check-declare.el ends here.