[PATCH] Fix to PR5683 - issue diagnostic for pointer subtraction with type of size zero.

Serge Pavlov sepavloff at gmail.com
Mon Apr 8 11:16:06 PDT 2013


Thank you, John.

2013/4/9 John McCall <rjmccall at apple.com>:
> On Apr 8, 2013, at 10:18 AM, Serge Pavlov <sepavloff at gmail.com> wrote:
>> +        // If pointee type is a structure or union of zero size (GCC extension),
>> +        // the subtraction does not make sense.
>> +        CharUnits ElementSize = Context.getTypeSizeInChars(rpointee);
>> +        if (ElementSize.isZero() &&
>> +            (rpointee.getTypePtr()->isStructureType() ||
>> +             rpointee.getTypePtr()->isUnionType())) {
>
> This is rpointee->isRecordType(), and it's much cheaper to check that
> *before* getting the type size.
>
> Also, this is only possible in C, so please add !getLangOpts().CPlusPlus to
> your check.

This code is already in the else-branch which is executed if not C++.

> You should also test what happens when you try to subtract pointers of
> incomplete or ill-formed type:
>
>   struct A;
>   int test(A *x, A *y) { return (y - x); }

You are right, this case also causes compiler fail.


--
Thanks,
--Serge



More information about the cfe-commits mailing list