Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect handling of enums #24

Closed
jnastarot opened this issue Nov 26, 2024 · 3 comments
Closed

Incorrect handling of enums #24

jnastarot opened this issue Nov 26, 2024 · 3 comments

Comments

@jnastarot
Copy link
Contributor

The litgen tool does not correctly preserve the comments for enum values in the generated .pyi file. Specifically, if the enum value has a comment with the letter "E" or "F" (e.g., SHT_MIPS_OPTIONS = enum.auto() # (= 0x7000000)), these comments are omitted in the generated .pyi file, making the output incorrect and less informative.

def my_litgen_options() -> litgen.LitgenOptions: 
    options = litgen.LitgenOptions()
    
    options.srcmlcpp_options.code_preprocess_function = preprocess_change_source
    options.namespaces_root = ["mainns"]
    options.bind_library = litgen.BindLibraryType.nanobind
    options.python_convert_to_snake_case = False
    return options 

Processed C++ enum

/* MIPS specific definitions.  */
enum MIPS {    
    /* Legal values for sh_type field of Elf32_Shdr.  */
    SHT_MIPS_LIBLIST      = 0x70000000, /* Shared objects used in link.  */
    SHT_MIPS_MSYM         = 0x70000001,
    SHT_MIPS_CONFLICT     = 0x70000002, /* Conflicting symbols.  */
    SHT_MIPS_GPTAB        = 0x70000003, /* Global data area sizes.  */
    SHT_MIPS_UCODE        = 0x70000004, /* Reserved for SGI/MIPS compilers */
    SHT_MIPS_DEBUG        = 0x70000005, /* MIPS ECOFF debugging info.  */
    SHT_MIPS_REGINFO      = 0x70000006, /* Register usage information.  */
    SHT_MIPS_PACKAGE      = 0x70000007,
    SHT_MIPS_PACKSYM      = 0x70000008,
    SHT_MIPS_RELD         = 0x70000009,
    SHT_MIPS_IFACE        = 0x7000000b,
    SHT_MIPS_CONTENT      = 0x7000000c,
    SHT_MIPS_OPTIONS      = 0x7000000d, /* Miscellaneous options.  */
    SHT_MIPS_SHDR         = 0x70000010,
    SHT_MIPS_FDESC        = 0x70000011,
    SHT_MIPS_EXTSYM       = 0x70000012,
    SHT_MIPS_DENSE        = 0x70000013,
    SHT_MIPS_PDESC        = 0x70000014,
    SHT_MIPS_LOCSYM       = 0x70000015,
    SHT_MIPS_AUXSYM       = 0x70000016,
    SHT_MIPS_OPTSYM       = 0x70000017,
    SHT_MIPS_LOCSTR       = 0x70000018,
    SHT_MIPS_LINE         = 0x70000019,
    SHT_MIPS_RFDESC       = 0x7000001a,
    SHT_MIPS_DELTASYM     = 0x7000001b,
    SHT_MIPS_DELTAINST    = 0x7000001c,
    SHT_MIPS_DELTACLASS   = 0x7000001d,
    SHT_MIPS_DWARF        = 0x7000001e, /* DWARF debugging information.  */
    SHT_MIPS_DELTADECL    = 0x7000001f,
    SHT_MIPS_SYMBOL_LIB   = 0x70000020,
    SHT_MIPS_EVENTS       = 0x70000021, /* Event section.  */
    SHT_MIPS_TRANSLATE    = 0x70000022,
    SHT_MIPS_PIXIE        = 0x70000023,
    SHT_MIPS_XLATE        = 0x70000024,
    SHT_MIPS_XLATE_DEBUG  = 0x70000025,
    SHT_MIPS_WHIRL        = 0x70000026,
    SHT_MIPS_EH_REGION    = 0x70000027,
    SHT_MIPS_XLATE_OLD    = 0x70000028,
    SHT_MIPS_PDR_EXCEPTION = 0x70000029
};

Generated Pyi file

class MIPS(enum.Enum):
    """ MIPS specific definitions."""
    # Legal values for sh_type field of Elf32_Shdr.
    SHT_MIPS_LIBLIST = enum.auto()       # (= 0x70000000)  # Shared objects used in link.
    SHT_MIPS_MSYM = enum.auto()          # (= 0x70000001)
    SHT_MIPS_CONFLICT = enum.auto()      # (= 0x70000002)  # Conflicting symbols.
    SHT_MIPS_GPTAB = enum.auto()         # (= 0x70000003)  # Global data area sizes.
    SHT_MIPS_UCODE = enum.auto()         # (= 0x70000004)  # Reserved for SGI/MIPS compilers
    SHT_MIPS_DEBUG = enum.auto()         # (= 0x70000005)  # MIPS ECOFF debugging info.
    SHT_MIPS_REGINFO = enum.auto()       # (= 0x70000006)  # Register usage information.
    SHT_MIPS_PACKAGE = enum.auto()       # (= 0x70000007)
    SHT_MIPS_PACKSYM = enum.auto()       # (= 0x70000008)
    SHT_MIPS_RELD = enum.auto()          # (= 0x70000009)
    SHT_MIPS_IFACE = enum.auto()         # (= 0x7000000b)
    SHT_MIPS_CONTENT = enum.auto()       # (= 0x7000000c)
    SHT_MIPS_OPTIONS = enum.auto()       # (= 0x7000000)  # Miscellaneous options.
    SHT_MIPS_SHDR = enum.auto()          # (= 0x70000010)
    SHT_MIPS_FDESC = enum.auto()         # (= 0x70000011)
    SHT_MIPS_EXTSYM = enum.auto()        # (= 0x70000012)
    SHT_MIPS_DENSE = enum.auto()         # (= 0x70000013)
    SHT_MIPS_PDESC = enum.auto()         # (= 0x70000014)
    SHT_MIPS_LOCSYM = enum.auto()        # (= 0x70000015)
    SHT_MIPS_AUXSYM = enum.auto()        # (= 0x70000016)
    SHT_MIPS_OPTSYM = enum.auto()        # (= 0x70000017)
    SHT_MIPS_LOCSTR = enum.auto()        # (= 0x70000018)
    SHT_MIPS_LINE = enum.auto()          # (= 0x70000019)
    SHT_MIPS_RFDESC = enum.auto()        # (= 0x7000001a)
    SHT_MIPS_DELTASYM = enum.auto()      # (= 0x7000001b)
    SHT_MIPS_DELTAINST = enum.auto()     # (= 0x7000001c)
    SHT_MIPS_DELTACLASS = enum.auto()    # (= 0x7000001)
    SHT_MIPS_DWARF = enum.auto()         # (= 0x7000001e)  # DWARF debugging information.
    SHT_MIPS_DELTADECL = enum.auto()     # (= 0x7000001)
    SHT_MIPS_SYMBOL_LIB = enum.auto()    # (= 0x70000020)
    SHT_MIPS_EVENTS = enum.auto()        # (= 0x70000021)  # Event section.
    SHT_MIPS_TRANSLATE = enum.auto()     # (= 0x70000022)
    SHT_MIPS_PIXIE = enum.auto()         # (= 0x70000023)
    SHT_MIPS_XLATE = enum.auto()         # (= 0x70000024)
    SHT_MIPS_XLATE_DEBUG = enum.auto()   # (= 0x70000025)
    SHT_MIPS_WHIRL = enum.auto()         # (= 0x70000026)
    SHT_MIPS_EH_REGION = enum.auto()     # (= 0x70000027)
    SHT_MIPS_XLATE_OLD = enum.auto()     # (= 0x70000028)
    SHT_MIPS_PDR_EXCEPTION = enum.auto() # (= 0x70000029)

The problem appears in the comments for SHT_MIPS_OPTIONS, SHT_MIPS_DELTACLASS, SHT_MIPS_DELTADECL

@jnastarot jnastarot changed the title Problem Incorrect handling of enums Nov 26, 2024
@jnastarot
Copy link
Contributor Author

I forgot to remove options.srcmlcpp_options.code_preprocess_function = preprocess_change_source from the config ;)
But it does nothing in my case.

@jnastarot
Copy link
Contributor Author

It looks like there is a problem with the regex:

([+-]?[0-9]+([.][0-9]*)?|[.][0-9]+)(d?) -> \1

I think it should be like this

(?:[^a-zA-Z0-9]|\b)([+-]?[0-9]+(?:\.[0-9]*)?|\.[0-9]+)([df]?)\b

@pthom
Copy link
Owner

pthom commented Nov 29, 2024

Hello, thanks for letting me know !

I corrected it. FYI, the final regex is even more complex:

(?<![a-zA-Z0-9])([+-]?[0-9]+(?:\.[0-9]*)?(?:[eE][+-]?[0-9]+)?)([fFdD])(?=\W|$) -> \1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants