On Thu, Apr 30, 2026 at 1:10 PM Yair Lenga via Gcc <[email protected]> wrote:
>
> 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.)
Inside GCC we're doing the "reverse", we have files like tree.def that do
DEFTREECODE (ERROR_MARK, "error_mark", tcc_exceptional, 0)
and then in the header do
#define DEFTREECODE(SYM, STRING, TYPE, NARGS) SYM,
enum tree_code {
#include "tree.def"
};
and somewhere else
const char *
tree_code_name (enum tree_code t)
{
#define DEFTREECODE(SYM, STRING, TYPE, NARGS) case SYM: return STRING;
switch (t)
{
#include "tree.def"
}
}
Richard.
> Github URL:https://github.com/yairlenga/c-enum-reflect/tree/main/gcc-plugin
>
> Thanks for you time!
> Yair