[Bug c/91285] New: _Pragma does not work in a useful fashion

konrad.schwarz at siemens dot com gcc-bugzilla@gcc.gnu.org
Mon Jul 29 12:06:00 GMT 2019


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91285

            Bug ID: 91285
           Summary: _Pragma does not work in a useful fashion
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: konrad.schwarz at siemens dot com
  Target Milestone: ---

Consider the following code:

# define        ENCLOSING_OBJECT(TYPE, MEMBER, OBJECT)\
        (sizeof (&((TYPE *) 0)->MEMBER - OBJECT),\
        (TYPE *) ((char *) OBJECT - offsetof (TYPE, MEMBER)))

What this does is find the address of an "enclosing object" given the
address of one of its members MEMBER as OBJECT.
This can be useful when interacting with
programming frameworks -- often, MEMBER is actually the first element
of the application-defined object, but this macro is generic enough
that it doesn't have to be so.

As an additional compile-time type-safety check, the macro expansion 
includes an additional clause that ensures that OBJECT is indeed of the
type of MEMBER by subtracting its address from a synthesized enclosing
objects member's address;
this subtraction is done as the argument of sizeof(), ensuring
it doesn't actually get executed at run-time.

Newer versions of GCC flag this construct with an "unused value" warning,
which can be disabled globally with
# pragma GCC diagnostic ignored "-Wunused-value".

In general however, the unused value warning is useful, so one
would like to restrict the scope of ignoring the warning to as
small an area as possible.  The GCC diagnostic pragmas allow this
to be done with the `diagnostic push' and `diagnostic pop'
sub-commands.

In C90, one would have to add # pragma directives around each use
of the macro, clearly an undesirable approach.  Hence, in C0x,
_Pragma was introduced to allow pragma directives to
result as a by-product of macro expansion.

Thus, the C0x solution would be to extend the definition of the macro
with
    _Pragma ("GCC diagnostic push")\
    _Pragma ("GCC diagnostic ignored \"-Wunused-value\"")\
    -- original macro text --\
    _Pragma ("GCC diagnostic pop")

However, GCC fails to compile this, because it can't handle
pragmas in the middle of expressions.

This makes the GCC implementation of _Pragma worthless.


More information about the Gcc-bugs mailing list