Bug 8524 - _Pragma within macros is improperly expanded
_Pragma within macros is improperly expanded
Status: RESOLVED FIXED
Product: gcc
Classification: Unclassified
Component: preprocessor
3.2.1
: P3 normal
: ---
Assigned To: Neil Booth
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2002-11-10 22:36 UTC by 157416, asuffield
Modified: 2003-07-25 17:33 UTC (History)
2 users (show)

See Also:
Host: i386-pc-linux-gnu
Target: i386-pc-linux-gnu
Build: i386-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description 157416, asuffield 2002-11-10 22:36:00 UTC
The original report from the Debian BTS, fix applied to head and branch:

aps100@cyclone:~$ cat foo.c
_Pragma("foo"); int y;
#define FOO _Pragma("foo"); int x;
FOO
aps100@cyclone:~$ cpp-3.2 foo.c
# 1 "foo.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "foo.c"

# 1 "foo.c"
#pragma foo 
# 1 "foo.c"
             ; int y;


# 3 "foo.c"
#pragma ; int x;foo 

The first line is expanded correctly; the third is not (rendering
_Pragma almost completely useless).


Fixed by Neil:
	* cpphash.h (FIRST, LAST, CUR, RLIMIT): Fix definitions.
	* cpplib.c (destringize_and_run): Kludge around getting
	tokens from in-progress macros.
	(_cpp_do__Pragma): Simplify. testsuite:
	* gcc.dg/cpp/_Pragma4.c: New test.

Bug submitter claims:

Nope, still not right. Here's a new case where it breaks:

aps100@cyclone:~/tmp$ cat foo.c
#define ALPHA(A) alpha_ ## A
#define BETA(B) beta_ ## B
#define GAMMA(C) _Pragma("moose") ALPHA(C) BETA(C)
GAMMA(baz);
aps100@cyclone:~/tmp$ cpp-3.2 foo.c
# 1 "foo.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "foo.c"




# 4 "foo.c"
#pragma moose=20
# 4 "foo.c"
alpha_baz beta_alpha_baz;
aps100@cyclone:~/tmp$ cpp-2.95 foo.c
# 1 "foo.c"



_Pragma("moose") alpha_baz    beta_baz    ;
=3D=3D

Note how beta_baz has become beta_alpha_baz in 3.2. This only happens
when the _Pragma is present, so I presume it's the same bug in another
form.

But wait! It gets better:

aps100@cyclone:~/tmp$ cat foo.c
#define ALPHA
#define BETA(B) beta_ ## B
#define GAMMA(C) _Pragma("moose") ALPHA BETA(C)
GAMMA(baz);
aps100@cyclone:~/tmp$ cpp-3.2 foo.c
# 1 "foo.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "foo.c"




# 4 "foo.c"
#pragma moose=20
# 4 "foo.c"
cpp-3.2: Internal error: Aborted (program cpp0)
Please submit a full bug report.

Release:
3.2.1 2002-11-03

Environment:
System: Linux smile 2.4.18-xfs #1 Tue Mar 12 22:42:02 CET 2002 i686 unknown
Architecture: i686

	
host: i386-pc-linux-gnu
build: i386-pc-linux-gnu
target: i386-pc-linux-gnu
Comment 1 Neil Booth 2002-11-10 22:44:07 UTC
Responsible-Changed-From-To: unassigned->neil
Responsible-Changed-Why: Mine.
Comment 2 Neil Booth 2002-11-10 22:44:07 UTC
State-Changed-From-To: open->analyzed
State-Changed-Why: Yah, I'm not entirely surprised; the code in question is not pretty.  I'll take another look.
Comment 3 Neil Booth 2002-11-17 14:07:28 UTC
State-Changed-From-To: analyzed->open
State-Changed-Why: Patch in progress.  Let's nail this for good.  Other than a rewrite the only possible fix is a kludge, so let's keep our fingers crossed.
    
    (GNATs insists I change state, consider it "analyzed").
Comment 4 Neil Booth 2002-11-18 12:51:59 UTC
State-Changed-From-To: open->closed
State-Changed-Why: Fixed, I hope.  Applied in 3.3, will soon apply in 3.2.2.
Comment 5 Neil Booth 2002-11-18 20:39:57 UTC
From: Neil Booth <neil@daikokuya.co.uk>
To: 157416@bugs.debian.org, Andrew Suffield <asuffield@debian.org>
Cc: gcc-gnats@gcc.gnu.org, debian-gcc@lists.debian.org,
	Zack Weinberg <zack@codesourcery.com>, gcc-patches@gcc.gnu.org
