Fixed class diagram generation with packages from directories

This commit is contained in:
Bartek Kryza
2023-05-24 22:22:47 +02:00
parent 10a28cf35e
commit 75c027262f
7 changed files with 226 additions and 64 deletions

View File

@@ -173,16 +173,56 @@ common::optional_ref<concept_> diagram::get_concept(
return {}; return {};
} }
bool diagram::add_package_fs(std::unique_ptr<common::model::package> &&p) bool diagram::add_class(
const common::model::path &parent_path, std::unique_ptr<class_> &&c)
{
if (parent_path.type() == common::model::path_type::kNamespace) {
return add_class_ns(std::move(c));
}
return add_class_fs(parent_path, std::move(c));
}
bool diagram::add_enum(const path &parent_path, std::unique_ptr<enum_> &&e)
{
if (parent_path.type() == common::model::path_type::kNamespace) {
return add_enum_ns(std::move(e));
}
return add_enum_fs(parent_path, std::move(e));
}
bool diagram::add_concept(
const path &parent_path, std::unique_ptr<concept_> &&c)
{
if (parent_path.type() == common::model::path_type::kNamespace) {
return add_concept_ns(std::move(c));
}
return add_concept_fs(parent_path, std::move(c));
}
bool diagram::add_package(
const path &parent_path, std::unique_ptr<common::model::package> &&p)
{
if (parent_path.type() == common::model::path_type::kNamespace) {
return add_package_ns(std::move(p));
}
return add_package_fs(parent_path, std::move(p));
}
bool diagram::add_package_fs(
const path &parent_path, std::unique_ptr<common::model::package> &&p)
{ {
LOG_DBG("Adding filesystem package: {}, {}", p->name(), p->full_name(true)); LOG_DBG("Adding filesystem package: {}, {}", p->name(), p->full_name(true));
auto ns = p->get_namespace(); auto ns = p->get_relative_namespace();
return add_element(ns, std::move(p)); return add_element(ns, std::move(p));
} }
bool diagram::add_package(std::unique_ptr<common::model::package> &&p) bool diagram::add_package_ns(std::unique_ptr<common::model::package> &&p)
{ {
LOG_DBG("Adding namespace package: {}, {}", p->name(), p->full_name(true)); LOG_DBG("Adding namespace package: {}, {}", p->name(), p->full_name(true));
@@ -192,14 +232,28 @@ bool diagram::add_package(std::unique_ptr<common::model::package> &&p)
} }
bool diagram::add_class_fs( bool diagram::add_class_fs(
const common::model::path &p, std::unique_ptr<class_> &&c) const common::model::path &parent_path, std::unique_ptr<class_> &&c)
{ {
// Make sure all parent directories are already packages in the
// model
for (auto it = parent_path.begin(); it != parent_path.end(); it++) {
auto pkg =
std::make_unique<common::model::package>(c->using_namespace());
pkg->set_name(*it);
auto ns = common::model::path(parent_path.begin(), it);
// ns.pop_back();
pkg->set_namespace(ns);
pkg->set_id(common::to_id(pkg->full_name(false)));
add_package_fs(ns, std::move(pkg));
}
const auto base_name = c->name(); const auto base_name = c->name();
const auto full_name = c->full_name(false); const auto full_name = c->full_name(false);
const auto id = c->id(); const auto id = c->id();
auto &cc = *c; auto &cc = *c;
if (add_element(p, std::move(c))) { if (add_element(parent_path, std::move(c))) {
classes_.push_back(std::ref(cc)); classes_.push_back(std::ref(cc));
return true; return true;
} }
@@ -210,7 +264,7 @@ bool diagram::add_class_fs(
return false; return false;
} }
bool diagram::add_class(std::unique_ptr<class_> &&c) bool diagram::add_class_ns(std::unique_ptr<class_> &&c)
{ {
const auto base_name = c->name(); const auto base_name = c->name();
const auto full_name = c->full_name(false); const auto full_name = c->full_name(false);
@@ -259,7 +313,7 @@ bool diagram::add_class(std::unique_ptr<class_> &&c)
return false; return false;
} }
bool diagram::add_enum(std::unique_ptr<enum_> &&e) bool diagram::add_enum_ns(std::unique_ptr<enum_> &&e)
{ {
const auto full_name = e->name(); const auto full_name = e->name();
@@ -282,7 +336,40 @@ bool diagram::add_enum(std::unique_ptr<enum_> &&e)
return false; return false;
} }
bool diagram::add_concept(std::unique_ptr<concept_> &&c) bool diagram::add_enum_fs(
const common::model::path &parent_path, std::unique_ptr<enum_> &&e)
{
// Make sure all parent directories are already packages in the
// model
for (auto it = parent_path.begin(); it != parent_path.end(); it++) {
auto pkg =
std::make_unique<common::model::package>(e->using_namespace());
pkg->set_name(*it);
auto ns = common::model::path(parent_path.begin(), it);
// ns.pop_back();
pkg->set_namespace(ns);
pkg->set_id(common::to_id(pkg->full_name(false)));
add_package_fs(ns, std::move(pkg));
}
const auto base_name = e->name();
const auto full_name = e->full_name(false);
const auto id = e->id();
auto &cc = *e;
if (add_element(parent_path, std::move(e))) {
enums_.push_back(std::ref(cc));
return true;
}
else {
LOG_WARN("Cannot add class {} with id {} due to: {}", base_name, id);
}
return false;
}
bool diagram::add_concept_ns(std::unique_ptr<concept_> &&c)
{ {
const auto base_name = c->name(); const auto base_name = c->name();
const auto full_name = c->full_name(false); const auto full_name = c->full_name(false);
@@ -331,6 +418,38 @@ bool diagram::add_concept(std::unique_ptr<concept_> &&c)
return false; return false;
} }
bool diagram::add_concept_fs(
const common::model::path &parent_path, std::unique_ptr<concept_> &&c)
{
// Make sure all parent directories are already packages in the
// model
for (auto it = parent_path.begin(); it != parent_path.end(); it++) {
auto pkg =
std::make_unique<common::model::package>(c->using_namespace());
pkg->set_name(*it);
auto ns = common::model::path(parent_path.begin(), it);
// ns.pop_back();
pkg->set_namespace(ns);
pkg->set_id(common::to_id(pkg->full_name(false)));
add_package_fs(ns, std::move(pkg));
}
const auto base_name = c->name();
const auto full_name = c->full_name(false);
const auto id = c->id();
auto &cc = *c;
if (add_element(parent_path, std::move(c))) {
concepts_.push_back(std::ref(cc));
return true;
}
LOG_WARN("Cannot add class {} with id {} due to: {}", base_name, id);
return false;
}
void diagram::get_parents( void diagram::get_parents(
clanguml::common::reference_set<class_> &parents) const clanguml::common::reference_set<class_> &parents) const
{ {

View File

@@ -34,6 +34,8 @@ namespace clanguml::class_diagram::model {
using common::opt_ref; using common::opt_ref;
using common::model::diagram_element; using common::model::diagram_element;
using common::model::diagram_t; using common::model::diagram_t;
using common::model::path;
using common::model::path_type;
using nested_trait_ns = using nested_trait_ns =
clanguml::common::model::nested_trait<clanguml::common::model::element, clanguml::common::model::nested_trait<clanguml::common::model::element,
@@ -78,18 +80,14 @@ public:
opt_ref<concept_> get_concept(diagram_element::id_t id) const; opt_ref<concept_> get_concept(diagram_element::id_t id) const;
bool add_class_fs( bool add_class(const path &parent_path, std::unique_ptr<class_> &&c);
const common::model::path &p, std::unique_ptr<class_> &&c);
bool add_class(std::unique_ptr<class_> &&c); bool add_enum(const path &parent_path, std::unique_ptr<enum_> &&e);
bool add_enum(std::unique_ptr<enum_> &&e); bool add_concept(const path &parent_path, std::unique_ptr<concept_> &&e);
bool add_concept(std::unique_ptr<concept_> &&e); bool add_package(
const path &parent_path, std::unique_ptr<common::model::package> &&p);
bool add_package(std::unique_ptr<common::model::package> &&p);
bool add_package_fs(std::unique_ptr<common::model::package> &&p);
std::string to_alias(diagram_element::id_t id) const; std::string to_alias(diagram_element::id_t id) const;
@@ -102,6 +100,22 @@ public:
inja::json context() const override; inja::json context() const override;
private: private:
bool add_class_ns(std::unique_ptr<class_> &&c);
bool add_class_fs(
const common::model::path &parent_path, std::unique_ptr<class_> &&c);
bool add_enum_ns(std::unique_ptr<enum_> &&c);
bool add_enum_fs(
const common::model::path &parent_path, std::unique_ptr<enum_> &&c);
bool add_package_ns(std::unique_ptr<common::model::package> &&p);
bool add_package_fs(const common::model::path &parent_path,
std::unique_ptr<common::model::package> &&p);
bool add_concept_ns(std::unique_ptr<concept_> &&p);
bool add_concept_fs(
const common::model::path &parent_path, std::unique_ptr<concept_> &&p);
common::reference_vector<class_> classes_; common::reference_vector<class_> classes_;
common::reference_vector<enum_> enums_; common::reference_vector<enum_> enums_;

View File

@@ -23,14 +23,13 @@
namespace clanguml::class_diagram::visitor { namespace clanguml::class_diagram::visitor {
template_builder::template_builder(class_diagram::model::diagram &d, template_builder::template_builder(
const config::class_diagram &config, clanguml::class_diagram::visitor::translation_unit_visitor &visitor)
common::visitor::ast_id_mapper &id_mapper, : diagram_{visitor.diagram()}
clang::SourceManager &source_manager) , config_{visitor.config()}
: diagram_{d} , id_mapper_{visitor.id_mapper()}
, config_{config} , source_manager_{visitor.source_manager()}
, id_mapper_{id_mapper} , visitor_{visitor}
, source_manager_{source_manager}
{ {
} }
@@ -268,6 +267,8 @@ std::unique_ptr<class_> template_builder::build(const clang::NamedDecl *cls,
template_instantiation.set_id( template_instantiation.set_id(
common::to_id(template_instantiation_ptr->full_name(false))); common::to_id(template_instantiation_ptr->full_name(false)));
visitor_.set_source_location(*template_decl, *template_instantiation_ptr);
return template_instantiation_ptr; return template_instantiation_ptr;
} }
@@ -1043,7 +1044,7 @@ template_builder::try_as_template_specialization_type(
} }
if (diagram().should_include(nested_template_instantiation_full_name)) { if (diagram().should_include(nested_template_instantiation_full_name)) {
diagram().add_class(std::move(nested_template_instantiation)); visitor_.add_class(std::move(nested_template_instantiation));
} }
return argument; return argument;
@@ -1155,7 +1156,7 @@ std::optional<template_parameter> template_builder::try_as_record_type(
parent.value()->add_relationship( parent.value()->add_relationship(
{relationship_t::kDependency, tag_argument->id()}); {relationship_t::kDependency, tag_argument->id()});
diagram().add_class(std::move(tag_argument)); visitor_.add_class(std::move(tag_argument));
} }
} }
} }

View File

@@ -34,12 +34,12 @@ using found_relationships_t =
std::vector<std::pair<clanguml::common::model::diagram_element::id_t, std::vector<std::pair<clanguml::common::model::diagram_element::id_t,
common::model::relationship_t>>; common::model::relationship_t>>;
class translation_unit_visitor;
class template_builder { class template_builder {
public: public:
template_builder(class_diagram::model::diagram &d, template_builder(
const config::class_diagram &config, clanguml::class_diagram::visitor::translation_unit_visitor &visitor);
common::visitor::ast_id_mapper &id_mapper,
clang::SourceManager &source_manager);
class_diagram::model::diagram &diagram(); class_diagram::model::diagram &diagram();
@@ -192,6 +192,8 @@ private:
common::visitor::ast_id_mapper &id_mapper_; common::visitor::ast_id_mapper &id_mapper_;
clang::SourceManager &source_manager_; clang::SourceManager &source_manager_;
clanguml::class_diagram::visitor::translation_unit_visitor &visitor_;
}; };
} // namespace clanguml::class_diagram::visitor } // namespace clanguml::class_diagram::visitor

View File

@@ -32,7 +32,7 @@ translation_unit_visitor::translation_unit_visitor(clang::SourceManager &sm,
: common::visitor::translation_unit_visitor{sm, config} : common::visitor::translation_unit_visitor{sm, config}
, diagram_{diagram} , diagram_{diagram}
, config_{config} , config_{config}
, template_builder_{diagram_, config_, id_mapper_, sm} , template_builder_{*this}
{ {
} }
@@ -84,7 +84,7 @@ bool translation_unit_visitor::VisitNamespaceDecl(clang::NamespaceDecl *ns)
} }
if (!p->skip()) { if (!p->skip()) {
diagram().add_package(std::move(p)); diagram().add_package(package_path, std::move(p));
} }
} }
@@ -170,7 +170,7 @@ bool translation_unit_visitor::VisitEnumDecl(clang::EnumDecl *enm)
} }
if (diagram().should_include(qualified_name)) if (diagram().should_include(qualified_name))
diagram().add_enum(std::move(e_ptr)); add_enum(std::move(e_ptr));
return true; return true;
} }
@@ -225,14 +225,14 @@ bool translation_unit_visitor::VisitClassTemplateSpecializationDecl(
{relationship_t::kInstantiation, maybe_id.value()}); {relationship_t::kInstantiation, maybe_id.value()});
} }
if (diagram_.should_include(template_specialization)) { if (diagram().should_include(template_specialization)) {
const auto full_name = template_specialization.full_name(false); const auto full_name = template_specialization.full_name(false);
const auto id = template_specialization.id(); const auto id = template_specialization.id();
LOG_DBG("Adding class template specialization {} with id {}", full_name, LOG_DBG("Adding class template specialization {} with id {}", full_name,
id); id);
diagram_.add_class(std::move(template_specialization_ptr)); add_class(std::move(template_specialization_ptr));
} }
return true; return true;
@@ -265,13 +265,13 @@ bool translation_unit_visitor::VisitTypeAliasTemplateDecl(
if (!template_specialization_ptr) if (!template_specialization_ptr)
return true; return true;
if (diagram_.should_include(*template_specialization_ptr)) { if (diagram().should_include(*template_specialization_ptr)) {
const auto name = template_specialization_ptr->full_name(); const auto name = template_specialization_ptr->full_name();
const auto id = template_specialization_ptr->id(); const auto id = template_specialization_ptr->id();
LOG_DBG("Adding class {} with id {}", name, id); LOG_DBG("Adding class {} with id {}", name, id);
diagram_.add_class(std::move(template_specialization_ptr)); add_class(std::move(template_specialization_ptr));
} }
return true; return true;
@@ -327,11 +327,11 @@ bool translation_unit_visitor::VisitClassTemplateDecl(
process_class_declaration(*cls->getTemplatedDecl(), *c_ptr); process_class_declaration(*cls->getTemplatedDecl(), *c_ptr);
forward_declarations_.erase(id); forward_declarations_.erase(id);
if (diagram_.should_include(*c_ptr)) { if (diagram().should_include(*c_ptr)) {
const auto name = c_ptr->full_name(); const auto name = c_ptr->full_name();
LOG_DBG("Adding class template {} with id {}", name, id); LOG_DBG("Adding class template {} with id {}", name, id);
diagram_.add_class(std::move(c_ptr)); add_class(std::move(c_ptr));
} }
return true; return true;
@@ -380,11 +380,11 @@ bool translation_unit_visitor::VisitRecordDecl(clang::RecordDecl *rec)
} }
forward_declarations_.erase(id); forward_declarations_.erase(id);
if (diagram_.should_include(record_model)) { if (diagram().should_include(record_model)) {
LOG_DBG("Adding struct/union {} with id {}", LOG_DBG("Adding struct/union {} with id {}",
record_model.full_name(false), record_model.id()); record_model.full_name(false), record_model.id());
diagram_.add_class(std::move(record_ptr)); add_class(std::move(record_ptr));
} }
else { else {
LOG_DBG("Skipping struct/union {} with id {}", record_model.full_name(), LOG_DBG("Skipping struct/union {} with id {}", record_model.full_name(),
@@ -436,11 +436,11 @@ bool translation_unit_visitor::TraverseConceptDecl(clang::ConceptDecl *cpt)
*concept_model, cpt->getConstraintExpr()); *concept_model, cpt->getConstraintExpr());
} }
if (diagram_.should_include(*concept_model)) { if (diagram().should_include(*concept_model)) {
LOG_DBG("Adding concept {} with id {}", concept_model->full_name(false), LOG_DBG("Adding concept {} with id {}", concept_model->full_name(false),
concept_model->id()); concept_model->id());
diagram_.add_concept(std::move(concept_model)); add_concept(std::move(concept_model));
} }
else { else {
LOG_DBG("Skipping concept {} with id {}", concept_model->full_name(), LOG_DBG("Skipping concept {} with id {}", concept_model->full_name(),
@@ -2105,23 +2105,46 @@ void translation_unit_visitor::add_class(std::unique_ptr<class_> &&c)
common::model::path p{file, common::model::path_type::kFilesystem}; common::model::path p{file, common::model::path_type::kFilesystem};
p.pop_back(); p.pop_back();
// Make sure all parent directories are already packages in the model diagram().add_class(p, std::move(c));
for (auto it = p.begin(); it != p.end(); it++) {
auto pkg = std::make_unique<common::model::package>(
config().using_namespace());
pkg->set_name(*it);
auto ns = common::model::path(p.begin(), it);
// ns.pop_back();
pkg->set_namespace(ns);
pkg->set_id(common::to_id(pkg->full_name(false)));
diagram().add_package_fs(std::move(pkg));
}
diagram().add_class_fs(p, std::move(c));
} }
else { else {
diagram().add_class(std::move(c)); diagram().add_class(c->path(), std::move(c));
}
}
void translation_unit_visitor::add_enum(std::unique_ptr<enum_> &&e)
{
if ((config().generate_packages() &&
config().package_type() == config::package_type_t::kDirectory)) {
assert(!e->file().empty());
const auto file = config().make_path_relative(e->file());
common::model::path p{file, common::model::path_type::kFilesystem};
p.pop_back();
diagram().add_enum(p, std::move(e));
}
else {
diagram().add_enum(e->path(), std::move(e));
}
}
void translation_unit_visitor::add_concept(std::unique_ptr<concept_> &&c)
{
if ((config().generate_packages() &&
config().package_type() == config::package_type_t::kDirectory)) {
assert(!c->file().empty());
const auto file = config().make_path_relative(c->file());
common::model::path p{file, common::model::path_type::kFilesystem};
p.pop_back();
diagram().add_concept(p, std::move(c));
}
else {
diagram().add_concept(c->path(), std::move(c));
} }
} }

View File

@@ -43,6 +43,7 @@ using clanguml::class_diagram::model::class_;
using clanguml::class_diagram::model::class_member; using clanguml::class_diagram::model::class_member;
using clanguml::class_diagram::model::class_method; using clanguml::class_diagram::model::class_method;
using clanguml::class_diagram::model::class_parent; using clanguml::class_diagram::model::class_parent;
using clanguml::class_diagram::model::concept_;
using clanguml::class_diagram::model::diagram; using clanguml::class_diagram::model::diagram;
using clanguml::class_diagram::model::enum_; using clanguml::class_diagram::model::enum_;
using clanguml::class_diagram::model::method_parameter; using clanguml::class_diagram::model::method_parameter;
@@ -118,9 +119,13 @@ public:
*/ */
void finalize(); void finalize();
private: common::visitor::ast_id_mapper &id_mapper() const { return id_mapper_; }
void add_class(std::unique_ptr<class_> &&c);
void add_class(std::unique_ptr<class_> &&c);
void add_enum(std::unique_ptr<enum_> &&e);
void add_concept(std::unique_ptr<concept_> &&c);
private:
bool should_include(const clang::NamedDecl *decl); bool should_include(const clang::NamedDecl *decl);
std::unique_ptr<clanguml::class_diagram::model::class_> std::unique_ptr<clanguml::class_diagram::model::class_>
@@ -220,8 +225,6 @@ private:
bool has_processed_template_class(const std::string &qualified_name) const; bool has_processed_template_class(const std::string &qualified_name) const;
common::visitor::ast_id_mapper &id_mapper() const { return id_mapper_; }
template_builder &tbuilder() { return template_builder_; } template_builder &tbuilder() { return template_builder_; }
// Reference to the output diagram model // Reference to the output diagram model

View File

@@ -62,7 +62,6 @@ public:
*/ */
clang::SourceManager &source_manager() const; clang::SourceManager &source_manager() const;
protected:
/** /**
* @brief Set source location in diagram element * @brief Set source location in diagram element
* *
@@ -93,6 +92,7 @@ protected:
void set_source_location(const clang::SourceLocation &location, void set_source_location(const clang::SourceLocation &location,
clanguml::common::model::source_location &element); clanguml::common::model::source_location &element);
protected:
/** /**
* @brief Set source location in diagram element * @brief Set source location in diagram element
* *