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

            Bug ID: 87569
           Summary: defining type in ‘sizeof’ expression is invalid in C++
                    references wrong operator
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

While enhancing the C parser to recognize a new built-in similar to
alignof/sizeof/typeof I noticed the in_alignof, in_sizeof and in_typeof
handling by -Wc++-compat isn't quite right.  The warning code uses the in_xxx
counters to reference the operator whose operand is a type definition, like
this:

    warning_at (loc, OPT_Wc___compat,
                "defining type in %qs expression is invalid in C++",
                (in_sizeof
                 ? "sizeof"
                 : (in_typeof ? "typeof" : "alignof")));

But this approach is inaccurate whew multiple counters are set because it fails
to consider that the current operator may be different between the same counter
values.  Below is a test case that shows the problem (note the warning mentions
sizeof in both instances, even though only one of them defines a type in a
sizeof operand; the other one defines it in a typeof operand).  To accurately
reflect the current operator the code would need to maintain a data structure
like a stack.  It's not really a big deal but if one expects the message to
accurately reflect the operator (e.g., when writing tests for a new operator),
one realizes that this sort of accuracy is not possible with the current
design.

$ cat x.c && gcc -S -Wall -Wextra -Wc++-compat x.c

int i = sizeof (__typeof (enum { e0 }));   // type defined in typeof

__typeof (sizeof (enum { e1 })) e;         // type defined in sizeof

x.c:1:32: warning: defining type in ‘sizeof’ expression is invalid in C++
[-Wc++-compat]
1 | int i = sizeof (__typeof (enum { e0 }));   // type defined in typeof
  |                                ^
x.c:3:24: warning: defining type in ‘sizeof’ expression is invalid in C++
[-Wc++-compat]
3 | __typeof (sizeof (enum { e1 })) e;         // type defined in sizeof
  |                        ^

Reply via email to