Parse cpp_member_variable and cpp_bitfield

This commit is contained in:
Jonathan Müller 2017-02-25 11:17:18 +01:00
commit c87b296d4e
8 changed files with 238 additions and 18 deletions

View file

@ -0,0 +1,103 @@
// 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_member_variable.hpp>
#include "test_parser.hpp"
using namespace cppast;
TEST_CASE("cpp_member_variable")
{
auto code = R"(
struct foo
{
int a;
float b = 3.14f;
mutable char c;
};
)";
cpp_entity_index idx;
auto file = parse(idx, "cpp_member_variable.cpp", code);
auto count = test_visit<cpp_member_variable>(*file, [&](const cpp_member_variable& var) {
if (var.name() == "a")
{
auto type = cpp_builtin_type::build("int");
REQUIRE(equal_types(idx, var.type(), *type));
REQUIRE(!var.default_value());
REQUIRE(!var.is_mutable());
}
else if (var.name() == "b")
{
auto type = cpp_builtin_type::build("float");
REQUIRE(equal_types(idx, var.type(), *type));
// all initializers are unexposed
auto def = cpp_unexposed_expression::build(cpp_builtin_type::build("float"), "3.14f");
REQUIRE(var.default_value());
REQUIRE(equal_expressions(var.default_value().value(), *def));
REQUIRE(!var.is_mutable());
}
else if (var.name() == "c")
{
auto type = cpp_builtin_type::build("char");
REQUIRE(equal_types(idx, var.type(), *type));
REQUIRE(!var.default_value());
REQUIRE(var.is_mutable());
}
else
REQUIRE(false);
});
REQUIRE(count == 3u);
}
TEST_CASE("cpp_bitfield")
{
auto code = R"(
struct foo
{
char a : 3;
mutable char b : 2;
char : 0;
char c : 3;
char : 4;
};
)";
auto file = parse({}, "cpp_bitfield.cpp", code);
auto count = test_visit<cpp_bitfield>(*file, [&](const cpp_bitfield& bf) {
REQUIRE(!bf.default_value());
REQUIRE(equal_types({}, bf.type(), *cpp_builtin_type::build("char")));
if (bf.name() == "a")
{
REQUIRE(bf.no_bits() == 3u);
REQUIRE(!bf.is_mutable());
}
else if (bf.name() == "b")
{
REQUIRE(bf.no_bits() == 2u);
REQUIRE(bf.is_mutable());
}
else if (bf.name() == "c")
{
REQUIRE(bf.no_bits() == 3u);
REQUIRE(!bf.is_mutable());
}
else if (bf.name() == "")
{
REQUIRE(!bf.is_mutable());
if (bf.no_bits() != 0u && bf.no_bits() != 4u)
{
INFO(bf.no_bits());
REQUIRE(false);
}
}
else
REQUIRE(false);
});
REQUIRE(count == 5u);
}