Parse static class functions
This commit is contained in:
parent
9267bbbff2
commit
39b5b01ae3
4 changed files with 118 additions and 39 deletions
|
|
@ -82,12 +82,10 @@ namespace
|
||||||
"unexpected token for function body kind");
|
"unexpected token for function body kind");
|
||||||
return cpp_function_declaration;
|
return cpp_function_declaration;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
std::unique_ptr<cpp_entity> detail::parse_cpp_function(const detail::parse_context& context,
|
std::unique_ptr<cpp_entity> parse_cpp_function_impl(const detail::parse_context& context,
|
||||||
const CXCursor& cur)
|
const CXCursor& cur)
|
||||||
{
|
{
|
||||||
DEBUG_ASSERT(clang_getCursorKind(cur) == CXCursor_FunctionDecl, detail::assert_handler{});
|
|
||||||
auto name = detail::get_cursor_name(cur);
|
auto name = detail::get_cursor_name(cur);
|
||||||
|
|
||||||
cpp_function::builder builder(name.c_str(),
|
cpp_function::builder builder(name.c_str(),
|
||||||
|
|
@ -111,7 +109,8 @@ std::unique_ptr<cpp_entity> detail::parse_cpp_function(const detail::parse_conte
|
||||||
// skip parameters
|
// skip parameters
|
||||||
skip_parameters(stream);
|
skip_parameters(stream);
|
||||||
|
|
||||||
auto body = clang_isCursorDefinition(cur) ? cpp_function_definition : cpp_function_declaration;
|
auto body =
|
||||||
|
clang_isCursorDefinition(cur) ? cpp_function_definition : cpp_function_declaration;
|
||||||
// parse suffix
|
// parse suffix
|
||||||
// tokenizer only tokenizes signature, so !stream.done() is sufficient
|
// tokenizer only tokenizes signature, so !stream.done() is sufficient
|
||||||
while (!stream.done())
|
while (!stream.done())
|
||||||
|
|
@ -125,4 +124,21 @@ std::unique_ptr<cpp_entity> detail::parse_cpp_function(const detail::parse_conte
|
||||||
}
|
}
|
||||||
|
|
||||||
return builder.finish(*context.idx, detail::get_entity_id(cur), body);
|
return builder.finish(*context.idx, detail::get_entity_id(cur), body);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<cpp_entity> detail::parse_cpp_function(const detail::parse_context& context,
|
||||||
|
const CXCursor& cur)
|
||||||
|
{
|
||||||
|
DEBUG_ASSERT(clang_getCursorKind(cur) == CXCursor_FunctionDecl, detail::assert_handler{});
|
||||||
|
return parse_cpp_function_impl(context, cur);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<cpp_entity> detail::try_parse_static_cpp_function(
|
||||||
|
const detail::parse_context& context, const CXCursor& cur)
|
||||||
|
{
|
||||||
|
DEBUG_ASSERT(clang_getCursorKind(cur) == CXCursor_CXXMethod, detail::assert_handler{});
|
||||||
|
if (clang_CXXMethod_isStatic(cur))
|
||||||
|
return parse_cpp_function_impl(context, cur);
|
||||||
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -87,6 +87,11 @@ std::unique_ptr<cpp_entity> detail::parse_entity(const detail::parse_context& co
|
||||||
|
|
||||||
case CXCursor_FunctionDecl:
|
case CXCursor_FunctionDecl:
|
||||||
return parse_cpp_function(context, cur);
|
return parse_cpp_function(context, cur);
|
||||||
|
case CXCursor_CXXMethod:
|
||||||
|
// check for static function
|
||||||
|
if (auto func = try_parse_static_cpp_function(context, cur))
|
||||||
|
return func;
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
|
|
@ -53,11 +53,15 @@ namespace cppast
|
||||||
|
|
||||||
// parse_entity() dispatches on the cursor type
|
// parse_entity() dispatches on the cursor type
|
||||||
// it calls one of the other parse functions defined elsewhere
|
// it calls one of the other parse functions defined elsewhere
|
||||||
// try_parse_XXX are not exposed entities
|
// try_parse_XXX are not exposed/differently exposed entities
|
||||||
// they are called on an unexposed cursor and see whether they match
|
// they are called on corresponding cursor and see whether they match
|
||||||
|
|
||||||
|
// unexposed
|
||||||
std::unique_ptr<cpp_entity> try_parse_cpp_language_linkage(const parse_context& context,
|
std::unique_ptr<cpp_entity> try_parse_cpp_language_linkage(const parse_context& context,
|
||||||
const CXCursor& cur);
|
const CXCursor& cur);
|
||||||
|
// CXXMethod
|
||||||
|
std::unique_ptr<cpp_entity> try_parse_static_cpp_function(const parse_context& context,
|
||||||
|
const CXCursor& cur);
|
||||||
|
|
||||||
std::unique_ptr<cpp_entity> parse_cpp_namespace(const parse_context& context,
|
std::unique_ptr<cpp_entity> parse_cpp_namespace(const parse_context& context,
|
||||||
const CXCursor& cur);
|
const CXCursor& cur);
|
||||||
|
|
|
||||||
|
|
@ -193,3 +193,57 @@ void l()
|
||||||
});
|
});
|
||||||
REQUIRE(count == 12u);
|
REQUIRE(count == 12u);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("static cpp_function")
|
||||||
|
{
|
||||||
|
auto code = R"(
|
||||||
|
// no need to test anything special
|
||||||
|
struct foo
|
||||||
|
{
|
||||||
|
static void a();
|
||||||
|
|
||||||
|
static int b() noexcept {}
|
||||||
|
|
||||||
|
static constexpr char c() = delete;
|
||||||
|
};
|
||||||
|
)";
|
||||||
|
|
||||||
|
cpp_entity_index idx;
|
||||||
|
auto file = parse(idx, "static_cpp_function.cpp", code);
|
||||||
|
auto count = test_visit<cpp_function>(*file, [&](const cpp_function& func) {
|
||||||
|
REQUIRE(!func.is_variadic());
|
||||||
|
REQUIRE(count_children(func) == 0u);
|
||||||
|
REQUIRE(func.storage_class() == cpp_storage_class_static);
|
||||||
|
|
||||||
|
if (func.name() == "a")
|
||||||
|
{
|
||||||
|
REQUIRE(equal_types(idx, func.return_type(), *cpp_builtin_type::build("void")));
|
||||||
|
REQUIRE(!func.noexcept_condition());
|
||||||
|
REQUIRE(!func.is_constexpr());
|
||||||
|
REQUIRE(func.body_kind() == cpp_function_declaration);
|
||||||
|
}
|
||||||
|
else if (func.name() == "b")
|
||||||
|
{
|
||||||
|
REQUIRE(equal_types(idx, func.return_type(), *cpp_builtin_type::build("int")));
|
||||||
|
REQUIRE(func.noexcept_condition());
|
||||||
|
REQUIRE(
|
||||||
|
equal_expressions(func.noexcept_condition().value(),
|
||||||
|
*cpp_literal_expression::build(cpp_builtin_type::build("bool"),
|
||||||
|
"true")));
|
||||||
|
REQUIRE(!func.is_constexpr());
|
||||||
|
REQUIRE(func.body_kind() == cpp_function_definition);
|
||||||
|
}
|
||||||
|
else if (func.name() == "c")
|
||||||
|
{
|
||||||
|
REQUIRE(equal_types(idx, func.return_type(), *cpp_builtin_type::build("char")));
|
||||||
|
REQUIRE(!func.noexcept_condition());
|
||||||
|
REQUIRE(func.is_constexpr());
|
||||||
|
REQUIRE(func.body_kind() == cpp_function_deleted);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
REQUIRE(false);
|
||||||
|
});
|
||||||
|
REQUIRE(count == 3u);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: friend functions (clang 4.0 required)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue