Discussion:
Unspecified behavior in macro substitution
(too old to reply)
Tim Rentsch
2015-08-18 15:17:31 UTC
Permalink
Raw Message
I have a question regarding macro replacement.

In (informative) section J.1, listing unspecified behaviors, one
of the listed items reads as follows:

When a fully expanded macro replacement list contains a
function-like macro name as its last preprocessing token and
the next preprocessing token from the source file is a (,
and the fully expanded replacement of that macro ends with
the name of the first macro and the next preprocessing token
from the source file is again a (, whether that is
considered a nested replacement (6.10.3).

My question is easy to state but may not be easy to answer.
Which normative text gives rise to this result? More
specifically, since the normative text doesn't mention this
situation explicitly, apparently the text must have an ambiguity
that admits the two possibilities mentioned above. What part of
the normative text allows this ambiguous reading, and by what
reasoning must that text be read as ambiguous (ie, considering
just normative text)? To ask the question another way, if we
only had normative text available, what would lead us to conclude
that the result given in J.1 must apply, rather than some other
result, eg, that the given circumstance should always count as a
nested replacement (or alternatively, that it should never count
as a nested replacement)?
Kaz Kylheku
2015-08-18 16:13:03 UTC
Permalink
Raw Message
Post by Tim Rentsch
I have a question regarding macro replacement.
In (informative) section J.1, listing unspecified behaviors, one
When a fully expanded macro replacement list contains a
function-like macro name as its last preprocessing token and
the next preprocessing token from the source file is a (,
and the fully expanded replacement of that macro ends with
the name of the first macro and the next preprocessing token
from the source file is again a (, whether that is
considered a nested replacement (6.10.3).
The ambiguity is in the sentence "furthermore, if any nested replacements
encounter the name of the macro being replaced, it is not replaced."
^^^ ^^^

One macro? Which one? The top-level one? Or the nested one?

Given:

#define FOO() BAR
#define BAR() FOO

FOO()()()

at some point we end up with the tokens

FOO()

where FOO was the result of expanding BAR() which was the
result of expanding FOO() together with the trailing context ().

So if "the macro being replaced" is BAR, then it is not the case that
the name of the macro being replaced is being encountered; what is
encountered is FOO. (FOO is the outer-most macro which has already been
expanded, and whose replacement sequence is now being re-scanned.)

Note how the text does *not* say anything of the following sort:

"If any nested replacements encounter the name of *any* macro that is being
replaced in the entire chain of nesting, that macro is not replaced."

Since under nesting there is more than one active macro, and the sentence
refers to "the macro" (i.e. some specific, single one), it is ambiguous.
Tim Rentsch
2015-09-06 18:16:33 UTC
Permalink
Raw Message
Post by Kaz Kylheku
Post by Tim Rentsch
I have a question regarding macro replacement.
In (informative) section J.1, listing unspecified behaviors, one
When a fully expanded macro replacement list contains a
function-like macro name as its last preprocessing token and
the next preprocessing token from the source file is a (,
and the fully expanded replacement of that macro ends with
the name of the first macro and the next preprocessing token
from the source file is again a (, whether that is
considered a nested replacement (6.10.3).
The ambiguity is in the sentence "furthermore, if any nested replacements
encounter the name of the macro being replaced, it is not replaced."
^^^ ^^^
[snip elaboration]
Thank you for the suggestion. I don't it's really the
answer to my question, but I still appreciate the comment.

Loading...