From: tim
Subject: Creating compilation errors in macros
Date: 
Message-ID: <13sjnthkoq7q412@corp.supernews.com>
Currently I am doing considerable code generation and one thing that
inhibits converting to macros is error handling. I would like to be
able to create compiler errors, warnings, and style-warnings that are
similar to those issued by the compiler. Looking though the hyperspec and
"On Lisp" there do not seem to be good ways. 

I can use error and warn. In sbcl, "error" aborts the compilation so I only
get one error message per compilation. "warn" seems to work and produces a
real warning. There seems to be no way to create a style warning that I
can see. Any suggestions?

Tim

From: Barry Margolin
Subject: Re: Creating compilation errors in macros
Date: 
Message-ID: <barmar-B04790.18333801032008@newsgroups.comcast.net>
In article <···············@corp.supernews.com>,
 tim <····@internet.com> wrote:

> Currently I am doing considerable code generation and one thing that
> inhibits converting to macros is error handling. I would like to be
> able to create compiler errors, warnings, and style-warnings that are
> similar to those issued by the compiler. Looking though the hyperspec and
> "On Lisp" there do not seem to be good ways. 
> 
> I can use error and warn. In sbcl, "error" aborts the compilation so I only
> get one error message per compilation. "warn" seems to work and produces a
> real warning. There seems to be no way to create a style warning that I
> can see. Any suggestions?
> 
> Tim

Have you tried using SIGNAL with a type of STYLE-WARNING?

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Kent M Pitman
Subject: Re: Creating compilation errors in macros
Date: 
Message-ID: <ur6et4wu0.fsf@nhplace.com>
Barry Margolin <······@alum.mit.edu> writes:

> In article <···············@corp.supernews.com>,
>  tim <····@internet.com> wrote:
> 
> > Currently I am doing considerable code generation and one thing that
> > inhibits converting to macros is error handling. I would like to be
> > able to create compiler errors, warnings, and style-warnings that are
> > similar to those issued by the compiler. Looking though the hyperspec and
> > "On Lisp" there do not seem to be good ways. 
> > 
> > I can use error and warn. In sbcl, "error" aborts the compilation so I only
> > get one error message per compilation. "warn" seems to work and produces a
> > real warning.

Only by default.  You can give WARN an instantiated condition or 
a condition type and args.  

> > There seems to be no way to create a style warning that I can see.
> > Any suggestions?

(define-condition simple-style-warning (style-warning simple-warning) ())
=> SIMPLE-STYLE-WARNING

(warn 'simple-style-warning :format-control "Foo: ~S" :format-arguments '(x))
Warning: Foo: X
=> NIL

