Refactored process_function_parameter method

This commit is contained in:
Bartek Kryza
2022-02-23 20:00:51 +01:00
parent 32ba144943
commit 2554008f85
2 changed files with 91 additions and 90 deletions

View File

@@ -999,10 +999,11 @@ void translation_unit_visitor::process_function_parameter(
if (!mp.skip_relationship()) { if (!mp.skip_relationship()) {
// find relationship for the type // find relationship for the type
std::vector<std::pair<std::string, relationship_t>> relationships; std::vector<std::pair<std::string, relationship_t>> relationships;
find_relationships(cppast::remove_cv(param.type()), relationships, find_relationships(cppast::remove_cv(param.type()), relationships,
relationship_t::kDependency); relationship_t::kDependency);
for (const auto &[type, relationship_type] : relationships) {
for (const auto &[type, relationship_type] : relationships) {
if (ctx.config().should_include(cx::util::split_ns(type)) && if (ctx.config().should_include(cx::util::split_ns(type)) &&
(relationship_type != relationship_t::kNone) && (relationship_type != relationship_t::kNone) &&
(type != c.name_and_ns())) { (type != c.name_and_ns())) {
@@ -1017,24 +1018,30 @@ void translation_unit_visitor::process_function_parameter(
} }
} }
// Also consider the container itself if it is user defined type // Also consider the container itself if it is a template instantiation
// it's arguments could count as reference to relevant types
const auto &t = cppast::remove_cv(cx::util::unreferenced(param.type())); const auto &t = cppast::remove_cv(cx::util::unreferenced(param.type()));
if (t.kind() == cppast::cpp_type_kind::template_instantiation_t) { if (t.kind() == cppast::cpp_type_kind::template_instantiation_t) {
process_function_parameter_find_relationships_in_template(
c, template_parameter_names, t);
}
}
m.add_parameter(std::move(mp));
}
void translation_unit_visitor::
process_function_parameter_find_relationships_in_template(class_ &c,
const std::set<std::string> &template_parameter_names,
const cppast::cpp_type &t)
{
auto &template_instantiation_type = auto &template_instantiation_type =
static_cast<const cppast::cpp_template_instantiation_type &>(t); static_cast<const cppast::cpp_template_instantiation_type &>(t);
if (template_instantiation_type.primary_template() if (template_instantiation_type.primary_template()
.get(ctx.entity_index()) .get(ctx.entity_index())
.size()) { .size()) {
// Check if the template arguments of this function param
// Here we need the name of the primary template with full
// namespace prefix to apply config inclusion filters
// auto primary_template_name =
// cx::util::full_name(ctx.get_namespace(),
// template_instantiation_type.primary_template()
// .get(ctx.entity_index())[0]
// .get());
// Now check if the template arguments of this function param
// are a subset of the method template params - if yes this is // are a subset of the method template params - if yes this is
// not an instantiation but just a reference to an existing // not an instantiation but just a reference to an existing
// template // template
@@ -1047,8 +1054,8 @@ void translation_unit_visitor::process_function_parameter(
template_instantiation_type.arguments().value()) { template_instantiation_type.arguments().value()) {
const auto template_argument_name = const auto template_argument_name =
cppast::to_string(template_argument.type().value()); cppast::to_string(template_argument.type().value());
if (template_parameter_names.count( if (template_parameter_names.count(template_argument_name) >
template_argument_name) > 0) { 0) {
template_is_not_instantiation = true; template_is_not_instantiation = true;
break; break;
} }
@@ -1062,44 +1069,39 @@ void translation_unit_visitor::process_function_parameter(
// arguments string // arguments string
} }
// LOG_DBG("Maybe building instantiation for: if (!ctx.config().should_include(ctx.get_namespace(),
// {}",
// primary_template_name);
if (ctx.config().should_include(ctx.get_namespace(),
template_instantiation_type.primary_template() template_instantiation_type.primary_template()
.get(ctx.entity_index())[0] .get(ctx.entity_index())[0]
.get() .get()
.name())) { .name())) {
return;
}
if (template_is_not_instantiation) { if (template_is_not_instantiation) {
LOG_DBG("Template is not an instantiation - " LOG_DBG("Template is not an instantiation - "
"only adding reference to template {}", "only adding reference to template {}",
cx::util::full_name(cppast::remove_cv(t), cx::util::full_name(
ctx.entity_index(), false)); cppast::remove_cv(t), ctx.entity_index(), false));
relationship rr{relationship_t::kDependency, relationship rr{relationship_t::kDependency,
cx::util::full_name(cppast::remove_cv(t), cx::util::full_name(
ctx.entity_index(), false)}; cppast::remove_cv(t), ctx.entity_index(), false)};
LOG_DBG("Adding field template dependency relationship " LOG_DBG("Adding field template dependency relationship "
"{} {} {} " "{} {} {} "
": {}", ": {}",
rr.destination(), rr.destination(), common::model::to_string(rr.type()),
clanguml::common::model::to_string(rr.type()),
c.full_name(), rr.label()); c.full_name(), rr.label());
c.add_relationship(std::move(rr)); c.add_relationship(std::move(rr));
} }
else { else {
// First check if tinst already exists // First check if tinst already exists
auto tinst_ptr = build_template_instantiation( auto tinst_ptr =
template_instantiation_type); build_template_instantiation(template_instantiation_type);
auto &tinst = *tinst_ptr; auto &tinst = *tinst_ptr;
relationship rr{ relationship rr{relationship_t::kDependency, tinst.full_name()};
relationship_t::kDependency, tinst.full_name()};
LOG_DBG("Adding field dependency relationship {} {} {} " LOG_DBG("Adding field dependency relationship {} {} {} "
": {}", ": {}",
rr.destination(), rr.destination(), common::model::to_string(rr.type()),
clanguml::common::model::to_string(rr.type()),
c.full_name(), rr.label()); c.full_name(), rr.label());
c.add_relationship(std::move(rr)); c.add_relationship(std::move(rr));
@@ -1107,11 +1109,6 @@ void translation_unit_visitor::process_function_parameter(
ctx.diagram().add_class(std::move(tinst_ptr)); ctx.diagram().add_class(std::move(tinst_ptr));
} }
} }
}
}
}
m.add_parameter(std::move(mp));
} }
void translation_unit_visitor::process_template_type_parameter( void translation_unit_visitor::process_template_type_parameter(

View File

@@ -215,6 +215,10 @@ private:
int arg_index, bool variadic_params, int arg_index, bool variadic_params,
const model::class_template &ct) const; const model::class_template &ct) const;
void process_function_parameter_find_relationships_in_template(
model::class_ &c, const std::set<std::string> &template_parameter_names,
const cppast::cpp_type &t);
// ctx allows to track current visitor context, e.g. current namespace // ctx allows to track current visitor context, e.g. current namespace
translation_unit_context ctx; translation_unit_context ctx;
}; };