Re: Expanding macros within #error

Discussion in 'C Programming' started by Kaz Kylheku, Dec 22, 2011.

  1. Kaz Kylheku

    Kaz Kylheku Guest

    On 2011-12-22, Kenneth Brody <> 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.

    :)
     
    Kaz Kylheku, Dec 22, 2011
    #1
    1. Advertisements

  2. Kaz Kylheku

    Kaz Kylheku Guest

    On 2011-12-22, Kaz Kylheku <> wrote:
    > On 2011-12-22, Kenneth Brody <> 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.
     
    Kaz Kylheku, Dec 22, 2011
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QW5hbWlrYQ==?=

    URgent!!!!!!!!!! Macros in Excel give aan error

    =?Utf-8?B?QW5hbWlrYQ==?=, Apr 6, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    1,788
    Raymond Lewallen
    Apr 6, 2004
  2. Replies:
    80
    Views:
    3,118
    Stephen J. Bevan
    Nov 7, 2003
  3. Replies:
    1
    Views:
    664
    Marco Antoniotti
    Oct 7, 2003
  4. Replies:
    5
    Views:
    692
  5. Andrew Arro

    macros-loop? calling macros X times?

    Andrew Arro, Jul 23, 2004, in forum: C Programming
    Replies:
    2
    Views:
    628
    S.Tobias
    Jul 24, 2004
  6. Anthony de Almeida Lopes

    Macros within function-like macros?

    Anthony de Almeida Lopes, Dec 26, 2005, in forum: C Programming
    Replies:
    13
    Views:
    962
  7. Kenneth Brody

    __FILE__ and __LINE__ within macros

    Kenneth Brody, Apr 4, 2006, in forum: C Programming
    Replies:
    7
    Views:
    1,097
    Keith Thompson
    Apr 5, 2006
  8. Thant Tessman

    macros that build macros

    Thant Tessman, Sep 11, 2007, in forum: C Programming
    Replies:
    6
    Views:
    498
    Dave Hansen
    Sep 11, 2007
Loading...