Skip to content

Pilusx/gcc-plugin-deriving

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gcc-plugin-deriving

This repository contains a proof of concept of a gcc plugin enabling automatic code generation for bidirectional conversion of enums into strings. The idea comes from the deriving Enum attribute used in Haskell. The API is inspired by the magic_enum repository, which seems to be quite complicated and also has some small limitations. It was also a small sanity test of cooperation with Copilot. Anyways reflection in C++26 will probably support printing enums as a basic usecase, so it is left as a cool experimental feature.

Testing

make clean build test

Limitations

As it is just a proof of concept it does not support all the possible edge cases.

Namespace support (no)

There are some small issues with namespaces and the possible solution may require more advanced handling.

Template support (no)

Do not use #pragma deriving in the templated class context. It keeps a seperate type of enum for each template specialization. Thus leading to a lot of redundancy. Apart from that it makes things unnecessarily complex which is counterproductive.

template <typename T> class spdlog {
public:
  enum level_enum : T {
    trace,
    debug,
    info,
    warn,
    err,
    critical,
    off,
    n_levels
  };
  // Don't do it, this will not work...
  // #pragma deriving level_enum Enum
};

Instead do:

class spdlog_base {
public:
  enum level_enum : int {
    trace,
    debug,
    info,
    warn,
    err,
    critical,
    off,
    n_levels
  }
#pragma deriving level_enum Enum
}

template <typename T>
class spdlog {
    using level_enum = spdlog_base::level_enum;
};

template <typename T>
class spdlog : public spdlog_base {
};

Constexpr support (no)

For simplicity constexpr is not supported. From my research it seems to be a quite complicated endeavour to hack gcc to process the autogenerated code in the right order. In Haskell there are no header files, so maybe modules are a better solution for it.

File generation (no)

It is also not supported as it would require per file configuration in multithreaded setup.

Sources

About

#pragma deriving enum - a C++ adaptation of a Haskell feature

Resources

License

Stars

Watchers

Forks