Parse cpp_language_linkage

This commit is contained in:
Jonathan Müller 2017-02-22 20:44:19 +01:00
commit 8d864bdbe1
7 changed files with 80 additions and 2 deletions

View file

@ -15,6 +15,8 @@ namespace cppast
public cpp_entity_container<cpp_language_linkage, cpp_entity>
{
public:
static cpp_entity_kind kind() noexcept;
/// Builds a [cppast::cpp_language_linkage]().
class builder
{

View file

@ -8,6 +8,11 @@
using namespace cppast;
cpp_entity_kind cpp_language_linkage::kind() noexcept
{
return cpp_entity_kind::language_linkage_t;
}
bool cpp_language_linkage::is_block() const noexcept
{
DEBUG_ASSERT(begin() != end(), detail::assert_handler{}, "empty container");
@ -16,5 +21,5 @@ bool cpp_language_linkage::is_block() const noexcept
cpp_entity_kind cpp_language_linkage::do_get_entity_kind() const noexcept
{
return cpp_entity_kind::language_linkage_t;
return kind();
}

View file

@ -0,0 +1,46 @@
// 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 "parse_functions.hpp"
#include <cppast/cpp_language_linkage.hpp>
#include <clang-c/Index.h>
#include "libclang_visitor.hpp"
using namespace cppast;
namespace
{
cpp_language_linkage::builder make_ll_builder(const detail::parse_context& context,
const CXCursor& cur)
{
}
}
std::unique_ptr<cpp_entity> detail::try_parse_cpp_language_linkage(const parse_context& context,
const CXCursor& cur)
{
DEBUG_ASSERT(cur.kind == CXCursor_UnexposedDecl,
detail::assert_handler{}); // not exposed currently
detail::tokenizer tokenizer(context.tu, context.file, cur);
detail::token_stream stream(tokenizer, cur);
// extern <name> ...
if (!detail::skip_if(stream, "extern"))
return nullptr;
// unexposed variable starting with extern - must be a language linkage
// (function, variables are not unexposed)
auto& name = stream.get().value();
auto builder = cpp_language_linkage::builder(name.c_str());
detail::visit_children(cur, [&](const CXCursor& child) {
auto entity = parse_entity(context, child);
if (entity)
builder.add_child(std::move(entity));
});
return builder.finish();
}

View file

@ -5,7 +5,6 @@
#include "parse_functions.hpp"
#include <cppast/cpp_namespace.hpp>
#include <clang-c/Index.h>
#include "libclang_visitor.hpp"

View file

@ -19,6 +19,12 @@ std::unique_ptr<cpp_entity> detail::parse_entity(const detail::parse_context& co
auto kind = clang_getCursorKind(cur);
switch (kind)
{
case CXCursor_UnexposedDecl:
// go through all the try_parse_XXX functions
if (auto entity = try_parse_cpp_language_linkage(context, cur))
return std::move(entity);
break;
case CXCursor_Namespace:
return parse_cpp_namespace(context, cur);
case CXCursor_NamespaceAlias:

View file

@ -26,6 +26,14 @@ namespace cppast
type_safe::object_ref<const cpp_entity_index> idx;
};
// parse_entity() dispatches on the cursor type
// it calls one of the other parse functions defined elsewhere
// try_parse_XXX are not exposed entities
// they are called on an unexposed cursor and see whether they match
std::unique_ptr<cpp_entity> try_parse_cpp_language_linkage(const parse_context& context,
const CXCursor& cur);
std::unique_ptr<cpp_entity> parse_cpp_namespace(const parse_context& context,
const CXCursor& cur);
std::unique_ptr<cpp_entity> parse_cpp_namespace_alias(const parse_context& context,

View file

@ -0,0 +1,12 @@
// 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 "test_parser.hpp"
using namespace cppast;
TEST_CASE("cpp_language_linkage")
{
// TODO: need actual entities to parse
}