Scope guards seem like a really good idea. Has there been any effort to write a C to -betterC transpiler just like there has for Rust (e.g. C2Rust [0], Corrode [1])?
Yes. D's design is based around the idea that semantically valid C will compile in D (apart from crud like function pointer syntax etc.) but there are tools to convert C to D (As used in the dmd compiler backend (which dates back to the 80s I believe, but is now in a tasty form of spaghetti D)
> D's design is based around the idea that semantically valid C will compile in D
AFAIK it's that code which compiles in both c and d should have the same behaviour; not that c code should necessarily compile in d. Examples of c that don't work in d are trivial.
> there are tools to convert C to D (As used in the dmd compiler backend (which dates back to the 80s I believe, but is now in a tasty form of spaghetti D)
Dmd's backend was ported by hand. What tools are those? I know there are tools to convert header files, but I don't know of any that actually convert code.
Not quite, the c++ source was massaged to be free of weird corner cases of the grammar and preprocessor (e.g. enforcing no spaces between '#' and the preprocessor keyword) and then a tool called magicport was used to convert the frontend in one shot. The backend was mostly converted by "hand" (i.e. grep after some more massaging).
There are several tools to translate headers though dstep[1] and dpp[2] come to mind
Could you write(apologies if I'm missing something) some documentation on how the backend is organised and the overall motion through it? I've often looked at it but been almost totally unable to grok how any thing actually works beyond the abstract of what the optimisations say on tin.
Could a step forward be to start afresh and then call into the old backend stages but ultimately rewrite them in more bearable code where possible? Might be a compile time trade-off there, to an extent.
[0] https://www.github.com/immunant/c2rust
[1] https://www.github.com/jameysharp/corrode