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
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/>.
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:
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)
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
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.
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
)
92 (defun cc-bytecomp-compiling-or-loading ()
93 ;; Determine whether byte-compilation or loading is currently active,
94 ;; returning 'compiling, 'loading or nil.
95 ;; If both are active, the "innermost" activity counts. Note that
96 ;; compilation can trigger loading (various `require' type forms)
97 ;; and loading can trigger compilation (the package manager does
98 ;; this). We walk the lisp stack if necessary.
100 ((and load-in-progress
101 (boundp 'byte-compile-dest-file
)
102 (stringp byte-compile-dest-file
))
105 (setq elt
(backtrace-frame n
))
109 byte-compile-file byte-recompile-directory
110 batch-byte-compile
)))))
113 ((memq (cadr elt
) '(load require
))
115 ((memq (cadr elt
) '(byte-compile-file
116 byte-recompile-directory
120 (message "cc-bytecomp-compiling-or-loading: System flags spuriously set")
125 ((and (boundp 'byte-compile-dest-file
)
126 (stringp byte-compile-dest-file
))
130 ;; Being evaluated interactively.
133 (defsubst cc-bytecomp-is-compiling
()
134 "Return non-nil if eval'ed during compilation."
135 (eq (cc-bytecomp-compiling-or-loading) 'compiling
))
137 (defsubst cc-bytecomp-is-loading
()
138 "Return non-nil if eval'ed during loading.
139 Nil will be returned if we're in a compilation triggered by the loading."
140 (eq (cc-bytecomp-compiling-or-loading) 'loading
))
142 (defun cc-bytecomp-setup-environment ()
143 ;; Eval'ed during compilation to setup variables, functions etc
144 ;; declared with `cc-bytecomp-defvar' et al.
145 (if (not (cc-bytecomp-is-loading))
147 (if cc-bytecomp-environment-set
148 (error "Byte compilation environment already set - \
149 perhaps a `cc-bytecomp-restore-environment' is forgotten somewhere"))
150 (setq p cc-bytecomp-unbound-variables
)
152 (if (not (boundp (car p
)))
154 (eval `(defvar ,(car p
)))
155 (set (car p
) (intern (concat "cc-bytecomp-ignore-var:"
156 (symbol-name (car p
)))))
157 (cc-bytecomp-debug-msg
158 "cc-bytecomp-setup-environment: Covered variable %s"
161 (setq p cc-bytecomp-original-functions
)
163 (let ((fun (car (car p
)))
164 (temp-macro (car (cdr (car p
)))))
165 (if (not (fboundp fun
))
168 (eval `(defmacro ,fun
,@temp-macro
))
169 (cc-bytecomp-debug-msg
170 "cc-bytecomp-setup-environment: Bound macro %s" fun
))
171 (fset fun
(intern (concat "cc-bytecomp-ignore-fun:"
173 (cc-bytecomp-debug-msg
174 "cc-bytecomp-setup-environment: Covered function %s" fun
))))
176 (setq p cc-bytecomp-original-properties
)
178 (let ((sym (car (car (car p
))))
179 (prop (cdr (car (car p
))))
180 (tempdef (car (cdr (car p
)))))
181 (put sym prop tempdef
)
182 (cc-bytecomp-debug-msg
183 "cc-bytecomp-setup-environment: Bound property %s for %s to %s"
186 (setq cc-bytecomp-environment-set t
)
187 (cc-bytecomp-debug-msg
188 "cc-bytecomp-setup-environment: Done"))))
190 (defun cc-bytecomp-restore-environment ()
191 ;; Eval'ed during compilation to restore variables, functions etc
192 ;; declared with `cc-bytecomp-defvar' et al.
193 (if (not (cc-bytecomp-is-loading))
195 (setq p cc-bytecomp-unbound-variables
)
199 (if (eq (intern (concat "cc-bytecomp-ignore-var:"
204 (cc-bytecomp-debug-msg
205 "cc-bytecomp-restore-environment: Unbound variable %s"
207 (cc-bytecomp-debug-msg
208 "cc-bytecomp-restore-environment: Not restoring variable %s"
211 (setq p cc-bytecomp-original-functions
)
213 (let ((fun (car (car p
)))
214 (temp-macro (car (cdr (car p
))))
215 (def (car (cdr (cdr (car p
))))))
217 (if (eq (or temp-macro
218 (intern (concat "cc-bytecomp-ignore-fun:"
220 (symbol-function fun
))
221 (if (eq def
'unbound
)
224 (cc-bytecomp-debug-msg
225 "cc-bytecomp-restore-environment: Unbound function %s"
228 (cc-bytecomp-debug-msg
229 "cc-bytecomp-restore-environment: Restored function %s"
231 (cc-bytecomp-debug-msg
232 "cc-bytecomp-restore-environment: Not restoring function %s"
235 (setq p cc-bytecomp-original-properties
)
237 (let ((sym (car (car (car p
))))
238 (prop (cdr (car (car p
))))
239 (tempdef (car (cdr (car p
))))
240 (origdef (cdr (cdr (car p
)))))
241 (if (eq (get sym prop
) tempdef
)
243 (put sym prop origdef
)
244 (cc-bytecomp-debug-msg
245 "cc-bytecomp-restore-environment: Restored property %s for %s to %s"
247 (cc-bytecomp-debug-msg
248 "cc-bytecomp-restore-environment: Not restoring property %s for %s"
251 (setq cc-bytecomp-environment-set nil
)
252 (cc-bytecomp-debug-msg
253 "cc-bytecomp-restore-environment: Done"))))
256 ;; This eval is to avoid byte compilation of the function below.
257 ;; There's some bug in XEmacs 21.4.6 that can cause it to dump core
258 ;; here otherwise. My theory is that `cc-bytecomp-load' might be
259 ;; redefined recursively during the `load' inside it, and if it in
260 ;; that case is byte compiled then the byte interpreter gets
261 ;; confused. I haven't succeeded in isolating the bug, though. /mast
263 '(defun cc-bytecomp-load (cc-part)
264 ;; Eval'ed during compilation to load a CC Mode file from the source
265 ;; directory (assuming it's the same as the compiled file
267 (if (and (boundp 'byte-compile-dest-file
)
268 (stringp byte-compile-dest-file
))
270 (cc-bytecomp-restore-environment)
272 (cons (file-name-directory byte-compile-dest-file
)
274 (cc-file (concat cc-part
".el")))
275 (if (member cc-file cc-bytecomp-loaded-files
)
277 (setq cc-bytecomp-loaded-files
278 (cons cc-file cc-bytecomp-loaded-files
))
279 (cc-bytecomp-debug-msg
280 "cc-bytecomp-load: Loading %S" cc-file
)
281 (load cc-file nil t t
)
282 (cc-bytecomp-debug-msg
283 "cc-bytecomp-load: Loaded %S" cc-file
)))
284 (cc-bytecomp-setup-environment)
287 (defvar cc-bytecomp-noruntime-functions nil
288 "Saved value of `byte-compile-noruntime-functions'.")
290 (defmacro cc-require
(cc-part)
291 "Force loading of the corresponding .el file in the current directory
292 during compilation, but compile in a `require'. Don't use within
295 Having cyclic cc-require's will result in infinite recursion. That's
296 somewhat intentional."
299 (if (boundp 'byte-compile-noruntime-functions
) ; in case load uncompiled
300 (setq cc-bytecomp-noruntime-functions
301 byte-compile-noruntime-functions
))
302 (cc-bytecomp-load (symbol-name ,cc-part
)))
303 ;; Hack to suppress spurious "might not be defined at runtime" warnings.
304 ;; The basic issue is that
305 ;; (eval-when-compile (require 'foo))
307 ;; produces bogus noruntime warnings about functions from foo.
309 (setq byte-compile-noruntime-functions cc-bytecomp-noruntime-functions
))
312 (defmacro cc-provide
(feature)
313 "A replacement for the `provide' form that restores the environment
314 after the compilation. Don't use within `eval-when-compile'."
316 (eval-when-compile (cc-bytecomp-restore-environment))
319 (defmacro cc-load
(cc-part)
320 "Force loading of the corresponding .el file in the current directory
321 during compilation. Don't use outside `eval-when-compile' or
324 Having cyclic cc-load's will result in infinite recursion. That's
325 somewhat intentional."
326 `(or (and (featurep 'cc-bytecomp
)
327 (cc-bytecomp-load ,cc-part
))
328 (load ,cc-part nil t nil
)))
330 (defmacro cc-require-when-compile
(cc-part)
331 "Force loading of the corresponding .el file in the current directory
332 during compilation, but do a compile time `require' otherwise. Don't
333 use within `eval-when-compile'."
335 (if (and (fboundp 'cc-bytecomp-is-compiling
)
336 (cc-bytecomp-is-compiling))
337 (if (not (featurep ,cc-part
))
338 (cc-bytecomp-load (symbol-name ,cc-part
)))
339 (require ,cc-part
))))
341 (defmacro cc-external-require
(feature)
342 "Do a `require' of an external package.
343 This restores and sets up the compilation environment before and
344 afterwards. Don't use within `eval-when-compile'."
346 (eval-when-compile (cc-bytecomp-restore-environment))
348 (eval-when-compile (cc-bytecomp-setup-environment))))
350 (defmacro cc-bytecomp-defvar
(var)
351 "Binds the symbol as a variable during compilation of the file,
352 to silence the byte compiler. Don't use within `eval-when-compile'."
355 (cc-bytecomp-debug-msg
356 "cc-bytecomp-defvar: %s bound already as variable" ',var
)
357 (if (not (memq ',var cc-bytecomp-unbound-variables
))
359 (cc-bytecomp-debug-msg
360 "cc-bytecomp-defvar: Saving %s (as unbound)" ',var
)
361 (setq cc-bytecomp-unbound-variables
362 (cons ',var cc-bytecomp-unbound-variables
))))
363 (if (cc-bytecomp-is-compiling)
366 (set ',var
(intern (concat "cc-bytecomp-ignore-var:"
367 (symbol-name ',var
))))
368 (cc-bytecomp-debug-msg
369 "cc-bytecomp-defvar: Covered variable %s" ',var
))))))
371 (defmacro cc-bytecomp-defun
(fun)
372 "Bind the symbol as a function during compilation of the file,
373 to silence the byte compiler. Don't use within `eval-when-compile'.
375 If the symbol already is bound as a function, it will keep that
376 definition. That means that this macro will not shut up warnings
377 about incorrect number of arguments. It's dangerous to try to replace
378 existing functions since the byte compiler might need the definition
379 at compile time, e.g. for macros and inline functions."
382 (cc-bytecomp-debug-msg
383 "cc-bytecomp-defun: %s bound already as function" ',fun
)
384 (if (not (assq ',fun cc-bytecomp-original-functions
))
386 (cc-bytecomp-debug-msg
387 "cc-bytecomp-defun: Saving %s (as unbound)" ',fun
)
388 (setq cc-bytecomp-original-functions
389 (cons (list ',fun nil
'unbound
)
390 cc-bytecomp-original-functions
))))
391 (if (cc-bytecomp-is-compiling)
393 (fset ',fun
(intern (concat "cc-bytecomp-ignore-fun:"
394 (symbol-name ',fun
))))
395 (cc-bytecomp-debug-msg
396 "cc-bytecomp-defun: Covered function %s" ',fun
))))))
398 (defmacro cc-bytecomp-put
(symbol propname value
)
399 "Set a property on a symbol during compilation (and evaluation) of
400 the file. Don't use outside `eval-when-compile'."
402 (if (not (assoc (cons ,symbol
,propname
) cc-bytecomp-original-properties
))
404 (cc-bytecomp-debug-msg
405 "cc-bytecomp-put: Saving property %s for %s with value %s"
406 ,propname
,symbol
(get ,symbol
,propname
))
407 (setq cc-bytecomp-original-properties
408 (cons (cons (cons ,symbol
,propname
)
409 (cons ,value
(get ,symbol
,propname
)))
410 cc-bytecomp-original-properties
))))
411 (put ,symbol
,propname
,value
)
412 (cc-bytecomp-debug-msg
413 "cc-bytecomp-put: Bound property %s for %s to %s"
414 ,propname
,symbol
,value
)))
416 (defmacro cc-bytecomp-boundp
(symbol)
417 "Return non-nil if the given symbol is bound as a variable outside
418 the compilation. This is the same as using `boundp' but additionally
419 exclude any variables that have been bound during compilation with
420 `cc-bytecomp-defvar'."
421 (if (and (cc-bytecomp-is-compiling)
422 (memq (car (cdr symbol
)) cc-bytecomp-unbound-variables
))
426 (defmacro cc-bytecomp-fboundp
(symbol)
427 "Return non-nil if the given symbol is bound as a function outside
428 the compilation. This is the same as using `fboundp' but additionally
429 exclude any functions that have been bound during compilation with
430 `cc-bytecomp-defun'."
432 (if (and (cc-bytecomp-is-compiling)
433 (setq fun-elem
(assq (car (cdr symbol
))
434 cc-bytecomp-original-functions
))
435 (eq (elt fun-elem
2) 'unbound
))
437 `(fboundp ,symbol
))))
440 (provide 'cc-bytecomp
)
443 ;;; indent-tabs-mode: t
446 ;;; cc-bytecomp.el ends here