#pragma push and pop (actually it's pragma warning push(...) and pop(...) )

Kevin Buettner kev@primenet.com
Tue, 5 Sep 2000 14:56:12 -0700


On Sep 5,  1:31pm, Lucas Vogel wrote:

> 	Unless there is a different (or even standard) way to produce the
> same results I kind of think the gcc reason that you mentioned doesn't seem
> to be a very strong argument. [...]

#pragma is a standards conforming mechanism for providing additional
information to the compiler.  By this I mean that the #pragma
directive is in ISO-C.  However, the meanings of the preprocessor
tokens that follow the #pragma directive are left up to the C
compiler.

Here again are the reasons that the GCC manual states for not
using #pragma:

   1. It is impossible to generate #pragma commands from a macro. 
   2. There is no telling what the same #pragma might mean in another
      compiler.  

Number 1 may not be very persasive anymore since macros are falling
out of favor.  But the general idea is that if a compiler
implementation goes to the effort of providing some additional
facilities, they should be usable from within a macro.  E.g, using
your #pragma warning(push) example, you might like to do something
like the following:

    #define WARNINGS_OFF #pragma warning(pop)
    #define WARNINGS_ON #pragma warning(push)

Unfortunately, this won't work; the C preprocessor doesn't allow
this type of construct.

Number 2, I think, is a much more persasive argument.  Compiler A
might define

    #pragma foo

to be one thing and and compiler B to be something completely
different (even contradictory).  In order to be certain that your
pragmas are doing what you expect, you'd have to arrange for any use
of these pragmas to be guarded with preprocessor conditionals, e.g,

    #ifdef USING_COMPILER_A
    #pragma foo
    #endif

The GNU C compiler avoids the use of pragmas by (instead) extending
the C language.  E.g, when using the -Wunused switch with gcc, the
`unused' attribute may be used as follows to suppress warning messages:

int
sum (int a, int b, int u __attribute__ ((unused)))
{
  return a + b;
}

The bad thing about this is that it *is* a GNU extension and will
likely not work with other compilers.  But, fortunately, it is
possible to define a macro which expands to nothing for other
compilers, but expands to `__attribute__ ((unused))' for gcc:

#if (!defined (__GNUC__) || (__GNUC__ < 2) \
                         || (__GNUC__ == 2 && __GNUC_MINOR__ < 7))
#define UNUSED
#else
#define UNUSED __attribute__((__unused__))
#endif

You'd then use it as follows:

int
sum (int a, int b, int u UNUSED)
{
  return a + b;
}

GNU C has some other extensions for aligning/packing things into
structures which may also be used inside of macros in order to
achieve better portability.

Having said all of this, GNU C does indeed have pragmas, but they are
either generally not documented or poorly documented, existing
primarily to provide compatability with other C language
implementations for a certain platform.

Kevin