Improved rendering of template methods in class diagrams

This commit is contained in:
Bartek Kryza
2023-01-25 00:56:33 +01:00
parent 0e6532f66c
commit 9a7d66f93f
24 changed files with 335 additions and 226 deletions

View File

@@ -144,6 +144,10 @@ void generator::generate(const class_ &c, std::ostream &ostr) const
ostr << plantuml_common::to_plantuml(m.access()) << m.name();
if (!m.templates().empty()) {
m.render_template_params(ostr, m_config.using_namespace(), false);
}
ostr << "(";
if (m_config.generate_method_arguments() !=
config::method_arguments::none) {

View File

@@ -68,29 +68,12 @@ void class_::add_parent(class_parent &&parent)
bases_.emplace_back(std::move(parent));
}
void class_::add_template(template_parameter &&tmplt)
{
templates_.emplace_back(std::move(tmplt));
}
const std::vector<class_member> &class_::members() const { return members_; }
const std::vector<class_method> &class_::methods() const { return methods_; }
const std::vector<class_parent> &class_::parents() const { return bases_; }
const std::vector<template_parameter> &class_::templates() const
{
return templates_;
}
void class_::set_base_template(const std::string &full_name)
{
base_template_full_name_ = full_name;
}
std::string class_::base_template() const { return base_template_full_name_; }
bool operator==(const class_ &l, const class_ &r) { return l.id() == r.id(); }
std::string class_::full_name_no_ns() const
@@ -101,7 +84,7 @@ std::string class_::full_name_no_ns() const
ostr << name();
render_template_params(ostr, false);
render_template_params(ostr, using_namespace(), false);
return ostr.str();
}
@@ -114,7 +97,8 @@ std::string class_::full_name(bool relative) const
std::ostringstream ostr;
ostr << name_and_ns();
render_template_params(ostr, relative);
render_template_params(ostr, using_namespace(), relative);
std::string res;
@@ -129,26 +113,6 @@ std::string class_::full_name(bool relative) const
return res;
}
std::ostringstream &class_::render_template_params(
std::ostringstream &ostr, bool relative) const
{
using clanguml::common::model::namespace_;
if (!templates_.empty()) {
std::vector<std::string> tnames;
std::vector<std::string> tnames_simplified;
std::transform(templates_.cbegin(), templates_.cend(),
std::back_inserter(tnames),
[ns = using_namespace(), relative](
const auto &tmplt) { return tmplt.to_string(ns, relative); });
ostr << fmt::format("<{}>", fmt::join(tnames, ","));
}
return ostr;
}
bool class_::is_abstract() const
{
// TODO check if all base abstract methods are overriden

View File

@@ -23,8 +23,9 @@
#include "common/model/element.h"
#include "common/model/enums.h"
#include "common/model/stylable_element.h"
#include "common/model/template_parameter.h"
#include "common/model/template_trait.h"
#include "common/types.h"
#include "template_parameter.h"
#include <string>
#include <vector>
@@ -32,7 +33,8 @@
namespace clanguml::class_diagram::model {
class class_ : public common::model::element,
public common::model::stylable_element {
public common::model::stylable_element,
public template_trait {
public:
class_(const common::model::namespace_ &using_namespace);
@@ -55,12 +57,10 @@ public:
void add_member(class_member &&member);
void add_method(class_method &&method);
void add_parent(class_parent &&parent);
void add_template(template_parameter &&tmplt);
const std::vector<class_member> &members() const;
const std::vector<class_method> &methods() const;
const std::vector<class_parent> &parents() const;
const std::vector<template_parameter> &templates() const;
void set_base_template(const std::string &full_name);
std::string base_template() const;
@@ -85,9 +85,6 @@ public:
const class_ &other, const std::string &full_name) const;
private:
std::ostringstream &render_template_params(
std::ostringstream &ostr, bool relative) const;
bool is_struct_{false};
bool is_template_{false};
bool is_template_instantiation_{false};
@@ -95,7 +92,6 @@ private:
std::vector<class_member> members_;
std::vector<class_method> methods_;
std::vector<class_parent> bases_;
std::vector<template_parameter> templates_;
std::string base_template_full_name_;
std::string full_name_;

View File

@@ -61,4 +61,5 @@ void class_method::add_parameter(method_parameter &&parameter)
{
parameters_.emplace_back(std::move(parameter));
}
} // namespace clanguml::class_diagram::model

View File

@@ -18,6 +18,8 @@
#pragma once
#include "class_element.h"
#include "common/model/template_parameter.h"
#include "common/model/template_trait.h"
#include "method_parameter.h"
#include <string>
@@ -25,7 +27,9 @@
namespace clanguml::class_diagram::model {
class class_method : public class_element {
using clanguml::common::model::template_trait;
class class_method : public class_element, public template_trait {
public:
class_method(common::model::access_t access, const std::string &name,
const std::string &type);
@@ -52,6 +56,7 @@ public:
private:
std::vector<method_parameter> parameters_;
bool is_pure_virtual_{false};
bool is_virtual_{false};
bool is_const_{false};

View File

@@ -1,219 +0,0 @@
/**
* src/class_diagram/model/template_parameter.cc
*
* Copyright (c) 2021-2023 Bartek Kryza <bkryza@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "template_parameter.h"
#include "common/model/enums.h"
#include <common/model/namespace.h>
#include <utility>
namespace clanguml::class_diagram::model {
template_parameter::template_parameter(const std::string &type,
const std::string &name, std::string default_value, bool is_variadic)
: default_value_{std::move(default_value)}
, is_variadic_{is_variadic}
{
set_name(name);
set_type(type);
}
void template_parameter::set_type(const std::string &type)
{
if (util::ends_with(type, std::string{"..."})) {
type_ = type.substr(0, type.size() - 3);
is_variadic_ = true;
}
else
type_ = type;
}
std::string template_parameter::type() const
{
if (is_variadic_ && !type_.empty())
return type_ + "...";
return type_;
}
void template_parameter::set_name(const std::string &name)
{
if (util::ends_with(name, std::string{"..."})) {
name_ = name.substr(0, name.size() - 3);
is_variadic_ = true;
}
else
name_ = name;
}
std::string template_parameter::name() const
{
if (is_variadic_ && type_.empty())
return name_ + "...";
return name_;
}
void template_parameter::set_default_value(const std::string &value)
{
default_value_ = value;
}
std::string template_parameter::default_value() const { return default_value_; }
void template_parameter::is_variadic(bool is_variadic) noexcept
{
is_variadic_ = is_variadic;
}
bool template_parameter::is_variadic() const noexcept { return is_variadic_; }
bool template_parameter::is_specialization_of(
const template_parameter &ct) const
{
return (ct.is_template_parameter() ||
ct.is_template_template_parameter()) &&
!is_template_parameter();
}
void template_parameter::add_template_param(template_parameter &&ct)
{
template_params_.emplace_back(std::move(ct));
}
void template_parameter::add_template_param(const template_parameter &ct)
{
template_params_.push_back(ct);
}
const std::vector<template_parameter> &
template_parameter::template_params() const
{
return template_params_;
}
bool operator==(const template_parameter &l, const template_parameter &r)
{
bool res{false};
if (l.is_template_parameter() != r.is_template_parameter())
return res;
if (l.is_template_parameter()) {
// If this is a template parameter (e.g. 'typename T' or 'typename U'
// we don't actually care what it is called
res = (l.is_variadic() == r.is_variadic()) &&
(l.default_value() == r.default_value());
}
else
res = (l.name() == r.name()) && (l.type() == r.type()) &&
(l.default_value() == r.default_value());
return res && (l.template_params_ == r.template_params_);
}
bool operator!=(const template_parameter &l, const template_parameter &r)
{
return !(l == r);
}
std::string template_parameter::to_string(
const clanguml::common::model::namespace_ &using_namespace,
bool relative) const
{
using clanguml::common::model::namespace_;
std::string res;
if (!type().empty()) {
if (!relative)
res += namespace_{type()}.to_string();
else
res += namespace_{type()}.relative_to(using_namespace).to_string();
}
if (!name().empty()) {
if (!type().empty())
res += " ";
if (!relative)
res += namespace_{name()}.to_string();
else
res += namespace_{name()}.relative_to(using_namespace).to_string();
}
// Render nested template params
if (!template_params_.empty()) {
std::vector<std::string> params;
for (const auto &template_param : template_params_) {
params.push_back(
template_param.to_string(using_namespace, relative));
}
res += fmt::format("<{}>", fmt::join(params, ","));
}
if (!default_value().empty()) {
res += "=";
res += default_value();
}
return res;
}
bool template_parameter::find_nested_relationships(
std::vector<std::pair<int64_t, common::model::relationship_t>>
&nested_relationships,
common::model::relationship_t hint,
const std::function<bool(const std::string &full_name)> &should_include)
const
{
bool added_aggregation_relationship{false};
// If this type argument should be included in the relationship
// just add it and skip recursion (e.g. this is a user defined type)
if (should_include(name())) {
if (id()) {
nested_relationships.emplace_back(id().value(), hint);
added_aggregation_relationship =
(hint == common::model::relationship_t::kAggregation);
}
}
// Otherwise (e.g. this is a std::shared_ptr) and we're actually
// interested what is stored inside it
else {
for (const auto &template_argument : template_params()) {
if (should_include(template_argument.name()) &&
template_argument.id()) {
nested_relationships.emplace_back(
template_argument.id().value(), hint);
added_aggregation_relationship =
(hint == common::model::relationship_t::kAggregation);
}
else {
added_aggregation_relationship =
template_argument.find_nested_relationships(
nested_relationships, hint, should_include);
}
}
}
return added_aggregation_relationship;
}
} // namespace clanguml::class_diagram::model

View File

@@ -1,133 +0,0 @@
/**
* src/class_diagram/model/template_parameter.h
*
* Copyright (c) 2021-2023 Bartek Kryza <bkryza@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include "common/model/enums.h"
#include "common/model/namespace.h"
#include <optional>
#include <string>
#include <vector>
namespace clanguml::class_diagram::model {
/// @brief Represents template parameter or template argument
///
/// This class can represent both template parameter and template arguments,
/// including variadic parameters and instantiations with
/// nested templates
class template_parameter {
public:
template_parameter(const std::string &type = "",
const std::string &name = "", std::string default_value = "",
bool is_variadic = false);
template_parameter(const template_parameter &right) = default;
void set_type(const std::string &type);
std::string type() const;
void set_id(const int64_t id) { id_ = id; }
std::optional<int64_t> id() const { return id_; }
void set_name(const std::string &name);
std::string name() const;
void set_default_value(const std::string &value);
std::string default_value() const;
void is_variadic(bool is_variadic) noexcept;
bool is_variadic() const noexcept;
void is_pack(bool is_pack) noexcept;
bool is_pack() const noexcept;
bool is_specialization_of(const template_parameter &ct) const;
friend bool operator==(
const template_parameter &l, const template_parameter &r);
friend bool operator!=(
const template_parameter &l, const template_parameter &r);
bool is_template_parameter() const { return is_template_parameter_; }
void is_template_parameter(bool is_template_parameter)
{
is_template_parameter_ = is_template_parameter;
}
bool is_template_template_parameter() const
{
return is_template_template_parameter_;
}
void is_template_template_parameter(bool is_template_template_parameter)
{
is_template_template_parameter_ = is_template_template_parameter;
}
std::string to_string(
const clanguml::common::model::namespace_ &using_namespace,
bool relative) const;
void add_template_param(template_parameter &&ct);
void add_template_param(const template_parameter &ct);
const std::vector<template_parameter> &template_params() const;
void clear_params() { template_params_.clear(); }
bool find_nested_relationships(
std::vector<std::pair<int64_t, common::model::relationship_t>>
&nested_relationships,
common::model::relationship_t hint,
const std::function<bool(const std::string &full_name)> &should_include)
const;
private:
/// Represents the type of non-type template parameters
/// e.g. 'int'
std::string type_;
/// The name of the parameter (e.g. 'T' or 'N')
std::string name_;
/// Default value of the template parameter
std::string default_value_;
/// Whether the template parameter is a regular template parameter
/// When false, it is a non-type template parameter
bool is_template_parameter_{false};
/// Whether the template parameter is a template template parameter
/// Can only be true when is_template_parameter_ is true
bool is_template_template_parameter_{false};
/// Whether the template parameter is variadic
bool is_variadic_{false};
/// Whether the argument specializes argument pack from parent template
bool is_pack_{false};
// Nested template parameters
std::vector<template_parameter> template_params_;
std::optional<int64_t> id_;
};
} // namespace clanguml::class_diagram::model

View File

@@ -26,19 +26,6 @@
namespace clanguml::class_diagram::visitor {
using clanguml::class_diagram::model::class_;
using clanguml::class_diagram::model::class_member;
using clanguml::class_diagram::model::class_method;
using clanguml::class_diagram::model::class_parent;
using clanguml::class_diagram::model::diagram;
using clanguml::class_diagram::model::enum_;
using clanguml::class_diagram::model::method_parameter;
using clanguml::class_diagram::model::template_parameter;
using clanguml::common::model::access_t;
using clanguml::common::model::namespace_;
using clanguml::common::model::relationship;
using clanguml::common::model::relationship_t;
translation_unit_visitor::translation_unit_visitor(clang::SourceManager &sm,
clanguml::class_diagram::model::diagram &diagram,
const clanguml::config::class_diagram &config)
@@ -513,7 +500,8 @@ void translation_unit_visitor::process_class_declaration(
}
bool translation_unit_visitor::process_template_parameters(
const clang::ClassTemplateDecl &template_declaration, class_ &c)
const clang::TemplateDecl &template_declaration,
common::model::template_trait &c)
{
LOG_DBG("Processing class {} template parameters...",
common::get_qualified_name(template_declaration));
@@ -966,6 +954,8 @@ void translation_unit_visitor::process_template_method(
method.is_defaulted(mf.getTemplatedDecl()->isDefaulted());
method.is_static(mf.getTemplatedDecl()->isStatic());
process_template_parameters(mf, method);
process_comment(mf, method);
if (method.skip())

View File

@@ -20,6 +20,7 @@
#include "class_diagram/model/class.h"
#include "class_diagram/model/diagram.h"
#include "common/model/enums.h"
#include "common/model/template_trait.h"
#include "common/visitor/translation_unit_visitor.h"
#include "config/config.h"
@@ -34,6 +35,20 @@
namespace clanguml::class_diagram::visitor {
using clanguml::class_diagram::model::class_;
using clanguml::class_diagram::model::class_member;
using clanguml::class_diagram::model::class_method;
using clanguml::class_diagram::model::class_parent;
using clanguml::class_diagram::model::diagram;
using clanguml::class_diagram::model::enum_;
using clanguml::class_diagram::model::method_parameter;
using clanguml::common::model::access_t;
using clanguml::common::model::namespace_;
using clanguml::common::model::relationship;
using clanguml::common::model::relationship_t;
using clanguml::common::model::template_parameter;
using clanguml::common::model::template_trait;
using found_relationships_t =
std::vector<std::pair<clanguml::common::model::diagram_element::id_t,
common::model::relationship_t>>;
@@ -108,12 +123,11 @@ private:
clang::ClassTemplateSpecializationDecl *cls);
void process_template_specialization_children(
const clang::ClassTemplateSpecializationDecl *cls,
clanguml::class_diagram::model::class_ &c);
const clang::ClassTemplateSpecializationDecl *cls, class_ &c);
bool process_template_parameters(
const clang::ClassTemplateDecl &template_declaration,
clanguml::class_diagram::model::class_ &c);
const clang::TemplateDecl &template_declaration,
clanguml::common::model::template_trait &t);
void process_template_specialization_argument(
const clang::ClassTemplateSpecializationDecl *cls,
@@ -171,7 +185,7 @@ private:
clanguml::class_diagram::model::class_ &tinst,
std::deque<std::tuple<std::string, int, bool>> &template_base_params,
int arg_index, bool variadic_params,
const clanguml::class_diagram::model::template_parameter &ct) const;
const clanguml::common::model::template_parameter &ct) const;
void build_template_instantiation_process_template_arguments(
std::optional<clanguml::class_diagram::model::class_ *> &parent,
@@ -186,15 +200,15 @@ private:
const std::string &full_template_specialization_name,
const clang::TemplateDecl *template_decl,
const clang::TemplateArgument &arg,
model::template_parameter &argument);
common::model::template_parameter &argument);
void build_template_instantiation_process_expression_argument(
const clang::TemplateArgument &arg,
model::template_parameter &argument) const;
common::model::template_parameter &argument) const;
void build_template_instantiation_process_integral_argument(
const clang::TemplateArgument &arg,
model::template_parameter &argument) const;
common::model::template_parameter &argument) const;
void build_template_instantiation_process_type_argument(
std::optional<clanguml::class_diagram::model::class_ *> &parent,
@@ -202,11 +216,11 @@ private:
const clang::TemplateDecl *template_decl,
const clang::TemplateArgument &arg,
model::class_ &template_instantiation,
model::template_parameter &argument);
common::model::template_parameter &argument);
void build_template_instantiation_process_template_argument(
const clang::TemplateArgument &arg,
model::template_parameter &argument) const;
common::model::template_parameter &argument) const;
void ensure_lambda_type_is_relative(std::string &parameter_type) const;
@@ -220,19 +234,19 @@ private:
void process_unexposed_template_specialization_parameters(
const std::string &tspec,
clanguml::class_diagram::model::template_parameter &tp,
clanguml::common::model::template_parameter &tp,
clanguml::class_diagram::model::class_ &c);
bool find_relationships_in_unexposed_template_params(
const clanguml::class_diagram::model::template_parameter &ct,
const clanguml::common::model::template_parameter &ct,
found_relationships_t &relationships);
void add_incomplete_forward_declarations();
void resolve_local_to_global_ids();
bool simplify_system_template(
model::template_parameter &ct, const std::string &full_name) const;
bool simplify_system_template(common::model::template_parameter &ct,
const std::string &full_name) const;
/// Store the mapping from local clang entity id (obtained using
/// getID()) method to clang-uml global id