]> code.delx.au - gnu-emacs/blobdiff - lisp/progmodes/cc-bytecomp.el
Merge branch 'master' of git.sv.gnu.org:/srv/git/emacs
[gnu-emacs] / lisp / progmodes / cc-bytecomp.el
index 2db5a1000504f319dc0f7235b29b0244717dfd48..81b7a822b82e94f8446e7fac7edf9fea2b38c71d 100644 (file)
@@ -1,6 +1,6 @@
 ;;; cc-bytecomp.el --- compile time setup for proper compilation
 
-;; Copyright (C) 2000-2014 Free Software Foundation, Inc.
+;; Copyright (C) 2000-2015 Free Software Foundation, Inc.
 
 ;; Author:     Martin Stjernholm
 ;; Maintainer: bug-cc-mode@gnu.org
   ;;`(message ,@args)
   )
 
+(defun cc-bytecomp-compiling-or-loading ()
+  ;; Determine whether byte-compilation or loading is currently active,
+  ;; returning 'compiling, 'loading or nil.
+  ;; If both are active, the "innermost" activity counts.  Note that
+  ;; compilation can trigger loading (various `require' type forms)
+  ;; and loading can trigger compilation (the package manager does
+  ;; this).  We walk the lisp stack if necessary.
+  (cond
+   ((and load-in-progress
+        (boundp 'byte-compile-dest-file)
+        (stringp byte-compile-dest-file))
+    (let ((n 0) elt)
+      (while (and
+             (setq elt (backtrace-frame n))
+             (not (and (car elt)
+                       (memq (cadr elt)
+                             '(load require
+                               byte-compile-file byte-recompile-directory
+                               batch-byte-compile)))))
+       (setq n (1+ n)))
+      (cond
+       ((memq (cadr elt) '(load require))
+       'loading)
+       ((memq (cadr elt) '(byte-compile-file
+                          byte-recompile-directory
+                          batch-byte-compile))
+       'compiling)
+       (t                              ; Can't happen.
+       (message "cc-bytecomp-compiling-or-loading: System flags spuriously set")
+       nil))))
+   (load-in-progress
+    ;; Being loaded.
+    'loading)
+   ((and (boundp 'byte-compile-dest-file)
+        (stringp byte-compile-dest-file))
+    ;; Being compiled.
+    'compiling)
+   (t
+    ;; Being evaluated interactively.
+    nil)))
+
+(defsubst cc-bytecomp-is-compiling ()
+  "Return non-nil if eval'ed during compilation."
+  (eq (cc-bytecomp-compiling-or-loading) 'compiling))
+
+(defsubst cc-bytecomp-is-loading ()
+  "Return non-nil if eval'ed during loading.
+Nil will be returned if we're in a compilation triggered by the loading."
+  (eq (cc-bytecomp-compiling-or-loading) 'loading))
+
 (defun cc-bytecomp-setup-environment ()
   ;; Eval'ed during compilation to setup variables, functions etc
   ;; declared with `cc-bytecomp-defvar' et al.
-  (if (not load-in-progress)
-      ;; Look at `load-in-progress' to tell whether we're called
-      ;; directly in the file being compiled or just from some file
-      ;; being loaded during compilation.
+  (if (not (cc-bytecomp-is-loading))
       (let (p)
        (if cc-bytecomp-environment-set
            (error "Byte compilation environment already set - \
@@ -143,7 +190,7 @@ perhaps a `cc-bytecomp-restore-environment' is forgotten somewhere"))
 (defun cc-bytecomp-restore-environment ()
   ;; Eval'ed during compilation to restore variables, functions etc
   ;; declared with `cc-bytecomp-defvar' et al.
-  (if (not load-in-progress)
+  (if (not (cc-bytecomp-is-loading))
       (let (p)
        (setq p cc-bytecomp-unbound-variables)
        (while p
@@ -287,8 +334,7 @@ use within `eval-when-compile'."
   `(eval-when-compile
      (if (and (fboundp 'cc-bytecomp-is-compiling)
              (cc-bytecomp-is-compiling))
-        (if (or (not load-in-progress)
-                (not (featurep ,cc-part)))
+        (if (not (featurep ,cc-part))
             (cc-bytecomp-load (symbol-name ,cc-part)))
        (require ,cc-part))))
 
@@ -301,12 +347,6 @@ afterwards.  Don't use within `eval-when-compile'."
      (require ,feature)
      (eval-when-compile (cc-bytecomp-setup-environment))))
 
-(defun cc-bytecomp-is-compiling ()
-  "Return non-nil if eval'ed during compilation.  Don't use outside
-`eval-when-compile'."
-  (and (boundp 'byte-compile-dest-file)
-       (stringp byte-compile-dest-file)))
-
 (defmacro cc-bytecomp-defvar (var)
   "Binds the symbol as a variable during compilation of the file,
 to silence the byte compiler.  Don't use within `eval-when-compile'."
@@ -320,8 +360,7 @@ to silence the byte compiler.  Don't use within `eval-when-compile'."
              "cc-bytecomp-defvar: Saving %s (as unbound)" ',var)
             (setq cc-bytecomp-unbound-variables
                   (cons ',var cc-bytecomp-unbound-variables))))
-       (if (and (cc-bytecomp-is-compiling)
-               (not load-in-progress))
+       (if (cc-bytecomp-is-compiling)
           (progn
             (defvar ,var)
             (set ',var (intern (concat "cc-bytecomp-ignore-var:"
@@ -349,8 +388,7 @@ at compile time, e.g. for macros and inline functions."
             (setq cc-bytecomp-original-functions
                   (cons (list ',fun nil 'unbound)
                         cc-bytecomp-original-functions))))
-       (if (and (cc-bytecomp-is-compiling)
-               (not load-in-progress))
+       (if (cc-bytecomp-is-compiling)
           (progn
             (fset ',fun (intern (concat "cc-bytecomp-ignore-fun:"
                                         (symbol-name ',fun))))
@@ -401,8 +439,8 @@ exclude any functions that have been bound during compilation with
 \f
 (provide 'cc-bytecomp)
 
-;;; Local Variables:
-;;; indent-tabs-mode: t
-;;; tab-width: 8
-;;; End:
+;; Local Variables:
+;; indent-tabs-mode: t
+;; tab-width: 8
+;; End:
 ;;; cc-bytecomp.el ends here