Recent

Author Topic: fp-h2pas: New C header translation tool using clang  (Read 1157 times)

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1353
  • Professional amateur ;-P
fp-h2pas: New C header translation tool using clang
« on: April 11, 2026, 05:20:21 am »
Hey Y'All,

I've decided that the code that codex generated is now good enough for public consumption.

The repo is: fp-h2pas

It now has full CI/CD support and some pre-built binaries are already there for consumption.

BIG DISCLAIMER: This is still completely A.I. generated. I've not had the time yet to go through the code with a fine tooth comb. So, please, have a look at it and see if it's hallucinating in any way.

In the early stages I asked codex to run it through raylib.h, and make the necessary changes to, at least, get something compilable from that. It left some stuff lying around related to raylib.h that I now have nipped in the bud.

I would really appreciate some help with code reviews on this. I'm still busy updating the OPM's new system/website and don't really have the mental power to dedicate to such a complicated code parsing type of a tool. For that I'm deeply sorry!!!

This all started because I got nerd snipped by a Delphi MVP on the Slack private channel, when he produced this: https://github.com/tregubovav-dev/C2Pas-Jinja
His approach is Python based, and I'm always trying to not go that route.
Also, @retrofoxed journey of producing Pixie with A.I. gave me the motivation to start using codex for this.

Many thanks in advance for any help you can provide!!

Cheers,
Gus
« Last Edit: April 11, 2026, 05:23:06 am by Gustavo 'Gus' Carreno »

ccrause

  • Hero Member
  • *****
  • Posts: 1116
Re: fp-h2pas: New C header translation tool using clang
« Reply #1 on: April 11, 2026, 09:48:31 am »
Hi Gus!  Thank you for sharing your project.  I have bashed my head a lot trying to create create Pascal wrappers for esp-idf. Take esp_wifi.h as an example:

1. How should the environment be prepared to handle included header files, and also header files implicitly used.
  a) Can a manual list of header files be specified so that fp-h2pas can pass these to clang so that it can resolve the references?
  b) Can fp-h2pas parse the header file automatically and try to locate at least the explicit include files?
  c) "Standard" types such as uint32_t are defined in <stdint.h>, can clang be convinced to resolve these definitions automatically?

2. Error messages are currently obscured by the current console text generated, even if an output file was specified.
  a) Do not output the header text by default, make it optional for example only emit this if a verbose option is specified for fp-h2pas.
  b) Perhaps summarize errors at the end of the output stream, this makes it easy to see.

3. Can a custom mapping of C types to Pascal types be specified?  E.g. fpc already knows int8 which corresponds to the C type int8_t.  I feel queasy when I see type definitions declared with the _t appendix.

4. Currently bitpacked structs such as wifi_ap_record_t is not correctly converted to Pascal. A working solution is to use advanced Pascal records with helpers to pack/unpack bits.

BildatBoffin

  • New Member
  • *
  • Posts: 48
Re: fp-h2pas: New C header translation tool using clang
« Reply #2 on: April 11, 2026, 07:22:29 pm »
The appoach used is similar to https://github.com/jacob-carlborg/dstep excepted that dstep uses clang as a library. You might take a look at the source code to eventually detect corner details that you might have missed.

A few years ago I've forked the project to make a translator for styx-lang. One reason for that is that IIRC the ast dump produced by clang did not include the documentation-comments, which are essential (since used to render documentation, typically using Doxygen).

One detail mentioned by 'Gus' is the includes. I think that often this is not a problem because headers are designed with the idea that they have to be translatable to foreign languages.

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1353
  • Professional amateur ;-P
Re: fp-h2pas: New C header translation tool using clang
« Reply #3 on: April 13, 2026, 06:32:22 pm »
Hey ccrause,

Thank you for sharing your project. 

You're more than welcomed!!

If I'm completely honest, this is pretty much in a state of incompleteness. I say this because the only real trial, was against raylib.h, and that was all through codex. I didn't really double check things.
The tool works with 2 passes. Both using clang. The first one is to untangle the pre-processor mess. But it's not fully clang driven.
The first pass then has to be parsed manually in order to make sense of it. Also, any function like macro is not parsed into inline functions in Pascal. That would make it go into quite the dark sides of black magic... :D
The second pass is the AST itself, and at the moment, basic types( mostly covered in the Pascal ctypes unit ), struct types and functions is the only thing it knows.

I have bashed my head a lot trying to create create Pascal wrappers for esp-idf. Take esp_wifi.h as an example:

1. How should the environment be prepared to handle included header files, and also header files implicitly used.
  a) Can a manual list of header files be specified so that fp-h2pas can pass these to clang so that it can resolve the references?
  b) Can fp-h2pas parse the header file automatically and try to locate at least the explicit include files?
  c) "Standard" types such as uint32_t are defined in <stdint.h>, can clang be convinced to resolve these definitions automatically?

1.a) At the moment, the tool accepts a single header file, and outputs the translation to STDOUT, or to a file via -o file.pas, so one file at a time.
1.b) Yes, the parsing is automatic, but it will not drill down on any include files.
1.c) If the type can be matched to the Pascal unit ctypes, you're good. Outside of that, I'm not sure. Remember that after it was generated by codex, I haven't had time to look into the generated code.

2. Error messages are currently obscured by the current console text generated, even if an output file was specified.
  a) Do not output the header text by default, make it optional for example only emit this if a verbose option is specified for fp-h2pas.
  b) Perhaps summarize errors at the end of the output stream, this makes it easy to see.

2.a) I can maybe do that. But these tools are quite crude: Get input from file, spew translation to STDOUT if no flag says otherwise. You can maybe pipe STDERR to another file, just as a quick fix.
2.b) Not a bad idea. I'll put that on my todo list!!

3. Can a custom mapping of C types to Pascal types be specified?  E.g. fpc already knows int8 which corresponds to the C type int8_t.  I feel queasy when I see type definitions declared with the _t appendix.

No, the tool is still quite toy like. So, no advanced features yet.

4. Currently bitpacked structs such as wifi_ap_record_t is not correctly converted to Pascal. A working solution is to use advanced Pascal records with helpers to pack/unpack bits.

Yeah, like I said, if it's under Pascal's ctypes unit, you're golden. Anything outside that, I need to investigate.

Cheers,
Gus

 

TinyPortal © 2005-2018