Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Expanding macros within #error

Reply
Thread Tools

Re: Expanding macros within #error

 
 
Kaz Kylheku
Guest
Posts: n/a
 
      12-22-2011
On 2011-12-22, Kenneth Brody <(E-Mail Removed)> wrote:
> Is there any "trick", or is this just one of those "sorry, you can't do
> that" things?


I just thought of an ugly one:

#define str(x) #x
#define bad_symbol_value_error(x) str(macro_ ## x has bad value x)
#define foo 42
#include bad_symbol_value_error(foo)

$ gcc test.c
test.c:4: fatal error: macro_foo has bad value 42: No such file or directory
compilation terminated.


 
Reply With Quote
 
 
 
 
Kaz Kylheku
Guest
Posts: n/a
 
      12-22-2011
On 2011-12-22, Kaz Kylheku <(E-Mail Removed)> wrote:
> On 2011-12-22, Kenneth Brody <(E-Mail Removed)> wrote:
>> Is there any "trick", or is this just one of those "sorry, you can't do
>> that" things?

>
> I just thought of an ugly one:
>
> #define str(x) #x
> #define bad_symbol_value_error(x) str(macro_ ## x has bad value x)
> #define foo 42
> #include bad_symbol_value_error(foo)
>
> $ gcc test.c
> test.c:4: fatal error: macro_foo has bad value 42: No such file or directory
> compilation terminated.


Here is a variation on a theme: another way of forcing the preprocessor into
generating an error message which includes both "foo" and its expansion, if it
has one:

#define bad_value_ex(x, y) x ## y
#define bad_value(x) bad_value_ex(#x, x)
#define foo 42
bad_value(foo)

$ gcc test.c
test.c:4: error: pasting ""foo"" and "42" does not give a valid preprocessing token
test.c:4: error: expected identifier or ‘(’ before string constant

I think this is relying on undefined behavior. If I recall, bad token pastes do
not require a diagnostic, but I don't care enough to look it up.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macros within function-like macros? Anthony de Almeida Lopes C Programming 13 12-27-2005 08:38 PM
Explanation of macros; Haskell macros mike420@ziplip.com Python 80 11-07-2003 02:22 AM
Re: Explanation of macros; Haskell macros Michael T. Babcock Python 0 11-03-2003 01:54 PM
Re: Explanation of macros; Haskell macros mike420@ziplip.com Python 5 11-01-2003 01:09 AM
Re: Explanation of macros; Haskell macros mike420@ziplip.com Python 1 10-07-2003 04:07 PM



Advertisments