86178
|
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
|
|
33 ;;; Code:
|
|
34
|
|
35 (defconst check-declare-warning-buffer "*Check Declarations Warnings*"
|
|
36 "Name of buffer used to display any `check-declare' warnings.")
|
|
37
|
|
38 (defun check-declare-scan (file)
|
|
39 "Scan FILE for `declare-function' calls.
|
|
40 Return a list with elements of the form (FNFILE FN ARGLIST), where
|
|
41 ARGLIST may be absent. This claims that FNFILE defines FN, with ARGLIST."
|
|
42 (let ((m (format "Scanning %s..." file))
|
|
43 alist fnfile fn)
|
|
44 (message "%s" m)
|
|
45 (with-temp-buffer
|
|
46 (insert-file-contents file)
|
|
47 (while (re-search-forward
|
|
48 "^[ \t]*(declare-function[ \t]+\\(\\S-+\\)[ \t]+\
|
|
49 \"\\(\\S-+\\)\"" nil t)
|
|
50 (setq fn (match-string 1)
|
|
51 fnfile (match-string 2))
|
|
52 (or (file-name-absolute-p fnfile)
|
|
53 (setq fnfile (expand-file-name fnfile (file-name-directory file))))
|
|
54 (setq alist (cons
|
|
55 (list fnfile fn
|
|
56 (progn
|
|
57 (skip-chars-forward " \t\n")
|
|
58 ;; Use `t' to distinguish no arglist
|
|
59 ;; specified from an empty one.
|
|
60 (if (looking-at "\\((\\|nil\\)")
|
|
61 (read (current-buffer))
|
|
62 t)))
|
|
63 alist))))
|
|
64 (message "%sdone" m)
|
|
65 alist))
|
|
66
|
|
67 (autoload 'byte-compile-arglist-signature "bytecomp")
|
|
68
|
|
69 (defun check-declare-verify (fnfile fnlist)
|
|
70 "Check that FNFILE contains function definitions matching FNLIST.
|
|
71 Each element of FNLIST has the form (FILE FN ARGLIST), where
|
|
72 ARGLIST is optional. This means FILE claimed FN was defined in
|
|
73 FNFILE with the specified ARGLIST. Returns nil if all claims are
|
|
74 found to be true, otherwise a list of errors with elements of the form
|
|
75 \(FILE FN TYPE), where TYPE is a string giving details of the error."
|
|
76 (let ((m (format "Checking %s..." fnfile))
|
|
77 re fn sig siglist arglist type errlist)
|
|
78 (message "%s" m)
|
|
79 (if (file-exists-p fnfile)
|
|
80 (with-temp-buffer
|
|
81 (insert-file-contents fnfile)
|
|
82 (setq re (format "^[ \t]*(defun[ \t]+%s\\>"
|
|
83 (regexp-opt (mapcar 'cadr fnlist) t)))
|
|
84 (while (re-search-forward re nil t)
|
|
85 (skip-chars-forward " \t\n")
|
|
86 (setq fn (match-string 1)
|
|
87 sig (if (looking-at "\\((\\|nil\\)")
|
|
88 (byte-compile-arglist-signature
|
|
89 (read (current-buffer))))
|
|
90 ;; alist of functions and arglist signatures.
|
|
91 siglist (cons (cons fn sig) siglist)))))
|
|
92 (dolist (e fnlist)
|
|
93 (setq arglist (nth 2 e)
|
|
94 type
|
|
95 (if re ; re non-nil means found a file
|
|
96 (if (setq sig (assoc (cadr e) siglist))
|
|
97 ;; Recall we use t to mean no arglist specified,
|
|
98 ;; to distinguish from an empty arglist.
|
|
99 (unless (eq arglist t)
|
|
100 (unless (equal (byte-compile-arglist-signature arglist)
|
|
101 (cdr sig))
|
|
102 "arglist mismatch"))
|
|
103 "function not found")
|
|
104 "file not found"))
|
|
105 (when type
|
|
106 (setq errlist (cons (list (car e) (cadr e) type) errlist))))
|
|
107 (message "%s%s" m (if errlist "problems found" "OK"))
|
|
108 errlist))
|
|
109
|
|
110 (defun check-declare-sort (alist)
|
|
111 "Sort a list with elements FILE (FNFILE ...).
|
|
112 Returned list has elements FNFILE (FILE ...)."
|
|
113 (let (file fnfile rest sort a)
|
|
114 (dolist (e alist)
|
|
115 (setq file (car e))
|
|
116 (dolist (f (cdr e))
|
|
117 (setq fnfile (car f)
|
|
118 rest (cdr f))
|
|
119 (if (setq a (assoc fnfile sort))
|
|
120 (setcdr a (append (cdr a) (list (cons file rest))))
|
|
121 (setq sort (cons (list fnfile (cons file rest)) sort)))))
|
|
122 sort))
|
|
123
|
|
124 (defun check-declare-warn (file fn fnfile type)
|
|
125 "Warn that FILE made a false claim about FN in FNFILE.
|
|
126 TYPE is a string giving the nature of the error. Warning is displayed in
|
|
127 `check-declare-warning-buffer'."
|
|
128 (display-warning 'check-declare
|
|
129 (format "%s said `%s' was defined in %s: %s"
|
|
130 (file-name-nondirectory file) fn
|
|
131 (file-name-nondirectory fnfile)
|
|
132 type)
|
|
133 nil check-declare-warning-buffer))
|
|
134
|
|
135 (defun check-declare-files (&rest files)
|
|
136 "Check veracity of all `declare-function' statements in FILES.
|
|
137 Return a list of any errors found."
|
|
138 (let (alist err errlist)
|
|
139 (dolist (file files)
|
|
140 (setq alist (cons (cons file (check-declare-scan file)) alist)))
|
|
141 ;; Sort so that things are ordered by the files supposed to
|
|
142 ;; contain the defuns.
|
|
143 (dolist (e (check-declare-sort alist))
|
|
144 (if (setq err (check-declare-verify (car e) (cdr e)))
|
|
145 (setq errlist (cons (cons (car e) err) errlist))))
|
|
146 (if (get-buffer check-declare-warning-buffer)
|
|
147 (kill-buffer check-declare-warning-buffer))
|
|
148 ;; Sort back again so that errors are ordered by the files
|
|
149 ;; containing the declare-function statements.
|
|
150 (dolist (e (check-declare-sort errlist))
|
|
151 (dolist (f (cdr e))
|
|
152 (check-declare-warn (car e) (cadr f) (car f) (nth 2 f))))
|
|
153 errlist))
|
|
154
|
|
155 ;;;###autoload
|
|
156 (defun check-declare-file (file)
|
|
157 "Check veracity of all `declare-function' statements in FILE.
|
|
158 See `check-declare-directory' for more information."
|
|
159 (interactive "fFile to check: ")
|
|
160 (or (file-exists-p file)
|
|
161 (error "File `%s' not found" file))
|
|
162 (let ((m (format "Checking %s..." file))
|
|
163 errlist)
|
|
164 (message "%s" m)
|
|
165 (setq errlist (check-declare-files file))
|
|
166 (message "%s%s" m (if errlist "problems found" "OK"))
|
|
167 errlist))
|
|
168
|
|
169 ;;;###autoload
|
|
170 (defun check-declare-directory (root)
|
|
171 "Check veracity of all `declare-function' statements under directory ROOT.
|
|
172 Returns non-nil if any false statements are found. For this to
|
|
173 work correctly, the statements must adhere to the format
|
|
174 described in the documentation of `declare-function'."
|
|
175 (interactive "DDirectory to check: ")
|
|
176 (or (file-directory-p (setq root (expand-file-name root)))
|
|
177 (error "Directory `%s' not found" root))
|
|
178 (let ((m "Checking `declare-function' statements...")
|
|
179 (m2 "Finding files with declarations...")
|
|
180 errlist files)
|
|
181 (message "%s" m)
|
|
182 (message "%s" m2)
|
|
183 (setq files (process-lines "find" root "-name" "*.el"
|
|
184 "-exec" "grep" "-l"
|
|
185 "^[ ]*(declare-function" "{}" ";"))
|
|
186 (message "%s%d found" m2 (length files))
|
|
187 (when files
|
|
188 (setq errlist (apply 'check-declare-files files))
|
|
189 (message "%s%s" m (if errlist "problems found" "OK"))
|
|
190 errlist)))
|
|
191
|
|
192 (provide 'check-declare)
|
|
193
|
|
194 ;; arch-tag: a4d6cdc4-deb7-4502-b327-0e4ef3d82d96
|
|
195 ;;; check-declare.el ends here.
|