BerkeleyLab / caffeine

A parallel runtime library for Fortran compilers
https://berkeleylab.github.io/caffeine/
Other
36 stars 7 forks source link

Add script to automate creation of C signature declarations from BIND(C) interfaces #63

Open ktras opened 5 months ago

ktras commented 5 months ago

Add script to automate creation of C signature declarations from BIND(C) interfaces in caffeine_h_m. Script only needs to be general enough to handle interfaces in caffeine_h_m.

bonachea commented 4 months ago

To clarify, the motivation for this request is the fact that bind(C) functions that cross the C-Fortran language barrier have to be redundantly declared (once in each language). The linker effectively checks for spelling errors in the function names that might not match between redundant declarations. However no compiler is capable of checking that the argument signatures for the two declarations for the same function actually match each other, and if/when they do not match the result of calls to those functions can be silent undefined behavior.

This insidious problem has already independently arisen multiple times in the course of Caffeine development, despite the relatively small number of relevant functions. I expect this will be an ongoing QA problem until we devise an automated mechanism to detect such defects.

The idea with this enhancement is an automated script that is just barely smart enough to parse the BIND(C) Fortran interfaces defined in src/caffeine/caffeine_h_m.f90 and emit a generated C header file containing the conforming C-side declarations. That header file can then be #include'd in caffeine.c, enabling the C compiler to detect mismatched function argument signatures and flag an error.