[Patch] (general Fortran + OpenMP) [Fortran] Fix/modify present() handling for assumed-shape optional (PR 94672)

Thomas Koenig tkoenig@netcologne.de
Sun May 10 07:11:21 GMT 2020


Hi Tobias,

> For assumed shape, gfortran generates an "arg.0 = arg->data"
> artificial variable – and with optional one has something like
> 
> if (arg != NULL && arg->data != NULL)
>    {
>      arg.0 = arg->data;
>      lbound.0 = ...
>    }
> 
> And an "if (present(arg))" becomes
> "if (arg != NULL && arg->data != NULL)".
> 
> The proposed change changes the init to:
> 
> if (arg != NULL && arg->data != NULL)
>    {
>      arg.0 = arg->data;
>      lbound.0 = ...
>    }
> else
>    arg.0 = NULL;  // <-- new

I think this is a good idea in general. We later use the arg.0
variable to access the contents of the argument, so this generates
a clear segfault for all cases where the user accesses non-present
opiontal arguments instead of something random.

Also, this avoids potential warnings about undefined variables.


> Thus: I think either variant (checking arg directly vs. checking arg.0
> plus setting it to NULL) works equally well with normal Fortran code;

Like I said, I think it is better :-)

> one can probably design code where one or the other is slightly faster,
> but at the end it should not matter.
> And for OpenMP/OpenACC, the new variant avoids several problems.
> 
> Hence:
> OK for the trunk – and GCC 10 (regression, rejects valid code)?

OK for both, and thanks for the patch!

Regards

	Thomas


More information about the Gcc-patches mailing list