Access to enum definition at run time

Yair Lenga via Gcc <[email protected]>
Newsgroups gmane.comp.gcc.devel
Message-ID <CAK3_KpMGe9WQ4iOEistwQKgNrcVaMp=RoE0WD-Dq=SBFpsu+mA@mail.gmail.com>
Hi,

I apologize upfront if this email is outside the scope of this email listing.

I've recently wrote an article about "Automatic Enum Stringification
in C via Build-Time Code Generation"
(https://medium.com/@yair.lenga/automatic-enum-stringification-in-c-via-build-time-code-generation-659b67133125)

The short version is that it extracts enum definitions from the debug
(DWARF) section, and generates functions: enum labels->values, enum
value->enum, enum value iterators. There are no runtime dependencies
on DWARF. Sample Code:

enum country_code {  ISO3_AFG = 4, ... } ;

// This macro trigger including of enum metadata structures.
ENUM_DESCRIBE(country3, country_code)

char * country_label(enum country_code c){
   // Use the meta data to lookup matching label
   return ENUM_LABEL_OF(country3, c) ;
}

Current implementation relies on python tool (using pyelftools) to
extract the enum definition and generate C structures to support the
runtime lookup/enumerations.

As an experiment, I work on GCC plugin that will perform this
generation at compile time - injecting the data structures (mostly
name/value pairs) directly into the object file. I have a prototype
that works (most of the time ...). It is 'ugly', as this is my first
attempt to develop a GCC plugin.

Looking for any feedback about the
1, Idea/concept.
2. The implementation of the pugin (Github URL)
3. Is there a path for making it an experimental/official GCC
extension? (after cleanup, reviews, adding test cases/, etc.)

Github URL:https://github.com/yairlenga/c-enum-reflect/tree/main/gcc-plugin

Thanks for you time!
Yair
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.