How do I disable warnings in lisp (sbcl)
Asked Answered
B

8

21

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

Brush answered 29/3, 2010 at 2:25 Comment(0)
S
11

this is what i use to muffle both compile-time and runtime (load-time) redefinition warnings:

(locally
    (declare #+sbcl(sb-ext:muffle-conditions sb-kernel:redefinition-warning))
  (handler-bind
      (#+sbcl(sb-kernel:redefinition-warning #'muffle-warning))
    ;; stuff that emits redefinition-warning's
    ))

following this pattern you can install these handlers on superclasses like cl:style-warning to muffle all style warnings.

Savick answered 31/3, 2010 at 18:34 Comment(2)
Is that supposed to work if I just add it to the end of my .sbclrc file? Cuz I did that, started the REPL from the command line, entered (setq x 1), and it still spewed out a warning... (; in: SETQ X; (SETQ X 1); ; caught WARNING:; undefined variable: X; ; compilation unit finished; Undefined variable:; X; caught 1 WARNING condition1* )Inflexed
read more carefully. (locally (declare #+sbcl(sb-ext:muffle-conditions cl:warning)) (handler-bind (#+sbcl(cl:warning #'muffle-warning)) (setq x 1)))Savick
I
12

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.
]

Inflexed answered 4/5, 2016 at 17:22 Comment(2)
Its been a while but I just took an interest again when I noticed this. I agree that its useful to have this for mucking about in the REPL. Here's a version that performs the boundedness check at evaluation time, not macroexpansion time, and supports setting documentation too: gist.github.com/4ea0ae200c4cb2ce7a8e4a7fd5f4029aLinnea
The code here has double evaluation problems. Might want to fix it up.Seigel
S
11

this is what i use to muffle both compile-time and runtime (load-time) redefinition warnings:

(locally
    (declare #+sbcl(sb-ext:muffle-conditions sb-kernel:redefinition-warning))
  (handler-bind
      (#+sbcl(sb-kernel:redefinition-warning #'muffle-warning))
    ;; stuff that emits redefinition-warning's
    ))

following this pattern you can install these handlers on superclasses like cl:style-warning to muffle all style warnings.

Savick answered 31/3, 2010 at 18:34 Comment(2)
Is that supposed to work if I just add it to the end of my .sbclrc file? Cuz I did that, started the REPL from the command line, entered (setq x 1), and it still spewed out a warning... (; in: SETQ X; (SETQ X 1); ; caught WARNING:; undefined variable: X; ; compilation unit finished; Undefined variable:; X; caught 1 WARNING condition1* )Inflexed
read more carefully. (locally (declare #+sbcl(sb-ext:muffle-conditions cl:warning)) (handler-bind (#+sbcl(cl:warning #'muffle-warning)) (setq x 1)))Savick
B
10

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).

Bowery answered 29/3, 2010 at 7:37 Comment(0)
T
4

I couldn't get SB-EXT:MUFFLE-CONDITIONS to work for the highly annoying undefined variable warning even after much googling. That warning drives me nuts when experimenting at the REPL, so I did what all the books suggest we should do: extend lisp to suit my needs/preferences!

I wrote my own setq that shut up the sbcl warnings, my first macro ever :). I'm sure there are better ways to do it but this works great for me, and it's going right into my ~/.sbclrc!

(defmacro sq (var value)
  `(progn
      (defvar ,var ,value)
      (setq ,var ,value)))
Tandem answered 3/10, 2011 at 11:42 Comment(7)
I usually just define the variables before using them, either with defvar ((defvar x 3) not being much longer than (setq x 3)) or let.Skirr
For programs it's not an issue. For REPL experiments, predefining variables to silence the warning is a major PITA.Tandem
Just so you know, you're evaluating value twice which is poor form. See alexandria:once-onlyLinnea
@Linnea As another newb coming across this, I wrote a quick demo showing that the macro evals twice, and showing a version of the macro that evaluates only once. I edited it in at the end of the question (although the edit is prolly still "awaiting peer review").Inflexed
This question although prevents the warnings from happening introduces the severe side effect of double evaluation. It does not silence other warnings either. Don't use this in serious code.Seigel
Jargon: 'REPL' = Read-eval-print loop; I'll let you figure out 'PITA' yourself.Freedwoman
Instead of defining a custom macro, you might also use defparameter that declares a variable with a given value, and that can be reevaluated with a different variable value later (in contrast to defvar).Blockhouse
F
2

You probably want to look at SB-EXT:MUFFLE-CONDITIONS.

Feria answered 29/3, 2010 at 2:28 Comment(2)
Where are the different conditions which can be muffled? I want to muffle all style warnings, and the this documentation is weak.Brush
That's getting a 404 now.Inflexed
S
2

If warnings are all you care about you can set:

(setf sb-ext:*muffled-warnings* 'style-warning)

This will only apply to style warnings and allow other warnings and conditions to print out. Any warning that shares the same parent will be automatically muffled.

Seigel answered 5/9, 2017 at 4:53 Comment(0)
O
0

For me (and probably others), most of the warnings were actually being piped to stdErr.
So this silenced the annoying output:
sbcl 2>/dev/null/

Alternatively, you can pipe to a file.
sbcl 2>myTempLog.txt

Organize answered 11/3, 2019 at 2:54 Comment(0)
A
0

When starting sbcl, the problem is that at least in my configuration, alexandria spews out a tonne of method warnings and redifining warnings because of asdf, alexandria and readline, regardless of the mute solutions.

User theoski's solutions (sbcl 2>/dev/null ...) totally work to get rid of those, but at the expense of warnings that might actually be useful.

Still, I always have a repl open in a terminal as a scratch for quick hacks and experimenting, and it's a LOT nicer not seeing that avalanche when loading it up.

Ambassadoratlarge answered 20/7, 2019 at 14:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.