Re: Preprocessing directive in the middle of macro arguments



John Bode wrote:
On Jan 16, 8:57 am, Francois Grieu <fgr...@xxxxxxxxx> wrote:
One of the C compiler that I use <OT>(Keil's CX51)</OT> barks at

#define a(b) b
int main(void){return a(
#if 0
#endif
0);}

More generally, this compiler seems confused by any preprocessing
directive in the middle of macro arguments, which comes handy e.g. to
conditionaly select one of several arguments passed to a macro.

Is it a compiler bug? Where does C89 define if this is valid or not?

TIA.
Francois Grieu

It's not a bug; you cannot embed preprocessor commands in a macro.
All preprocessor directives are processed before any macro expansion
takes place, so any preprocessor directives embedded in a macro will
not be recognized as such, but will instead be translated as regular C
code.
The OP's code has no embedded directives; it's equivalent to
#define a(b) b
int main(void){return a(
0);}
Which is good C, IMHO. So I think it's a compiler bug.
<OT>
Getting the C front end right is surprisingly hard; there are enough C vendors to make a market for C front end parsers, and it exists.
Still, some people do it on their own, and sometimes incorrectly. I had trouble with Keil for ARM (see http://www.macroexpressions.com/dl/compileme.c), but I never tried their latest version.
I wonder if this fixes the OP problem :)
#de\
fine \
a(b\
b)=bb
</OT>
--
Ark
.



Relevant Pages

  • Re: How to define a const and initialize it?
    ... > do the same work as const int MYCONST will do.... ... But the thing is that macro substitution takes ... C++ compiler isn't even aware of its existence. ... This Item might better be called "prefer the compiler to the preprocessor," because #define is often ...
    (alt.comp.lang.learn.c-cpp)
  • Re: #define and (brackets)
    ... The compiler doesn't second-guess. ... STRIZE is passed a value x as NUM. ... The preprocessor again creates ... it must evaluate the macro but this time instead of #NUM ...
    (microsoft.public.vc.language)
  • Re: String comparison in preprocessor commands
    ... that won't work: The preprocessor can generate ... string literals, but it can't actually work with strings. ... and rely on the compiler to eliminate dead code: ... > position to change the macro definition. ...
    (comp.lang.c)
  • Re: Why code completion and early error checking are needed
    ... > Removing the mechanism to include one sourcefile within another, ... Conditional compilation could be handled by the compiler by not producing ... run it through the preprocessor first. ... preprocessor directives in that confined part of the program, ...
    (comp.lang.cpp)
  • Re: #ifdef inside macro expansion
    ... The preprocessor is what you are looking at - not the compiler. ... a macro with, so this is expected bhavior - not a bug. ...
    (microsoft.public.vc.language)