[patch, fortran] Warn about suspicious assignment to contiguous pointers

Thomas Koenig tkoenig@netcologne.de
Mon Aug 28 15:22:00 GMT 2017


Hi Janus,


> 
>> I think an unconditional warning is OK
>> in this case because
>>
>> - Assigning to a pointer from an obvious non-contiguous target
>>    is not useful at all, that I can see
> 
> I guess you're talking about a *contiguous* pointer here,

Correct.

> and in that
> case I would argue that, beyond being "not useful", it's even illegal,
> so why not throw a hard error, if we can infer at compile-time that
> the target is non-contiguous?

Problem is, we cannot infer this from the tests done.
We would also have to add a test if the array is empty
or that it contains only a single element, and that (I think)
is a) impossible in the general case, and b) not worth the bother.

> 
>> - Some language laywer will come up with the fact that it is,
>>    in fact, legal if the target is empty or has a single
>>    element only, so a hard error would be a rejects-valid.
> 
> Should be possible to detect and ignore such cases, shouldn't it?

Not in general.

> 
>> However, I can also make this into a warning depending on
>> -Wall, if this is preferred.
> 
> As explained above, I'd vote for an error (or at least a conditional
> warning, so that it can be disabled, like most(?) other gfortran
> warnings).

Attached is a version which makes this a warning enabled by -Wall;
this should be enough to give people a heads-up.

I have introduced most of your comments into the revised patch.

> Also I noticed that there is a function called
> "gfc_is_simply_contiguous" in expr.c. Could that be useful for your
> purpose? (Some of the code in there looks very similar to the logic
> you're adding.)

There is a subtle distinction between "simply contiguous" where
the compiler _has_ to know at compile-time that the expression
is contigous (and failure to diagnose is is a compiler error)
and "contiguous" where the burden is on the programmer.
My patch is intended to be an aid to the programmer for
the "continuous" case.

Because of the "simple contiguous" thing, the function does
not quite match the requirements.

So, here is the updated patch.  Regression-tested on
powerpc-linux, make dvi and make pdf also passed.
OK for trunk?

Regards

	Thomas

2017-08-27  Thomas Koenig  <tkoenig@gcc.gnu.org>

         PR fortran/49232
         * expr.c (gfc_check_pointer_assign): Warn for
         suspicious assignments with contiguous pointrs if
         -Wcontiguous is given.
         * lang.opt (Wcontiguous): New option, implied
         by -Wall.
         * invoke.texi: Document -Wcontiguous.

2017-08-27  Thomas Koenig  <tkoenig@gcc.gnu.org>

         PR fortran/49232
         * gfortran.dg/contiguous_4.f90: New test.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: p6.diff
Type: text/x-patch
Size: 3743 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20170828/fd76dd45/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: contiguous_4.f90
Type: text/x-fortran
Size: 465 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20170828/fd76dd45/attachment-0001.bin>


More information about the Gcc-patches mailing list