[PATCH] Fix to PR5683 - issue diagnostic for pointer subtraction with type of size zero.
Arthur O'Dwyer
arthur.j.odwyer at gmail.com
Thu Jun 20 11:32:07 PDT 2013
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?
-Arthur
More information about the cfe-commits
mailing list