NO

Author Topic: Too early evaluation of macro name ?  (Read 2310 times)

aMarCruz

  • Guest
Too early evaluation of macro name ?
« on: April 19, 2014, 11:51:13 PM »
Hi Pelle,
thanks for your excellent work.
Does the behavior of this code is correct?

Code: [Select]
#define TRIGGER_ZERO(...) 0
#define TEST_ZERO_CONCAT(...)  TRIGGER_ZERO ##__VA_ARGS__()
#define TEST_ZERO_DIRECT(...)  TRIGGER_ZERO __VA_ARGS__()

int _cdecl main(void) {
    int i = 0;
    i = TEST_ZERO_CONCAT();                  //Ok, emit 0 with simple/no arguments
    i = TEST_ZERO_CONCAT((void),foo);   //emit '0,foo()' but expected warning #1058
    i = TEST_ZERO_DIRECT();                   //emit literal "TEST_ZERO_DIRECT()" and obvious linkage error
    return 0;
}

I think this is a bug, TEST_ZERO_DIRECT() should generate 0, as do Clang and MinGW.
Thanks.

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: Too early evaluation of macro name ?
« Reply #1 on: April 20, 2014, 11:57:40 AM »
As written, the code looks wrong...
/Pelle

aMarCruz

  • Guest
Re: Too early evaluation of macro name ?
« Reply #2 on: April 21, 2014, 10:11:48 AM »
Hi Pelle.
mmm... maybe not explained well (sorry, english is not my language).

The way is not TRIGGER_ZERO ##__VA_ARGS__(),  but TRIGGER_ZERO __VA_ARGS__()

In the example above, the line with
Code: [Select]
i = TEST_ZERO_DIRECT();
"TEST_ZERO_DIRECT()" should be replaced and evaluated as "TRIGGER_ZERO ()" (because __VA_ARGS__ is blank), and then generate the i = 0;.

This is to detect 0 arguments cases, and avoid the warnings with ## -- i.e: pasting TRIGGER_ZERO with something like (char*)p.

Thanks for your quick reply.

@beto
« Last Edit: April 21, 2014, 10:20:15 AM by aMarCruz »

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: Too early evaluation of macro name ?
« Reply #3 on: April 26, 2014, 03:33:08 PM »
You define TRIGGER_ZERO as a function-like macro. You later use TRIGGER_ZERO without parenthesis. Like I said, it's a problem with your code.
/Pelle