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

Serge Pavlov sepavloff at gmail.com
Thu Jun 20 22:20:17 PDT 2013


Good catch, thank you!
It looks like the check for language is useless, an array of zero length
may be found in any of the supported languages.

Thanks,
--Serge


2013/6/21 Richard Smith <richard at metafoo.co.uk>

> 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;
>



-- 
Thanks,
--Serge
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130621/aecfd9be/attachment.html>


More information about the cfe-commits mailing list