]> code.delx.au - gnu-emacs/blobdiff - lisp/emacs-lisp/unsafep.el
* lisp/emacs-lisp/pcase.el (pcase--if): Try to invert test to reduce depth.
[gnu-emacs] / lisp / emacs-lisp / unsafep.el
index 197728d2327eb32fdd977c83da2b582010fb1d73..0f08d77d4c39a3b7e46368513efb6b814394c065 100644 (file)
@@ -1,6 +1,6 @@
 ;;;; unsafep.el -- Determine whether a Lisp form is safe to evaluate
 
-;; Copyright (C)2002 Free Software Foundation, Inc.
+;; Copyright (C) 2002-2011 Free Software Foundation, Inc.
 
 ;; Author: Jonathan Yavner <jyavner@member.fsf.org>
 ;; Maintainer: Jonathan Yavner <jyavner@member.fsf.org>
@@ -8,10 +8,10 @@
 
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -19,9 +19,7 @@
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; Commentary:
 
 ;;      passed a circular list to `assoc', Emacs would crash.  Historically,
 ;;      problems of this kind have been few and short-lived.
 
+;;; Code:
+
 (provide 'unsafep)
 (require 'byte-opt)  ;Set up the `side-effect-free' properties
 
 (defcustom safe-functions nil
-  "t to disable `unsafep', or a list of assumed-safe functions."
+  "A list of assumed-safe functions, or t to disable `unsafep'."
   :group 'lisp
   :type  '(choice (const :tag "No" nil) (const :tag "Yes" t) hook))
 
@@ -101,22 +101,20 @@ in the parse.")
 (dolist (x '(;;Special forms
             and catch if or prog1 prog2 progn while unwind-protect
             ;;Safe subrs that have some side-effects
-            ding error message minibuffer-message random read-minibuffer
-            signal sleep-for string-match throw y-or-n-p yes-or-no-p
+            ding error random signal sleep-for string-match throw
             ;;Defsubst functions from subr.el
             caar cadr cdar cddr
             ;;Macros from subr.el
-            save-match-data unless when with-temp-message
+            save-match-data unless when
             ;;Functions from subr.el that have side effects
-            read-passwd split-string replace-regexp-in-string
-            play-sound-file))
+            split-string replace-regexp-in-string play-sound-file))
   (put x 'safe-function t))
 
 ;;;###autoload
 (defun unsafep (form &optional unsafep-vars)
-  "Return nil if evaluating FORM couldn't possibly do any harm;
-otherwise result is a reason why FORM is unsafe.  UNSAFEP-VARS is a list
-of symbols with local bindings."
+  "Return nil if evaluating FORM couldn't possibly do any harm.
+Otherwise result is a reason why FORM is unsafe.
+UNSAFEP-VARS is a list of symbols with local bindings."
   (catch 'unsafep
     (if (or (eq safe-functions t)          ;User turned off safety-checking
            (atom form))                    ;Atoms are never unsafe
@@ -146,10 +144,10 @@ of symbols with local bindings."
        ((eq fun 'lambda)
        ;;First arg is temporary bindings
        (mapc #'(lambda (x)
-                 (let ((y (unsafep-variable x t)))
-                   (if y (throw 'unsafep y)))
                  (or (memq x '(&optional &rest))
-                     (push x unsafep-vars)))
+                     (let ((y (unsafep-variable x t)))
+                       (if y (throw 'unsafep y))
+                       (push x unsafep-vars))))
              (cadr form))
        (unsafep-progn (cddr form)))
        ((eq fun 'let)
@@ -204,15 +202,18 @@ of symbols with local bindings."
              (dolist (x (nthcdr 3 form))
                (setq reason (unsafep-progn (cdr x)))
                (if reason (throw 'unsafep reason))))))
+       ((eq fun '\`)
+       ;; Backquoted form - safe if its expansion is.
+       (unsafep (cdr (backquote-process (cadr form)))))
        (t
        ;;First unsafep-function call above wasn't nil, no special case applies
        reason)))))
 
 
 (defun unsafep-function (fun)
-  "Return nil if FUN is a safe function
-\(either a safe lambda or a symbol that names a safe function).  Otherwise
-result is a reason code."
+  "Return nil if FUN is a safe function.
+\(Either a safe lambda or a symbol that names a safe function).
+Otherwise result is a reason code."
   (cond
    ((eq (car-safe fun) 'lambda)
     (unsafep fun unsafep-vars))
@@ -224,8 +225,8 @@ result is a reason code."
     `(function ,fun))))
 
 (defun unsafep-progn (list)
-  "Return nil if all forms in LIST are safe, or the reason
-for the first unsafe form."
+  "Return nil if all forms in LIST are safe.
+Else, return the reason for the first unsafe form."
   (catch 'unsafep-progn
     (let (reason)
       (dolist (x list)
@@ -233,8 +234,10 @@ for the first unsafe form."
        (if reason (throw 'unsafep-progn reason))))))
 
 (defun unsafep-let (clause)
-  "CLAUSE is a let-binding, either SYM or (SYM) or (SYM VAL).  Checks VAL
-and throws a reason to `unsafep' if unsafe.  Returns SYM."
+  "Check the safety of a let binding.
+CLAUSE is a let-binding, either SYM or (SYM) or (SYM VAL).
+Check VAL and throw a reason to `unsafep' if unsafe.
+Return SYM."
   (let (reason sym)
     (if (atom clause)
        (setq sym clause)
@@ -244,20 +247,18 @@ and throws a reason to `unsafep' if unsafe.  Returns SYM."
     (if reason (throw 'unsafep reason))
     sym))
 
-(defun unsafep-variable (sym global-okay)
-  "Returns nil if SYM is safe as a let-binding sym
-\(because it already has a temporary binding or is a non-risky buffer-local
-variable), otherwise a reason why it is unsafe.  Failing to be locally bound
-is okay if GLOBAL-OKAY is non-nil."
+(defun unsafep-variable (sym to-bind)
+  "Return nil if SYM is safe to set or bind, or a reason why not.
+If TO-BIND is nil, check whether SYM is safe to set.
+If TO-BIND is t, check whether SYM is safe to bind."
   (cond
    ((not (symbolp sym))
     `(variable ,sym))
    ((risky-local-variable-p sym nil)
     `(risky-local-variable ,sym))
-   ((not (or global-okay
+   ((not (or to-bind
             (memq sym unsafep-vars)
             (local-variable-p sym)))
     `(global-variable ,sym))))
 
-;;; arch-tag: 6216f98b-eb8f-467a-9c33-7a7644f50658
-;; unsafep.el ends here.
+;;; unsafep.el ends here