From 96534f8e421d8f7b79e0a4c3a1623ddcd0cf62a7 Mon Sep 17 00:00:00 2001 From: Bartek Kryza Date: Wed, 22 Jun 2022 23:11:31 +0200 Subject: [PATCH] Fixed friend class root namespace handling --- .../visitor/translation_unit_visitor.cc | 14 ++++++++++++-- tests/t00045/t00045.cc | 14 ++++++++++++++ tests/t00045/test_case.h | 6 ++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/class_diagram/visitor/translation_unit_visitor.cc b/src/class_diagram/visitor/translation_unit_visitor.cc index cb795653..05ec6880 100644 --- a/src/class_diagram/visitor/translation_unit_visitor.cc +++ b/src/class_diagram/visitor/translation_unit_visitor.cc @@ -1384,7 +1384,7 @@ void translation_unit_visitor::process_friend(const cppast::cpp_friend &f, r.set_destination(name); } else if (f.entity()) { - std::string name; + std::string name = f.entity().value().name(); if (f.entity().value().kind() == cppast::cpp_entity_kind::class_template_t) { @@ -1411,7 +1411,17 @@ void translation_unit_visitor::process_friend(const cppast::cpp_friend &f, cppast::is_templated(f.entity().value()), cppast::to_string(f.entity().value().kind())); - name = cx::util::full_name(ctx.get_namespace(), f.entity().value()); + const auto &maybe_definition = + cppast::get_definition(ctx.entity_index(), f.entity().value()); + + if (maybe_definition.has_value()) { + const auto &friend_class = maybe_definition.value(); + + auto entity_ns = + common::model::namespace_{cx::util::ns(friend_class)}; + + name = cx::util::full_name(entity_ns, f.entity().value()); + } } if (!ctx.diagram().should_include(ctx.get_namespace(), name)) diff --git a/tests/t00045/t00045.cc b/tests/t00045/t00045.cc index 3d8da7c2..6ef0cbda 100644 --- a/tests/t00045/t00045.cc +++ b/tests/t00045/t00045.cc @@ -4,6 +4,13 @@ class A { class AA { }; +class AAA { +}; + +template class AAAA { + T t; +}; + namespace ns1 { class A { @@ -26,6 +33,9 @@ class D : public ns1::ns2::A { class E : public ::A { }; +class AAA { +}; + class R { public: A *a; @@ -33,6 +43,10 @@ public: ns1::ns2::A *ns1_ns2_a; ::A *root_a; + friend ::AAA; + // TODO: + // template friend class ::AAAA; + void foo(::AA &aa) { (void)aa; } }; } diff --git a/tests/t00045/test_case.h b/tests/t00045/test_case.h index e7087fb9..30a7351c 100644 --- a/tests/t00045/test_case.h +++ b/tests/t00045/test_case.h @@ -58,6 +58,12 @@ TEST_CASE("t00045", "[test-case][class]") REQUIRE_THAT(puml, IsDependency(_A("ns1::ns2::R"), _A("AA"))); + REQUIRE_THAT(puml, IsFriend(_A("ns1::ns2::R"), _A("AAA"))); + REQUIRE_THAT( + puml, !IsFriend(_A("ns1::ns2::R"), _A("ns1::ns2::AAA"))); + // TODO: + // REQUIRE_THAT(puml, IsFriend(_A("ns1::ns2::R"), _A("AAAA"))); + save_puml( "./" + config.output_directory() + "/" + diagram->name + ".puml", puml); }