Constant and pass-by-reference

Mark Eggleston mark.eggleston@codethink.co.uk
Mon Jun 24 13:47:00 GMT 2019


On 21/06/2019 14:58, Steve Kargl wrote:
> On Fri, Jun 21, 2019 at 09:29:53AM +0100, Mark Eggleston wrote:
>> On 18/06/2019 14:22, Arjen Markus wrote:
>>> Hello Hafiz,
>>>
>>> I do not know if the compiler can be expected to catch all such cases
>>> - after all, the module may be compiled and stored in some library and
>>> then used in a separate build step. In that case the information that
>>> the actual argument is not a variable would be lost. You can, however,
>>> help the compiler by stating the intent for the dummy argument (var):
>>>
>>> integer, intent(out) :: var
>>>
>>> Now, the compiler will complain when it tries to compile the main program.
>> I have also come across this problem. In my opinion it is better to
>> catch such a fault at compile time rather than run time.
>>
>> A scheme that checks a dummy variable's intent is OUT or INOUT when a
>> value is assigned and issues a warning would help.
>>
>> Such a warning could be controlled by -Wall with perhaps an new option
>> -Wintent. When used with -Werror the program will then fail to compile.
>>
>> This would encourage the use of intent with dummy.
>>
> I suspect that this will have a high false positive rate.

I have a preliminary patch that implements this scheme (attached). 
Initially it was unaware of the VALUE attribute and was later changed. 
Running the gfortran test using "make -j 8 check-fortran" resulted in 8 
failures from 3 test cases (earlier version):

c_by_val_5.f90
coarray_24.f90
warn_concat.f90

Once the patch was aware of VALUE, the failures for c_by_val_5.f90 no 
longer occurred. Adding intent to the dummy variables in coarray_24.f90 
and warn_concart.f90 Rrduced to the failures to zero.

I not sure what you mean by "false positive" unless it is for FORTRAN 
code earlier than Fortran 90 which does not have intent. Dummy variables 
can simply have INTENT attributes added resulting in clearer code.

The patch currently includes this warning message:

"Dummy variable %qs in assignment has no INTENT specified, use 
INTENT(INOUT) or INTENT(OUT) at %L"

I'm not happy with it as it was written before taking into account the 
VALUE attribute. Any suggestions will be appreciated.

I do think that this patch will be valuable as it can help catch bugs at 
compile time instead of run time. If there is a consensus that the use 
of -Wall is a bad idea I can disassociate it from -Wintent.

> My Makefiles already contain
>
> # gfortran is too noisy
> FFLAGS += -Wno-maybe-uninitialized -Wno-conversion -Wno-integer-division
>
> to suppress issues with these options.
>
> I would not support adding it to -Wall.
>
-- 
https://www.codethink.co.uk/privacy.html

-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-Constant-and-pass-by-reference.patch
Type: text/x-patch
Size: 2967 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/fortran/attachments/20190624/839fa76b/attachment.bin>


More information about the Fortran mailing list