Update copyright year to 2015
[emacs.git] / lisp / progmodes / cc-bytecomp.el
blobbf7803c85ca243e6946c1725b578fc902cba86a1
1 ;;; cc-bytecomp.el --- compile time setup for proper compilation
3 ;; Copyright (C) 2000-2015 Free Software Foundation, Inc.
5 ;; Author: Martin Stjernholm
6 ;; Maintainer: bug-cc-mode@gnu.org
7 ;; Created: 15-Jul-2000
8 ;; Keywords: c languages
9 ;; Package: cc-mode
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; This file is used to ensure that the CC Mode files are correctly
29 ;; compiled regardless the environment (e.g. if an older CC Mode with
30 ;; outdated macros are loaded during compilation). It also provides
31 ;; features to defeat the compiler warnings for selected symbols.
33 ;; There's really nothing CC Mode specific here; this functionality
34 ;; ought to be provided by the byte compilers or some accompanying
35 ;; library. To use it from some package "foo.el", begin by putting
36 ;; the following blurb at the top of the file:
38 ;; (eval-when-compile
39 ;; (let ((load-path
40 ;; (if (and (boundp 'byte-compile-dest-file)
41 ;; (stringp byte-compile-dest-file))
42 ;; (cons (file-name-directory byte-compile-dest-file) load-path)
43 ;; load-path)))
44 ;; (load "cc-bytecomp" nil t))
46 ;; This (unfortunately rather clumsy) form will ensure that the
47 ;; cc-bytecomp.el in the same directory as foo.el is loaded during
48 ;; byte compilation of the latter.
50 ;; At the end of foo.el there should normally be a "(provide 'foo)".
51 ;; Replace it with "(cc-provide 'foo)"; that is necessary to restore
52 ;; the environment after the byte compilation. If you don't have a
53 ;; `provide' at the end, you have to add the following as the very
54 ;; last form in the file:
56 ;; (eval-when-compile (cc-bytecomp-restore-environment))
58 ;; Now everything is set to use the various functions and macros in
59 ;; this package.
61 ;; If your package is split into several files, you should use
62 ;; `cc-require', `cc-require-when-compile' or `cc-load' to load them.
63 ;; That ensures that the files in the same directory always are
64 ;; loaded, to avoid mixup with other versions of them that might exist
65 ;; elsewhere in the load path.
67 ;; To suppress byte compiler warnings, use the macros
68 ;; `cc-bytecomp-defun' and `cc-bytecomp-defvar'.
70 ;; This file is not used at all after the package has been byte
71 ;; compiled. It is however necessary when running uncompiled.
74 ;;; Code:
76 (defvar cc-bytecomp-unbound-variables nil)
77 (defvar cc-bytecomp-original-functions nil)
78 (defvar cc-bytecomp-original-properties nil)
79 (defvar cc-bytecomp-loaded-files nil)
81 (setq cc-bytecomp-unbound-variables nil)
82 (setq cc-bytecomp-original-functions nil)
83 (setq cc-bytecomp-original-properties nil)
84 (setq cc-bytecomp-loaded-files nil)
86 (defvar cc-bytecomp-environment-set nil)
88 (defmacro cc-bytecomp-debug-msg (&rest args)
89 ;;`(message ,@args)
92 (defun cc-bytecomp-setup-environment ()
93 ;; Eval'ed during compilation to setup variables, functions etc
94 ;; declared with `cc-bytecomp-defvar' et al.
95 (if (not load-in-progress)
96 ;; Look at `load-in-progress' to tell whether we're called
97 ;; directly in the file being compiled or just from some file
98 ;; being loaded during compilation.
99 (let (p)
100 (if cc-bytecomp-environment-set
101 (error "Byte compilation environment already set - \
102 perhaps a `cc-bytecomp-restore-environment' is forgotten somewhere"))
103 (setq p cc-bytecomp-unbound-variables)
104 (while p
105 (if (not (boundp (car p)))
106 (progn
107 (eval `(defvar ,(car p)))
108 (set (car p) (intern (concat "cc-bytecomp-ignore-var:"
109 (symbol-name (car p)))))
110 (cc-bytecomp-debug-msg
111 "cc-bytecomp-setup-environment: Covered variable %s"
112 (car p))))
113 (setq p (cdr p)))
114 (setq p cc-bytecomp-original-functions)
115 (while p
116 (let ((fun (car (car p)))
117 (temp-macro (car (cdr (car p)))))
118 (if (not (fboundp fun))
119 (if temp-macro
120 (progn
121 (eval `(defmacro ,fun ,@temp-macro))
122 (cc-bytecomp-debug-msg
123 "cc-bytecomp-setup-environment: Bound macro %s" fun))
124 (fset fun (intern (concat "cc-bytecomp-ignore-fun:"
125 (symbol-name fun))))
126 (cc-bytecomp-debug-msg
127 "cc-bytecomp-setup-environment: Covered function %s" fun))))
128 (setq p (cdr p)))
129 (setq p cc-bytecomp-original-properties)
130 (while p
131 (let ((sym (car (car (car p))))
132 (prop (cdr (car (car p))))
133 (tempdef (car (cdr (car p)))))
134 (put sym prop tempdef)
135 (cc-bytecomp-debug-msg
136 "cc-bytecomp-setup-environment: Bound property %s for %s to %s"
137 prop sym tempdef))
138 (setq p (cdr p)))
139 (setq cc-bytecomp-environment-set t)
140 (cc-bytecomp-debug-msg
141 "cc-bytecomp-setup-environment: Done"))))
143 (defun cc-bytecomp-restore-environment ()
144 ;; Eval'ed during compilation to restore variables, functions etc
145 ;; declared with `cc-bytecomp-defvar' et al.
146 (if (not load-in-progress)
147 (let (p)
148 (setq p cc-bytecomp-unbound-variables)
149 (while p
150 (let ((var (car p)))
151 (if (boundp var)
152 (if (eq (intern (concat "cc-bytecomp-ignore-var:"
153 (symbol-name var)))
154 (symbol-value var))
155 (progn
156 (makunbound var)
157 (cc-bytecomp-debug-msg
158 "cc-bytecomp-restore-environment: Unbound variable %s"
159 var))
160 (cc-bytecomp-debug-msg
161 "cc-bytecomp-restore-environment: Not restoring variable %s"
162 var))))
163 (setq p (cdr p)))
164 (setq p cc-bytecomp-original-functions)
165 (while p
166 (let ((fun (car (car p)))
167 (temp-macro (car (cdr (car p))))
168 (def (car (cdr (cdr (car p))))))
169 (if (fboundp fun)
170 (if (eq (or temp-macro
171 (intern (concat "cc-bytecomp-ignore-fun:"
172 (symbol-name fun))))
173 (symbol-function fun))
174 (if (eq def 'unbound)
175 (progn
176 (fmakunbound fun)
177 (cc-bytecomp-debug-msg
178 "cc-bytecomp-restore-environment: Unbound function %s"
179 fun))
180 (fset fun def)
181 (cc-bytecomp-debug-msg
182 "cc-bytecomp-restore-environment: Restored function %s"
183 fun))
184 (cc-bytecomp-debug-msg
185 "cc-bytecomp-restore-environment: Not restoring function %s"
186 fun))))
187 (setq p (cdr p)))
188 (setq p cc-bytecomp-original-properties)
189 (while p
190 (let ((sym (car (car (car p))))
191 (prop (cdr (car (car p))))
192 (tempdef (car (cdr (car p))))
193 (origdef (cdr (cdr (car p)))))
194 (if (eq (get sym prop) tempdef)
195 (progn
196 (put sym prop origdef)
197 (cc-bytecomp-debug-msg
198 "cc-bytecomp-restore-environment: Restored property %s for %s to %s"
199 prop sym origdef))
200 (cc-bytecomp-debug-msg
201 "cc-bytecomp-restore-environment: Not restoring property %s for %s"
202 prop sym)))
203 (setq p (cdr p)))
204 (setq cc-bytecomp-environment-set nil)
205 (cc-bytecomp-debug-msg
206 "cc-bytecomp-restore-environment: Done"))))
208 (eval
209 ;; This eval is to avoid byte compilation of the function below.
210 ;; There's some bug in XEmacs 21.4.6 that can cause it to dump core
211 ;; here otherwise. My theory is that `cc-bytecomp-load' might be
212 ;; redefined recursively during the `load' inside it, and if it in
213 ;; that case is byte compiled then the byte interpreter gets
214 ;; confused. I haven't succeeded in isolating the bug, though. /mast
216 '(defun cc-bytecomp-load (cc-part)
217 ;; Eval'ed during compilation to load a CC Mode file from the source
218 ;; directory (assuming it's the same as the compiled file
219 ;; destination dir).
220 (if (and (boundp 'byte-compile-dest-file)
221 (stringp byte-compile-dest-file))
222 (progn
223 (cc-bytecomp-restore-environment)
224 (let ((load-path
225 (cons (file-name-directory byte-compile-dest-file)
226 load-path))
227 (cc-file (concat cc-part ".el")))
228 (if (member cc-file cc-bytecomp-loaded-files)
230 (setq cc-bytecomp-loaded-files
231 (cons cc-file cc-bytecomp-loaded-files))
232 (cc-bytecomp-debug-msg
233 "cc-bytecomp-load: Loading %S" cc-file)
234 (load cc-file nil t t)
235 (cc-bytecomp-debug-msg
236 "cc-bytecomp-load: Loaded %S" cc-file)))
237 (cc-bytecomp-setup-environment)
238 t))))
240 (defvar cc-bytecomp-noruntime-functions nil
241 "Saved value of `byte-compile-noruntime-functions'.")
243 (defmacro cc-require (cc-part)
244 "Force loading of the corresponding .el file in the current directory
245 during compilation, but compile in a `require'. Don't use within
246 `eval-when-compile'.
248 Having cyclic cc-require's will result in infinite recursion. That's
249 somewhat intentional."
250 `(progn
251 (eval-when-compile
252 (if (boundp 'byte-compile-noruntime-functions) ; in case load uncompiled
253 (setq cc-bytecomp-noruntime-functions
254 byte-compile-noruntime-functions))
255 (cc-bytecomp-load (symbol-name ,cc-part)))
256 ;; Hack to suppress spurious "might not be defined at runtime" warnings.
257 ;; The basic issue is that
258 ;; (eval-when-compile (require 'foo))
259 ;; (require 'foo)
260 ;; produces bogus noruntime warnings about functions from foo.
261 (eval-when-compile
262 (setq byte-compile-noruntime-functions cc-bytecomp-noruntime-functions))
263 (require ,cc-part)))
265 (defmacro cc-provide (feature)
266 "A replacement for the `provide' form that restores the environment
267 after the compilation. Don't use within `eval-when-compile'."
268 `(progn
269 (eval-when-compile (cc-bytecomp-restore-environment))
270 (provide ,feature)))
272 (defmacro cc-load (cc-part)
273 "Force loading of the corresponding .el file in the current directory
274 during compilation. Don't use outside `eval-when-compile' or
275 `eval-and-compile'.
277 Having cyclic cc-load's will result in infinite recursion. That's
278 somewhat intentional."
279 `(or (and (featurep 'cc-bytecomp)
280 (cc-bytecomp-load ,cc-part))
281 (load ,cc-part nil t nil)))
283 (defmacro cc-require-when-compile (cc-part)
284 "Force loading of the corresponding .el file in the current directory
285 during compilation, but do a compile time `require' otherwise. Don't
286 use within `eval-when-compile'."
287 `(eval-when-compile
288 (if (and (fboundp 'cc-bytecomp-is-compiling)
289 (cc-bytecomp-is-compiling))
290 (if (or (not load-in-progress)
291 (not (featurep ,cc-part)))
292 (cc-bytecomp-load (symbol-name ,cc-part)))
293 (require ,cc-part))))
295 (defmacro cc-external-require (feature)
296 "Do a `require' of an external package.
297 This restores and sets up the compilation environment before and
298 afterwards. Don't use within `eval-when-compile'."
299 `(progn
300 (eval-when-compile (cc-bytecomp-restore-environment))
301 (require ,feature)
302 (eval-when-compile (cc-bytecomp-setup-environment))))
304 (defun cc-bytecomp-is-compiling ()
305 "Return non-nil if eval'ed during compilation. Don't use outside
306 `eval-when-compile'."
307 (and (boundp 'byte-compile-dest-file)
308 (stringp byte-compile-dest-file)))
310 (defmacro cc-bytecomp-defvar (var)
311 "Binds the symbol as a variable during compilation of the file,
312 to silence the byte compiler. Don't use within `eval-when-compile'."
313 `(eval-when-compile
314 (if (boundp ',var)
315 (cc-bytecomp-debug-msg
316 "cc-bytecomp-defvar: %s bound already as variable" ',var)
317 (if (not (memq ',var cc-bytecomp-unbound-variables))
318 (progn
319 (cc-bytecomp-debug-msg
320 "cc-bytecomp-defvar: Saving %s (as unbound)" ',var)
321 (setq cc-bytecomp-unbound-variables
322 (cons ',var cc-bytecomp-unbound-variables))))
323 (if (and (cc-bytecomp-is-compiling)
324 (not load-in-progress))
325 (progn
326 (defvar ,var)
327 (set ',var (intern (concat "cc-bytecomp-ignore-var:"
328 (symbol-name ',var))))
329 (cc-bytecomp-debug-msg
330 "cc-bytecomp-defvar: Covered variable %s" ',var))))))
332 (defmacro cc-bytecomp-defun (fun)
333 "Bind the symbol as a function during compilation of the file,
334 to silence the byte compiler. Don't use within `eval-when-compile'.
336 If the symbol already is bound as a function, it will keep that
337 definition. That means that this macro will not shut up warnings
338 about incorrect number of arguments. It's dangerous to try to replace
339 existing functions since the byte compiler might need the definition
340 at compile time, e.g. for macros and inline functions."
341 `(eval-when-compile
342 (if (fboundp ',fun)
343 (cc-bytecomp-debug-msg
344 "cc-bytecomp-defun: %s bound already as function" ',fun)
345 (if (not (assq ',fun cc-bytecomp-original-functions))
346 (progn
347 (cc-bytecomp-debug-msg
348 "cc-bytecomp-defun: Saving %s (as unbound)" ',fun)
349 (setq cc-bytecomp-original-functions
350 (cons (list ',fun nil 'unbound)
351 cc-bytecomp-original-functions))))
352 (if (and (cc-bytecomp-is-compiling)
353 (not load-in-progress))
354 (progn
355 (fset ',fun (intern (concat "cc-bytecomp-ignore-fun:"
356 (symbol-name ',fun))))
357 (cc-bytecomp-debug-msg
358 "cc-bytecomp-defun: Covered function %s" ',fun))))))
360 (defmacro cc-bytecomp-put (symbol propname value)
361 "Set a property on a symbol during compilation (and evaluation) of
362 the file. Don't use outside `eval-when-compile'."
363 `(eval-when-compile
364 (if (not (assoc (cons ,symbol ,propname) cc-bytecomp-original-properties))
365 (progn
366 (cc-bytecomp-debug-msg
367 "cc-bytecomp-put: Saving property %s for %s with value %s"
368 ,propname ,symbol (get ,symbol ,propname))
369 (setq cc-bytecomp-original-properties
370 (cons (cons (cons ,symbol ,propname)
371 (cons ,value (get ,symbol ,propname)))
372 cc-bytecomp-original-properties))))
373 (put ,symbol ,propname ,value)
374 (cc-bytecomp-debug-msg
375 "cc-bytecomp-put: Bound property %s for %s to %s"
376 ,propname ,symbol ,value)))
378 (defmacro cc-bytecomp-boundp (symbol)
379 "Return non-nil if the given symbol is bound as a variable outside
380 the compilation. This is the same as using `boundp' but additionally
381 exclude any variables that have been bound during compilation with
382 `cc-bytecomp-defvar'."
383 (if (and (cc-bytecomp-is-compiling)
384 (memq (car (cdr symbol)) cc-bytecomp-unbound-variables))
386 `(boundp ,symbol)))
388 (defmacro cc-bytecomp-fboundp (symbol)
389 "Return non-nil if the given symbol is bound as a function outside
390 the compilation. This is the same as using `fboundp' but additionally
391 exclude any functions that have been bound during compilation with
392 `cc-bytecomp-defun'."
393 (let (fun-elem)
394 (if (and (cc-bytecomp-is-compiling)
395 (setq fun-elem (assq (car (cdr symbol))
396 cc-bytecomp-original-functions))
397 (eq (elt fun-elem 2) 'unbound))
399 `(fboundp ,symbol))))
402 (provide 'cc-bytecomp)
404 ;;; Local Variables:
405 ;;; indent-tabs-mode: t
406 ;;; tab-width: 8
407 ;;; End:
408 ;;; cc-bytecomp.el ends here