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

@ -6,11 +6,15 @@
#include "test_parser.hpp"
using namespace cppast;
TEST_CASE("code_generator")
{
// no need to check much here, as each entity check separately
// only write some file with equivalent code and synopsis
auto code = R"(using type=int;
SECTION("basic")
{
// no need to check much here, as each entity check separately
// only write some file with equivalent code and synopsis
auto code = R"(using type=int;
struct foo{
int a;
@ -34,6 +38,60 @@ void func(int(*)(int));
extern void(* ptr)(int(*)(int))=&func;
)";
auto file = parse({}, "code_generator.cpp", code);
REQUIRE(get_code(*file) == code);
auto file = parse({}, "code_generator.cpp", code);
REQUIRE(get_code(*file) == code);
}
SECTION("exclude target")
{
auto code = R"(
namespace a {}
namespace b = a;
using c = int*;
typedef int d;
)";
auto synopsis = R"(namespace a{
}
namespace b=excluded;
using c=excluded;
using d=excluded;
)";
auto file = parse({}, "code_generator_exclude_target.cpp", code);
REQUIRE(get_code(*file, code_generator::exclude_target) == synopsis);
}
SECTION("exclude return")
{
auto code = R"(
void a();
template <typename T>
auto b() -> int*;
struct foo
{
int c() const&;
operator const int ();
};
)";
auto synopsis = R"(excluded a();
template<typename T>
excluded b();
struct foo{
excluded c()const&;
operator excluded();
};
)";
auto file = parse({}, "code_generator_exclude_return.cpp", code);
REQUIRE(get_code(*file, code_generator::exclude_return) == synopsis);
}
}

View file

@ -45,12 +45,26 @@ inline std::unique_ptr<cppast::cpp_file> parse(const cppast::cpp_entity_index& i
class test_generator : public cppast::code_generator
{
public:
test_generator(synopsis_options options) : options_(std::move(options))
{
}
const std::string& str() const noexcept
{
return str_;
}
private:
synopsis_options on_container_begin(const cppast::cpp_entity&) override
{
return options_;
}
synopsis_options on_leaf(const cppast::cpp_entity&) override
{
return options_;
}
void do_indent() override
{
++indent_;
@ -78,14 +92,16 @@ private:
was_newline_ = true;
}
std::string str_;
unsigned indent_ = 0;
bool was_newline_ = false;
std::string str_;
synopsis_options options_;
unsigned indent_ = 0;
bool was_newline_ = false;
};
inline std::string get_code(const cppast::cpp_entity& e)
inline std::string get_code(const cppast::cpp_entity& e,
cppast::code_generator::synopsis_options options = {})
{
test_generator generator;
test_generator generator(options);
cppast::generate_code(generator, e);
auto str = generator.str();
if (!str.empty() && str.back() == '\n')