Add support for type definitions in using/alias

This commit is contained in:
Jonathan Müller 2017-03-02 16:16:02 +01:00
commit a635f5dd05
5 changed files with 85 additions and 11 deletions

View file

@ -29,7 +29,8 @@ bool equal_types(const cpp_entity_index& idx, const cpp_type& parsed, const cpp_
if (user_parsed.name() != user_synthesized.name())
return false;
auto entity = user_parsed.get(idx);
return entity.has_value() && entity.value().name() == user_parsed.name();
return entity.has_value() && entity.value().name().empty()
|| entity.value().name() == user_parsed.name();
}
case cpp_type_kind::cv_qualified:
@ -192,6 +193,11 @@ using r = void(foo::*)(int,...) const &;
// member data pointers
using s = int(foo::*);
// user-defined types inline definition
using t = struct t_ {};
using u = const struct u_ {}*;
using v = struct {};
)";
}
SECTION("typedef")
@ -233,6 +239,11 @@ typedef void(foo::*r)(int,...) const &;
// member data pointers
typedef int(foo::*s);
// user-defined types inline definition
typedef struct t_ {} t;
typedef const struct u_ {}* u;
typedef struct {} v;
)";
}
@ -393,8 +404,25 @@ typedef int(foo::*s);
REQUIRE(equal_types(idx, alias.underlying_type(), *type));
}
else if (alias.name() == "t")
{
auto type = cpp_user_defined_type::build(cpp_type_ref(cpp_entity_id(""), "t_"));
REQUIRE(equal_types(idx, alias.underlying_type(), *type));
}
else if (alias.name() == "u")
{
auto type = cpp_pointer_type::build(
add_cv(cpp_user_defined_type::build(cpp_type_ref(cpp_entity_id(""), "u_")),
cpp_cv_const));
REQUIRE(equal_types(idx, alias.underlying_type(), *type));
}
else if (alias.name() == "v")
{
auto type = cpp_user_defined_type::build(cpp_type_ref(cpp_entity_id(""), "v"));
REQUIRE(equal_types(idx, alias.underlying_type(), *type));
}
else
REQUIRE(false);
});
REQUIRE(count == 19u);
REQUIRE(count == 22u);
}

View file

@ -11,7 +11,7 @@ using namespace cppast;
TEST_CASE("cpp_variable")
{
auto code = R"(
// basic - global scope, so external storage
// basic
int a;
unsigned long long b = 42;
float c = 3.f + 0.14f;
@ -24,6 +24,12 @@ thread_local static int g;
// constexpr
constexpr int h = 12;
// inline definition
struct foo {} i;
const struct bar {} j = bar();
struct baz {} k{};
static struct {} l;
)";
cpp_entity_index idx;
@ -43,11 +49,10 @@ constexpr int h = 12;
REQUIRE(var.is_constexpr() == is_constexpr);
};
auto file = parse({}, "cpp_variable.cpp", code);
auto file = parse(idx, "cpp_variable.cpp", code);
auto int_type = cpp_builtin_type::build("int");
auto count = test_visit<cpp_variable>(*file, [&](const cpp_variable& var) {
INFO(var.name());
if (var.name() == "a")
check_variable(var, *int_type, nullptr, cpp_storage_class_none, false);
else if (var.name() == "b")
@ -74,8 +79,33 @@ constexpr int h = 12;
cpp_cv_const),
*cpp_literal_expression::build(cpp_builtin_type::build("int"), "12"),
cpp_storage_class_none, true);
else if (var.name() == "i")
check_variable(var,
*cpp_user_defined_type::build(cpp_type_ref(cpp_entity_id(""), "foo")),
nullptr, cpp_storage_class_none, false);
else if (var.name() == "j")
check_variable(var, *cpp_cv_qualified_type::build(cpp_user_defined_type::build(
cpp_type_ref(cpp_entity_id(""),
"bar")),
cpp_cv_const),
*cpp_unexposed_expression::build(cpp_user_defined_type::build(
cpp_type_ref(cpp_entity_id(""),
"bar")),
"bar()"),
cpp_storage_class_none, false);
else if (var.name() == "k")
check_variable(var,
*cpp_user_defined_type::build(cpp_type_ref(cpp_entity_id(""), "baz")),
*cpp_unexposed_expression::build(cpp_user_defined_type::build(
cpp_type_ref(cpp_entity_id(""),
"baz")),
"{}"),
cpp_storage_class_none, false);
else if (var.name() == "l")
check_variable(var, *cpp_user_defined_type::build(cpp_type_ref(cpp_entity_id(""), "")),
nullptr, cpp_storage_class_static, false);
else
REQUIRE(false);
});
REQUIRE(count == 8u);
REQUIRE(count == 12u);
}