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

Richard Smith richard at metafoo.co.uk
Thu Jun 20 12:18:07 PDT 2013


On Thu, Jun 20, 2013 at 11:32 AM, Arthur O'Dwyer
<arthur.j.odwyer at gmail.com> wrote:
> On Wed, Jun 19, 2013 at 8:12 PM, Serge Pavlov <sepavloff at gmail.com> wrote:
>>>
>>> +      if (!getLangOpts().CPlusPlus) {
>>> +        // If pointee type is a structure or union of zero size (GCC extension),
>>> +        // the subtraction does not make sense.
>>> +        if (!rpointee->isVoidType() && !rpointee->isFunctionType()) {
>>> +          CharUnits ElementSize = Context.getTypeSizeInChars(rpointee);
>>> +          if (ElementSize.isZero()) {
>>> +            Diag(Loc,diag::warn_sub_ptr_zero_size_types)
>>> +              << rpointee.getUnqualifiedType()
>>> +              << LHS.get()->getSourceRange() << RHS.get()->getSourceRange();
>>> +          }
>>> +        }
>>> +      }
>
> The logic here is "If rpointee is zero-sized, then emit a warning; and
> by the way this should never happen in (Objective-)C++ because C++
> doesn't have zero-sized types." I don't know the general project
> style, but I feel like this would be better expressed by something
> like
>
>>        // If pointee type is a structure or union of zero size (GCC extension),
>>        // the subtraction does not make sense.
>>        if (!rpointee->isVoidType() && !rpointee->isFunctionType()) {
>>          CharUnits ElementSize = Context.getTypeSizeInChars(rpointee);
>>          if (ElementSize.isZero()) {
>>            assert(!getLangOpts().CPlusPlus);
>>            Diag(Loc,diag::warn_sub_ptr_zero_size_types)
>>              << rpointee.getUnqualifiedType()
>>              << LHS.get()->getSourceRange() << RHS.get()->getSourceRange();
>>          }
>>        }
>
> That way, if someone somehow does manage to introduce a zero-sized
> type (extension?) into (Objective-?)C++, the symptom would be a
> failed-invariant assertion instead of silently skipping the
> diagnostic.  Thoughts?

Thanks for this comment; it turns out that we do actually have
zero-sized types in C++ as an extension already. Here's a C++ testcase
which should trigger the warning:

typedef int arr[0]; arr *x, *y; int k = x - y;



More information about the cfe-commits mailing list