I'd like to wrap a function-like C macro in a C function (and in turn wrap it in Haskell with a {#fun ... #}
block), but the c2hs
preprocessor chokes on the do.. while(0)
syntax;
here's the code:
module TestMacro where
#c
#define TestF1(n) do{if n==0 return 0;else return 1; } while(0)
int c_testF1(int x)
{ return ( TestF1(x) ); }
#endc
and here's the error:
c2hs TestMacro.chs
c2hs: C header contains errors:
TestMacro.chs.h:6: (column 12) [ERROR] >>> Syntax error !
The symbol `do' does not fit here.
make: *** [main] Error 1
What am I doing wrong?
My goal is wrapping the CHKERRQ
macro of the PETSc library, defined as follows in petscerror.h
(split onto multiple lines for readability):
#define CHKERRQ(n)
do {if (PetscUnlikely(n))
return PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,n,PETSC_ERROR_REPEAT," ");}
while (0)