Skip to content

Commit

Permalink
Added support for C++20 module based packages in class diagrams (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkryza committed Dec 18, 2023
1 parent ea6892f commit c51ae5b
Show file tree
Hide file tree
Showing 24 changed files with 296 additions and 12 deletions.
11 changes: 9 additions & 2 deletions src/class_diagram/generators/json/class_diagram_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand All @@ -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
Expand Down
27 changes: 27 additions & 0 deletions src/class_diagram/visitor/translation_unit_visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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));
}
Expand All @@ -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));
}
Expand Down
5 changes: 3 additions & 2 deletions src/common/model/diagram_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -292,15 +292,16 @@ 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 {};

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) {
Expand Down
7 changes: 5 additions & 2 deletions src/common/model/path.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
};

/**
Expand All @@ -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 "\\";
Expand Down
26 changes: 26 additions & 0 deletions src/config/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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{};
Expand Down Expand Up @@ -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
{
Expand Down
25 changes: 24 additions & 1 deletion src/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
*
Expand All @@ -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"};
Expand Down Expand Up @@ -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
*
Expand Down
4 changes: 4 additions & 0 deletions src/config/schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const std::string schema_str = R"(
package_type_t: !variant
- namespace
- directory
- module
member_order_t: !variant
- lexical
- as_is
Expand Down Expand Up @@ -161,6 +162,7 @@ const std::string schema_str = R"(
cmd: !optional string
relative_to: !optional string
using_namespace: !optional [string, [string]]
using_module: !optional string
generate_metadata: !optional bool
title: !optional string
#
Expand Down Expand Up @@ -239,6 +241,7 @@ const std::string schema_str = R"(
cmd: !optional string
relative_to: !optional string
using_namespace: !optional [string, [string]]
using_module: !optional string
generate_metadata: !optional bool
title: !optional string
#
Expand Down Expand Up @@ -318,6 +321,7 @@ const std::string schema_str = R"(
cmd: !optional string
relative_to: !optional string
using_namespace: !optional [string, [string]]
using_module: !optional string
generate_metadata: !optional bool
#
# Inheritable custom options
Expand Down
4 changes: 4 additions & 0 deletions src/config/yaml_decoders.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/config/yaml_emitters.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ file(GLOB_RECURSE TEST_CONFIG_YMLS test_config_data/*.yml
test_compilation_database_data/*.json)

set(TEST_CASES_REQUIRING_CXX20 t00056 t00058 t00059 t00065 t00069)
set(TEST_CASES_REQUIRING_CXX20_MODULES t00070)
set(TEST_CASES_REQUIRING_CXX20_MODULES t00070 t00071)

if(ENABLE_CXX_MODULES_TEST_CASES)
foreach(CXX20_MOD_TC ${TEST_CASES_REQUIRING_CXX20_MODULES})
Expand Down
2 changes: 1 addition & 1 deletion tests/t00065/test_case.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ TEST_CASE("t00065", "[test-case][class]")
// Check if all classes exist
REQUIRE_THAT(src, IsClass(_A("R")));
REQUIRE_THAT(src, IsClass(_A("A")));
REQUIRE_THAT(src, IsClass(_A("AImpl")));
REQUIRE_THAT(src, IsClass(_A("detail::AImpl")));
REQUIRE_THAT(src, IsEnum(_A("XYZ")));
REQUIRE_THAT(src, IsEnum(_A("ABC")));

Expand Down
12 changes: 12 additions & 0 deletions tests/t00071/.clang-uml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
diagrams:
t00071_class:
type: class
glob:
- t00071.cc
include:
namespaces:
- clanguml::t00071
generate_packages: true
package_type: module
using_namespace: clanguml::t00071
using_module: t00071
13 changes: 13 additions & 0 deletions tests/t00071/src/lib1.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export module t00071.app.lib1;

export namespace clanguml::t00071 {
class B { };

template <typename T> class BB {
T t;
};

namespace detail {
enum class BBB { bbb1, bbb2 };
} // namespace detail
}
5 changes: 5 additions & 0 deletions tests/t00071/src/lib1mod1.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export module t00071.app.lib1.mod1;

export namespace clanguml::t00071 {
class D { };
}
5 changes: 5 additions & 0 deletions tests/t00071/src/lib1mod2.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export module t00071.app.lib1.mod2;

export namespace clanguml::t00071 {
class E { };
}
13 changes: 13 additions & 0 deletions tests/t00071/src/lib2.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export module t00071.app.lib2;

export namespace clanguml::t00071 {
class C { };

template <typename T> class CC {
T t;
};

namespace detail {
enum class CCC { ccc1, ccc2 };
}
}
11 changes: 11 additions & 0 deletions tests/t00071/src/t00071_mod.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export module t00071.app;
export import t00071.app.lib1;
export import t00071.app.lib2;

export namespace clanguml::t00071 {
class A {
int get() { return a; }

int a;
};
}
Loading

0 comments on commit c51ae5b

Please sign in to comment.