In a previous post, Automatic Enum Stringification in C via Build-Time Code Generation , I described how to extract enum labels and values directly from DWARF debug information at build time.
enum color { C_NONE , C_RED , C_YELLOW , C_GREEN } ;
// Request enum descriptor for e_color ENUM_DESCRIBE ( e_color , enum color ) void foo ( enum color c ) { printf ( "Color=%s(%d) \n " , ENUM_LABEL_OF ( e_color , c ), c ) ; }
Enter fullscreen mode
Exit fullscreen mode
In a follow-up article, I wrote about the next logical step: reverse conversion from symbolic name to value.…