[petsc-dev] Request for comments: allow C99 internally

Satish Balay balay at mcs.anl.gov
Sat Mar 7 08:32:01 CST 2020


On Sat, 7 Mar 2020, Patrick Sanan wrote:

> Perhaps naively, I‘d assume that while there may well be someone out there
> relying on compilers for which this would be a problem, that same person is
> also less likely to be able to upgrade PETSc.
> 
> The benefits seem well worth it. It‘ll make things just that much easier to
> work with.
> 
> +1 for the for-loop declarations.
> 
> No more need to police the use of // C++-style comments?

I think the objection was more about the code style. Mixing both types of comments does not look good.

Satish

> 
> 
> 
> Jed Brown <jed at jedbrown.org> schrieb am Sa. 7. März 2020 um 06:48:
> 
> > I have a question for petsc-dev: Do you know anyone who needs to build
> > PETSc with a compiler that doesn't support variadic macros and for-loop
> > declarations?  (Both of these are in C99 and C++11, and supported by all
> > tested configurations including compilers that don't fully implement
> > these standards.)  Both MPICH and Open MPI use variable-length arrays
> > and for-loop declarations, so you'd be hard-pressed building a modern
> > stack with such a compiler.  I'm not proposing that we put these macros
> > unguarded into the public headers, so a user of PETSc could still build
> > with -std=c89 and the like.
> >
> >
> > ## Background
> >
> > There is a common pattern in PETSc where we write
> >
> >   PetscInt some,several,variables;
> >
> >   // code
> > #if defined(PETSC_HAVE_MAGIC)
> >   function(several,&variables);
> > #endif
> >   use(some,variables);
> >
> >
> > Of course this gives unused variable warnings, so we tear our code apart
> > like
> >
> >   PetscInt some,variables;
> > #if defined(PETSC_HAVE_MAGIC)
> >   PetscInt several;
> > #endif
> >
> >   // code
> > #if defined(PETSC_HAVE_MAGIC)
> >   function(several,&variables);
> > #endif
> >   use(some,variables);
> >
> >
> > but the bigger problem is that we need different configurations just to
> > check syntax of our compiled out blocks.  I propose allowing variadic
> > macros (a C99 and C++11 feature) to allow code like
> >
> >   PetscInt some,several,variables;
> >
> >   // code
> >   if (PetscDefined(HAVE_MAGIC)) {
> >     function(several,&variables);
> >   }
> >   use(some,variables);
> >
> >
> > This approach could also be used to avoid needing separate macros for
> > every SETERRQ1-SETERRQ9, etc.  I have an example implementation in this
> > MR, and it passes the full pipeline (after relaxing the -std=c89
> > -pedantic build).
> >
> > https://gitlab.com/petsc/petsc/-/merge_requests/157/diffs
> >
> >
> > We could also consider allowing for-loop declarations, which I believe
> > leads to tighter and more understandable code because the reader doesn't
> > have to wonder whether the variable is used after the loop.
> >
> >   for (PetscInt i=0; i<n; i++) { ... }
> >
> >
> > Note that we cannot use variable-length arrays (VLA) because they are
> > not in the intersection of C and C++.
> >
> 


More information about the petsc-dev mailing list