From 02f470e563f80bbb741ac1ad99198637d34db944 Mon Sep 17 00:00:00 2001 From: Bartek Kryza Date: Sun, 14 May 2023 18:57:40 +0200 Subject: [PATCH] Fixed rendering of methods in template class specializations --- .../visitor/translation_unit_visitor.cc | 28 +++++++++++++------ src/common/clang_utils.cc | 2 +- tests/t00044/t00044.cc | 2 ++ tests/t00044/test_case.h | 2 ++ 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/class_diagram/visitor/translation_unit_visitor.cc b/src/class_diagram/visitor/translation_unit_visitor.cc index a6169bfe..5158736e 100644 --- a/src/class_diagram/visitor/translation_unit_visitor.cc +++ b/src/class_diagram/visitor/translation_unit_visitor.cc @@ -1269,8 +1269,16 @@ void translation_unit_visitor::process_method( ensure_lambda_type_is_relative(method_return_type); + auto method_name = mf.getNameAsString(); + if (mf.isTemplated()) { + // Sometimes in template specializations method names contain the + // template parameters for some reason - drop them + // Is there a better way to do this? + method_name = method_name.substr(0, method_name.find('<')); + } + class_method method{common::access_specifier_to_access_t(mf.getAccess()), - util::trim(mf.getNameAsString()), method_return_type}; + util::trim(method_name), method_return_type}; method.is_pure_virtual(mf.isPure()); method.is_virtual(mf.isVirtual()); @@ -1302,16 +1310,18 @@ void translation_unit_visitor::process_method( unaliased_type = unaliased_type->getAliasedType() ->getAs(); - auto template_specialization_ptr = tbuilder().build( - unaliased_type->getTemplateName().getAsTemplateDecl(), - *unaliased_type, &c); + if (unaliased_type != nullptr) { + auto template_specialization_ptr = tbuilder().build( + unaliased_type->getTemplateName().getAsTemplateDecl(), + *unaliased_type, &c); - if (diagram().should_include( - template_specialization_ptr->full_name(false))) { - relationships.emplace_back( - template_specialization_ptr->id(), relationship_t::kDependency); + if (diagram().should_include( + template_specialization_ptr->full_name(false))) { + relationships.emplace_back(template_specialization_ptr->id(), + relationship_t::kDependency); - diagram().add_class(std::move(template_specialization_ptr)); + diagram().add_class(std::move(template_specialization_ptr)); + } } } diff --git a/src/common/clang_utils.cc b/src/common/clang_utils.cc index 885fc2fc..8e44133d 100644 --- a/src/common/clang_utils.cc +++ b/src/common/clang_utils.cc @@ -161,7 +161,7 @@ std::string to_string(const clang::QualType &type, const clang::ASTContext &ctx, clanguml::util::replace_all(result, ", ", ","); clanguml::util::replace_all(result, "> >", ">>"); - // Get rid of 'type-parameter-X-Y' ugliness + // Try to get rid of 'type-parameter-X-Y' ugliness if (result.find("type-parameter-") != std::string::npos) { util::apply_if_not_null( common::dereference(type)->getAs(), diff --git a/tests/t00044/t00044.cc b/tests/t00044/t00044.cc index 33fb26d4..2d71d9bd 100644 --- a/tests/t00044/t00044.cc +++ b/tests/t00044/t00044.cc @@ -15,6 +15,8 @@ public: { } + template CastTo *get_signal() { return (CastTo *)signal; } + private: signal_t *signal; }; diff --git a/tests/t00044/test_case.h b/tests/t00044/test_case.h index 8ae83a72..e48ce76d 100644 --- a/tests/t00044/test_case.h +++ b/tests/t00044/test_case.h @@ -35,6 +35,8 @@ TEST_CASE("t00044", "[test-case][class]") REQUIRE_THAT(puml, StartsWith("@startuml")); REQUIRE_THAT(puml, EndsWith("@enduml\n")); + REQUIRE_THAT(puml, !Contains("type-parameter-")); + REQUIRE_THAT(puml, IsClassTemplate("sink", "T")); REQUIRE_THAT(puml, IsClassTemplate("signal_handler", "T,A"));