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