Parse enums

This commit is contained in:
Jonathan Müller 2017-02-23 17:52:11 +01:00
commit a47e66e2c4
16 changed files with 262 additions and 109 deletions

112
test/cpp_enum.cpp Normal file
View file

@ -0,0 +1,112 @@
// 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_enum.hpp>
#include "test_parser.hpp"
using namespace cppast;
TEST_CASE("cpp_enum")
{
auto code = R"(
enum ignore_me : int;
enum a
{
a_a,
a_b = 42,
a_c,
a_d = a_a + 2,
};
enum class ignore_me2;
enum class b : int
{
b_a,
b_b = 42,
b_c
};
)";
cpp_entity_index idx;
auto file = parse(idx, "cpp_enum.cpp", code);
auto count = test_visit<cpp_enum>(*file, [&](const cpp_enum& e) {
if (e.name() == "a")
{
REQUIRE(!e.is_scoped());
REQUIRE(!e.underlying_type());
auto no_vals = 0u;
for (auto& val : e)
{
if (val.name() == "a_a" || val.name() == "a_c")
{
++no_vals;
REQUIRE(!val.value());
}
else if (val.name() == "a_b")
{
++no_vals;
REQUIRE(val.value());
auto& expr = val.value().value();
REQUIRE(expr.kind() == cpp_expression_kind::unexposed);
REQUIRE(static_cast<const cpp_unexposed_expression&>(expr).expression()
== "42");
REQUIRE(
equal_types(idx, expr.type(), *cpp_builtin_type::build("unsigned int")));
}
else if (val.name() == "a_d")
{
++no_vals;
REQUIRE(val.value());
auto& expr = val.value().value();
// this is unexposed for some reason
REQUIRE(expr.kind() == cpp_expression_kind::unexposed);
REQUIRE(static_cast<const cpp_unexposed_expression&>(expr).expression()
== "a_a+2");
REQUIRE(
equal_types(idx, expr.type(), *cpp_builtin_type::build("unsigned int")));
}
else
REQUIRE(false);
}
REQUIRE(no_vals == 4u);
}
else if (e.name() == "b")
{
REQUIRE(e.is_scoped());
REQUIRE(e.underlying_type());
REQUIRE(equal_types(idx, e.underlying_type().value(), *cpp_builtin_type::build("int")));
auto no_vals = 0u;
for (auto& val : e)
{
REQUIRE(full_name(val) == "b::" + val.name());
if (val.name() == "b_a" || val.name() == "b_c")
{
++no_vals;
REQUIRE(!val.value());
}
else if (val.name() == "b_b")
{
++no_vals;
REQUIRE(val.value());
auto& expr = val.value().value();
REQUIRE(expr.kind() == cpp_expression_kind::literal);
REQUIRE(static_cast<const cpp_literal_expression&>(expr).value() == "42");
REQUIRE(equal_types(idx, expr.type(), *cpp_builtin_type::build("int")));
}
else
REQUIRE(false);
}
REQUIRE(no_vals == 3u);
}
else
REQUIRE(false);
});
REQUIRE(count == 2u);
}

View file

@ -1,5 +0,0 @@
// 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.
#i

View file

@ -10,6 +10,7 @@
#include <catch.hpp>
#include <cppast/cpp_entity_kind.hpp>
#include <cppast/cpp_type.hpp>
#include <cppast/libclang_parser.hpp>
#include <cppast/visitor.hpp>
@ -71,4 +72,7 @@ inline void check_parent(const cppast::cpp_entity& e, const char* parent_name,
REQUIRE(cppast::full_name(e) == full_name);
}
bool equal_types(const cppast::cpp_entity_index& idx, const cppast::cpp_type& parsed,
const cppast::cpp_type& synthesized);
#endif // CPPAST_TEST_PARSER_HPP_INCLUDED