Added support for C++20 module based packages in class diagrams (#101)

This commit is contained in:
Bartek Kryza
2023-12-18 21:55:18 +01:00
parent ea6892f754
commit c51ae5b6ee
24 changed files with 296 additions and 12 deletions

View File

@@ -170,10 +170,17 @@ void generator::generate(const package &p, nlohmann::json &parent) const
if (!uns.starts_with({p.full_name(false)})) {
LOG_DBG("Generating package {}", p.name());
if (config().package_type() == config::package_type_t::kDirectory)
switch (config().package_type()) {
case config::package_type_t::kDirectory:
package_object["type"] = "directory";
else
break;
case config::package_type_t::kModule:
package_object["type"] = "module";
break;
case config::package_type_t::kNamespace:
package_object["type"] = "namespace";
break;
}
package_object["name"] = p.name();
package_object["display_name"] = p.full_name(false);

View File

@@ -67,7 +67,7 @@ void generator::generate_alias(const class_ &c, std::ostream &ostr) const
class_type = "abstract";
std::string full_name;
if (config().generate_packages())
if (config().generate_fully_qualified_name())
full_name = c.full_name_no_ns();
else
full_name = c.full_name();
@@ -89,7 +89,7 @@ void generator::generate_alias(const enum_ &e, std::ostream &ostr) const
{
print_debug(e, ostr);
if (config().generate_packages())
if (config().generate_fully_qualified_name())
ostr << "enum"
<< " \"" << e.name();
else
@@ -106,7 +106,7 @@ void generator::generate_alias(const concept_ &c, std::ostream &ostr) const
{
print_debug(c, ostr);
if (config().generate_packages())
if (config().generate_fully_qualified_name())
ostr << "class"
<< " \"" << c.name();
else

View File

@@ -2150,6 +2150,15 @@ void translation_unit_visitor::add_class(std::unique_ptr<class_> &&c)
diagram().add(p, std::move(c));
}
else if ((config().generate_packages() &&
config().package_type() == config::package_type_t::kModule)) {
const auto module_path = config().make_module_relative(c->module());
common::model::path p{module_path, common::model::path_type::kModule};
diagram().add(p, std::move(c));
}
else {
diagram().add(c->path(), std::move(c));
}
@@ -2169,6 +2178,15 @@ void translation_unit_visitor::add_enum(std::unique_ptr<enum_> &&e)
diagram().add(p, std::move(e));
}
else if ((config().generate_packages() &&
config().package_type() == config::package_type_t::kModule)) {
const auto module_path = config().make_module_relative(e->module());
common::model::path p{module_path, common::model::path_type::kModule};
diagram().add(p, std::move(e));
}
else {
diagram().add(e->path(), std::move(e));
}
@@ -2188,6 +2206,15 @@ void translation_unit_visitor::add_concept(std::unique_ptr<concept_> &&c)
diagram().add(p, std::move(c));
}
else if ((config().generate_packages() &&
config().package_type() == config::package_type_t::kModule)) {
const auto module_path = config().make_module_relative(c->module());
common::model::path p{module_path, common::model::path_type::kModule};
diagram().add(p, std::move(c));
}
else {
diagram().add(c->path(), std::move(c));
}

View File

@@ -292,7 +292,8 @@ modules_filter::modules_filter(
{
}
tvl::value_t modules_filter::match(const diagram &d, const element &e) const
tvl::value_t modules_filter::match(
const diagram & /*d*/, const element &e) const
{
if (modules_.empty())
return {};
@@ -300,7 +301,7 @@ tvl::value_t modules_filter::match(const diagram &d, const element &e) const
if (!e.module().has_value())
return {false};
const auto module_toks = util::split(e.module().value(), ".");
const auto module_toks = util::split(e.module().value(), "."); // NOLINT
auto result = tvl::any_of(modules_.begin(), modules_.end(),
[&e, &module_toks](const auto &modit) {

View File

@@ -32,8 +32,9 @@ namespace clanguml::common::model {
* a nested set of namespaces or nested set of directories.
*/
enum class path_type {
kNamespace, /*!< Namespace path */
kFilesystem /*!< Filesystem path */
kNamespace, /*!< Namespace path */
kFilesystem, /*!< Filesystem path */
kModule /*!< Module path */
};
/**
@@ -54,6 +55,8 @@ class path {
switch (path_type_) {
case path_type::kNamespace:
return "::";
case path_type::kModule:
return ".";
case path_type::kFilesystem:
#ifdef _WIN32
return "\\";

View File

@@ -187,6 +187,7 @@ void inheritable_diagram_options::inherit(
{
glob.override(parent.glob);
using_namespace.override(parent.using_namespace);
using_module.override(parent.using_module);
include_relations_also_as_members.override(
parent.include_relations_also_as_members);
include.override(parent.include);
@@ -229,6 +230,12 @@ std::string inheritable_diagram_options::simplify_template_type(
return full_name;
}
bool inheritable_diagram_options::generate_fully_qualified_name() const
{
return generate_packages() &&
(package_type() == package_type_t::kNamespace);
}
std::vector<std::string> diagram::get_translation_units() const
{
std::vector<std::string> translation_units{};
@@ -264,6 +271,25 @@ std::filesystem::path diagram::make_path_relative(
return relative(p, root_directory()).lexically_normal().string();
}
std::vector<std::string> diagram::make_module_relative(
const std::optional<std::string> &maybe_module) const
{
if (!maybe_module)
return {};
auto module_path = util::split(maybe_module.value(), ".");
if (using_module.has_value) {
auto using_module_path = util::split(using_module(), ".");
if (util::starts_with(module_path, using_module_path)) {
util::remove_prefix(module_path, using_module_path);
}
}
return module_path;
}
std::optional<std::string> diagram::get_together_group(
const std::string &full_name) const
{

View File

@@ -87,7 +87,8 @@ std::string to_string(callee_type mt);
/*! How packages in diagrams should be generated */
enum class package_type_t {
kNamespace, /*!< From namespaces */
kDirectory /*!< From directories */
kDirectory, /*!< From directories */
kModule /*!< From modules */
};
std::string to_string(package_type_t mt);
@@ -469,6 +470,18 @@ struct inheritable_diagram_options {
std::string simplify_template_type(std::string full_name) const;
/**
* @brief Whether the diagram element should be fully qualified in diagram
*
* This method determines whether an elements' name should include
* fully qualified namespace name (however relative to using_namespace), or
* whether it should just contain it's name. This depends on whether the
* diagram has packages and if they are based on namespaces or sth else.
*
* @return True, if element should include it's namespace
*/
bool generate_fully_qualified_name() const;
/**
* @brief Get reference to `relative_to` diagram config option
*
@@ -483,6 +496,7 @@ struct inheritable_diagram_options {
option<std::vector<std::string>> glob{"glob"};
option<common::model::namespace_> using_namespace{"using_namespace"};
option<std::string> using_module{"using_module"};
option<bool> include_relations_also_as_members{
"include_relations_also_as_members", true};
option<filter> include{"include"};
@@ -566,6 +580,15 @@ struct diagram : public inheritable_diagram_options {
std::filesystem::path make_path_relative(
const std::filesystem::path &p) const;
/**
* @brief Make module path relative to `using_module` configuration option
*
* @param p Input path
* @return Relative path
*/
std::vector<std::string> make_module_relative(
const std::optional<std::string> &maybe_module) const;
/**
* @brief Returns absolute path of the `relative_to` option
*

View File

@@ -58,6 +58,7 @@ types:
package_type_t: !variant
- namespace
- directory
- module
member_order_t: !variant
- lexical
- as_is
@@ -161,6 +162,7 @@ types:
cmd: !optional string
relative_to: !optional string
using_namespace: !optional [string, [string]]
using_module: !optional string
generate_metadata: !optional bool
title: !optional string
#
@@ -239,6 +241,7 @@ types:
cmd: !optional string
relative_to: !optional string
using_namespace: !optional [string, [string]]
using_module: !optional string
generate_metadata: !optional bool
title: !optional string
#
@@ -318,6 +321,7 @@ root:
cmd: !optional string
relative_to: !optional string
using_namespace: !optional [string, [string]]
using_module: !optional string
generate_metadata: !optional bool
#
# Inheritable custom options

View File

@@ -133,6 +133,8 @@ void get_option<package_type_t>(
option.set(package_type_t::kNamespace);
else if (val == "directory")
option.set(package_type_t::kDirectory);
else if (val == "module")
option.set(package_type_t::kModule);
else
throw std::runtime_error(
"Invalid generate_method_arguments value: " + val);
@@ -573,6 +575,7 @@ template <typename T> bool decode_diagram(const Node &node, T &rhs)
// Decode options common for all diagrams
get_option(node, rhs.glob);
get_option(node, rhs.using_namespace);
get_option(node, rhs.using_module);
get_option(node, rhs.include);
get_option(node, rhs.exclude);
get_option(node, rhs.puml);
@@ -787,6 +790,7 @@ template <> struct convert<config> {
{
get_option(node, rhs.glob);
get_option(node, rhs.using_namespace);
get_option(node, rhs.using_module);
get_option(node, rhs.output_directory);
get_option(node, rhs.compilation_database_dir);
get_option(node, rhs.add_compile_flags);

View File

@@ -310,6 +310,7 @@ YAML::Emitter &operator<<(
out << c.puml;
out << c.relative_to;
out << c.using_namespace;
out << c.using_module;
out << c.generate_metadata;
if (const auto *cd = dynamic_cast<const class_diagram *>(&c);