Add exclude_return and exclude_target code generation options

This commit is contained in:
Jonathan Müller 2017-05-25 21:04:50 +02:00
commit c86e950847
5 changed files with 151 additions and 29 deletions

View file

@ -8,6 +8,7 @@
#include <cstring>
#include <type_safe/index.hpp>
#include <type_safe/flag_set.hpp>
#include <cppast/cpp_entity.hpp>
#include <cppast/cpp_entity_ref.hpp>
@ -129,14 +130,19 @@ namespace cppast
code_generator& operator=(const code_generator&) = delete;
virtual ~code_generator() noexcept = default;
/// Options that control the synopsis.
enum synopsis_options
/// Flags that control the synopsis.
enum synopsis_flags
{
exclude, //< Exclude the entire entity.
declaration, //< Only write declaration.
definition, //< Also write definition.
exclude, //< Exclude the entire entity.
exclude_return, //< Exclude the return type of a function entity.
exclude_target, //< Exclude the underlying entity of an alias (e.g. typedef).
declaration, //< Only write declaration.
_flag_set_size, //< \exclude
};
/// Options that control the synopsis.
using synopsis_options = type_safe::flag_set<synopsis_flags>;
/// Sentinel type used to output a given entity.
class output
{
@ -177,10 +183,16 @@ namespace cppast
return options_ != exclude;
}
/// \returns The synopsis options.
synopsis_options options() const noexcept
{
return options_;
}
/// \returns Whether or not the definition should be generated as well.
bool generate_definition() const noexcept
{
return options_ == definition;
return !(options_ & declaration);
}
/// \returns A reference to the generator.
@ -280,6 +292,14 @@ namespace cppast
return *this;
}
/// \effects Calls `do_write_excluded()`.
const output& excluded(const cpp_entity& e) const
{
if (*this)
gen_->do_write_excluded(e);
return *this;
}
/// \effects Calls `do_write_newline()`.
const output& operator<<(newl_t) const
{
@ -309,11 +329,11 @@ namespace cppast
/// \effects Will be invoked before code of a container entity is generated.
/// The base class version has no effect.
/// \returns The synopsis options for that entity,
/// the base class version always returns `definition`.
/// the base class version always returns no special options.
virtual synopsis_options on_container_begin(const cpp_entity& e)
{
(void)e;
return definition;
return {};
}
/// \effects Will be invoked after all code of a container entity has been generated.
@ -326,11 +346,11 @@ namespace cppast
/// \effects Will be invoked before code of a non-container entity is generated.
/// The base class version has no effect.
/// \returns The synopsis options for that entity,
/// the base class version always returns `definition`.
/// the base class version always returns no special options.
virtual synopsis_options on_leaf(const cpp_entity& e)
{
(void)e;
return definition;
return {};
}
/// \effects Will be invoked when the indentation level should be increased by one.
@ -390,6 +410,14 @@ namespace cppast
do_write_token_seq(punct);
}
/// \effects Writes a string for an excluded target or return type for the given entity.
/// The base class version writes the identifier `excluded`.
virtual void do_write_excluded(const cpp_entity& e)
{
(void)e;
do_write_identifier("excluded");
}
/// \effects Writes a newline.
/// It is guaranteed that this is the only way a newline will be printed.
/// The base class forwards to `do_write_token_seq()`.