Bug 17426 - Emit mandatory warning for manual expansions of offsetof
Summary: Emit mandatory warning for manual expansions of offsetof
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.0.0
: P2 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: new-warning, new_warning
  Show dependency treegraph
 
Reported: 2004-09-12 01:37 UTC by Giovanni Bajo
Modified: 2022-10-09 23:45 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2005-12-18 00:41:27


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Giovanni Bajo 2004-09-12 01:37:48 UTC
RTH's patch for PR 17396 reintroduced support for the old-skool manual 
expansion of offsetof within constant expression - a thing which is forbidden 
by C90 rules and that we will stop accepting one day.

Meanwhile, it makes sense to warn people about this change, since it is a 
widespread idiom. I propose a mandatory warning to be emitted in this case:

warning: this custom definition of offsetof is not a valid constant expression 
in ISO C
info: use the offsetof macro from <stddef.h> to disable this warning

JSM, I would appreciate your comments on this PR (especially, the wording of 
the message, pedwarn vs warn, mandatory or not), before I write up the patch.
Comment 1 Andrew Pinski 2004-09-12 01:39:19 UTC
Confirmed.
Comment 2 Joseph S. Myers 2004-09-12 11:11:07 UTC
Subject: Re:  New: Emit mandatory warning for manual expansions
 of offsetof

On Sun, 12 Sep 2004, giovannibajo at libero dot it wrote:

> Meanwhile, it makes sense to warn people about this change, since it is a 
> widespread idiom. I propose a mandatory warning to be emitted in this case:
> 
> warning: this custom definition of offsetof is not a valid constant expression 
> in ISO C
> info: use the offsetof macro from <stddef.h> to disable this warning
> 
> JSM, I would appreciate your comments on this PR (especially, the wording of 
> the message, pedwarn vs warn, mandatory or not), before I write up the patch.

In what cases do you propose to diagnose this?  After all, it's perfectly 
valid if a constant isn't required (using the implementation-defined 
conversions between integers and pointers), and fits the long-established 
GCC extension of symbolic difference constant expressions if being used in 
a static initializer; it's only where an integer constant expression is 
required, as in bug 17396 (static array dimension) or for case labels, 
enum values, bit-field widths, null pointer constants, designators for 
array initializers, that there's a problem.  I would guess that array 
dimensions and maybe designators are the cases fake-offsetof is most 
likely to be being used in.

Comment 3 Giovanni Bajo 2004-09-12 13:29:26 UTC
(In reply to comment #2)

> it's only where an integer constant expression is 
> required, as in bug 17396 (static array dimension) or for case labels, 
> enum values, bit-field widths, null pointer constants, designators for 
> array initializers, that there's a problem.

Thanks for the list. I will try to activate the warning in these contexts, but 
I do not know the C frontend, so maybe I'll need to do this incrementally.

> fits the long-established 
> GCC extension of symbolic difference constant expressions if being used in 
> a static initializer

This could be a pedwarn, then, right?
Comment 4 Joseph S. Myers 2004-09-12 14:30:03 UTC
Subject: Re:  Emit mandatory warning for manual expansions of
 offsetof

On Sun, 12 Sep 2004, giovannibajo at libero dot it wrote:

> > fits the long-established 
> > GCC extension of symbolic difference constant expressions if being used in 
> > a static initializer
> 
> This could be a pedwarn, then, right?

A pedwarn for uses where integer constant expressions are required.  The 
intention as described in my proposed formal models is that symbolic 
difference constant expressions are an implementation-defined other kind 
of constant expression acceptable in initializers, i.e. not diagnosed 
there.

Comment 5 Andrew Pinski 2005-03-13 05:38:57 UTC
A note here, glibc and a couple of other projects would have been helped by this warning as they have 
code which uses the manual expansion of offsetof.
Comment 6 Eric Gallager 2018-03-02 11:55:50 UTC
(In reply to Giovanni Bajo from comment #3)
> (In reply to comment #2)
> 
> > it's only where an integer constant expression is 
> > required, as in bug 17396 (static array dimension) or for case labels, 
> > enum values, bit-field widths, null pointer constants, designators for 
> > array initializers, that there's a problem.
> 
> Thanks for the list. I will try to activate the warning in these contexts,
> but 
> I do not know the C frontend, so maybe I'll need to do this incrementally.
> 
> > fits the long-established 
> > GCC extension of symbolic difference constant expressions if being used in 
> > a static initializer
> 
> This could be a pedwarn, then, right?

Are you still working on this?
Comment 7 Eric Gallager 2018-09-02 11:38:33 UTC
(In reply to Eric Gallager from comment #6)
> (In reply to Giovanni Bajo from comment #3)
> > (In reply to comment #2)
> > 
> > > it's only where an integer constant expression is 
> > > required, as in bug 17396 (static array dimension) or for case labels, 
> > > enum values, bit-field widths, null pointer constants, designators for 
> > > array initializers, that there's a problem.
> > 
> > Thanks for the list. I will try to activate the warning in these contexts,
> > but 
> > I do not know the C frontend, so maybe I'll need to do this incrementally.
> > 
> > > fits the long-established 
> > > GCC extension of symbolic difference constant expressions if being used in 
> > > a static initializer
> > 
> > This could be a pedwarn, then, right?
> 
> Are you still working on this?

No reply; taking that as a no.
Comment 8 Eric Gallager 2019-03-03 04:35:13 UTC
this warning would be new; making it block the relevant meta-bug