Mercurial > emacs
annotate lisp/emacs-lisp/check-declare.el @ 86297:a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
(check-declare-verify): Handle .c files (without arg checking).
author | Glenn Morris <rgm@gnu.org> |
---|---|
date | Thu, 22 Nov 2007 04:18:54 +0000 |
parents | 2f50908378c9 |
children | 96fee69c65a2 |
rev | line source |
---|---|
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 | |
86220
1244ab609a99
(check-declare-verify): If fnfile does not exist, try adding `.el'
Glenn Morris <rgm@gnu.org>
parents:
86178
diff
changeset
|
33 ;;; TODO: |
1244ab609a99
(check-declare-verify): If fnfile does not exist, try adding `.el'
Glenn Morris <rgm@gnu.org>
parents:
86178
diff
changeset
|
34 |
1244ab609a99
(check-declare-verify): If fnfile does not exist, try adding `.el'
Glenn Morris <rgm@gnu.org>
parents:
86178
diff
changeset
|
35 ;; 1. Handle defstructs (eg uniquify-item-base in desktop.el). |
1244ab609a99
(check-declare-verify): If fnfile does not exist, try adding `.el'
Glenn Morris <rgm@gnu.org>
parents:
86178
diff
changeset
|
36 |
86297
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
37 ;; 2. Argument checking for functions defined in C. |
86278
2f50908378c9
(check-declare-verify): Skip C files for now. Handle
Glenn Morris <rgm@gnu.org>
parents:
86244
diff
changeset
|
38 |
86178 | 39 ;;; Code: |
40 | |
41 (defconst check-declare-warning-buffer "*Check Declarations Warnings*" | |
42 "Name of buffer used to display any `check-declare' warnings.") | |
43 | |
44 (defun check-declare-scan (file) | |
45 "Scan FILE for `declare-function' calls. | |
46 Return a list with elements of the form (FNFILE FN ARGLIST), where | |
47 ARGLIST may be absent. This claims that FNFILE defines FN, with ARGLIST." | |
48 (let ((m (format "Scanning %s..." file)) | |
49 alist fnfile fn) | |
50 (message "%s" m) | |
51 (with-temp-buffer | |
52 (insert-file-contents file) | |
53 (while (re-search-forward | |
54 "^[ \t]*(declare-function[ \t]+\\(\\S-+\\)[ \t]+\ | |
55 \"\\(\\S-+\\)\"" nil t) | |
56 (setq fn (match-string 1) | |
57 fnfile (match-string 2)) | |
58 (or (file-name-absolute-p fnfile) | |
86297
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
59 (setq fnfile |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
60 (expand-file-name fnfile |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
61 ;; .c files are assumed to be |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
62 ;; relative to the Emacs src/ directory. |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
63 (if (string-equal |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
64 "c" (file-name-extension fnfile)) |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
65 (expand-file-name "src" |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
66 source-directory) |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
67 (file-name-directory file))))) |
86178 | 68 (setq alist (cons |
69 (list fnfile fn | |
70 (progn | |
71 (skip-chars-forward " \t\n") | |
72 ;; Use `t' to distinguish no arglist | |
73 ;; specified from an empty one. | |
74 (if (looking-at "\\((\\|nil\\)") | |
75 (read (current-buffer)) | |
76 t))) | |
77 alist)))) | |
78 (message "%sdone" m) | |
79 alist)) | |
80 | |
81 (autoload 'byte-compile-arglist-signature "bytecomp") | |
82 | |
83 (defun check-declare-verify (fnfile fnlist) | |
84 "Check that FNFILE contains function definitions matching FNLIST. | |
85 Each element of FNLIST has the form (FILE FN ARGLIST), where | |
86 ARGLIST is optional. This means FILE claimed FN was defined in | |
87 FNFILE with the specified ARGLIST. Returns nil if all claims are | |
88 found to be true, otherwise a list of errors with elements of the form | |
89 \(FILE FN TYPE), where TYPE is a string giving details of the error." | |
90 (let ((m (format "Checking %s..." fnfile)) | |
86297
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
91 (cflag (string-equal "c" (file-name-extension fnfile))) |
86178 | 92 re fn sig siglist arglist type errlist) |
93 (message "%s" m) | |
86297
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
94 (or cflag |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
95 (file-exists-p fnfile) |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
96 (setq fnfile (concat fnfile ".el"))) |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
97 (if (file-exists-p fnfile) |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
98 (with-temp-buffer |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
99 (insert-file-contents fnfile) |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
100 ;; defsubst's don't _have_ to be known at compile time. |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
101 (setq re (format (if cflag |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
102 "^[ \t]*\\(DEFUN\\)[ \t]*([ \t]*\"%s\"" |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
103 "^[ \t]*(\\(def\\(?:un\\|subst\\|\ |
86278
2f50908378c9
(check-declare-verify): Skip C files for now. Handle
Glenn Morris <rgm@gnu.org>
parents:
86244
diff
changeset
|
104 ine-derived-mode\\|ine-minor-mode\\|alias[ \t]+'\\)\\)\ |
86297
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
105 \[ \t]*%s\\([ \t;]+\\|$\\)") |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
106 (regexp-opt (mapcar 'cadr fnlist) t))) |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
107 (while (re-search-forward re nil t) |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
108 (skip-chars-forward " \t\n") |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
109 (setq fn (match-string 2) |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
110 ;; (min . max) for a fixed number of arguments, or |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
111 ;; arglists with optional elements. |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
112 ;; (min) for arglists with &rest. |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
113 sig (cond ((string-equal (match-string 1) |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
114 "define-derived-mode") |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
115 '(0 . 0)) |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
116 ((string-equal (match-string 1) |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
117 "define-minor-mode") |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
118 '(0 . 1)) |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
119 ;; Can't easily check alias arguments. |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
120 ((string-equal (match-string 1) |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
121 "defalias") |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
122 t) |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
123 (t |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
124 (if (looking-at "\\((\\|nil\\)") |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
125 (byte-compile-arglist-signature |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
126 (read (current-buffer)))))) |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
127 ;; alist of functions and arglist signatures. |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
128 siglist (cons (cons fn sig) siglist))))) |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
129 (dolist (e fnlist) |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
130 (setq arglist (nth 2 e) |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
131 type |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
132 (if re ; re non-nil means found a file |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
133 (if (setq sig (assoc (cadr e) siglist)) |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
134 ;; Recall we use t to mean no arglist specified, |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
135 ;; to distinguish from an empty arglist. |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
136 ;; FIXME c arg checking not yet implemented. |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
137 (unless (or cflag |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
138 (eq arglist t) |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
139 (eq sig t)) |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
140 (unless (equal (byte-compile-arglist-signature arglist) |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
141 (cdr sig)) |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
142 "arglist mismatch")) |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
143 "function not found") |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
144 "file not found")) |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
145 (when type |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
146 (setq errlist (cons (list (car e) (cadr e) type) errlist)))) |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
147 (message "%s%s" m (if errlist "problems found" "OK")) |
a80b13d94c3c
(check-declare-scan): Expand .c files relative to src/ directory.
Glenn Morris <rgm@gnu.org>
parents:
86278
diff
changeset
|
148 errlist)) |
86178 | 149 |
150 (defun check-declare-sort (alist) | |
151 "Sort a list with elements FILE (FNFILE ...). | |
152 Returned list has elements FNFILE (FILE ...)." | |
153 (let (file fnfile rest sort a) | |
154 (dolist (e alist) | |
155 (setq file (car e)) | |
156 (dolist (f (cdr e)) | |
157 (setq fnfile (car f) | |
158 rest (cdr f)) | |
159 (if (setq a (assoc fnfile sort)) | |
160 (setcdr a (append (cdr a) (list (cons file rest)))) | |
161 (setq sort (cons (list fnfile (cons file rest)) sort))))) | |
162 sort)) | |
163 | |
164 (defun check-declare-warn (file fn fnfile type) | |
165 "Warn that FILE made a false claim about FN in FNFILE. | |
166 TYPE is a string giving the nature of the error. Warning is displayed in | |
167 `check-declare-warning-buffer'." | |
168 (display-warning 'check-declare | |
169 (format "%s said `%s' was defined in %s: %s" | |
170 (file-name-nondirectory file) fn | |
171 (file-name-nondirectory fnfile) | |
172 type) | |
173 nil check-declare-warning-buffer)) | |
174 | |
175 (defun check-declare-files (&rest files) | |
176 "Check veracity of all `declare-function' statements in FILES. | |
177 Return a list of any errors found." | |
178 (let (alist err errlist) | |
179 (dolist (file files) | |
180 (setq alist (cons (cons file (check-declare-scan file)) alist))) | |
181 ;; Sort so that things are ordered by the files supposed to | |
182 ;; contain the defuns. | |
183 (dolist (e (check-declare-sort alist)) | |
184 (if (setq err (check-declare-verify (car e) (cdr e))) | |
185 (setq errlist (cons (cons (car e) err) errlist)))) | |
186 (if (get-buffer check-declare-warning-buffer) | |
187 (kill-buffer check-declare-warning-buffer)) | |
188 ;; Sort back again so that errors are ordered by the files | |
189 ;; containing the declare-function statements. | |
190 (dolist (e (check-declare-sort errlist)) | |
191 (dolist (f (cdr e)) | |
192 (check-declare-warn (car e) (cadr f) (car f) (nth 2 f)))) | |
193 errlist)) | |
194 | |
195 ;;;###autoload | |
196 (defun check-declare-file (file) | |
197 "Check veracity of all `declare-function' statements in FILE. | |
198 See `check-declare-directory' for more information." | |
199 (interactive "fFile to check: ") | |
200 (or (file-exists-p file) | |
201 (error "File `%s' not found" file)) | |
202 (let ((m (format "Checking %s..." file)) | |
203 errlist) | |
204 (message "%s" m) | |
205 (setq errlist (check-declare-files file)) | |
206 (message "%s%s" m (if errlist "problems found" "OK")) | |
207 errlist)) | |
208 | |
209 ;;;###autoload | |
210 (defun check-declare-directory (root) | |
211 "Check veracity of all `declare-function' statements under directory ROOT. | |
212 Returns non-nil if any false statements are found. For this to | |
213 work correctly, the statements must adhere to the format | |
214 described in the documentation of `declare-function'." | |
215 (interactive "DDirectory to check: ") | |
216 (or (file-directory-p (setq root (expand-file-name root))) | |
217 (error "Directory `%s' not found" root)) | |
218 (let ((m "Checking `declare-function' statements...") | |
219 (m2 "Finding files with declarations...") | |
220 errlist files) | |
221 (message "%s" m) | |
222 (message "%s" m2) | |
223 (setq files (process-lines "find" root "-name" "*.el" | |
224 "-exec" "grep" "-l" | |
225 "^[ ]*(declare-function" "{}" ";")) | |
226 (message "%s%d found" m2 (length files)) | |
227 (when files | |
228 (setq errlist (apply 'check-declare-files files)) | |
229 (message "%s%s" m (if errlist "problems found" "OK")) | |
230 errlist))) | |
231 | |
232 (provide 'check-declare) | |
233 | |
234 ;; arch-tag: a4d6cdc4-deb7-4502-b327-0e4ef3d82d96 | |
235 ;;; check-declare.el ends here. |