]> code.delx.au - gnu-emacs/blobdiff - lisp/progmodes/cc-bytecomp.el
Merge branch 'emacs-25-merge'
[gnu-emacs] / lisp / progmodes / cc-bytecomp.el
index e02a3cf89cc73492ac1ca1cae638e132ca3c7a01..d3b4db74c160b917171b5bb1ab3d128e95fbc31e 100644 (file)
@@ -65,8 +65,7 @@
 ;; elsewhere in the load path.
 ;;
 ;; To suppress byte compiler warnings, use the macros
-;; `cc-bytecomp-defun', `cc-bytecomp-defvar',
-;; `cc-bytecomp-obsolete-fun', and `cc-bytecomp-obsolete-var'.
+;; `cc-bytecomp-defun' and `cc-bytecomp-defvar'.
 ;;
 ;; This file is not used at all after the package has been byte
 ;; compiled.  It is however necessary when running uncompiled.
 (defvar cc-bytecomp-original-functions nil)
 (defvar cc-bytecomp-original-properties nil)
 (defvar cc-bytecomp-loaded-files nil)
+
+(setq cc-bytecomp-unbound-variables nil)
+(setq cc-bytecomp-original-functions nil)
+(setq cc-bytecomp-original-properties nil)
+(setq cc-bytecomp-loaded-files nil)
+
 (defvar cc-bytecomp-environment-set nil)
 
 (defmacro cc-bytecomp-debug-msg (&rest args)
   ;;`(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 - \
@@ -138,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
@@ -200,6 +252,11 @@ perhaps a `cc-bytecomp-restore-environment' is forgotten somewhere"))
        (cc-bytecomp-debug-msg
         "cc-bytecomp-restore-environment: Done"))))
 
+(defun cc-bytecomp-load (cc-part)
+  ;; A dummy function which will immediately be overwritten by the
+  ;; following at load time.  This should suppress the byte compiler
+  ;; error that the function is "not known to be defined".
+)
 (eval
  ;; This eval is to avoid byte compilation of the function below.
  ;; There's some bug in XEmacs 21.4.6 that can cause it to dump core
@@ -232,9 +289,6 @@ perhaps a `cc-bytecomp-restore-environment' is forgotten somewhere"))
          (cc-bytecomp-setup-environment)
          t))))
 
-(defvar cc-bytecomp-noruntime-functions nil
-  "Saved value of `byte-compile-noruntime-functions'.")
-
 (defmacro cc-require (cc-part)
   "Force loading of the corresponding .el file in the current directory
 during compilation, but compile in a `require'.  Don't use within
@@ -244,19 +298,37 @@ Having cyclic cc-require's will result in infinite recursion.  That's
 somewhat intentional."
   `(progn
      (eval-when-compile
-       (if (boundp 'byte-compile-noruntime-functions) ; in case load uncompiled
-          (setq cc-bytecomp-noruntime-functions
-                byte-compile-noruntime-functions))
        (cc-bytecomp-load (symbol-name ,cc-part)))
-     ;; Hack to suppress spurious "might not be defined at runtime" warnings.
-     ;; The basic issue is that
-     ;;   (eval-when-compile (require 'foo))
-     ;;   (require 'foo)
-     ;; produces bogus noruntime warnings about functions from foo.
-     (eval-when-compile
-       (setq byte-compile-noruntime-functions cc-bytecomp-noruntime-functions))
      (require ,cc-part)))
 
+(defmacro cc-conditional-require (cc-part condition)
+  "If the CONDITION is satisfied at compile time, (i) force the
+file CC-PART.el in the current directory to be loaded at compile
+time, (ii) generate code to load the file at load time.
+
+CC-PART will normally be a quoted name such as 'cc-fix.
+CONDITION should not be quoted."
+  (if (eval condition)
+      (progn
+       (cc-bytecomp-load (symbol-name (eval cc-part)))
+       `(require ,cc-part))
+    '(progn)))
+
+(defmacro cc-conditional-require-after-load (cc-part file condition)
+  "If the CONDITION is satisfied at compile time, (i) force the
+file CC-PART.el in the current directory to be loaded at compile
+time, (ii) generate an `eval-after-load' form to load CC-PART.el
+after the loading of FILE.
+
+CC-PART will normally be a quoted name such as 'cc-fix.  FILE
+should be a string.  CONDITION should not be quoted."
+  (if (eval condition)
+      (progn
+       (cc-bytecomp-load (symbol-name (eval cc-part)))
+       `(eval-after-load ,file
+          '(require ,cc-part)))
+    '(progn)))
+
 (defmacro cc-provide (feature)
   "A replacement for the `provide' form that restores the environment
 after the compilation.  Don't use within `eval-when-compile'."
@@ -282,8 +354,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))))
 
@@ -296,12 +367,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'."
@@ -315,8 +380,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:"
@@ -344,8 +408,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))))
@@ -370,33 +433,6 @@ the file.  Don't use outside `eval-when-compile'."
       "cc-bytecomp-put: Bound property %s for %s to %s"
       ,propname ,symbol ,value)))
 
-(defmacro cc-bytecomp-obsolete-var (symbol)
-  "Suppress warnings that the given symbol is an obsolete variable.
-Don't use within `eval-when-compile'."
-  `(eval-when-compile
-     (if (get ',symbol 'byte-obsolete-variable)
-        (cc-bytecomp-put ',symbol 'byte-obsolete-variable nil)
-       ;; This avoids a superfluous compiler warning
-       ;; about calling `get' for effect.
-       t)))
-
-(defun cc-bytecomp-ignore-obsolete (form)
-  ;; Wraps a call to `byte-compile-obsolete' that suppresses the warning.
-  (let ((byte-compile-warnings byte-compile-warnings))
-    (byte-compile-disable-warning 'obsolete)
-    (byte-compile-obsolete form)))
-
-(defmacro cc-bytecomp-obsolete-fun (symbol)
-  "Suppress warnings that the given symbol is an obsolete function.
-Don't use within `eval-when-compile'."
-  `(eval-when-compile
-     (if (eq (get ',symbol 'byte-compile) 'byte-compile-obsolete)
-        (cc-bytecomp-put ',symbol 'byte-compile
-                         'cc-bytecomp-ignore-obsolete)
-       ;; This avoids a superfluous compiler warning
-       ;; about calling `get' for effect.
-       t)))
-
 (defmacro cc-bytecomp-boundp (symbol)
   "Return non-nil if the given symbol is bound as a variable outside
 the compilation.  This is the same as using `boundp' but additionally
@@ -423,4 +459,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:
 ;;; cc-bytecomp.el ends here