1 ;;; cc-bytecomp.el --- compile time setup for proper compilation
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 ;; Free Software Foundation, Inc.
6 ;; Author: Martin Stjernholm
7 ;; Maintainer: bug-cc-mode@gnu.org
8 ;; Created: 15-Jul-2000
9 ;; Version: See cc-mode.el
10 ;; Keywords: c languages oop
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 3, or (at your option)
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with this program; see the file COPYING. If not, write to
26 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
31 ;; This file is used to ensure that the CC Mode files are correctly
32 ;; compiled regardless the environment (e.g. if an older CC Mode with
33 ;; outdated macros are loaded during compilation). It also provides
34 ;; features to defeat the compiler warnings for selected symbols.
36 ;; There's really nothing CC Mode specific here; this functionality
37 ;; ought to be provided by the byte compilers or some accompanying
38 ;; library. To use it from some package "foo.el", begin by putting
39 ;; the following blurb at the top of the file:
43 ;; (if (and (boundp 'byte-compile-dest-file)
44 ;; (stringp byte-compile-dest-file))
45 ;; (cons (file-name-directory byte-compile-dest-file) load-path)
47 ;; (load "cc-bytecomp" nil t))
49 ;; This (unfortunately rather clumsy) form will ensure that the
50 ;; cc-bytecomp.el in the same directory as foo.el is loaded during
51 ;; byte compilation of the latter.
53 ;; At the end of foo.el there should normally be a "(provide 'foo)".
54 ;; Replace it with "(cc-provide 'foo)"; that is necessary to restore
55 ;; the environment after the byte compilation. If you don't have a
56 ;; `provide' at the end, you have to add the following as the very
57 ;; last form in the file:
59 ;; (eval-when-compile (cc-bytecomp-restore-environment))
61 ;; Now everything is set to use the various functions and macros in
64 ;; If your package is split into several files, you should use
65 ;; `cc-require', `cc-require-when-compile' or `cc-load' to load them.
66 ;; That ensures that the files in the same directory always are
67 ;; loaded, to avoid mixup with other versions of them that might exist
68 ;; elsewhere in the load path.
70 ;; To suppress byte compiler warnings, use the macros
71 ;; `cc-bytecomp-defun', `cc-bytecomp-defvar',
72 ;; `cc-bytecomp-obsolete-fun', and `cc-bytecomp-obsolete-var'.
74 ;; This file is not used at all after the package has been byte
75 ;; compiled. It is however necessary when running uncompiled.
80 (defvar cc-bytecomp-unbound-variables nil
)
81 (defvar cc-bytecomp-original-functions nil
)
82 (defvar cc-bytecomp-original-properties nil
)
83 (defvar cc-bytecomp-loaded-files nil
)
84 (defvar cc-bytecomp-environment-set nil
)
86 (defmacro cc-bytecomp-debug-msg
(&rest args
)
90 (defun cc-bytecomp-setup-environment ()
91 ;; Eval'ed during compilation to setup variables, functions etc
92 ;; declared with `cc-bytecomp-defvar' et al.
93 (if (not load-in-progress
)
94 ;; Look at `load-in-progress' to tell whether we're called
95 ;; directly in the file being compiled or just from some file
96 ;; being loaded during compilation.
98 (if cc-bytecomp-environment-set
99 (error "Byte compilation environment already set - \
100 perhaps a `cc-bytecomp-restore-environment' is forgotten somewhere"))
101 (setq p cc-bytecomp-unbound-variables
)
103 (if (not (boundp (car p
)))
105 (eval `(defvar ,(car p
)))
106 (set (car p
) (intern (concat "cc-bytecomp-ignore-var:"
107 (symbol-name (car p
)))))
108 (cc-bytecomp-debug-msg
109 "cc-bytecomp-setup-environment: Covered variable %s"
112 (setq p cc-bytecomp-original-functions
)
114 (let ((fun (car (car p
)))
115 (temp-macro (car (cdr (car p
)))))
116 (if (not (fboundp fun
))
119 (eval `(defmacro ,fun
,@temp-macro
))
120 (cc-bytecomp-debug-msg
121 "cc-bytecomp-setup-environment: Bound macro %s" fun
))
122 (fset fun
(intern (concat "cc-bytecomp-ignore-fun:"
124 (cc-bytecomp-debug-msg
125 "cc-bytecomp-setup-environment: Covered function %s" fun
))))
127 (setq p cc-bytecomp-original-properties
)
129 (let ((sym (car (car (car p
))))
130 (prop (cdr (car (car p
))))
131 (tempdef (car (cdr (car p
)))))
132 (put sym prop tempdef
)
133 (cc-bytecomp-debug-msg
134 "cc-bytecomp-setup-environment: Bound property %s for %s to %s"
137 (setq cc-bytecomp-environment-set t
)
138 (cc-bytecomp-debug-msg
139 "cc-bytecomp-setup-environment: Done"))))
141 (defun cc-bytecomp-restore-environment ()
142 ;; Eval'ed during compilation to restore variables, functions etc
143 ;; declared with `cc-bytecomp-defvar' et al.
144 (if (not load-in-progress
)
146 (setq p cc-bytecomp-unbound-variables
)
150 (if (eq (intern (concat "cc-bytecomp-ignore-var:"
155 (cc-bytecomp-debug-msg
156 "cc-bytecomp-restore-environment: Unbound variable %s"
158 (cc-bytecomp-debug-msg
159 "cc-bytecomp-restore-environment: Not restoring variable %s"
162 (setq p cc-bytecomp-original-functions
)
164 (let ((fun (car (car p
)))
165 (temp-macro (car (cdr (car p
))))
166 (def (car (cdr (cdr (car p
))))))
168 (if (eq (or temp-macro
169 (intern (concat "cc-bytecomp-ignore-fun:"
171 (symbol-function fun
))
172 (if (eq def
'unbound
)
175 (cc-bytecomp-debug-msg
176 "cc-bytecomp-restore-environment: Unbound function %s"
179 (cc-bytecomp-debug-msg
180 "cc-bytecomp-restore-environment: Restored function %s"
182 (cc-bytecomp-debug-msg
183 "cc-bytecomp-restore-environment: Not restoring function %s"
186 (setq p cc-bytecomp-original-properties
)
188 (let ((sym (car (car (car p
))))
189 (prop (cdr (car (car p
))))
190 (tempdef (car (cdr (car p
))))
191 (origdef (cdr (cdr (car p
)))))
192 (if (eq (get sym prop
) tempdef
)
194 (put sym prop origdef
)
195 (cc-bytecomp-debug-msg
196 "cc-bytecomp-restore-environment: Restored property %s for %s to %s"
198 (cc-bytecomp-debug-msg
199 "cc-bytecomp-restore-environment: Not restoring property %s for %s"
202 (setq cc-bytecomp-environment-set nil
)
203 (cc-bytecomp-debug-msg
204 "cc-bytecomp-restore-environment: Done"))))
207 ;; This eval is to avoid byte compilation of the function below.
208 ;; There's some bug in XEmacs 21.4.6 that can cause it to dump core
209 ;; here otherwise. My theory is that `cc-bytecomp-load' might be
210 ;; redefined recursively during the `load' inside it, and if it in
211 ;; that case is byte compiled then the byte interpreter gets
212 ;; confused. I haven't succeeded in isolating the bug, though. /mast
214 '(defun cc-bytecomp-load (cc-part)
215 ;; Eval'ed during compilation to load a CC Mode file from the source
216 ;; directory (assuming it's the same as the compiled file
218 (if (and (boundp 'byte-compile-dest-file
)
219 (stringp byte-compile-dest-file
))
221 (cc-bytecomp-restore-environment)
223 (cons (file-name-directory byte-compile-dest-file
)
225 (cc-file (concat cc-part
".el")))
226 (if (member cc-file cc-bytecomp-loaded-files
)
228 (setq cc-bytecomp-loaded-files
229 (cons cc-file cc-bytecomp-loaded-files
))
230 (cc-bytecomp-debug-msg
231 "cc-bytecomp-load: Loading %S" cc-file
)
232 (load cc-file nil t t
)
233 (cc-bytecomp-debug-msg
234 "cc-bytecomp-load: Loaded %S" cc-file
)))
235 (cc-bytecomp-setup-environment)
238 (defmacro cc-require
(cc-part)
239 "Force loading of the corresponding .el file in the current directory
240 during compilation, but compile in a `require'. Don't use within
243 Having cyclic cc-require's will result in infinite recursion. That's
244 somewhat intentional."
246 (eval-when-compile (cc-bytecomp-load (symbol-name ,cc-part
)))
249 (defmacro cc-provide
(feature)
250 "A replacement for the `provide' form that restores the environment
251 after the compilation. Don't use within `eval-when-compile'."
253 (eval-when-compile (cc-bytecomp-restore-environment))
256 (defmacro cc-load
(cc-part)
257 "Force loading of the corresponding .el file in the current directory
258 during compilation. Don't use outside `eval-when-compile' or
261 Having cyclic cc-load's will result in infinite recursion. That's
262 somewhat intentional."
263 `(or (and (featurep 'cc-bytecomp
)
264 (cc-bytecomp-load ,cc-part
))
265 (load ,cc-part nil t nil
)))
267 (defmacro cc-require-when-compile
(cc-part)
268 "Force loading of the corresponding .el file in the current directory
269 during compilation, but do a compile time `require' otherwise. Don't
270 use within `eval-when-compile'."
272 (if (and (featurep 'cc-bytecomp
)
273 (cc-bytecomp-is-compiling))
274 (if (or (not load-in-progress
)
275 (not (featurep ,cc-part
)))
276 (cc-bytecomp-load (symbol-name ,cc-part
)))
277 (require ,cc-part
))))
279 (defmacro cc-external-require
(feature)
280 "Do a `require' of an external package.
281 This restores and sets up the compilation environment before and
282 afterwards. Don't use within `eval-when-compile'."
284 (eval-when-compile (cc-bytecomp-restore-environment))
286 (eval-when-compile (cc-bytecomp-setup-environment))))
288 (defun cc-bytecomp-is-compiling ()
289 "Return non-nil if eval'ed during compilation. Don't use outside
290 `eval-when-compile'."
291 (and (boundp 'byte-compile-dest-file
)
292 (stringp byte-compile-dest-file
)))
294 (defmacro cc-bytecomp-defvar
(var)
295 "Binds the symbol as a variable during compilation of the file,
296 to silence the byte compiler. Don't use within `eval-when-compile'."
299 (cc-bytecomp-debug-msg
300 "cc-bytecomp-defvar: %s bound already as variable" ',var
)
301 (if (not (memq ',var cc-bytecomp-unbound-variables
))
303 (cc-bytecomp-debug-msg
304 "cc-bytecomp-defvar: Saving %s (as unbound)" ',var
)
305 (setq cc-bytecomp-unbound-variables
306 (cons ',var cc-bytecomp-unbound-variables
))))
307 (if (and (cc-bytecomp-is-compiling)
308 (not load-in-progress
))
311 (set ',var
(intern (concat "cc-bytecomp-ignore-var:"
312 (symbol-name ',var
))))
313 (cc-bytecomp-debug-msg
314 "cc-bytecomp-defvar: Covered variable %s" ',var
))))))
316 (defmacro cc-bytecomp-defun
(fun)
317 "Bind the symbol as a function during compilation of the file,
318 to silence the byte compiler. Don't use within `eval-when-compile'.
320 If the symbol already is bound as a function, it will keep that
321 definition. That means that this macro will not shut up warnings
322 about incorrect number of arguments. It's dangerous to try to replace
323 existing functions since the byte compiler might need the definition
324 at compile time, e.g. for macros and inline functions."
327 (cc-bytecomp-debug-msg
328 "cc-bytecomp-defun: %s bound already as function" ',fun
)
329 (if (not (assq ',fun cc-bytecomp-original-functions
))
331 (cc-bytecomp-debug-msg
332 "cc-bytecomp-defun: Saving %s (as unbound)" ',fun
)
333 (setq cc-bytecomp-original-functions
334 (cons (list ',fun nil
'unbound
)
335 cc-bytecomp-original-functions
))))
336 (if (and (cc-bytecomp-is-compiling)
337 (not load-in-progress
))
339 (fset ',fun
(intern (concat "cc-bytecomp-ignore-fun:"
340 (symbol-name ',fun
))))
341 (cc-bytecomp-debug-msg
342 "cc-bytecomp-defun: Covered function %s" ',fun
))))))
344 (put 'cc-bytecomp-defmacro
'lisp-indent-function
'defun
)
345 (defmacro cc-bytecomp-defmacro
(fun &rest temp-macro
)
346 "Bind the symbol as a macro during compilation (and evaluation) of the
347 file. Don't use outside `eval-when-compile'."
348 `(let ((orig-fun (assq ',fun cc-bytecomp-original-functions
)))
355 (cc-bytecomp-debug-msg
356 "cc-bytecomp-defmacro: Saving %s" ',fun
)
357 (symbol-function ',fun
))
358 (cc-bytecomp-debug-msg
359 "cc-bytecomp-defmacro: Saving %s as unbound" ',fun
)
361 cc-bytecomp-original-functions
362 (cons orig-fun cc-bytecomp-original-functions
)))
363 (defmacro ,fun
,@temp-macro
)
364 (cc-bytecomp-debug-msg
365 "cc-bytecomp-defmacro: Bound macro %s" ',fun
)
366 (setcar (cdr orig-fun
) (symbol-function ',fun
))))
368 (defmacro cc-bytecomp-put
(symbol propname value
)
369 "Set a property on a symbol during compilation (and evaluation) of
370 the file. Don't use outside `eval-when-compile'."
372 (if (not (assoc (cons ,symbol
,propname
) cc-bytecomp-original-properties
))
374 (cc-bytecomp-debug-msg
375 "cc-bytecomp-put: Saving property %s for %s with value %s"
376 ,propname
,symbol
(get ,symbol
,propname
))
377 (setq cc-bytecomp-original-properties
378 (cons (cons (cons ,symbol
,propname
)
379 (cons ,value
(get ,symbol
,propname
)))
380 cc-bytecomp-original-properties
))))
381 (put ,symbol
,propname
,value
)
382 (cc-bytecomp-debug-msg
383 "cc-bytecomp-put: Bound property %s for %s to %s"
384 ,propname
,symbol
,value
)))
386 (defmacro cc-bytecomp-obsolete-var
(symbol)
387 "Suppress warnings that the given symbol is an obsolete variable.
388 Don't use within `eval-when-compile'."
390 (if (get ',symbol
'byte-obsolete-variable
)
391 (cc-bytecomp-put ',symbol
'byte-obsolete-variable nil
)
392 ;; This avoids a superfluous compiler warning
393 ;; about calling `get' for effect.
396 (defun cc-bytecomp-ignore-obsolete (form)
397 ;; Wraps a call to `byte-compile-obsolete' that suppresses the warning.
398 (let ((byte-compile-warnings byte-compile-warnings
))
399 (byte-compile-disable-warning 'obsolete
)
400 (byte-compile-obsolete form
)))
402 (defmacro cc-bytecomp-obsolete-fun
(symbol)
403 "Suppress warnings that the given symbol is an obsolete function.
404 Don't use within `eval-when-compile'."
406 (if (eq (get ',symbol
'byte-compile
) 'byte-compile-obsolete
)
407 (cc-bytecomp-put ',symbol
'byte-compile
408 'cc-bytecomp-ignore-obsolete
)
409 ;; This avoids a superfluous compiler warning
410 ;; about calling `get' for effect.
413 (defmacro cc-bytecomp-boundp
(symbol)
414 "Return non-nil if the given symbol is bound as a variable outside
415 the compilation. This is the same as using `boundp' but additionally
416 exclude any variables that have been bound during compilation with
417 `cc-bytecomp-defvar'."
418 (if (and (cc-bytecomp-is-compiling)
419 (memq (car (cdr symbol
)) cc-bytecomp-unbound-variables
))
423 (defmacro cc-bytecomp-fboundp
(symbol)
424 "Return non-nil if the given symbol is bound as a function outside
425 the compilation. This is the same as using `fboundp' but additionally
426 exclude any functions that have been bound during compilation with
427 `cc-bytecomp-defun'."
429 (if (and (cc-bytecomp-is-compiling)
430 (setq fun-elem
(assq (car (cdr symbol
))
431 cc-bytecomp-original-functions
))
432 (eq (elt fun-elem
2) 'unbound
))
434 `(fboundp ,symbol
))))
437 (provide 'cc-bytecomp
)
439 ;;; arch-tag: 2d71b3ad-57b0-4b13-abd3-ab836e08f975
440 ;;; cc-bytecomp.el ends here