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