crystal-lang / crystal_lib

Automatic binding generator for native libraries in Crystal
138 stars 30 forks source link

Parse macro definitions within enum declaration #15

Closed ysbaddaden closed 8 years ago

ysbaddaden commented 8 years ago

I stumbled upon some enums in linux libc headers that look like this (I'm trying to automatize Crystal's LibC definitions):

enum {
  IPPROTO_IP = 0,
  #define IPPROTO_IP IPPROTO_IP
  IPPROTO_TCP = 8,
  #define IPPROTO_TCP IPPROTO_TCP
};

When trying to extract the constants, CrystalLib will output the following, when I would have expected the actual integers:

IPPROTO_IP = IPPROTO_IP
IPPROTO_TCP = IPPROTO_TCP

I tried to investigate if we could know that the macro definition was inside an enum but to no avail, the visit(cursor) method first receives the macros then the enum declaration. I can't even export the enum and have constants use its values since the enum is unamed —and this is an internal implementation detail that wouldn't play well with other libc anyway.