Subject: Re: preprocessor/8524: _Pragma within macros is improperly expanded
Date: Mon, 18 Nov 2002 20:39:57 +0000

 I think this fixes it for good.  I'm applying this to 3.3, and 3.2.2
 when it arrives.
 
 Neil.
 
 	PR preprocessor/8524
 	* cpplib.c (run_directive): Remove previous kludge to _Pragma.
 	Add a new one in its place, which hopefully works.
 	(skip_rest_of_line): Change test for bottom-of-context-stack.
 testsuite:
 	* gcc.dg/cpp/_Pragma5.c: New test.
 
 Index: cpplib.c
 ===================================================================
 RCS file: /cvs/gcc/gcc/gcc/cpplib.c,v
 retrieving revision 1.324
 diff -u -p -r1.324 cpplib.c
 --- cpplib.c	22 Sep 2002 02:03:17 -0000	1.324
 +++ cpplib.c	17 Nov 2002 22:11:21 -0000
 @@ -207,7 +207,7 @@ skip_rest_of_line (pfile)
       cpp_reader *pfile;
  {
    /* Discard all stacked contexts.  */
 -  while (pfile->context != &pfile->base_context)
 +  while (pfile->context->prev)
      _cpp_pop_context (pfile);
  
    /* Sweep up all tokens remaining on the line.  */
 @@ -1277,9 +1277,6 @@ destringize_and_run (pfile, in)
  {
    const unsigned char *src, *limit;
    char *dest, *result;
 -  cpp_context saved_context;
 -  cpp_context *saved_cur_context;
 -  unsigned int saved_line;
  
    dest = result = alloca (in->len + 1);
    for (src = in->text, limit = src + in->len; src < limit;)
 @@ -1291,24 +1288,29 @@ destringize_and_run (pfile, in)
      }
    *dest = '\0';
  
 -  /* FIXME.  All this saving is a horrible kludge to handle the case
 -     when we're in a macro expansion.
 -
 -     A better strategy it to not convert _Pragma to #pragma if doing
 -     preprocessed output, but to just pass it through as-is, unless it
 -     is a CPP pragma in which case is should be processed normally.
 -     When compiling the preprocessed output the _Pragma should be
 -     handled.  This will be become necessary when we move to
 -     line-at-a-time lexing since we will be macro-expanding the line
 -     before outputting / compiling it.  */
 -  saved_line = pfile->line;
 -  saved_context = pfile->base_context;
 -  saved_cur_context = pfile->context;
 -  pfile->context = &pfile->base_context;
 -  run_directive (pfile, T_PRAGMA, result, dest - result);
 -  pfile->context = saved_cur_context;
 -  pfile->base_context = saved_context;
 -  pfile->line = saved_line;
 +  /* Ugh; an awful kludge.  We are really not set up to be lexing
 +     tokens when in the middle of a macro expansion.  Use a new
 +     context to force cpp_get_token to lex, and so skip_rest_of_line
 +     doesn't go beyond the end of the text.  Also, remember the
 +     current lexing position so we can return to it later.
 +
 +     Something like line-at-a-time lexing should remove the need for
 +     this.  */
 +  {
 +    cpp_context *saved_context = pfile->context;
 +    cpp_token *saved_cur_token = pfile->cur_token;
 +    tokenrun *saved_cur_run = pfile->cur_run;
 +
 +    pfile->context = xnew (cpp_context);
 +    pfile->context->macro = 0;
 +    pfile->context->prev = 0;
 +    run_directive (pfile, T_PRAGMA, result, dest - result);
 +    free (pfile->context);
 +    pfile->context = saved_context;
 +    pfile->cur_token = saved_cur_token;
 +    pfile->cur_run = saved_cur_run;
 +    pfile->line--;
 +  }
  
    /* See above comment.  For the moment, we'd like
  

Comment 6 Neil Booth 2002-11-18 20:43:38 UTC
From: Neil Booth <neil@daikokuya.co.uk>
To: 157416@bugs.debian.org, Andrew Suffield <asuffield@debian.org>
Cc: gcc-gnats@gcc.gnu.org, debian-gcc@lists.debian.org,
	Zack Weinberg <zack@codesourcery.com>, gcc-patches@gcc.gnu.org
Subject: Re: preprocessor/8524: _Pragma within macros is improperly expanded
Date: Mon, 18 Nov 2002 20:43:38 +0000

 Neil Booth wrote:-
 
 > I think this fixes it for good.  I'm applying this to 3.3, and 3.2.2
 > when it arrives.
 > 
 > Neil.
 > 
 > 	PR preprocessor/8524
 > 	* cpplib.c (run_directive): Remove previous kludge to _Pragma.
 > 	Add a new one in its place, which hopefully works.
 > 	(skip_rest_of_line): Change test for bottom-of-context-stack.
 > testsuite:
 > 	* gcc.dg/cpp/_Pragma5.c: New test.
 
 Oops, the test got lost waiting for CVS.
 
 Neil.
 
 Index: testsuite/gcc.dg/cpp/_Pragma5.c
 ===================================================================
 RCS file: testsuite/gcc.dg/cpp/_Pragma5.c
 diff -N testsuite/gcc.dg/cpp/_Pragma5.c
 --- /dev/null	1 Jan 1970 00:00:00 -0000
 +++ testsuite/gcc.dg/cpp/_Pragma5.c	18 Nov 2002 20:40:43 -0000
 @@ -0,0 +1,14 @@
 +/* { dg-do preprocess } */
 +
 +/* Based on Debian GNATS PR 8524.  17 Nov 2002.  */
 +
 +#define ALPHA(A) alpha_ ## A
 +#define BETA(B) beta_ ## B
 +#define GAMMA(C) _Pragma("moose") ALPHA(C) BETA(C)
 +GAMMA(baz);
 +
 +/*
 +   { dg-final { if ![file exists _Pragma5.i] { return }                   } }
 +   { dg-final { if { [grep _Pragma5.i "alpha_baz beta_baz;"] != "" } { return }  } }
 +   { dg-final { fail "_Pragma5.c: _Pragma in macro"                       } }
 +*/
Comment 7 Neil Booth 2002-11-18 20:43:41 UTC
From: neil@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: preprocessor/8524
Date: 18 Nov 2002 20:43:41 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Changes by:	neil@gcc.gnu.org	2002-11-18 12:43:40
 
 Modified files:
 	gcc            : ChangeLog cpplib.c 
 	gcc/testsuite  : ChangeLog 
 Added files:
 	gcc/testsuite/gcc.dg/cpp: _Pragma5.c 
 
 Log message:
 	PR preprocessor/8524
 	* cpplib.c (run_directive): Remove previous kludge to _Pragma.
 	Add a new one in its place, which hopefully works.
 	(skip_rest_of_line): Change test for bottom-of-context-stack.
 	testsuite:
 	* gcc.dg/cpp/_Pragma5.c: New test.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=1.15934&r2=1.15935
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cpplib.c.diff?cvsroot=gcc&r1=1.324&r2=1.325
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.2218&r2=1.2219
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/cpp/_Pragma5.c.diff?cvsroot=gcc&r1=NONE&r2=1.1
 

Comment 8 Neil Booth 2002-11-26 20:40:46 UTC
From: neil@gcc.gnu.org
To: gcc-gnats@gcc.gnu.org
Cc:  
Subject: preprocessor/8524
Date: 26 Nov 2002 20:40:46 -0000

 CVSROOT:	/cvs/gcc
 Module name:	gcc
 Branch: 	gcc-3_2-branch
 Changes by:	neil@gcc.gnu.org	2002-11-26 12:40:46
 
 Modified files:
 	gcc            : ChangeLog cpplib.c 
 	gcc/testsuite  : ChangeLog 
 Added files:
 	gcc/testsuite/gcc.dg/cpp: _Pragma5.c 
 
 Log message:
 	PR preprocessor/8524
 	* cpplib.c (run_directive): Remove previous kludge to _Pragma.
 	Add a new one in its place, which hopefully works.
 	(skip_rest_of_line): Change test for bottom-of-context-stack.
 	testsuite:
 	* gcc.dg/cpp/_Pragma5.c: New test.
 
 Patches:
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.13152.2.657.2.151&r2=1.13152.2.657.2.152
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cpplib.c.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.290.2.1.4.2&r2=1.290.2.1.4.3
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=1.1672.2.166.2.58&r2=1.1672.2.166.2.59
 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/cpp/_Pragma5.c.diff?cvsroot=gcc&only_with_tag=gcc-3_2-branch&r1=NONE&r2=1.1.6.1