Refactored template specialization matching

This commit is contained in:
Bartek Kryza
2023-04-05 21:57:44 +02:00
parent cb74864d0d
commit 38928cf86f
11 changed files with 174 additions and 126 deletions

View File

@@ -86,7 +86,7 @@ void to_json(nlohmann::json &j, const class_ &c)
j["methods"] = c.methods();
j["bases"] = c.parents();
j["template_parameters"] = c.templates();
j["template_parameters"] = c.template_params();
}
void to_json(nlohmann::json &j, const enum_ &c)

View File

@@ -166,7 +166,7 @@ void generator::generate(const class_ &c, std::ostream &ostr) const
ostr << plantuml_common::to_plantuml(m.access()) << m.name();
if (!m.templates().empty()) {
if (!m.template_params().empty()) {
m.render_template_params(ostr, m_config.using_namespace(), false);
}

View File

@@ -111,15 +111,13 @@ bool class_::is_abstract() const
[](const auto &method) { return method.is_pure_virtual(); });
}
int class_::calculate_template_specialization_match(
const class_ &other, const std::string &full_name) const
int class_::calculate_template_specialization_match(const class_ &other) const
{
int res{0};
const std::string left = name_and_ns();
// TODO: handle variadic templates
if ((left != full_name) ||
(templates().size() != other.templates().size())) {
if (left != other.name_and_ns()) {
return res;
}

View File

@@ -70,8 +70,7 @@ public:
bool is_abstract() const;
int calculate_template_specialization_match(
const class_ &other, const std::string &full_name) const;
int calculate_template_specialization_match(const class_ &other) const;
private:
bool is_struct_{false};

View File

@@ -1884,7 +1884,8 @@ void translation_unit_visitor::process_template_specialization_argument(
argument->set_id(nested_template_instantiation->id());
for (const auto &t : nested_template_instantiation->templates())
for (const auto &t :
nested_template_instantiation->template_params())
argument->add_template_param(t);
}
else if (arg.getAsType()->getAs<clang::TemplateTypeParmType>() !=
@@ -2118,18 +2119,19 @@ std::unique_ptr<class_> translation_unit_visitor::
int best_match{};
common::model::diagram_element::id_t best_match_id{0};
for (const auto c : diagram().classes()) {
if (c.get() == template_instantiation)
for (const auto templ : diagram().classes()) {
if (templ.get() == template_instantiation)
continue;
auto c_full_name = c.get().full_name(false);
auto match = c.get().calculate_template_specialization_match(
template_instantiation, template_instantiation.name_and_ns());
auto c_full_name = templ.get().full_name(false);
auto match =
template_instantiation.calculate_template_specialization_match(
templ.get());
if (match > best_match) {
best_match = match;
best_match_full_name = c_full_name;
best_match_id = c.get().id();
best_match_id = templ.get().id();
}
}
@@ -2307,18 +2309,19 @@ std::unique_ptr<class_> translation_unit_visitor::build_template_instantiation(
int best_match{};
common::model::diagram_element::id_t best_match_id{0};
for (const auto c : diagram().classes()) {
if (c.get() == template_instantiation)
for (const auto templ : diagram().classes()) {
if (templ.get() == template_instantiation)
continue;
auto c_full_name = c.get().full_name(false);
auto match = c.get().calculate_template_specialization_match(
template_instantiation, template_instantiation.name_and_ns());
auto c_full_name = templ.get().full_name(false);
auto match =
template_instantiation.calculate_template_specialization_match(
templ.get());
if (match > best_match) {
best_match = match;
best_match_full_name = c_full_name;
best_match_id = c.get().id();
best_match_id = templ.get().id();
}
}
@@ -2542,7 +2545,7 @@ translation_unit_visitor::build_template_instantiation_process_type_argument(
argument.set_id(nested_template_instantiation->id());
for (const auto &t : nested_template_instantiation->templates())
for (const auto &t : nested_template_instantiation->template_params())
argument.add_template_param(t);
// Check if this template should be simplified (e.g. system
@@ -2804,7 +2807,7 @@ void translation_unit_visitor::process_field(
if (template_field_type != nullptr) {
// Skip types which are template template parameters of the parent
// template
for (const auto &class_template_param : c.templates()) {
for (const auto &class_template_param : c.template_params()) {
if (class_template_param.name() ==
template_field_type->getTemplateName()
.getAsTemplateDecl()
@@ -2851,7 +2854,7 @@ void translation_unit_visitor::process_field(
found_relationships_t nested_relationships;
if (!template_instantiation_added_as_aggregation) {
for (const auto &template_argument :
template_specialization.templates()) {
template_specialization.template_params()) {
LOG_DBG("Looking for nested relationships from {}::{} in "
"template {}",