Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I disable warnings in lisp (sbcl)

How do I disable all warnings in sbcl? The extra output is rather annoying.

like image 929
Stefan Kendall Avatar asked Mar 29 '10 02:03

Stefan Kendall


2 Answers

You can either use SB-EXT:MUFFLE-CONDITIONS as Pillsy said, the other alternative is to read through the warnings and use them to modify your code to remove the warnings. Especially if they're actually warnings (rather than, say, optimization notes).

like image 169
Vatine Avatar answered Sep 18 '22 12:09

Vatine


After much faffing about
and slogging through documentation written by people who are apparently allergic to simple concrete examples
(which seems to be most documentation for most things)
I think all you need to do to disable all warnings
is add this line in your .sbclrc file:

(declaim (sb-ext:muffle-conditions cl:warning))

To disable only style-warnings, it's:

(declaim (sb-ext:muffle-conditions cl:style-warning))

I tried to disable specifically the warning that comes up if you enter eg (setq x 1) at a fresh REPL

; in: SETQ X
;     (SETQ X 1)
; 
; caught WARNING:
;   undefined variable: X
; 
; compilation unit finished
;   Undefined variable:
;     X
;   caught 1 WARNING condition

By using this:

(declaim (sb-ext:muffle-conditions sb-kernel:redefinition-warning))

but it didn't work,
(apparently redefinition-warning means something else)
and I can't find what it should be.
I guessed sb-kernel:undefined-warning
but that doesn't exist.

Using a macro

Also,
in regards @Bogatyr's answer
(using a macro to automatically run defvar)
and @spacebat's comment
(that the macro evaluated the value twice)
I have this to say:

As another newb coming across this,
I wanted to make demo showing that the macro evals twice,
and showing a version that evaluates only once.

(
I originally edited it in at the end of the question
but it was rejected because:
"This edit was intended to address the author of the post and makes no sense as an edit. It should have been written as a comment or an answer."

Well, you can't answer an answer,
but comments can't take blocks of code,
so I guess I should put it here instead?
)

original

(defmacro sq (var value)
  `(progn
      (defvar ,var ,value)
      (setq ,var ,value)))

    (sq v (princ "hi"))
  • side-effects: prints hihi
  • return value: "hi"

rewrite 2 - only evals once, always runs defvar

(defmacro sq2 (var value)
 (let
   ((value-to-set value))
   `(progn
      (defvar ,var)
      (setq ,var ,value-to-set))))

    (sq2 v (princ "hi"))
  • side-effects: prints hi
  • return value: "hi"

rewrite 3 - same as above, but trickier to read

I used value-to-set for clarity,
but you could just use value again with no problems:

(defmacro sq3 (var value)
 (let
   ((value value))
   `(progn
      (defvar ,var)
      (setq ,var ,value))))

    (sq3 v (princ "hi"))

rewrite 4 - only runs defvar if the variable is unbound

Running those macros will always define the variable before setting it,
so if v was already "bound" but not "defined"
(ie you had introduced it with setq)
then won't get any more error messages when you use the variable,
or reset it with setq.

Here's a version of the macro
that only runs defvar if the variable is not already bound:

(defmacro sq4 (var value)
  (let
    ((value-to-set value))
    (if (boundp var)
        `(setq ,var ,value-to-set)
        `(progn
           (defvar ,var)
           (setq ,var ,value-to-set)))))

    (sq4 v (princ "hi"))

So if you use it to set a variable that is bound but not defined
it will keep giving you error messages.
(Which is maybe a good thing?
Like, for the same reason-I-don't-actually-know-why the error message exists in the first place.)

[
Also,
I tested the macro on these:

(sq4 value           1              )
(sq4 value           'value         )
(sq4 value           'value-to-set  )
(sq4 value           'var           )
(sq4 value-to-set    1              )
(sq4 value-to-set    'value         )
(sq4 value-to-set    'value-to-set  )
(sq4 value-to-set    'var           )
(sq4 var             1              )
(sq4 var            'value          )
(sq4 var            'value-to-set   )
(sq4 var            'var            )

(You know, checking I hadn't screwed up and... done something weird.)

The ones where I tried to use var as a variable spewed errors.

At first I thought I had messed something up,
but it's actually just reserved for something special in SBCL(?) itself.

(defvar var) gets:

; debugger invoked on a SYMBOL-PACKAGE-LOCKED-ERROR in thread
; #<THREAD "main thread" RUNNING {AB5D0A1}>:
;   Lock on package SB-DEBUG violated when globally declaring VAR SPECIAL while
;   in package COMMON-LISP-USER.
; See also:
;   The SBCL Manual, Node "Package Locks"

So... when in doubt, avoid using the symbol var, I guess.
]

like image 26
Owen_AR Avatar answered Sep 18 '22 12:09

Owen_AR