Access to enum definition at run time
Yair Lenga seeks feedback on a GCC plugin that generates enum metadata at compile time for runtime access in C.
Yair Lenga is seeking feedback on a GCC plugin that extracts enum definitions and generates C structures to support runtime lookup and enumeration. The current implementation relies on a Python tool to extract enum definitions from the DWARF section, but Lenga is experimenting with a GCC plugin to perform this generation at compile time. He is looking for feedback on the idea, the plugin implementation, and the possibility of making it an official GCC extension.
- proposer
Seeks feedback on a GCC plugin that enables runtime access to enum definitions by generating metadata at compile time.
“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.)”
- other
Responded to the initial email but the content is not included in the provided data.
- contributor
Suggests that C++26 reflection features might eliminate the need for a plugin for this functionality.
“BTW, in C++26 one doesn't need a plugin for such things, in a few line”
- proposer
Clarifies that the focus is on the C compiler and asks if C programs can access enum metadata stored in structures/variables.
“My focus is on the C compiler. Assuming the enum "metadata" is stored in a structure/variables that are readable by the program - can C programs access it ? Does it apply to "C" enums, or only to "C++" enum classes.”
- contributor
Explains how C enums can be accessed using reflection in C++26.
“Any kind of enum. It is roughly #include <meta> template <typename E> constexpr const char * enum_to_string (E v) { template for (constexpr auto e : std::define_static_array (enumera”
- contributor
Points to a list of known GCC plugins.
Technical Tradeoffs
- Increased compile time due to plugin execution.
- Potential for increased object file size due to injected metadata.
- Complexity of maintaining a GCC plugin.
- Alternative approaches exist, such as using C++26 reflection or post-compile DWARF analysis.
In Details
This thread discusses a GCC plugin that uses compile-time reflection to generate metadata for C enums, enabling runtime access to enum names and values. GCC plugins are loaded by the compiler at startup. The plugin would inject data structures into the object file, mapping enum values to their names. Inspection of DWARF is an alternative approach for post-compile analysis.
For Context
Enumerations (enums) in C allow programmers to define a set of named integer constants. By default, the names of these constants are only available at compile time. This proposal aims to provide a way to access the names of enum constants at runtime, which can be useful for debugging, serialization, and other tasks. The approach involves creating a GCC plugin, which is a way to extend the compiler's functionality with custom code.