Skip to content

Support extracting macro numeric and string literal constants #390

@keynmol

Description

@keynmol

Currently, code like #define CONST 0x25 does not register with sn-bindgen at all.

Some libraries have a lot of constants, requiring the users of bindings to manually add the constants, e.g. https://github.com/libsdl-org/SDL/blob/SDL2/include/SDL_audio.h#L96 and https://github.com/JD557/minart/blob/master/backend/native/src/main/scala/sdl2/SDL.scala#L99-L133

It seems that if we set a particular flag during parsing, we can get access to macro definitions – then we manually reparse a list of tokens to one of the known patterns used by C libraries, and define those macro constants inline.

  1. libclang flag: CXTranslationUnit_DetailedPreprocessingRecord
  2. tokenisation: https://clang.llvm.org/doxygen/group__CINDEX__LEX.html#ga6b315a71102d4f6c95eb68894a3bda8a
  3. The cursor type is https://clang.llvm.org/doxygen/group__CINDEX.html#ggaaccc432245b4cd9f2d470913f9ef0013aaf2a8a83171b883cabe83ef3d729e138 (CXCursor_MacroDefinition)

This feature should be delivered in steps:

  1. Extract numeric constants (integral and floating point) e.g. #define AUDIO_S16LSB 0x8010
  2. Extract string literals #define BLA "string"
  3. Handle single back references e.g.
#define AUDIO_U16MSB    0x1010  /**< As above, but big-endian byte order */
#define AUDIO_S16MSB    0x9010  /**< As above, but big-endian byte order */
#define AUDIO_U16       AUDIO_U16LSB
#define AUDIO_S16       AUDIO_S16LSB
  1. Handle arithmetic/bit manipulation logic (only allow referencing previously registered constants)
#define SDL_AUDIO_ALLOW_FREQUENCY_CHANGE    0x00000001
#define SDL_AUDIO_ALLOW_FORMAT_CHANGE       0x00000002
#define SDL_AUDIO_ALLOW_CHANNELS_CHANGE     0x00000004
#define SDL_AUDIO_ALLOW_SAMPLES_CHANGE      0x00000008
#define SDL_AUDIO_ALLOW_ANY_CHANGE          (SDL_AUDIO_ALLOW_FREQUENCY_CHANGE|SDL_AUDIO_ALLOW_FORMAT_CHANGE|SDL_AUDIO_ALLOW_CHANNELS_CHANGE|SDL_AUDIO_ALLOW_SAMPLES_CHANGE)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions