Fixed class inner class and enum relationship generation

This commit is contained in:
Bartek Kryza
2021-03-24 11:30:08 +01:00
parent a26cfb6d60
commit ff2a08c3c6
4 changed files with 74 additions and 13 deletions

View File

@@ -23,8 +23,8 @@
#include "uml/class_diagram_visitor.h" #include "uml/class_diagram_visitor.h"
#include "util/util.h" #include "util/util.h"
#include <cppast/libclang_parser.hpp>
#include <cppast/cpp_entity_index.hpp> #include <cppast/cpp_entity_index.hpp>
#include <cppast/libclang_parser.hpp>
#include <glob/glob.hpp> #include <glob/glob.hpp>
#include <filesystem> #include <filesystem>
@@ -87,7 +87,7 @@ public:
case relationship_t::kAggregation: case relationship_t::kAggregation:
return "o--"; return "o--";
case relationship_t::kContainment: case relationship_t::kContainment:
return "+--"; return "--+";
case relationship_t::kAssociation: case relationship_t::kAssociation:
return "-->"; return "-->";
case relationship_t::kInstantiation: case relationship_t::kInstantiation:
@@ -224,6 +224,33 @@ public:
} }
ostr << "}" << std::endl; ostr << "}" << std::endl;
for (const auto &r : e.relationships) {
std::string destination;
if (r.destination.find("#") != std::string::npos ||
r.destination.find("@") != std::string::npos) {
destination = m_model.usr_to_name(
m_config.using_namespace, r.destination);
if (destination.empty()) {
ostr << "' ";
destination = r.destination;
}
}
else {
destination = r.destination;
}
ostr << m_model.to_alias(m_config.using_namespace,
ns_relative(m_config.using_namespace, e.name))
<< " " << to_string(r.type) << " "
<< m_model.to_alias(m_config.using_namespace,
ns_relative(m_config.using_namespace, destination));
if (!r.label.empty())
ostr << " : " << r.label;
ostr << std::endl;
}
} }
void generate(std::ostream &ostr) const void generate(std::ostream &ostr) const
@@ -287,7 +314,8 @@ clanguml::model::class_diagram::diagram generate(
} }
cppast::cpp_entity_index idx; cppast::cpp_entity_index idx;
cppast::simple_file_parser<cppast::libclang_parser> parser{type_safe::ref(idx)}; cppast::simple_file_parser<cppast::libclang_parser> parser{
type_safe::ref(idx)};
// Process all matching translation units // Process all matching translation units
clanguml::visitor::class_diagram::tu_visitor ctx(d, diagram); clanguml::visitor::class_diagram::tu_visitor ctx(d, diagram);

View File

@@ -211,6 +211,7 @@ public:
struct enum_ : public element { struct enum_ : public element {
std::vector<std::string> constants; std::vector<std::string> constants;
std::vector<class_relationship> relationships;
friend bool operator==(const enum_ &l, const enum_ &r) friend bool operator==(const enum_ &l, const enum_ &r)
{ {

View File

@@ -19,11 +19,11 @@
#include <cppast/cpp_array_type.hpp> #include <cppast/cpp_array_type.hpp>
#include <cppast/cpp_entity_kind.hpp> #include <cppast/cpp_entity_kind.hpp>
#include <cppast/cpp_enum.hpp>
#include <cppast/cpp_member_function.hpp> #include <cppast/cpp_member_function.hpp>
#include <cppast/cpp_member_variable.hpp> #include <cppast/cpp_member_variable.hpp>
#include <cppast/cpp_template.hpp> #include <cppast/cpp_template.hpp>
#include <cppast/cpp_variable.hpp> #include <cppast/cpp_variable.hpp>
#include <cppast/cpp_enum.hpp>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
namespace clanguml { namespace clanguml {
@@ -71,17 +71,17 @@ void tu_visitor::operator()(const cppast::cpp_entity &file)
cppast::visit(file, cppast::visit(file,
[&, this](const cppast::cpp_entity &e, cppast::visitor_info info) { [&, this](const cppast::cpp_entity &e, cppast::visitor_info info) {
if (e.kind() == cppast::cpp_entity_kind::class_t) { if (e.kind() == cppast::cpp_entity_kind::class_t) {
spdlog::debug("{}'{}' - {}", prefix, spdlog::debug("{}'{}' - {}", prefix, cx::util::full_name(e),
cx::util::full_name(e), cppast::to_string(e.kind())); cppast::to_string(e.kind()));
auto &cls = static_cast<const cppast::cpp_class &>(e); auto &cls = static_cast<const cppast::cpp_class &>(e);
if (ctx.config.should_include(cx::util::fully_prefixed(cls))) if (ctx.config.should_include(cx::util::fully_prefixed(cls)))
process_class_declaration(cls); process_class_declaration(cls);
} }
else if(e.kind() == cppast::cpp_entity_kind::enum_t) { else if (e.kind() == cppast::cpp_entity_kind::enum_t) {
spdlog::debug("{}'{}' - {}", prefix, spdlog::debug("{}'{}' - {}", prefix, cx::util::full_name(e),
cx::util::full_name(e), cppast::to_string(e.kind())); cppast::to_string(e.kind()));
auto &enm = static_cast<const cppast::cpp_enum &>(e); auto &enm = static_cast<const cppast::cpp_enum &>(e);
@@ -91,16 +91,32 @@ void tu_visitor::operator()(const cppast::cpp_entity &file)
}); });
} }
void tu_visitor::process_enum_declaration(const cppast::cpp_enum &enm) { void tu_visitor::process_enum_declaration(const cppast::cpp_enum &enm)
{
enum_ e; enum_ e;
e.name = cx::util::full_name(enm); e.name = cx::util::full_name(enm);
for(const auto& ev : enm) { for (const auto &ev : enm) {
if(ev.kind() == cppast::cpp_entity_kind::enum_value_t) { if (ev.kind() == cppast::cpp_entity_kind::enum_value_t) {
e.constants.push_back(ev.name()); e.constants.push_back(ev.name());
} }
} }
// Find if class is contained in another class
for (auto cur = enm.parent(); cur; cur = cur.value().parent()) {
// find nearest parent class, if any
if (cur.value().kind() == cppast::cpp_entity_kind::class_t) {
class_relationship containment;
containment.type = relationship_t::kContainment;
containment.destination = cx::util::full_name(cur.value());
e.relationships.emplace_back(std::move(containment));
spdlog::debug("Added relationship {} +-- {}", e.name,
containment.destination);
break;
}
}
ctx.d.add_enum(std::move(e)); ctx.d.add_enum(std::move(e));
} }
@@ -199,6 +215,22 @@ void tu_visitor::process_class_declaration(const cppast::cpp_class &cls)
} }
} }
// Find if class is contained in another class
for (auto cur = cls.parent(); cur; cur = cur.value().parent()) {
// find nearest parent class, if any
if (cur.value().kind() == cppast::cpp_entity_kind::class_t) {
class_relationship containment;
containment.type = relationship_t::kContainment;
containment.destination = cx::util::full_name(cur.value());
c.relationships.emplace_back(std::move(containment));
spdlog::debug("Added relationship {} +-- {}",
c.full_name(ctx.config.using_namespace),
containment.destination);
break;
}
}
ctx.d.add_class(std::move(c)); ctx.d.add_class(std::move(c));
} }

View File

@@ -265,7 +265,7 @@ ContainsMatcher IsInnerClass(std::string const &parent,
CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes) CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes)
{ {
return ContainsMatcher( return ContainsMatcher(
CasedString(parent + " +-- " + inner, caseSensitivity)); CasedString(inner + " --+ " + parent, caseSensitivity));
} }
ContainsMatcher IsAssociation(std::string const &from, std::string const &to, ContainsMatcher IsAssociation(std::string const &from, std::string const &to,