Parse cpp_function_template
This commit is contained in:
parent
d6f0997fb6
commit
eeb48f1df5
18 changed files with 397 additions and 60 deletions
|
|
@ -37,41 +37,26 @@ template <typename T>
|
|||
using h = g<T, a>;
|
||||
)";
|
||||
|
||||
auto check_parameters =
|
||||
[](const cpp_alias_template& alias,
|
||||
std::initializer_list<std::pair<cpp_entity_kind, const char*>> params) {
|
||||
// no need to check more
|
||||
auto cur = params.begin();
|
||||
for (auto& param : alias.parameters())
|
||||
{
|
||||
REQUIRE(cur != params.end());
|
||||
REQUIRE(param.kind() == cur->first);
|
||||
REQUIRE(param.name() == cur->second);
|
||||
++cur;
|
||||
}
|
||||
REQUIRE(cur == params.end());
|
||||
};
|
||||
|
||||
cpp_entity_index idx;
|
||||
auto file = parse(idx, "cpp_alias_template.cpp", code);
|
||||
auto count = test_visit<cpp_alias_template>(*file, [&](const cpp_alias_template& alias) {
|
||||
if (alias.name() == "a")
|
||||
{
|
||||
check_parameters(alias, {{cpp_entity_kind::template_type_parameter_t, "T"}});
|
||||
check_template_parameters(alias, {{cpp_entity_kind::template_type_parameter_t, "T"}});
|
||||
REQUIRE(equal_types(idx, alias.type_alias().underlying_type(),
|
||||
*cpp_builtin_type::build("int")));
|
||||
}
|
||||
else if (alias.name() == "b")
|
||||
{
|
||||
check_parameters(alias, {{cpp_entity_kind::non_type_template_parameter_t, "I"},
|
||||
{cpp_entity_kind::template_type_parameter_t, "T"}});
|
||||
check_template_parameters(alias, {{cpp_entity_kind::non_type_template_parameter_t, "I"},
|
||||
{cpp_entity_kind::template_type_parameter_t, "T"}});
|
||||
REQUIRE(equal_types(idx, alias.type_alias().underlying_type(),
|
||||
*cpp_template_parameter_type::build(
|
||||
cpp_template_type_parameter_ref(cpp_entity_id(""), "T"))));
|
||||
}
|
||||
else if (alias.name() == "c")
|
||||
{
|
||||
check_parameters(alias, {{cpp_entity_kind::template_type_parameter_t, "T"}});
|
||||
check_template_parameters(alias, {{cpp_entity_kind::template_type_parameter_t, "T"}});
|
||||
auto param = cpp_template_parameter_type::build(
|
||||
cpp_template_type_parameter_ref(cpp_entity_id(""), "T"));
|
||||
REQUIRE(equal_types(idx, alias.type_alias().underlying_type(),
|
||||
|
|
@ -80,7 +65,7 @@ using h = g<T, a>;
|
|||
}
|
||||
else if (alias.name() == "d")
|
||||
{
|
||||
check_parameters(alias, {{cpp_entity_kind::template_type_parameter_t, "T"}});
|
||||
check_template_parameters(alias, {{cpp_entity_kind::template_type_parameter_t, "T"}});
|
||||
|
||||
cpp_template_instantiation_type::builder builder(
|
||||
cpp_template_ref(cpp_entity_id(""), "a"));
|
||||
|
|
@ -89,7 +74,8 @@ using h = g<T, a>;
|
|||
}
|
||||
else if (alias.name() == "e")
|
||||
{
|
||||
check_parameters(alias, {{cpp_entity_kind::non_type_template_parameter_t, "I"}});
|
||||
check_template_parameters(alias,
|
||||
{{cpp_entity_kind::non_type_template_parameter_t, "I"}});
|
||||
|
||||
cpp_template_instantiation_type::builder builder(
|
||||
cpp_template_ref(cpp_entity_id(""), "b"));
|
||||
|
|
@ -100,7 +86,8 @@ using h = g<T, a>;
|
|||
}
|
||||
else if (alias.name() == "f")
|
||||
{
|
||||
check_parameters(alias, {{cpp_entity_kind::non_type_template_parameter_t, "I"}});
|
||||
check_template_parameters(alias,
|
||||
{{cpp_entity_kind::non_type_template_parameter_t, "I"}});
|
||||
|
||||
cpp_template_instantiation_type::builder builder(
|
||||
cpp_template_ref(cpp_entity_id(""), "b"));
|
||||
|
|
@ -111,8 +98,9 @@ using h = g<T, a>;
|
|||
}
|
||||
else if (alias.name() == "g")
|
||||
{
|
||||
check_parameters(alias, {{cpp_entity_kind::template_type_parameter_t, "T"},
|
||||
{cpp_entity_kind::template_template_parameter_t, "Templ"}});
|
||||
check_template_parameters(alias,
|
||||
{{cpp_entity_kind::template_type_parameter_t, "T"},
|
||||
{cpp_entity_kind::template_template_parameter_t, "Templ"}});
|
||||
|
||||
cpp_template_instantiation_type::builder builder(
|
||||
cpp_template_ref(cpp_entity_id(""), "Templ"));
|
||||
|
|
@ -121,7 +109,7 @@ using h = g<T, a>;
|
|||
}
|
||||
else if (alias.name() == "h")
|
||||
{
|
||||
check_parameters(alias, {{cpp_entity_kind::template_type_parameter_t, "T"}});
|
||||
check_template_parameters(alias, {{cpp_entity_kind::template_type_parameter_t, "T"}});
|
||||
|
||||
cpp_template_instantiation_type::builder builder(
|
||||
cpp_template_ref(cpp_entity_id(""), "g"));
|
||||
|
|
|
|||
142
test/cpp_function_template.cpp
Normal file
142
test/cpp_function_template.cpp
Normal file
|
|
@ -0,0 +1,142 @@
|
|||
// 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_function_template.hpp>
|
||||
|
||||
#include <cppast/cpp_member_function.hpp>
|
||||
|
||||
#include "test_parser.hpp"
|
||||
|
||||
using namespace cppast;
|
||||
|
||||
TEST_CASE("cpp_function_template")
|
||||
{
|
||||
// only check templated related stuff
|
||||
auto code = R"(
|
||||
template <typename T>
|
||||
T a(const T& t);
|
||||
|
||||
template <int I>
|
||||
using type = int;
|
||||
|
||||
struct d
|
||||
{
|
||||
template <int I, typename T>
|
||||
static type<I> b(T);
|
||||
|
||||
template <typename T = const int>
|
||||
auto c() -> T;
|
||||
|
||||
template <typename T>
|
||||
operator T() const;
|
||||
|
||||
template <typename T>
|
||||
d(const T&);
|
||||
};
|
||||
)";
|
||||
|
||||
cpp_entity_index idx;
|
||||
auto file = parse(idx, "cpp_function_template.cpp", code);
|
||||
auto count = test_visit<cpp_function_template>(*file, [&](const cpp_function_template& tfunc) {
|
||||
if (tfunc.name() == "a")
|
||||
{
|
||||
check_template_parameters(tfunc, {{cpp_entity_kind::template_type_parameter_t, "T"}});
|
||||
|
||||
REQUIRE(tfunc.function().kind() == cpp_entity_kind::function_t);
|
||||
auto& func = static_cast<const cpp_function&>(tfunc.function());
|
||||
|
||||
auto parameter = cpp_template_type_parameter_ref(cpp_entity_id(""), "T");
|
||||
REQUIRE(equal_types(idx, func.return_type(),
|
||||
*cpp_template_parameter_type::build(parameter)));
|
||||
|
||||
auto count = 0u;
|
||||
for (auto& param : func)
|
||||
{
|
||||
++count;
|
||||
REQUIRE(
|
||||
equal_types(idx, param.type(),
|
||||
*cpp_reference_type::
|
||||
build(cpp_cv_qualified_type::
|
||||
build(cpp_template_parameter_type::build(parameter),
|
||||
cpp_cv_const),
|
||||
cpp_ref_lvalue)));
|
||||
}
|
||||
REQUIRE(count == 1u);
|
||||
}
|
||||
else if (tfunc.name() == "b")
|
||||
{
|
||||
check_parent(tfunc, "d", "d::b");
|
||||
check_template_parameters(tfunc, {{cpp_entity_kind::non_type_template_parameter_t, "I"},
|
||||
{cpp_entity_kind::template_type_parameter_t, "T"}});
|
||||
|
||||
REQUIRE(tfunc.function().kind() == cpp_entity_kind::function_t);
|
||||
auto& func = static_cast<const cpp_function&>(tfunc.function());
|
||||
|
||||
cpp_template_instantiation_type::builder builder(
|
||||
cpp_template_ref(cpp_entity_id(""), "type"));
|
||||
builder.add_argument(
|
||||
cpp_unexposed_expression::build(cpp_builtin_type::build("int"), "I"));
|
||||
REQUIRE(equal_types(idx, func.return_type(), *builder.finish()));
|
||||
|
||||
auto type_parameter = cpp_template_type_parameter_ref(cpp_entity_id(""), "T");
|
||||
auto count = 0u;
|
||||
for (auto& param : func)
|
||||
{
|
||||
++count;
|
||||
REQUIRE(equal_types(idx, param.type(),
|
||||
*cpp_template_parameter_type::build(type_parameter)));
|
||||
}
|
||||
REQUIRE(count == 1u);
|
||||
}
|
||||
else if (tfunc.name() == "c")
|
||||
{
|
||||
check_template_parameters(tfunc, {{cpp_entity_kind::template_type_parameter_t, "T"}});
|
||||
|
||||
REQUIRE(tfunc.function().kind() == cpp_entity_kind::member_function_t);
|
||||
auto& func = static_cast<const cpp_member_function&>(tfunc.function());
|
||||
REQUIRE(func.cv_qualifier() == cpp_cv_none);
|
||||
|
||||
auto parameter = cpp_template_type_parameter_ref(cpp_entity_id(""), "T");
|
||||
REQUIRE(equal_types(idx, func.return_type(),
|
||||
*cpp_template_parameter_type::build(parameter)));
|
||||
}
|
||||
else if (tfunc.name() == "operator T")
|
||||
{
|
||||
check_template_parameters(tfunc, {{cpp_entity_kind::template_type_parameter_t, "T"}});
|
||||
|
||||
REQUIRE(tfunc.function().kind() == cpp_entity_kind::conversion_op_t);
|
||||
auto& func = static_cast<const cpp_conversion_op&>(tfunc.function());
|
||||
REQUIRE(func.cv_qualifier() == cpp_cv_const);
|
||||
|
||||
auto parameter = cpp_template_type_parameter_ref(cpp_entity_id(""), "T");
|
||||
REQUIRE(equal_types(idx, func.return_type(),
|
||||
*cpp_template_parameter_type::build(parameter)));
|
||||
}
|
||||
else if (tfunc.name() == "d")
|
||||
{
|
||||
check_template_parameters(tfunc, {{cpp_entity_kind::template_type_parameter_t, "T"}});
|
||||
|
||||
REQUIRE(tfunc.function().kind() == cpp_entity_kind::constructor_t);
|
||||
auto& func = static_cast<const cpp_constructor&>(tfunc.function());
|
||||
|
||||
auto parameter = cpp_template_type_parameter_ref(cpp_entity_id(""), "T");
|
||||
auto count = 0u;
|
||||
for (auto& param : func)
|
||||
{
|
||||
++count;
|
||||
REQUIRE(
|
||||
equal_types(idx, param.type(),
|
||||
*cpp_reference_type::
|
||||
build(cpp_cv_qualified_type::
|
||||
build(cpp_template_parameter_type::build(parameter),
|
||||
cpp_cv_const),
|
||||
cpp_ref_lvalue)));
|
||||
}
|
||||
REQUIRE(count == 1u);
|
||||
}
|
||||
else
|
||||
REQUIRE(false);
|
||||
});
|
||||
REQUIRE(count == 5u);
|
||||
}
|
||||
|
|
@ -151,7 +151,6 @@ struct foo
|
|||
cpp_entity_index idx;
|
||||
auto file = parse(idx, "cpp_conversion_op.cpp", code);
|
||||
auto count = test_visit<cpp_conversion_op>(*file, [&](const cpp_conversion_op& op) {
|
||||
REQUIRE(op.name().empty());
|
||||
REQUIRE(count_children(op) == 0u);
|
||||
REQUIRE(!op.is_variadic());
|
||||
REQUIRE(op.body_kind() == cpp_function_declaration);
|
||||
|
|
@ -161,20 +160,21 @@ struct foo
|
|||
|
||||
if (!op.is_explicit() && !op.is_constexpr())
|
||||
{
|
||||
REQUIRE(op.name() == "operator int &");
|
||||
REQUIRE(equal_types(idx, op.return_type(),
|
||||
*cpp_reference_type::build(cpp_builtin_type::build("int"),
|
||||
cpp_ref_lvalue)));
|
||||
REQUIRE(op.cv_qualifier() == cpp_cv_none);
|
||||
REQUIRE(!op.is_explicit());
|
||||
REQUIRE(!op.is_constexpr());
|
||||
}
|
||||
else if (op.is_explicit() && !op.is_constexpr())
|
||||
{
|
||||
REQUIRE(op.name() == "operator bool");
|
||||
REQUIRE(equal_types(idx, op.return_type(), *cpp_builtin_type::build("bool")));
|
||||
REQUIRE(op.cv_qualifier() == cpp_cv_const);
|
||||
}
|
||||
else if (!op.is_explicit() && op.is_constexpr())
|
||||
{
|
||||
REQUIRE(op.name() == "operator ns::type");
|
||||
REQUIRE(equal_types(idx, op.return_type(),
|
||||
*cpp_user_defined_type::build(
|
||||
cpp_type_ref(cpp_entity_id(""), "ns::type"))));
|
||||
|
|
|
|||
|
|
@ -100,4 +100,20 @@ inline bool equal_expressions(const cppast::cpp_expression& parsed,
|
|||
return false;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void check_template_parameters(
|
||||
const T& templ, std::initializer_list<std::pair<cppast::cpp_entity_kind, const char*>> params)
|
||||
{
|
||||
// no need to check more
|
||||
auto cur = params.begin();
|
||||
for (auto& param : templ.parameters())
|
||||
{
|
||||
REQUIRE(cur != params.end());
|
||||
REQUIRE(param.kind() == cur->first);
|
||||
REQUIRE(param.name() == cur->second);
|
||||
++cur;
|
||||
}
|
||||
REQUIRE(cur == params.end());
|
||||
}
|
||||
|
||||
#endif // CPPAST_TEST_PARSER_HPP_INCLUDED
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue