Parse cpp_namespace

This commit is contained in:
Jonathan Müller 2017-02-21 22:36:38 +01:00
commit ce69b0157f
11 changed files with 160 additions and 16 deletions

53
test/cpp_namespace.cpp Normal file
View file

@ -0,0 +1,53 @@
// 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_namespace.hpp>
#include "test_parser.hpp"
using namespace cppast;
TEST_CASE("cpp_namespace")
{
auto code = R"(
namespace a {}
inline namespace b {}
namespace c
{
namespace d {}
}
)";
auto file = parse({}, "cpp_namespace.cpp", code);
auto count = test_visit<cpp_namespace>(*file, [&](const cpp_namespace& ns) {
auto no_children = count_children(ns);
if (ns.name() == "a")
{
REQUIRE(!ns.is_inline());
REQUIRE(no_children == 0u);
}
else if (ns.name() == "b")
{
REQUIRE(ns.is_inline());
REQUIRE(no_children == 0u);
}
else if (ns.name() == "c")
{
REQUIRE(!ns.is_inline());
REQUIRE(no_children == 1u);
}
else if (ns.name() == "d")
{
REQUIRE(ns.parent());
REQUIRE(ns.parent().value().name() == "c");
REQUIRE(!ns.is_inline());
REQUIRE(no_children == 0u);
}
else
REQUIRE(false);
});
REQUIRE(count == 4u);
}

View file

@ -13,14 +13,14 @@
#include <cppast/libclang_parser.hpp>
#include <cppast/visitor.hpp>
void write_file(const char* name, const char* code)
inline void write_file(const char* name, const char* code)
{
std::ofstream file(name);
file << code;
}
std::unique_ptr<cppast::cpp_file> parse(const cppast::cpp_entity_index& idx, const char* name,
const char* code)
inline std::unique_ptr<cppast::cpp_file> parse(const cppast::cpp_entity_index& idx,
const char* name, const char* code)
{
using namespace cppast;
@ -38,7 +38,10 @@ template <typename T, typename Func>
unsigned test_visit(const cppast::cpp_file& file, Func f)
{
auto count = 0u;
cppast::visit(file, [&](const cppast::cpp_entity& e, cppast::visitor_info) {
cppast::visit(file, [&](const cppast::cpp_entity& e, cppast::visitor_info info) {
if (info == cppast::visitor_info::container_entity_exit)
return true; // already handled
if (e.kind() == T::kind())
{
auto& obj = static_cast<const T&>(e);
@ -52,4 +55,11 @@ unsigned test_visit(const cppast::cpp_file& file, Func f)
return count;
}
// number of direct children
template <class Entity>
unsigned count_children(const Entity& cont)
{
return std::distance(cont.begin(), cont.end());
}
#endif // CPPAST_TEST_PARSER_HPP_INCLUDED