This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
preprocessor/1890: macro evaluation order prevents termination
- To: gcc-gnats at gcc dot gnu dot org
- Subject: preprocessor/1890: macro evaluation order prevents termination
- From: b dot i dot mills at massey dot ac dot nz
- Date: 6 Feb 2001 20:02:37 -0000
- Reply-To: b dot i dot mills at massey dot ac dot nz
>Number: 1890
>Category: preprocessor
>Synopsis: macro evaluation order prevents termination
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: unassigned
>State: open
>Class: sw-bug
>Submitter-Id: net
>Arrival-Date: Tue Feb 06 12:06:00 PST 2001
>Closed-Date:
>Last-Modified:
>Originator: Bruce
>Release: gcc version 2.96 20000731 (Red Hat Linux 7.0)
>Organization:
>Environment:
>Description:
cpp appears to expand macro arguments first, and then the
macro. The result is that some macros don't terminate which
did terminate in earlier versions of cpp that I have used.
If outer-most-complete macro expansion is used then the
behaviour is cleaner and more general. Eg: if leftmost
syntactically correct macro is expanded, and then the line
is re-read the problem does not occur. I was going to use
the cpp to demonstrate some properties of macro expansion
to my class, but the version I have seems to be no good
for this.
>How-To-Repeat:
an example of a macro that terminates if outer-most
evaluation is used, but not if arguments are expanded first.
#define zero (0,true)
#define one inc(zero)
#define two inc(one)
#define inc (a,false)
#define head(a,b) a
#define tail(a,b) b
#define true(x,y) x
#define false(x,y) y
#define apply_head(x) head x
#define apply_tail(x) tail x
#define if(x,y,z) x(y,z)
#define add(x,y) if(apply_tail(x),y,inc(add(apply_head(x),y)))
now
add(two,two)
terminates correctly if left-most evaluation is used, but
if the arguments are expanded first, then the add macro
goes into an infinite recursive decent.
>Fix:
if the input is not re-read, and that seems to be the case
then there really is no fix to this.
>Release-Note:
>Audit-Trail:
>Unformatted: