Added test case for inner type aliases with parent class template args

This commit is contained in:
Bartek Kryza
2022-06-07 22:49:13 +02:00
parent 5231fb43b4
commit 1de4a40ae9
11 changed files with 160 additions and 37 deletions

View File

@@ -99,15 +99,16 @@ void diagram::add_type_alias(std::unique_ptr<type_alias> &&ta)
type_aliases_[ta->alias()] = std::move(ta);
}
void diagram::add_package(std::unique_ptr<common::model::package> &&p)
bool diagram::add_package(std::unique_ptr<common::model::package> &&p)
{
LOG_DBG("Adding namespace package: {}, {}", p->name(), p->full_name(true));
auto ns = p->get_relative_namespace();
add_element(ns, std::move(p));
return add_element(ns, std::move(p));
}
void diagram::add_class(std::unique_ptr<class_> &&c)
bool diagram::add_class(std::unique_ptr<class_> &&c)
{
const auto base_name = c->name();
const auto full_name = c->full_name(false);
@@ -128,33 +129,48 @@ void diagram::add_class(std::unique_ptr<class_> &&c)
auto name = base_name;
auto name_with_ns = c->name_and_ns();
auto name_and_ns = ns | name;
const auto &cc = *c;
if (!has_class(*c)) {
classes_.push_back(type_safe::ref(*c));
auto cc_ref = type_safe::ref(cc);
add_element(ns, std::move(c));
if (!has_class(cc)) {
if (add_element(ns, std::move(c)))
classes_.push_back(std::move(cc_ref));
const auto &el = get_element<class_>(name_and_ns).value();
assert(el.name() == name);
assert(el.get_relative_namespace() == ns);
return true;
}
else
LOG_DBG("Class {} ({}) already in the model", base_name, full_name);
LOG_DBG("Class {} ({}) already in the model", base_name, full_name);
return false;
}
void diagram::add_enum(std::unique_ptr<enum_> &&e)
bool diagram::add_enum(std::unique_ptr<enum_> &&e)
{
LOG_DBG("Adding enum: {}", e->name());
const auto full_name = e->name();
LOG_DBG("Adding enum: {}", full_name);
assert(!util::contains(e->name(), "::"));
auto e_ref = type_safe::ref(*e);
auto ns = e->get_relative_namespace();
if (!has_enum(*e)) {
enums_.emplace_back(*e);
auto ns = e->get_relative_namespace();
add_element(ns, std::move(e));
if (add_element(ns, std::move(e))) {
enums_.emplace_back(std::move(e_ref));
return true;
}
}
else
LOG_DBG("Enum {} already in the model", e->name());
LOG_DBG("Enum {} already in the model", full_name);
return false;
}
void diagram::get_parents(

View File

@@ -63,11 +63,11 @@ public:
void add_type_alias(std::unique_ptr<type_alias> &&ta);
void add_class(std::unique_ptr<class_> &&c);
bool add_class(std::unique_ptr<class_> &&c);
void add_enum(std::unique_ptr<enum_> &&e);
bool add_enum(std::unique_ptr<enum_> &&e);
void add_package(std::unique_ptr<common::model::package> &&p);
bool add_package(std::unique_ptr<common::model::package> &&p);
std::string to_alias(const std::string &full_name) const;

View File

@@ -765,9 +765,12 @@ bool translation_unit_visitor::process_field_with_template_instantiation(
}
}
const auto tinst_namespace = tinst.get_namespace();
const auto tinst_name = tinst.name();
// Add instantiation relationship from the generated template instantiation
// of the field type to its primary template
if (ctx.diagram().should_include(tinst.get_namespace(), tinst.name())) {
if (ctx.diagram().should_include(tinst_namespace, tinst_name)) {
LOG_DBG("Adding field instantiation relationship {} {} {} : {}",
rr.destination(), clanguml::common::model::to_string(rr.type()),
c.full_name(), rr.label());
@@ -787,7 +790,7 @@ bool translation_unit_visitor::process_field_with_template_instantiation(
// Only add nested template relationships to this class if the top level
// template is not in the diagram (e.g. it is a std::shared_ptr<>)
//
if (!ctx.diagram().should_include(tinst.get_namespace(), tinst.name())) {
if (!ctx.diagram().should_include(tinst_namespace, tinst_name)) {
res = add_nested_template_relationships(mv, c, member, as, tinst,
relationship_type, decorator_rtype, decorator_rmult);
}
@@ -1660,16 +1663,21 @@ std::unique_ptr<class_> translation_unit_visitor::build_template_instantiation(
tinst.set_namespace(ctx.get_namespace());
auto tinst_full_name = cppast::to_string(t);
std::string tinst_full_name;
//
// Typically, every template instantiation should have a
// primary_template() which should also be generated here if it doesn't
// primary_template(), which should also be generated here if it doesn't
// exist yet in the model
//
if (t.primary_template().get(ctx.entity_index()).size()) {
auto size = t.primary_template().get(ctx.entity_index()).size();
(void)size;
if (t_is_alias &&
unaliased.primary_template().get(ctx.entity_index()).size()) {
tinst_full_name = cppast::to_string(unaliased);
build_template_instantiation_primary_template(
unaliased, tinst, template_base_params, parent, full_template_name);
}
else if (t.primary_template().get(ctx.entity_index()).size()) {
tinst_full_name = cppast::to_string(t);
build_template_instantiation_primary_template(
t, tinst, template_base_params, parent, full_template_name);
}
@@ -1677,6 +1685,7 @@ std::unique_ptr<class_> translation_unit_visitor::build_template_instantiation(
LOG_DBG("Template instantiation {} has no primary template?",
cppast::to_string(t));
tinst_full_name = cppast::to_string(t);
full_template_name = cppast::to_string(t);
}

View File

@@ -38,21 +38,24 @@ public:
virtual ~nested_trait() = default;
template <typename V = T> void add_element(std::unique_ptr<V> p)
template <typename V = T>
[[nodiscard]] bool add_element(std::unique_ptr<V> p)
{
auto it = std::find_if(elements_.begin(), elements_.end(),
[&p](const auto &e) { return *e == *p; });
if (it != elements_.end()) {
(*it)->append(*p);
}
else {
elements_.emplace_back(std::move(p));
// Element already in element tree
return false;
}
elements_.emplace_back(std::move(p));
return true;
}
template <typename V = T>
void add_element(const Path &path, std::unique_ptr<V> p)
bool add_element(const Path &path, std::unique_ptr<V> p)
{
assert(p);
@@ -60,14 +63,13 @@ public:
path.to_string());
if (path.is_empty()) {
add_element(std::move(p));
return;
return add_element(std::move(p));
}
auto parent = get_element(path);
if (parent && dynamic_cast<nested_trait<T, Path> *>(&parent.value()))
dynamic_cast<nested_trait<T, Path> &>(parent.value())
return dynamic_cast<nested_trait<T, Path> &>(parent.value())
.template add_element<V>(std::move(p));
else {
spdlog::error("No parent element found at: {}", path.to_string());

View File

@@ -132,7 +132,8 @@ void translation_unit_visitor::process_external_system_header(
f->set_name(include_directive.name());
f->set_type(common::model::source_file_t::kHeader);
ctx.diagram().add_element(std::move(f));
if (!ctx.diagram().add_element(std::move(f)))
LOG_DBG("Include {} already in the model", include_directive.name());
auto dependency_relationship = common::model::relationship{
common::model::relationship_t::kDependency,