Add and parse unexposed entity

This commit is contained in:
Jonathan Müller 2017-03-30 12:04:18 +02:00
commit df684694cc
8 changed files with 94 additions and 2 deletions

View file

@ -119,6 +119,40 @@ namespace cppast
friend detail::intrusive_list_node<cpp_entity>;
};
/// A [cppast::cpp_entity]() that isn't exposed directly.
///
/// The only information available is the raw source code.
class cpp_unexposed_entity final : public cpp_entity
{
public:
static cpp_entity_kind kind() noexcept;
/// \returns A newly built and registered unexposed entity.
/// \notes It will be registered as a declaration.
static std::unique_ptr<cpp_entity> build(const cpp_entity_index& index, cpp_entity_id id,
std::string name, std::string spelling);
/// \returns A newly built unnamed unexposed entity.
/// It will not be registered.
static std::unique_ptr<cpp_entity> build(std::string spelling);
/// \returns The spelling of that entity.
const std::string& spelling() const noexcept
{
return spelling_;
}
private:
cpp_unexposed_entity(std::string name, std::string spelling)
: cpp_entity(std::move(name)), spelling_(std::move(spelling))
{
}
cpp_entity_kind do_get_entity_kind() const noexcept override;
std::string spelling_;
};
/// \returns Whether or not the entity is templated.
/// If this function returns `true` that means the entity is not the "real" entity,
/// but contains just the information for the template which is the parent entity.

View file

@ -55,6 +55,8 @@ namespace cppast
class_template_t,
class_template_specialization_t,
unexposed_t,
count,
};

View file

@ -810,6 +810,13 @@ namespace
generate_class(generator, templ.class_(), type_safe::ref(templ));
}
}
void generate_unexposed(code_generator& generator, const cpp_unexposed_entity& entity)
{
code_generator::output output(type_safe::ref(generator), type_safe::ref(entity), false);
if (output)
output << token_seq(entity.spelling());
}
}
void cppast::generate_code(code_generator& generator, const cpp_entity& e)
@ -863,6 +870,10 @@ void cppast::generate_code(code_generator& generator, const cpp_entity& e)
CPPAST_DETAIL_HANDLE(class_template)
CPPAST_DETAIL_HANDLE(class_template_specialization)
case cpp_entity_kind::unexposed_t:
generate_unexposed(generator, static_cast<const cpp_unexposed_entity&>(e));
break;
#undef CPPAST_DETAIL_HANDLE
case cpp_entity_kind::count:

View file

@ -4,10 +4,36 @@
#include <cppast/cpp_entity.hpp>
#include <cppast/cpp_entity_index.hpp>
#include <cppast/cpp_entity_kind.hpp>
using namespace cppast;
cpp_entity_kind cpp_unexposed_entity::kind() noexcept
{
return cpp_entity_kind::unexposed_t;
}
std::unique_ptr<cpp_entity> cpp_unexposed_entity::build(const cpp_entity_index& index,
cpp_entity_id id, std::string name,
std::string spelling)
{
std::unique_ptr<cpp_entity> result(
new cpp_unexposed_entity(std::move(name), std::move(spelling)));
index.register_forward_declaration(id, type_safe::ref(*result));
return result;
}
std::unique_ptr<cpp_entity> cpp_unexposed_entity::build(std::string spelling)
{
return std::unique_ptr<cpp_entity>(new cpp_unexposed_entity("", std::move(spelling)));
}
cpp_entity_kind cpp_unexposed_entity::do_get_entity_kind() const noexcept
{
return kind();
}
bool cppast::is_templated(const cpp_entity& e) noexcept
{
if (!e.parent())

View file

@ -85,6 +85,9 @@ const char* cppast::to_string(cpp_entity_kind kind) noexcept
case cpp_entity_kind::class_template_specialization_t:
return "class tempalte specialization";
case cpp_entity_kind::unexposed_t:
return "unexposed entity";
case cpp_entity_kind::count:
break;
}
@ -130,6 +133,7 @@ bool cppast::is_type(cpp_entity_kind kind) noexcept
case cpp_entity_kind::function_template_specialization_t:
case cpp_entity_kind::class_template_t:
case cpp_entity_kind::class_template_specialization_t:
case cpp_entity_kind::unexposed_t:
case cpp_entity_kind::count:
break;
}
@ -175,6 +179,7 @@ bool cppast::is_function(cpp_entity_kind kind) noexcept
case cpp_entity_kind::function_template_specialization_t:
case cpp_entity_kind::class_template_t:
case cpp_entity_kind::class_template_specialization_t:
case cpp_entity_kind::unexposed_t:
case cpp_entity_kind::count:
break;
}
@ -220,6 +225,7 @@ bool cppast::is_parameter(cpp_entity_kind kind) noexcept
case cpp_entity_kind::function_template_specialization_t:
case cpp_entity_kind::class_template_t:
case cpp_entity_kind::class_template_specialization_t:
case cpp_entity_kind::unexposed_t:
case cpp_entity_kind::count:
break;
}
@ -264,6 +270,7 @@ bool cppast::is_template(cpp_entity_kind kind) noexcept
case cpp_entity_kind::template_type_parameter_t:
case cpp_entity_kind::non_type_template_parameter_t:
case cpp_entity_kind::template_template_parameter_t:
case cpp_entity_kind::unexposed_t:
case cpp_entity_kind::count:
break;
}
@ -309,6 +316,7 @@ bool cppast::is_template_specialization(cpp_entity_kind kind) noexcept
case cpp_entity_kind::variable_template_t:
case cpp_entity_kind::function_template_t:
case cpp_entity_kind::class_template_t:
case cpp_entity_kind::unexposed_t:
case cpp_entity_kind::count:
break;
}

View file

@ -148,7 +148,16 @@ std::unique_ptr<cpp_entity> detail::parse_entity(const detail::parse_context& co
context.logger->log("libclang parser",
diagnostic{std::move(msg), detail::make_location(cur), severity::warning});
return nullptr;
// build unexposed entity
auto name = detail::get_cursor_name(cur);
detail::tokenizer tokenizer(context.tu, context.file, cur);
detail::token_stream stream(tokenizer, cur);
auto spelling = detail::to_string(stream, stream.end());
if (name.empty())
return cpp_unexposed_entity::build(std::move(spelling));
else
return cpp_unexposed_entity::build(*context.idx, detail::get_entity_id(cur), name.c_str(),
std::move(spelling));
}
catch (parse_error& ex)
{

View file

@ -93,6 +93,7 @@ bool detail::visit(const cpp_entity& e, detail::visitor_callback_t cb, void* fun
case cpp_entity_kind::destructor_t:
case cpp_entity_kind::template_type_parameter_t:
case cpp_entity_kind::non_type_template_parameter_t:
case cpp_entity_kind::unexposed_t:
return cb(functor, e, visitor_info::leaf_entity);
case cpp_entity_kind::count:

View file

@ -32,7 +32,8 @@ enum class bar
void func(int(*)(int));
extern void(* ptr)(int(*)(int))=&func;
)";
template<typename T>int var;)";
auto file = parse({}, "code_generator.cpp", code);
REQUIRE(get_code(*file) == code);