This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Add a -Wcast-align=strict warning


On 09/13/17 22:03, Joseph Myers wrote:
> On Wed, 13 Sep 2017, Bernd Edlinger wrote:
> 
>> On 09/13/17 19:06, Joseph Myers wrote:
>>> What does this warning do in cases where a type has different alignments
>>> inside and outside structs?  I'm thinking of something like
>>>
>>> struct s { long long x; } *p;
>>> /* ... */
>>> (long long *)p
>>>
>>> on 32-bit x86 - where long long's preferred alignment is 8 bytes, but in
>>> structures it's 4 bytes.  (Likewise for double in place of long long.)  I
>>> think a warning for a (long long *)p cast might be surprising in that
>>> case.
>>>
>>
>> Well, yes this does get a warning.  But doesn't that cast then violate
>> the underlying alignment requirement of long long* ?
> 
> That's the difference between preferred alignment (__alignof__) and
> alignment required in all contexts (C11 _Alignof).  The above seems valid,
> just like it's valid to take the address of a long long struct element.
> That is, the alignment for the target of a pointer to long long is really
> 4 bytes here, even though the alignment for a standalone long long object
> is 8 bytes.  And there's a case for the warning to look at the required
> alignment in all contexts, not TYPE_ALIGN.
> 

So you suggest to use min_align_of_type instead of TYPE_ALIGN.

That would also make sense for the traditional -Wcast-align on
strict-alignment targets, right?


Thanks,
Bernd.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]