Behaviour of #pragma in statement context

Stephen Huw CLARKE stephen.clarke@st.com
Tue Sep 29 17:59:00 GMT 2009


I'm having some difficulty understanding gcc behaviour
on the following example.  It relates to the treatment
of the pragma.

$ cat test22.c
#include <stdio.h>

int x;
void foo (void)
{
   int i, j;
   for (i = 0; i < 10; i++)
#pragma GCC visibility push(default)
     for (j = 0; j < 10; j++)
       x++;
}

int main(void)
{
   foo ();
   printf ("x = %d (should be 100)\n", x);
   return 0;
}


If I compile and run (using gcc 4.3.3, Target: i686-pc-linux-gnu)
I get:
$ gcc -Wall test22.c
$ ./a.out
x = 10 (should be 100)

If I compile it as C++ (same gcc version), I get the behaviour I was 
expecting:

$ g++ -Wall -xc++ test22.c
$ ./a.out
x = 100 (should be 100)

It seems that the C parser is treating the pragma as a statement
which becomes the entire body of the first for loop, whereas the
C++ parser is not treating the pragma as a statement.

My question is, what should I expect here?

(Disclaimer: I know the example is silly.  In the original code,
the pragma is one we have added locally to control the
degree of loop unrolling in the following loop.  But I wanted to
present here an example which is reproducible using stock gcc.)

Thanks for any insight,
Steve.



More information about the Gcc-help mailing list