nim-lang / c2nim

c2nim is a tool to translate Ansi C code to Nim. The output is human-readable Nim code that is meant to be tweaked by hand before and after the translation process.
MIT License
509 stars 63 forks source link

Reorder comments, function attributes, and macro def args #251

Closed elcritch closed 1 year ago

elcritch commented 1 year ago

Some changes I've wanted for a while. Still a bit of a WIP.

Example usage:

c2nim \
  --strict --header \
  --stdints # gives: --mangle:'{u?}int{\d+}_t=$1int$2' \
  --reordercomments \
  --def:RCL_PUBLIC='__attribute__ ()' \
  --def:RCL_WARN_UNUSED='__attribute__ ()' \
    testsuite/tests/rcl_arguments.h

Example re-order comments:

  /** * allocator objects.  */
  void * state;

becomes:

  void * state; ##\
      ## allocator objects. 

Parsing __attributes__ and --def: makes it posible to handle the GCC C function pragmas style. For example --def:RCL_WARN_UNUSED='__attribute__ ()' and --def:RCL_PUBLIC='__attribute__ ()' will make the below parse:

// function defined with macros attributes:
RCL_PUBLIC RCL_WARN_UNUSED int rcl_arguments_get_count_unparsed(const rcl_arguments_t * args);
// expands to:
__attribute__(public) __attribute__(...) int rcl_arguments_get_count_unparsed(const rcl_arguments_t * args);

That last change is responsible for a good portion of C files not parsing properly. Eventually it'd be nice to add pragmas to the Nim procs so people could define them, but that's a project for another day.

elcritch commented 1 year ago

Ok, I think this is ready. The new stuff has tests. Also added: