graphitemaster / gmqcc

An Improved Quake C Compiler
Other
163 stars 29 forks source link

ftepp doesn't do indirected macro expansion #36

Closed graphitemaster closed 10 years ago

graphitemaster commented 11 years ago

It's common behavior that that the following would work in terms of cpp behavior:

#define STR1(x) #x
#define STR2(x) STR1(x)
#define THE_ANSWER 42
#define THE_ANSWER_STR STR2(THE_ANSWER) /* "42" */

With ftepp THE_ANSWER_STR results in the following string: "THE_ANSWER" To figure out the exact expected behavior of macro substitution I looked in the C standard documentation to find this golden gem:

6.10.3.1/1:

... After the arguments for the invocation of a function-like macro have been identified,
argument substitution takes place. A parameter in the replacement list, unless preceded
by a # or ## preprocessing token or followed by a ## preprocessing token (see below), is
replaced by the corresponding argument after all macros contained therein have been expanded...

This sounds confusing due to the way ANSI tried to redact their documents, essentially (it means):

when we use: STR1(THE_ANSWER) we should get "THE_ANSWER", because the argument of STR1 is not macro-expanded. However, the argument of STR2 is macro-expanded when it's substituted into the definition of STR2, which therefore gives STR1 an argument of 42, with the result of "42".

I suspect there is some code that heavily requires this behavior (that didn't surface because valid strings were still generated, albeit semantically generated wrong)

Blub commented 11 years ago

The behaviour is consistent with fteqcc, however I'd gladly add C behavior as an option! (Switching this from E-Bug to C-Proposal)

divVerent commented 11 years ago

Yes, for Xonotic I would actually want to set it up to have the C-like behaviour instead.

-fftepp vs -fcpp ;)

graphitemaster commented 11 years ago

divVerent, the -fcpp behavior would most likely disable recursive macro definitions since those are not allowed in traditional cpp :P

Blub commented 11 years ago

Doesn't have to...