Macro re-entrancy problem

Discussion in 'C Programming' started by Lauri Alanko, Feb 4, 2011.

  1. Lauri Alanko

    Lauri Alanko Guest

    I encountered a somewhat exotic macro problem:


    #define FOO(x) BAR_##x
    #define BAR_z 42
    #define BAR_q FOO(z)
    #define BAZ(x) BAR_##x

    int main(void) {
    int a = FOO(z);
    int b = BAZ(q);
    int c = FOO(q); // line 9
    return 0;
    }

    $ gcc -o t t.c
    t.c: In function 'main':
    t.c:9: error: 'z' undeclared (first use in this function)
    t.c:9: error: (Each undeclared identifier is reported only once
    t.c:9: error: for each function it appears in.)

    The preprocessor output from gcc -E is:


    int main(void) {
    int a = 42;
    int b = 42;
    int c = FOO(z);
    return 0;
    }


    So it seems that a macro invocation cannot trigger the invocation of
    the same macro again, even indirectly, even if the expansion would
    ultimately terminate. Is this standard behavior, or a bug in gcc?

    Thanks,


    Lauri
    Lauri Alanko, Feb 4, 2011
    #1
    1. Advertising

  2. Lauri Alanko <> writes:

    > I encountered a somewhat exotic macro problem:
    >
    > #define FOO(x) BAR_##x
    > #define BAR_z 42
    > #define BAR_q FOO(z)
    > #define BAZ(x) BAR_##x
    >
    > int main(void) {
    > int a = FOO(z);
    > int b = BAZ(q);
    > int c = FOO(q); // line 9
    > return 0;
    > }
    >
    > $ gcc -o t t.c
    > t.c: In function 'main':
    > t.c:9: error: 'z' undeclared (first use in this function)
    > t.c:9: error: (Each undeclared identifier is reported only once
    > t.c:9: error: for each function it appears in.)
    >
    > The preprocessor output from gcc -E is:
    >
    > int main(void) {
    > int a = 42;
    > int b = 42;
    > int c = FOO(z);
    > return 0;
    > }
    >
    >
    > So it seems that a macro invocation cannot trigger the invocation of
    > the same macro again, even indirectly, even if the expansion would
    > ultimately terminate. Is this standard behavior, or a bug in gcc?


    It's standard. For chapter and verse, see 6.10.3.4 paragraph 2 of C99.

    The restriction applies to the macro text being expanded. When the
    macro expansion involves further "new" text from the source, the
    expansion will be made:

    #define M0(x) R1 M1
    #define M1(x) R2 M0

    M0(a)(b)(c)(d)

    expands to R1 R2 R1 R2 M0.

    --
    Ben.
    Ben Bacarisse, Feb 4, 2011
    #2
    1. Advertising

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. Dead RAM
    Replies:
    20
    Views:
    1,074
    John Harrison
    Jul 14, 2004
  2. D Senthil Kumar

    macro name from macro?

    D Senthil Kumar, Sep 20, 2003, in forum: C Programming
    Replies:
    1
    Views:
    555
    Jack Klein
    Sep 21, 2003
  3. apocalypznow

    re-entrancy question

    apocalypznow, Feb 8, 2005, in forum: Python
    Replies:
    0
    Views:
    288
    apocalypznow
    Feb 8, 2005
  4. Tim Golden

    RE: re-entrancy question

    Tim Golden, Feb 8, 2005, in forum: Python
    Replies:
    1
    Views:
    313
    Valentino Volonghi aka Dialtone
    Feb 8, 2005
  5. George2

    re-entrancy pattern issue setbacks

    George2, Apr 12, 2008, in forum: C Programming
    Replies:
    0
    Views:
    355
    George2
    Apr 12, 2008
Loading...

Share This Page