[petsc-dev] Compliant C89

Barry Smith bsmith at mcs.anl.gov
Mon Jan 14 11:17:22 CST 2013


On Jan 14, 2013, at 9:02 AM, Jed Brown <jedbrown at mcs.anl.gov> wrote:

> The cast macros like PetscBLASIntCast() are not "safe" in that they include two separate statements. Unfortunately, we cannot do better with C89 unless we pass the target as an argument. Regardless, these casts cannot be used in declarations.
> 
> PetscBLASInt   one = 1,bn = PetscBLASIntCast(xin->map->n);
> PetscErrorCode ierr;
> 
> This is invalid C89 because once the macro expands, there is a statement before the next declaration. Furthermore, the check calls SETERRQ in case of failure, so it must be inside a PetscFunctionBegin/PetscFunctionReturn block.
> 
> Should we change this cast to an inline function (or equivalent macro) so that this usage is more explicit?

  Note that even if you change it to an inline function it still will not give the correct stack frame since it is not inside the PetscFunctionBegin/End. 

  Shouldn't we simply ban its use in the declarations part of the routines? Note that its use in, for example, 

    } else {
      PetscBLASInt ione = 1;
      PetscScalar aone = 1.0, azero = 0.0;
      PetscBLASInt neqs = PetscBLASIntCast(bcgsl->ell-1);

is legal since it is within a PetscBegin/Return block and the last declaration given.

A quick etags search found that it is being used correctly already almost anywhere. The suspicious place is only in the documentation :-)

Thus I say, fix the documentation and check that it is never used incorrectly but don't change its form.

  Barry

> 
> Regardless, Satish, can you set up a build --with-64-bit-indices CFLAGS='-std=c89 -pedantic -Wno-long-long'?




More information about the petsc-dev mailing list