(handler-bind ((style-warning #'muffle-warning))
  (warn 'simple-style-warning
        :format-control "Foo: ~S"
        :format-arguments '(x)))
=> NIL

> Have you tried using SIGNAL with a type of STYLE-WARNING?

Using SIGNAL bypasses the protocol you're promised for warnings, 
which is that MUFFLE-WARNING will be available. 

(handler-bind ((style-warning #'muffle-warning))
  (signal 'simple-style-warning
          :format-control "Foo: ~S"
          :format-arguments '(x)))
Error: MUFFLE-WARNING failed to find a restart.

You _could_ work around this by manually providing the restart, around
the signal, but that's the primary service WARN already does for you ...
so why not just call WARN?

You could also be more careful in your own handlers, but you'd still have
to worry that someone had handlers that you didn't write, and so that's
an incomplete fix.  By being more careful, I mean:

(handler-bind ((style-warning #'(lambda (condition)
                                  (let ((restart (find-restart 'muffle-warning
                                                               condition)))
                                    (when restart (invoke-restart restart))))))
  (signal 'simple-style-warning
          :format-control "Foo: ~S"
          :format-arguments '(x)))
=> NIL
From: tim
Subject: Re: Creating compilation errors in macros
Date: 
Message-ID: <13sltvb64qrvj9c@corp.supernews.com>
On Sat, 01 Mar 2008 22:40:55 -0500, Kent M Pitman wrote:

> Barry Margolin <······@alum.mit.edu> writes:
> 
>> In article <···············@corp.supernews.com>,
>>  tim <····@internet.com> wrote:
>> 
>> > Currently I am doing considerable code generation and one thing that
>> > inhibits converting to macros is error handling. I would like to be
>> > able to create compiler errors, warnings, and style-warnings that are
>> > similar to those issued by the compiler. Looking though the hyperspec and
>> > "On Lisp" there do not seem to be good ways. 
>> > 
>> > I can use error and warn. In sbcl, "error" aborts the compilation so I only
>> > get one error message per compilation. "warn" seems to work and produces a
>> > real warning.
> 
> Only by default.  You can give WARN an instantiated condition or 
> a condition type and args.  
> 
>> > There seems to be no way to create a style warning that I can see.
>> > Any suggestions?
> 
> (define-condition simple-style-warning (style-warning simple-warning) ())
> => SIMPLE-STYLE-WARNING
> 
> (warn 'simple-style-warning :format-control "Foo: ~S" :format-arguments '(x))
> Warning: Foo: X
> => NIL
> 
> (handler-bind ((style-warning #'muffle-warning))
>   (warn 'simple-style-warning
>         :format-control "Foo: ~S"
>         :format-arguments '(x)))
> => NIL
> 
>> Have you tried using SIGNAL with a type of STYLE-WARNING?
> 
> Using SIGNAL bypasses the protocol you're promised for warnings, 
> which is that MUFFLE-WARNING will be available. 
> 
> (handler-bind ((style-warning #'muffle-warning))
>   (signal 'simple-style-warning
>           :format-control "Foo: ~S"
>           :format-arguments '(x)))
> Error: MUFFLE-WARNING failed to find a restart.
> 
> You _could_ work around this by manually providing the restart, around
> the signal, but that's the primary service WARN already does for you ...
> so why not just call WARN?
> 
> You could also be more careful in your own handlers, but you'd still have
> to worry that someone had handlers that you didn't write, and so that's
> an incomplete fix.  By being more careful, I mean:
> 
> (handler-bind ((style-warning #'(lambda (condition)
>                                   (let ((restart (find-restart 'muffle-warning
>                                                                condition)))
>                                     (when restart (invoke-restart restart))))))
>   (signal 'simple-style-warning
>           :format-control "Foo: ~S"
>           :format-arguments '(x)))
> => NIL

Thank you, also the other people who replied. 

Tim
From: Juho Snellman
Subject: Re: Creating compilation errors in macros
Date: 
Message-ID: <871w6tdhgw.fsf@vasara.proghammer.com>
tim <····@internet.com> writes:
> Currently I am doing considerable code generation and one thing that
> inhibits converting to macros is error handling. I would like to be
> able to create compiler errors, warnings, and style-warnings that are
> similar to those issued by the compiler. Looking though the hyperspec and
> "On Lisp" there do not seem to be good ways. 
> 
> I can use error and warn. In sbcl, "error" aborts the compilation so I only
> get one error message per compilation.

That should not be the case in sbcl. If expanding a macro signals an
error, the compiler will handle it, report it as a compiler error, and
continue compiling things. The only "aborting" that's done is to
return T for failure-p in COMPILE-FILE, and signal a runtime error if
the form that couldn't be macroexpanded is executed.

  CL-USER> (defmacro foo (var) (error "foo ~A" var))
  FOO
  CL-USER> (defun bar (x) (when x (foo x)))
  ; in: LAMBDA NIL
  ;     (FOO X)
  ; 
  ; caught ERROR:
  ;   (in macroexpansion of (FOO X))
  ;   (hint: For more precise location, try *BREAK-ON-SIGNALS*.)
  ;   foo X
  ; 
  ; compilation unit finished
  ;   caught 1 ERROR condition
  BAR
  CL-USER> (bar nil)
  NIL
  CL-USER> (bar t)
    Execution of a form compiled with errors.
    Form:
      (FOO X)
    Compile-time error:
      (in macroexpansion of (FOO X))
    [etc]

If this is not working for you, please send a bug report with a test case
to the sbcl-devel mailing list.

-- 
Juho Snellman
From: tim
Subject: Re: Creating compilation errors in macros
Date: 
Message-ID: <13slu0u6scbj3b9@corp.supernews.com>
On Sun, 02 Mar 2008 03:47:59 +0200, Juho Snellman wrote:

> tim <····@internet.com> writes:
>> Currently I am doing considerable code generation and one thing that
>> inhibits converting to macros is error handling. I would like to be
>> able to create compiler errors, warnings, and style-warnings that are
>> similar to those issued by the compiler. Looking though the hyperspec and
>> "On Lisp" there do not seem to be good ways. 
>> 
>> I can use error and warn. In sbcl, "error" aborts the compilation so I only
>> get one error message per compilation.
> 
> That should not be the case in sbcl. If expanding a macro signals an
> error, the compiler will handle it, report it as a compiler error, and
> continue compiling things. The only "aborting" that's done is to
> return T for failure-p in COMPILE-FILE, and signal a runtime error if
> the form that couldn't be macroexpanded is executed.
> 
>   CL-USER> (defmacro foo (var) (error "foo ~A" var))
>   FOO
>   CL-USER> (defun bar (x) (when x (foo x)))
>   ; in: LAMBDA NIL
>   ;     (FOO X)
>   ; 
>   ; caught ERROR:
>   ;   (in macroexpansion of (FOO X))
>   ;   (hint: For more precise location, try *BREAK-ON-SIGNALS*.)
>   ;   foo X
>   ; 
>   ; compilation unit finished
>   ;   caught 1 ERROR condition
>   BAR
>   CL-USER> (bar nil)
>   NIL
>   CL-USER> (bar t)
>     Execution of a form compiled with errors.
>     Form:
>       (FOO X)
>     Compile-time error:
>       (in macroexpansion of (FOO X))
>     [etc]
> 
> If this is not working for you, please send a bug report with a test case
> to the sbcl-devel mailing list.
>

You are right. This happens when I "load" a file but not when I compile it.

Tim