Parse cpp_namespace_alias
This commit is contained in:
parent
7e1ef01105
commit
45e9e5305b
9 changed files with 161 additions and 11 deletions
|
|
@ -9,6 +9,14 @@
|
||||||
|
|
||||||
namespace cppast
|
namespace cppast
|
||||||
{
|
{
|
||||||
|
enum class cpp_entity_kind;
|
||||||
|
|
||||||
|
/// \exclude
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
|
void check_entity_cast(const cpp_entity& e, cpp_entity_kind expected_kind);
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
/// A basic reference to some kind of [cppast::cpp_entity]().
|
/// A basic reference to some kind of [cppast::cpp_entity]().
|
||||||
template <typename T, typename Predicate>
|
template <typename T, typename Predicate>
|
||||||
class basic_cpp_entity_ref
|
class basic_cpp_entity_ref
|
||||||
|
|
@ -36,6 +44,9 @@ namespace cppast
|
||||||
type_safe::optional_ref<const T> get(const cpp_entity_index& idx) const noexcept
|
type_safe::optional_ref<const T> get(const cpp_entity_index& idx) const noexcept
|
||||||
{
|
{
|
||||||
auto entity = idx.lookup(target_);
|
auto entity = idx.lookup(target_);
|
||||||
|
if (!entity)
|
||||||
|
return type_safe::nullopt;
|
||||||
|
detail::check_entity_cast(entity.value(), T::kind());
|
||||||
return static_cast<const T&>(entity.value());
|
return static_cast<const T&>(entity.value());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -88,12 +88,16 @@ namespace cppast
|
||||||
class cpp_namespace_alias final : public cpp_entity
|
class cpp_namespace_alias final : public cpp_entity
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
static cpp_entity_kind kind() noexcept;
|
||||||
|
|
||||||
/// \returns A newly created and registered namespace alias.
|
/// \returns A newly created and registered namespace alias.
|
||||||
static std::unique_ptr<cpp_namespace_alias> build(const cpp_entity_index& idx,
|
static std::unique_ptr<cpp_namespace_alias> build(const cpp_entity_index& idx,
|
||||||
cpp_entity_id id, std::string name,
|
cpp_entity_id id, std::string name,
|
||||||
cpp_namespace_ref target);
|
cpp_namespace_ref target);
|
||||||
|
|
||||||
/// \returns The [cppast::cpp_namespace_ref]() to the aliased namespace.
|
/// \returns The [cppast::cpp_namespace_ref]() to the aliased namespace.
|
||||||
|
/// \notes If the namespace aliases aliases another namespace alias,
|
||||||
|
/// the target entity will still be the namespace, not the alias.
|
||||||
const cpp_namespace_ref& target() const noexcept
|
const cpp_namespace_ref& target() const noexcept
|
||||||
{
|
{
|
||||||
return target_;
|
return target_;
|
||||||
|
|
|
||||||
15
src/cpp_entity_ref.cpp
Normal file
15
src/cpp_entity_ref.cpp
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
// Copyright (C) 2017 Jonathan Müller <jonathanmueller.dev@gmail.com>
|
||||||
|
// This file is subject to the license terms in the LICENSE file
|
||||||
|
// found in the top-level directory of this distribution.
|
||||||
|
|
||||||
|
#include <cppast/cpp_entity_ref.hpp>
|
||||||
|
|
||||||
|
#include <cppast/cpp_entity.hpp>
|
||||||
|
|
||||||
|
using namespace cppast;
|
||||||
|
|
||||||
|
void detail::check_entity_cast(const cpp_entity& e, cpp_entity_kind expected_kind)
|
||||||
|
{
|
||||||
|
DEBUG_ASSERT(e.kind() == expected_kind, detail::precondition_error_handler{},
|
||||||
|
"mismatched entity kind");
|
||||||
|
}
|
||||||
|
|
@ -33,11 +33,16 @@ std::unique_ptr<cpp_namespace_alias> cpp_namespace_alias::build(const cpp_entity
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
cpp_entity_kind cpp_namespace_alias::do_get_entity_kind() const noexcept
|
cpp_entity_kind cpp_namespace_alias::kind() noexcept
|
||||||
{
|
{
|
||||||
return cpp_entity_kind::namespace_alias_t;
|
return cpp_entity_kind::namespace_alias_t;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cpp_entity_kind cpp_namespace_alias::do_get_entity_kind() const noexcept
|
||||||
|
{
|
||||||
|
return kind();
|
||||||
|
}
|
||||||
|
|
||||||
cpp_entity_kind cpp_using_directive::do_get_entity_kind() const noexcept
|
cpp_entity_kind cpp_using_directive::do_get_entity_kind() const noexcept
|
||||||
{
|
{
|
||||||
return cpp_entity_kind::using_directive_t;
|
return cpp_entity_kind::using_directive_t;
|
||||||
|
|
|
||||||
|
|
@ -15,15 +15,20 @@ namespace cppast
|
||||||
{
|
{
|
||||||
// visits direct children of an entity
|
// visits direct children of an entity
|
||||||
template <typename Func>
|
template <typename Func>
|
||||||
void visit_children(CXCursor parent, Func f)
|
void visit_children(CXCursor parent, Func f, bool recurse = false)
|
||||||
{
|
{
|
||||||
clang_visitChildren(parent,
|
auto continue_lambda = [](CXCursor cur, CXCursor, CXClientData data) {
|
||||||
[](CXCursor cur, CXCursor, CXClientData data) {
|
auto& actual_cb = *static_cast<Func*>(data);
|
||||||
auto& actual_cb = *static_cast<Func*>(data);
|
actual_cb(cur);
|
||||||
actual_cb(cur);
|
return CXChildVisit_Continue;
|
||||||
return CXChildVisit_Continue;
|
};
|
||||||
},
|
auto recurse_lambda = [](CXCursor cur, CXCursor, CXClientData data) {
|
||||||
&f);
|
auto& actual_cb = *static_cast<Func*>(data);
|
||||||
|
actual_cb(cur);
|
||||||
|
return CXChildVisit_Recurse;
|
||||||
|
};
|
||||||
|
|
||||||
|
clang_visitChildren(parent, recurse ? recurse_lambda : continue_lambda, &f);
|
||||||
}
|
}
|
||||||
|
|
||||||
// visits a translation unit
|
// visits a translation unit
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,8 @@ using namespace cppast;
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
cpp_namespace::builder make_builder(const detail::parse_context& context, const CXCursor& cur)
|
cpp_namespace::builder make_ns_builder(const detail::parse_context& context,
|
||||||
|
const CXCursor& cur)
|
||||||
{
|
{
|
||||||
detail::tokenizer tokenizer(context.tu, context.file, cur);
|
detail::tokenizer tokenizer(context.tu, context.file, cur);
|
||||||
detail::token_stream stream(tokenizer, cur);
|
detail::token_stream stream(tokenizer, cur);
|
||||||
|
|
@ -38,7 +39,7 @@ std::unique_ptr<cpp_entity> detail::parse_cpp_namespace(const detail::parse_cont
|
||||||
{
|
{
|
||||||
DEBUG_ASSERT(cur.kind == CXCursor_Namespace, detail::assert_handler{});
|
DEBUG_ASSERT(cur.kind == CXCursor_Namespace, detail::assert_handler{});
|
||||||
|
|
||||||
auto builder = make_builder(context, cur);
|
auto builder = make_ns_builder(context, cur);
|
||||||
detail::visit_children(cur, [&](const CXCursor& cur) {
|
detail::visit_children(cur, [&](const CXCursor& cur) {
|
||||||
auto entity = parse_entity(context, cur);
|
auto entity = parse_entity(context, cur);
|
||||||
if (entity)
|
if (entity)
|
||||||
|
|
@ -46,3 +47,49 @@ std::unique_ptr<cpp_entity> detail::parse_cpp_namespace(const detail::parse_cont
|
||||||
});
|
});
|
||||||
return builder.finish(*context.idx, get_entity_id(cur));
|
return builder.finish(*context.idx, get_entity_id(cur));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
cpp_entity_id ns_alias_target_id(const CXCursor& cur)
|
||||||
|
{
|
||||||
|
cpp_entity_id result("");
|
||||||
|
detail::visit_children(cur,
|
||||||
|
[&](const CXCursor& child) {
|
||||||
|
auto referenced = clang_getCursorReferenced(child);
|
||||||
|
auto kind = clang_getCursorKind(referenced);
|
||||||
|
if (kind == CXCursor_Namespace)
|
||||||
|
result = detail::get_entity_id(referenced);
|
||||||
|
else if (kind == CXCursor_NamespaceAlias)
|
||||||
|
// get target of namespace alias instead
|
||||||
|
result = ns_alias_target_id(referenced);
|
||||||
|
else
|
||||||
|
DEBUG_UNREACHABLE(detail::parse_error_handler{}, cur,
|
||||||
|
"unexpected child of namespace alias");
|
||||||
|
},
|
||||||
|
true);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<cpp_entity> detail::parse_cpp_namespace_alias(const detail::parse_context& context,
|
||||||
|
const CXCursor& cur)
|
||||||
|
{
|
||||||
|
DEBUG_ASSERT(cur.kind == CXCursor_NamespaceAlias, detail::assert_handler{});
|
||||||
|
|
||||||
|
detail::tokenizer tokenizer(context.tu, context.file, cur);
|
||||||
|
detail::token_stream stream(tokenizer, cur);
|
||||||
|
|
||||||
|
// namespace <identifier> = <nested identifier>;
|
||||||
|
detail::skip(stream, "namespace");
|
||||||
|
auto name = stream.get().c_str();
|
||||||
|
detail::skip(stream, "=");
|
||||||
|
|
||||||
|
// <nested identifier>;
|
||||||
|
std::string target_name;
|
||||||
|
while (!detail::skip_if(stream, ";"))
|
||||||
|
target_name += stream.get().c_str();
|
||||||
|
|
||||||
|
auto target = cpp_namespace_ref(ns_alias_target_id(cur), std::move(target_name));
|
||||||
|
return cpp_namespace_alias::build(*context.idx, get_entity_id(cur), std::move(name),
|
||||||
|
std::move(target));
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,8 @@ std::unique_ptr<cpp_entity> detail::parse_entity(const detail::parse_context& co
|
||||||
{
|
{
|
||||||
case CXCursor_Namespace:
|
case CXCursor_Namespace:
|
||||||
return parse_cpp_namespace(context, cur);
|
return parse_cpp_namespace(context, cur);
|
||||||
|
case CXCursor_NamespaceAlias:
|
||||||
|
return parse_cpp_namespace_alias(context, cur);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,9 @@ namespace cppast
|
||||||
std::unique_ptr<cpp_entity> parse_cpp_namespace(const parse_context& context,
|
std::unique_ptr<cpp_entity> parse_cpp_namespace(const parse_context& context,
|
||||||
const CXCursor& cur);
|
const CXCursor& cur);
|
||||||
|
|
||||||
|
std::unique_ptr<cpp_entity> parse_cpp_namespace_alias(const parse_context& context,
|
||||||
|
const CXCursor& cur);
|
||||||
|
|
||||||
std::unique_ptr<cpp_entity> parse_entity(const parse_context& context, const CXCursor& cur);
|
std::unique_ptr<cpp_entity> parse_entity(const parse_context& context, const CXCursor& cur);
|
||||||
}
|
}
|
||||||
} // namespace cppast::detail
|
} // namespace cppast::detail
|
||||||
|
|
|
||||||
|
|
@ -50,3 +50,61 @@ namespace c
|
||||||
});
|
});
|
||||||
REQUIRE(count == 4u);
|
REQUIRE(count == 4u);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("cpp_namespace_alias")
|
||||||
|
{
|
||||||
|
auto code = R"(
|
||||||
|
namespace ns1 {}
|
||||||
|
namespace ns2 {}
|
||||||
|
|
||||||
|
namespace a = ns1;
|
||||||
|
namespace b = ns2;
|
||||||
|
|
||||||
|
namespace outer
|
||||||
|
{
|
||||||
|
namespace ns {}
|
||||||
|
|
||||||
|
namespace c = ns;
|
||||||
|
namespace d = ns1;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace e = outer::ns;
|
||||||
|
namespace f = outer::c;
|
||||||
|
)";
|
||||||
|
|
||||||
|
cpp_entity_index idx;
|
||||||
|
auto check_alias = [&](const cpp_namespace_alias& alias, const char* target_name,
|
||||||
|
const char* target_full_name) {
|
||||||
|
auto& target = alias.target();
|
||||||
|
REQUIRE(target.name() == target_name);
|
||||||
|
|
||||||
|
auto entity = target.get(idx);
|
||||||
|
REQUIRE(entity);
|
||||||
|
REQUIRE(full_name(entity.value()) == target_full_name);
|
||||||
|
};
|
||||||
|
|
||||||
|
auto file = parse(idx, "cpp_namespace_alias.cpp", code);
|
||||||
|
auto count = test_visit<cpp_namespace_alias>(*file, [&](const cpp_namespace_alias& alias) {
|
||||||
|
if (alias.name() == "a")
|
||||||
|
check_alias(alias, "ns1", "ns1");
|
||||||
|
else if (alias.name() == "b")
|
||||||
|
check_alias(alias, "ns2", "ns2");
|
||||||
|
else if (alias.name() == "c")
|
||||||
|
{
|
||||||
|
check_parent(alias, "outer", "outer::c");
|
||||||
|
check_alias(alias, "ns", "outer::ns");
|
||||||
|
}
|
||||||
|
else if (alias.name() == "d")
|
||||||
|
{
|
||||||
|
check_parent(alias, "outer", "outer::d");
|
||||||
|
check_alias(alias, "ns1", "ns1");
|
||||||
|
}
|
||||||
|
else if (alias.name() == "e")
|
||||||
|
check_alias(alias, "outer::ns", "outer::ns");
|
||||||
|
else if (alias.name() == "f")
|
||||||
|
check_alias(alias, "outer::c", "outer::ns");
|
||||||
|
else
|
||||||
|
REQUIRE(false);
|
||||||
|
});
|
||||||
|
REQUIRE(count == 6u);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue