Workaround converison operator parsing bug

This commit is contained in:
Jonathan Müller 2017-06-23 13:59:10 +02:00
commit 4534bd2842
2 changed files with 68 additions and 28 deletions

View file

@ -392,32 +392,38 @@ namespace
const std::string& templ_name)
{
// look if the type has a declaration that is a template
auto decl = clang_getTypeDeclaration(type);
if (is_direct_templated(decl))
auto decl = clang_getTypeDeclaration(type);
auto count = clang_Type_getNumTemplateArguments(clang_getCursorType(decl));
if (count > 0 || is_direct_templated(decl))
return decl;
// look if the templ_name matches a template template parameter
auto param = clang_getNullCursor();
detail::visit_children(cur, [&](const CXCursor& child) {
if (clang_getCursorKind(child) == CXCursor_TemplateTemplateParameter
&& detail::get_cursor_name(child) == templ_name.c_str())
{
DEBUG_ASSERT(clang_Cursor_isNull(param), detail::parse_error_handler{}, cur,
"multiple template template parameters with the same name?!");
param = child;
}
});
return param;
else
{
// look if the templ_name matches a template template parameter
auto param = clang_getNullCursor();
detail::visit_children(cur, [&](const CXCursor& child) {
if (clang_getCursorKind(child) == CXCursor_TemplateTemplateParameter
&& detail::get_cursor_name(child) == templ_name.c_str())
{
DEBUG_ASSERT(clang_Cursor_isNull(param), detail::parse_error_handler{}, cur,
"multiple template template parameters with the same name?!");
param = child;
}
});
return param;
}
}
std::unique_ptr<cpp_type> try_parse_instantiation_type(const detail::parse_context&,
const CXCursor& cur, const CXType& type)
{
return make_leave_type(type, [&](std::string&& spelling) -> std::unique_ptr<cpp_type> {
auto ptr = spelling.c_str();
auto ptr = spelling.c_str();
std::string templ_name;
for (; *ptr && *ptr != '<'; ++ptr)
templ_name += *ptr;
if (*ptr != '<')
return nullptr;
++ptr;
auto templ = get_instantiation_template(cur, type, templ_name);
@ -429,7 +435,8 @@ namespace
// parse arguments
// i.e. not parse really, just add the string
DEBUG_ASSERT(!spelling.empty() && spelling.back() == '>', detail::assert_handler{});
if (spelling.empty() || spelling.back() != '>')
return nullptr;
spelling.pop_back();
builder.add_unexposed_arguments(ptr);

View file

@ -157,18 +157,36 @@ TEST_CASE("cpp_conversion_op")
namespace ns
{
template <typename T>
using type = char;
struct type {};
}
// most of it only need to be check in member function
struct foo
{
/// operator int&();
operator int&();
operator int&()
{
static int i;
return i;
}
/// explicit operator bool()const;
explicit operator bool() const;
explicit operator bool() const
{
return false;
}
/// constexpr operator ns::type<int>();
constexpr operator ns::type<int>();
constexpr operator ns::type<int>()
{
return {};
}
/// constexpr operator ns::type<char>();
constexpr operator ns::type<char>()
{
return {};
}
};
)";
@ -177,7 +195,7 @@ struct foo
auto count = test_visit<cpp_conversion_op>(*file, [&](const cpp_conversion_op& op) {
REQUIRE(count_children(op.parameters()) == 0u);
REQUIRE(!op.is_variadic());
REQUIRE(op.body_kind() == cpp_function_declaration);
REQUIRE(op.body_kind() == cpp_function_definition);
REQUIRE(op.ref_qualifier() == cpp_ref_none);
REQUIRE(!op.virtual_info());
REQUIRE(!op.noexcept_condition());
@ -198,17 +216,32 @@ struct foo
}
else if (!op.is_explicit() && op.is_constexpr())
{
REQUIRE(op.name() == "operator ns::type<int>");
cpp_template_instantiation_type::builder builder(
cpp_template_ref(cpp_entity_id(""), "ns::type"));
builder.add_unexposed_arguments("int");
REQUIRE(equal_types(idx, op.return_type(), *builder.finish()));
REQUIRE(op.cv_qualifier() == cpp_cv_none);
if (op.name() == "operator ns::type<int>")
{
REQUIRE(op.return_type().kind() == cpp_type_kind::template_instantiation_t);
auto& inst = static_cast<const cpp_template_instantiation_type&>(op.return_type());
REQUIRE(inst.primary_template().name() == "ns::type");
REQUIRE(!inst.arguments_exposed());
REQUIRE(inst.unexposed_arguments() == "int");
}
else if (op.name() == "operator ns::type<char>")
{
REQUIRE(op.return_type().kind() == cpp_type_kind::template_instantiation_t);
auto& inst = static_cast<const cpp_template_instantiation_type&>(op.return_type());
REQUIRE(inst.primary_template().name() == "ns::type");
REQUIRE(!inst.arguments_exposed());
REQUIRE(inst.unexposed_arguments() == "char");
}
else
REQUIRE(false);
}
else
REQUIRE(false);
});
REQUIRE(count == 3u);
REQUIRE(count == 4u);
}
TEST_CASE("cpp_constructor")