Merge pull request #148 from bkryza/add-packages-from-directory-structure

Add packages from directory structure
This commit is contained in:
Bartek Kryza
2023-05-28 22:58:45 +02:00
committed by GitHub
291 changed files with 7038 additions and 4416 deletions

View File

@@ -168,7 +168,11 @@ void generator::generate(const package &p, nlohmann::json &parent) const
if (!uns.starts_with({p.full_name(false)})) {
LOG_DBG("Generating package {}", p.name());
package_object["type"] = "namespace";
if (m_config.package_type() == config::package_type_t::kDirectory)
package_object["type"] = "directory";
else
package_object["type"] = "namespace";
package_object["name"] = p.name();
package_object["display_name"] = p.full_name(false);
}

View File

@@ -721,9 +721,14 @@ void generator::generate_relationships(
{
for (const auto &subpackage : p) {
if (dynamic_cast<package *>(subpackage.get()) != nullptr) {
// TODO: add option - generate_empty_packages
// TODO: add option - generate_empty_packages, currently
// packages which do not contain anything but other packages
// are skipped
const auto &sp = dynamic_cast<package &>(*subpackage);
if (!sp.is_empty())
if (!sp.is_empty() &&
!sp.all_of([this](const common::model::element &e) {
return !m_model.should_include(e);
}))
generate_relationships(sp, ostr);
}
else if (dynamic_cast<class_ *>(subpackage.get()) != nullptr) {
@@ -774,7 +779,10 @@ void generator::generate_top_level_elements(std::ostream &ostr) const
{
for (const auto &p : m_model) {
if (auto *pkg = dynamic_cast<package *>(p.get()); pkg) {
if (!pkg->is_empty())
if (!pkg->is_empty() &&
!pkg->all_of([this](const common::model::element &e) {
return !m_model.should_include(e);
}))
generate(*pkg, ostr);
}
else if (auto *cls = dynamic_cast<class_ *>(p.get()); cls) {

View File

@@ -21,20 +21,21 @@
#include "util/error.h"
#include "util/util.h"
#include <cassert>
namespace clanguml::class_diagram::model {
const common::reference_vector<class_> &diagram::classes() const
{
return classes_;
return element_view<class_>::view();
}
const common::reference_vector<enum_> &diagram::enums() const { return enums_; }
const common::reference_vector<enum_> &diagram::enums() const
{
return element_view<enum_>::view();
}
const common::reference_vector<concept_> &diagram::concepts() const
{
return concepts_;
return element_view<concept_>::view();
}
common::model::diagram_t diagram::type() const
@@ -46,17 +47,17 @@ common::optional_ref<clanguml::common::model::diagram_element> diagram::get(
const std::string &full_name) const
{
common::optional_ref<clanguml::common::model::diagram_element> res =
get_class(full_name);
find<class_>(full_name);
if (res.has_value())
return res;
res = get_enum(full_name);
res = find<enum_>(full_name);
if (res.has_value())
return res;
res = get_concept(full_name);
res = find<concept_>(full_name);
return res;
}
@@ -66,114 +67,24 @@ common::optional_ref<clanguml::common::model::diagram_element> diagram::get(
{
common::optional_ref<clanguml::common::model::diagram_element> res;
res = get_class(id);
res = find<class_>(id);
if (res.has_value())
return res;
res = get_enum(id);
res = find<enum_>(id);
if (res.has_value())
return res;
res = get_concept(id);
res = find<concept_>(id);
return res;
}
bool diagram::has_class(const class_ &c) const
{
return std::any_of(classes_.cbegin(), classes_.cend(),
[&c](const auto &cc) { return cc.get() == c; });
}
bool diagram::has_enum(const enum_ &e) const
{
return std::any_of(enums_.cbegin(), enums_.cend(),
[&e](const auto &ee) { return ee.get().full_name() == e.full_name(); });
}
bool diagram::has_concept(const concept_ &c) const
{
return std::any_of(concepts_.cbegin(), concepts_.cend(),
[&c](const auto &cc) { return cc.get() == c; });
}
common::optional_ref<class_> diagram::get_class(const std::string &name) const
{
for (const auto &c : classes_) {
const auto full_name = c.get().full_name(false);
if (full_name == name) {
return {c};
}
}
return {};
}
common::optional_ref<class_> diagram::get_class(
clanguml::common::model::diagram_element::id_t id) const
{
for (const auto &c : classes_) {
if (c.get().id() == id) {
return {c};
}
}
return {};
}
common::optional_ref<enum_> diagram::get_enum(const std::string &name) const
{
for (const auto &e : enums_) {
if (e.get().full_name(false) == name) {
return {e};
}
}
return {};
}
common::optional_ref<enum_> diagram::get_enum(
clanguml::common::model::diagram_element::id_t id) const
{
for (const auto &e : enums_) {
if (e.get().id() == id) {
return {e};
}
}
return {};
}
common::optional_ref<concept_> diagram::get_concept(
const std::string &name) const
{
for (const auto &c : concepts_) {
const auto full_name = c.get().full_name(false);
if (full_name == name) {
return {c};
}
}
return {};
}
common::optional_ref<concept_> diagram::get_concept(
clanguml::common::model::diagram_element::id_t id) const
{
for (const auto &c : concepts_) {
if (c.get().id() == id) {
return {c};
}
}
return {};
}
bool diagram::add_package(std::unique_ptr<common::model::package> &&p)
template <>
bool diagram::add_with_namespace_path<common::model::package>(
std::unique_ptr<common::model::package> &&p)
{
LOG_DBG("Adding namespace package: {}, {}", p->name(), p->full_name(true));
@@ -182,125 +93,16 @@ bool diagram::add_package(std::unique_ptr<common::model::package> &&p)
return add_element(ns, std::move(p));
}
bool diagram::add_class(std::unique_ptr<class_> &&c)
template <>
bool diagram::add_with_filesystem_path<common::model::package>(
const common::model::path & /*parent_path*/,
std::unique_ptr<common::model::package> &&p)
{
const auto base_name = c->name();
const auto full_name = c->full_name(false);
LOG_DBG("Adding filesystem package: {}, {}", p->name(), p->full_name(true));
LOG_DBG("Adding class: {}::{}, {}", c->get_namespace().to_string(),
base_name, full_name);
auto ns = p->get_relative_namespace();
if (util::contains(base_name, "::"))
throw std::runtime_error("Name cannot contain namespace: " + base_name);
if (util::contains(base_name, "*"))
throw std::runtime_error("Name cannot contain *: " + base_name);
const auto ns = c->get_relative_namespace();
auto name = base_name;
auto name_with_ns = c->name_and_ns();
auto name_and_ns = ns | name;
auto &cc = *c;
auto id = cc.id();
try {
if (!has_class(cc)) {
if (add_element(ns, std::move(c)))
classes_.push_back(std::ref(cc));
const auto &el = get_element<class_>(name_and_ns).value();
if ((el.name() != name) || !(el.get_relative_namespace() == ns))
throw std::runtime_error(
"Invalid element stored in the diagram tree");
LOG_DBG("Added class {} ({} - [{}])", base_name, full_name, id);
return true;
}
}
catch (const std::runtime_error &e) {
LOG_WARN(
"Cannot add concept {} with id {} due to: {}", name, id, e.what());
return false;
}
LOG_DBG(
"Class {} ({} - [{}]) already in the model", base_name, full_name, id);
return false;
}
bool diagram::add_enum(std::unique_ptr<enum_> &&e)
{
const auto full_name = e->name();
LOG_DBG("Adding enum: {}", full_name);
assert(!util::contains(e->name(), "::"));
auto e_ref = std::ref(*e);
auto ns = e->get_relative_namespace();
if (!has_enum(*e)) {
if (add_element(ns, std::move(e))) {
enums_.emplace_back(e_ref);
return true;
}
}
LOG_DBG("Enum {} already in the model", full_name);
return false;
}
bool diagram::add_concept(std::unique_ptr<concept_> &&c)
{
const auto base_name = c->name();
const auto full_name = c->full_name(false);
LOG_DBG("Adding concept: {}::{}, {}", c->get_namespace().to_string(),
base_name, full_name);
if (util::contains(base_name, "::"))
throw std::runtime_error("Name cannot contain namespace: " + base_name);
if (util::contains(base_name, "*"))
throw std::runtime_error("Name cannot contain *: " + base_name);
const auto ns = c->get_relative_namespace();
auto name = base_name;
auto name_with_ns = c->name_and_ns();
auto name_and_ns = ns | name;
auto &cc = *c;
auto id = cc.id();
try {
if (!has_concept(cc)) {
if (add_element(ns, std::move(c)))
concepts_.push_back(std::ref(cc));
const auto &el = get_element<concept_>(name_and_ns).value();
if ((el.name() != name) || !(el.get_relative_namespace() == ns))
throw std::runtime_error(
"Invalid element stored in the diagram tree");
LOG_DBG("Added concept {} ({} - [{}])", base_name, full_name, id);
return true;
}
}
catch (const std::runtime_error &e) {
LOG_WARN(
"Cannot add concept {} with id {} due to: {}", name, id, e.what());
return false;
}
LOG_DBG("Concept {} ({} - [{}]) already in the model", base_name, full_name,
id);
return false;
return add_element(ns, std::move(p));
}
void diagram::get_parents(
@@ -309,7 +111,7 @@ void diagram::get_parents(
bool found_new{false};
for (const auto &parent : parents) {
for (const auto &pp : parent.get().parents()) {
auto p = get_class(pp.id());
auto p = find<class_>(pp.id());
if (p.has_value()) {
auto [it, found] = parents.emplace(std::ref(p.value()));
@@ -327,13 +129,19 @@ void diagram::get_parents(
bool diagram::has_element(
clanguml::common::model::diagram_element::id_t id) const
{
const auto has_class = std::any_of(classes_.begin(), classes_.end(),
const auto has_class = std::any_of(classes().begin(), classes().end(),
[id](const auto &c) { return c.get().id() == id; });
if (has_class)
return true;
return std::any_of(enums_.begin(), enums_.end(),
const auto has_concept = std::any_of(classes().begin(), classes().end(),
[id](const auto &c) { return c.get().id() == id; });
if (has_concept)
return true;
return std::any_of(enums().begin(), enums().end(),
[id](const auto &c) { return c.get().id() == id; });
}
@@ -342,18 +150,18 @@ std::string diagram::to_alias(
{
LOG_DBG("Looking for alias for {}", id);
for (const auto &c : classes_) {
for (const auto &c : classes()) {
if (c.get().id() == id) {
return c.get().alias();
}
}
for (const auto &e : enums_) {
for (const auto &e : enums()) {
if (e.get().id() == id)
return e.get().alias();
}
for (const auto &c : concepts_) {
for (const auto &c : concepts()) {
if (c.get().id() == id)
return c.get().alias();
}

View File

@@ -19,6 +19,7 @@
#include "class.h"
#include "common/model/diagram.h"
#include "common/model/element_view.h"
#include "common/model/nested_trait.h"
#include "common/model/package.h"
#include "common/types.h"
@@ -31,10 +32,22 @@
namespace clanguml::class_diagram::model {
class diagram : public clanguml::common::model::diagram,
public clanguml::common::model::nested_trait<
clanguml::common::model::element,
clanguml::common::model::namespace_> {
using common::opt_ref;
using common::model::diagram_element;
using common::model::diagram_t;
using common::model::element_view;
using common::model::path;
using common::model::path_type;
using nested_trait_ns =
clanguml::common::model::nested_trait<clanguml::common::model::element,
clanguml::common::model::namespace_>;
class diagram : public common::model::diagram,
public element_view<class_>,
public element_view<enum_>,
public element_view<concept_>,
public nested_trait_ns {
public:
diagram() = default;
@@ -43,13 +56,11 @@ public:
diagram &operator=(const diagram &) = delete;
diagram &operator=(diagram &&) = default;
common::model::diagram_t type() const override;
diagram_t type() const override;
common::optional_ref<common::model::diagram_element> get(
const std::string &full_name) const override;
opt_ref<diagram_element> get(const std::string &full_name) const override;
common::optional_ref<common::model::diagram_element> get(
clanguml::common::model::diagram_element::id_t id) const override;
opt_ref<diagram_element> get(diagram_element::id_t id) const override;
const common::reference_vector<class_> &classes() const;
@@ -57,54 +68,173 @@ public:
const common::reference_vector<concept_> &concepts() const;
bool has_class(const class_ &c) const;
template <typename ElementT> bool contains(const ElementT &e);
bool has_enum(const enum_ &e) const;
template <typename ElementT>
opt_ref<ElementT> find(const std::string &name) const;
bool has_concept(const concept_ &e) const;
template <typename ElementT>
opt_ref<ElementT> find(diagram_element::id_t id) const;
common::optional_ref<class_> get_class(const std::string &name) const;
template <typename ElementT>
bool add(const path &parent_path, std::unique_ptr<ElementT> &&e)
{
if (parent_path.type() == common::model::path_type::kNamespace) {
return add_with_namespace_path(std::move(e));
}
common::optional_ref<class_> get_class(
clanguml::common::model::diagram_element::id_t id) const;
return add_with_filesystem_path(parent_path, std::move(e));
}
common::optional_ref<enum_> get_enum(const std::string &name) const;
common::optional_ref<enum_> get_enum(
clanguml::common::model::diagram_element::id_t id) const;
common::optional_ref<concept_> get_concept(const std::string &name) const;
common::optional_ref<concept_> get_concept(
clanguml::common::model::diagram_element::id_t id) const;
bool add_class(std::unique_ptr<class_> &&c);
bool add_enum(std::unique_ptr<enum_> &&e);
bool add_concept(std::unique_ptr<concept_> &&e);
bool add_package(std::unique_ptr<common::model::package> &&p);
std::string to_alias(
clanguml::common::model::diagram_element::id_t id) const;
std::string to_alias(diagram_element::id_t id) const;
void get_parents(clanguml::common::reference_set<class_> &parents) const;
friend void print_diagram_tree(const diagram &d, int level);
bool has_element(
clanguml::common::model::diagram_element::id_t id) const override;
bool has_element(diagram_element::id_t id) const override;
inja::json context() const override;
private:
common::reference_vector<class_> classes_;
template <typename ElementT>
bool add_with_namespace_path(std::unique_ptr<ElementT> &&e);
common::reference_vector<enum_> enums_;
common::reference_vector<concept_> concepts_;
template <typename ElementT>
bool add_with_filesystem_path(
const common::model::path &parent_path, std::unique_ptr<ElementT> &&e);
};
template <typename ElementT> bool diagram::contains(const ElementT &element)
{
return std::any_of(element_view<ElementT>::view().cbegin(),
element_view<ElementT>::view().cend(),
[&element](
const auto &element_opt) { return element_opt.get() == element; });
}
template <typename ElementT>
bool diagram::add_with_namespace_path(std::unique_ptr<ElementT> &&e)
{
const auto base_name = e->name();
const auto full_name = e->full_name(false);
const auto element_type = e->type_name();
LOG_DBG("Adding {}: {}::{}, {}", element_type,
e->get_namespace().to_string(), base_name, full_name);
if (util::contains(base_name, "::"))
throw std::runtime_error("Name cannot contain namespace: " + base_name);
if (util::contains(base_name, "*"))
throw std::runtime_error("Name cannot contain *: " + base_name);
const auto ns = e->get_relative_namespace();
auto name = base_name;
auto name_with_ns = e->name_and_ns();
auto name_and_ns = ns | name;
auto &e_ref = *e;
auto id = e_ref.id();
try {
if (!contains(e_ref)) {
if (add_element(ns, std::move(e)))
element_view<ElementT>::add(std::ref(e_ref));
const auto &el = get_element<ElementT>(name_and_ns).value();
if ((el.name() != name) || !(el.get_relative_namespace() == ns))
throw std::runtime_error(
"Invalid element stored in the diagram tree");
LOG_DBG("Added {} {} ({} - [{}])", element_type, base_name,
full_name, id);
return true;
}
}
catch (const std::runtime_error &e) {
LOG_WARN("Cannot add {} {} with id {} due to: {}", element_type, name,
id, e.what());
return false;
}
LOG_DBG("{} {} ({} - [{}]) already in the model", element_type, base_name,
full_name, id);
return false;
}
template <typename ElementT>
bool diagram::add_with_filesystem_path(
const common::model::path &parent_path, std::unique_ptr<ElementT> &&e)
{
const auto element_type = e->type_name();
// 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(ns, std::move(pkg));
}
const auto base_name = e->name();
const auto full_name = e->full_name(false);
auto &e_ref = *e;
if (add_element(parent_path, std::move(e))) {
element_view<ElementT>::add(std::ref(e_ref));
return true;
}
return false;
}
template <typename ElementT>
opt_ref<ElementT> diagram::find(const std::string &name) const
{
for (const auto &element : element_view<ElementT>::view()) {
const auto full_name = element.get().full_name(false);
if (full_name == name) {
return {element};
}
}
return {};
}
template <typename ElementT>
opt_ref<ElementT> diagram::find(diagram_element::id_t id) const
{
for (const auto &element : element_view<ElementT>::view()) {
if (element.get().id() == id) {
return {element};
}
}
return {};
}
//
// Template method specialization pre-declarations...
//
template <>
bool diagram::add_with_namespace_path<common::model::package>(
std::unique_ptr<common::model::package> &&p);
template <>
bool diagram::add_with_filesystem_path<common::model::package>(
const common::model::path &parent_path,
std::unique_ptr<common::model::package> &&p);
} // namespace clanguml::class_diagram::model
namespace clanguml::common::model {

View File

@@ -23,14 +23,13 @@
namespace clanguml::class_diagram::visitor {
template_builder::template_builder(class_diagram::model::diagram &d,
const config::class_diagram &config,
common::visitor::ast_id_mapper &id_mapper,
clang::SourceManager &source_manager)
: diagram_{d}
, config_{config}
, id_mapper_{id_mapper}
, source_manager_{source_manager}
template_builder::template_builder(
clanguml::class_diagram::visitor::translation_unit_visitor &visitor)
: diagram_{visitor.diagram()}
, config_{visitor.config()}
, id_mapper_{visitor.id_mapper()}
, source_manager_{visitor.source_manager()}
, visitor_{visitor}
{
}
@@ -268,6 +267,8 @@ std::unique_ptr<class_> template_builder::build(const clang::NamedDecl *cls,
template_instantiation.set_id(
common::to_id(template_instantiation_ptr->full_name(false)));
visitor_.set_source_location(*template_decl, *template_instantiation_ptr);
return template_instantiation_ptr;
}
@@ -936,9 +937,9 @@ std::optional<template_parameter> template_builder::try_as_decl_type(
std::optional<template_parameter> template_builder::try_as_typedef_type(
std::optional<clanguml::class_diagram::model::class_ *> &parent,
const clang::NamedDecl *cls, const clang::TemplateDecl *template_decl,
clang::QualType &type, class_ &template_instantiation,
size_t argument_index)
const clang::NamedDecl * /*cls*/,
const clang::TemplateDecl * /*template_decl*/, clang::QualType &type,
class_ & /*template_instantiation*/, size_t /*argument_index*/)
{
const auto *typedef_type =
common::dereference(type)->getAs<clang::TypedefType>();
@@ -1045,7 +1046,9 @@ template_builder::try_as_template_specialization_type(
}
if (diagram().should_include(nested_template_instantiation_full_name)) {
diagram().add_class(std::move(nested_template_instantiation));
visitor_.set_source_location(
*template_decl, *nested_template_instantiation);
visitor_.add_class(std::move(nested_template_instantiation));
}
return argument;
@@ -1089,7 +1092,7 @@ std::optional<template_parameter> template_builder::try_as_template_parm_type(
argument.is_variadic(is_variadic);
ensure_lambda_type_is_relative(type_parameter_name);
visitor_.ensure_lambda_type_is_relative(type_parameter_name);
return argument;
}
@@ -1108,7 +1111,7 @@ std::optional<template_parameter> template_builder::try_as_lambda(
auto argument = template_parameter::make_argument("");
type = consume_context(type, argument);
ensure_lambda_type_is_relative(type_name);
visitor_.ensure_lambda_type_is_relative(type_name);
argument.set_type(type_name);
return argument;
@@ -1156,8 +1159,8 @@ std::optional<template_parameter> template_builder::try_as_record_type(
if (parent.has_value())
parent.value()->add_relationship(
{relationship_t::kDependency, tag_argument->id()});
diagram().add_class(std::move(tag_argument));
visitor_.set_source_location(*template_decl, *tag_argument);
visitor_.add_class(std::move(tag_argument));
}
}
}
@@ -1256,40 +1259,4 @@ bool template_builder::add_base_classes(class_ &tinst,
return variadic_params;
}
void template_builder::ensure_lambda_type_is_relative(
std::string &parameter_type) const
{
#ifdef _MSC_VER
auto root_name = fmt::format(
"{}\\", std::filesystem::current_path().root_name().string());
if (root_name.back() == '\\') {
root_name.pop_back();
root_name.push_back('/');
}
#else
auto root_name = std::string{"/"};
#endif
std::string lambda_prefix{fmt::format("(lambda at {}", root_name)};
while (parameter_type.find(lambda_prefix) != std::string::npos) {
auto lambda_begin = parameter_type.find(lambda_prefix);
auto absolute_lambda_path_end =
parameter_type.find(':', lambda_begin + lambda_prefix.size());
auto absolute_lambda_path =
parameter_type.substr(lambda_begin + lambda_prefix.size() - 1,
absolute_lambda_path_end -
(lambda_begin + lambda_prefix.size() - 1));
auto relative_lambda_path = util::path_to_url(std::filesystem::relative(
absolute_lambda_path, config().relative_to())
.string());
parameter_type = fmt::format("{}(lambda at {}{}",
parameter_type.substr(0, lambda_begin), relative_lambda_path,
parameter_type.substr(absolute_lambda_path_end));
}
}
} // namespace clanguml::class_diagram::visitor

View File

@@ -34,12 +34,12 @@ using found_relationships_t =
std::vector<std::pair<clanguml::common::model::diagram_element::id_t,
common::model::relationship_t>>;
class translation_unit_visitor;
class template_builder {
public:
template_builder(class_diagram::model::diagram &d,
const config::class_diagram &config,
common::visitor::ast_id_mapper &id_mapper,
clang::SourceManager &source_manager);
template_builder(
clanguml::class_diagram::visitor::translation_unit_visitor &visitor);
class_diagram::model::diagram &diagram();
@@ -181,8 +181,6 @@ public:
clang::SourceManager &source_manager() const;
private:
void ensure_lambda_type_is_relative(std::string &parameter_type) const;
// Reference to the output diagram model
clanguml::class_diagram::model::diagram &diagram_;
@@ -192,6 +190,8 @@ private:
common::visitor::ast_id_mapper &id_mapper_;
clang::SourceManager &source_manager_;
clanguml::class_diagram::visitor::translation_unit_visitor &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}
, diagram_{diagram}
, config_{config}
, template_builder_{diagram_, config_, id_mapper_, sm}
, template_builder_{*this}
{
}
@@ -40,6 +40,9 @@ bool translation_unit_visitor::VisitNamespaceDecl(clang::NamespaceDecl *ns)
{
assert(ns != nullptr);
if (config().package_type() == config::package_type_t::kDirectory)
return true;
if (ns->isAnonymousNamespace() || ns->isInline())
return true;
@@ -81,7 +84,7 @@ bool translation_unit_visitor::VisitNamespaceDecl(clang::NamespaceDecl *ns)
}
if (!p->skip()) {
diagram().add_package(std::move(p));
diagram().add(package_path, std::move(p));
}
}
@@ -137,8 +140,8 @@ bool translation_unit_visitor::VisitEnumDecl(clang::EnumDecl *enm)
}
}
if (id_opt && diagram_.get_class(*id_opt)) {
auto parent_class = diagram_.get_class(*id_opt);
if (id_opt && diagram().find<class_>(*id_opt)) {
auto parent_class = diagram().find<class_>(*id_opt);
e.set_namespace(ns);
e.set_name(parent_class.value().name() + "##" + enm->getNameAsString());
@@ -167,7 +170,7 @@ bool translation_unit_visitor::VisitEnumDecl(clang::EnumDecl *enm)
}
if (diagram().should_include(qualified_name))
diagram().add_enum(std::move(e_ptr));
add_enum(std::move(e_ptr));
return true;
}
@@ -222,14 +225,14 @@ bool translation_unit_visitor::VisitClassTemplateSpecializationDecl(
{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 id = template_specialization.id();
LOG_DBG("Adding class template specialization {} with id {}", full_name,
id);
diagram_.add_class(std::move(template_specialization_ptr));
add_class(std::move(template_specialization_ptr));
}
return true;
@@ -262,13 +265,13 @@ bool translation_unit_visitor::VisitTypeAliasTemplateDecl(
if (!template_specialization_ptr)
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 id = template_specialization_ptr->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;
@@ -324,11 +327,11 @@ bool translation_unit_visitor::VisitClassTemplateDecl(
process_class_declaration(*cls->getTemplatedDecl(), *c_ptr);
forward_declarations_.erase(id);
if (diagram_.should_include(*c_ptr)) {
if (diagram().should_include(*c_ptr)) {
const auto name = c_ptr->full_name();
LOG_DBG("Adding class template {} with id {}", name, id);
diagram_.add_class(std::move(c_ptr));
add_class(std::move(c_ptr));
}
return true;
@@ -361,8 +364,8 @@ bool translation_unit_visitor::VisitRecordDecl(clang::RecordDecl *rec)
id_mapper().add(rec->getID(), rec_id);
auto &record_model = diagram().get_class(rec_id).has_value()
? *diagram().get_class(rec_id).get()
auto &record_model = diagram().find<class_>(rec_id).has_value()
? *diagram().find<class_>(rec_id).get()
: *record_ptr;
if (rec->isCompleteDefinition() && !record_model.complete()) {
@@ -377,11 +380,11 @@ bool translation_unit_visitor::VisitRecordDecl(clang::RecordDecl *rec)
}
forward_declarations_.erase(id);
if (diagram_.should_include(record_model)) {
if (diagram().should_include(record_model)) {
LOG_DBG("Adding struct/union {} with id {}",
record_model.full_name(false), record_model.id());
diagram_.add_class(std::move(record_ptr));
add_class(std::move(record_ptr));
}
else {
LOG_DBG("Skipping struct/union {} with id {}", record_model.full_name(),
@@ -433,11 +436,11 @@ bool translation_unit_visitor::TraverseConceptDecl(clang::ConceptDecl *cpt)
*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),
concept_model->id());
diagram_.add_concept(std::move(concept_model));
add_concept(std::move(concept_model));
}
else {
LOG_DBG("Skipping concept {} with id {}", concept_model->full_name(),
@@ -727,8 +730,8 @@ bool translation_unit_visitor::VisitCXXRecordDecl(clang::CXXRecordDecl *cls)
id_mapper().add(cls->getID(), cls_id);
auto &class_model = diagram().get_class(cls_id).has_value()
? *diagram().get_class(cls_id).get()
auto &class_model = diagram().find<class_>(cls_id).has_value()
? *diagram().find<class_>(cls_id).get()
: *c_ptr;
if (cls->isCompleteDefinition() && !class_model.complete())
@@ -741,11 +744,11 @@ bool translation_unit_visitor::VisitCXXRecordDecl(clang::CXXRecordDecl *cls)
}
forward_declarations_.erase(id);
if (diagram_.should_include(class_model)) {
if (diagram().should_include(class_model)) {
LOG_DBG("Adding class {} with id {}", class_model.full_name(false),
class_model.id());
diagram_.add_class(std::move(c_ptr));
add_class(std::move(c_ptr));
}
else {
LOG_DBG("Skipping class {} with id {}", class_model.full_name(),
@@ -896,11 +899,11 @@ void translation_unit_visitor::process_record_parent(
}
}
if (id_opt && diagram_.get_class(*id_opt)) {
if (id_opt && diagram_.find<class_>(*id_opt)) {
// Here we have 2 options, either:
// - the parent is a regular C++ class/struct
// - the parent is a class template declaration/specialization
auto parent_class = diagram_.get_class(*id_opt);
auto parent_class = diagram_.find<class_>(*id_opt);
c.set_namespace(parent_ns);
const auto cls_name = cls->getNameAsString();
@@ -1305,7 +1308,7 @@ void translation_unit_visitor::process_method(
.getUnqualifiedType()
->getAs<clang::TemplateSpecializationType>();
templ != nullptr) {
auto *unaliased_type = templ;
const auto *unaliased_type = templ;
if (unaliased_type->isTypeAlias())
unaliased_type = unaliased_type->getAliasedType()
->getAs<clang::TemplateSpecializationType>();
@@ -1320,7 +1323,7 @@ void translation_unit_visitor::process_method(
relationships.emplace_back(template_specialization_ptr->id(),
relationship_t::kDependency);
diagram().add_class(std::move(template_specialization_ptr));
add_class(std::move(template_specialization_ptr));
}
}
}
@@ -1657,7 +1660,7 @@ void translation_unit_visitor::process_function_parameter(
relationships.emplace_back(template_specialization_ptr->id(),
relationship_t::kDependency);
diagram().add_class(std::move(template_specialization_ptr));
add_class(std::move(template_specialization_ptr));
}
}
@@ -1686,12 +1689,8 @@ void translation_unit_visitor::ensure_lambda_type_is_relative(
std::string &parameter_type) const
{
#ifdef _MSC_VER
auto root_name = fmt::format(
"{}\\", std::filesystem::current_path().root_name().string());
if (root_name.back() == '\\') {
root_name.pop_back();
root_name.push_back('/');
}
auto root_name =
fmt::format("{}", std::filesystem::current_path().root_name().string());
#else
auto root_name = std::string{"/"};
#endif
@@ -1700,17 +1699,19 @@ void translation_unit_visitor::ensure_lambda_type_is_relative(
while (parameter_type.find(lambda_prefix) != std::string::npos) {
auto lambda_begin = parameter_type.find(lambda_prefix);
auto lambda_prefix_size = lambda_prefix.size();
#ifdef _MSC_VER
// Skip the `\` or `/` after drive letter and semicolon
lambda_prefix_size++;
#endif
auto absolute_lambda_path_end =
parameter_type.find(':', lambda_begin + lambda_prefix.size());
auto absolute_lambda_path =
parameter_type.substr(lambda_begin + lambda_prefix.size() - 1,
absolute_lambda_path_end -
(lambda_begin + lambda_prefix.size() - 1));
parameter_type.find(':', lambda_begin + lambda_prefix_size);
auto absolute_lambda_path = parameter_type.substr(
lambda_begin + lambda_prefix_size - 1,
absolute_lambda_path_end - (lambda_begin + lambda_prefix_size - 1));
auto relative_lambda_path = util::path_to_url(std::filesystem::relative(
absolute_lambda_path, config().relative_to())
.string());
auto relative_lambda_path = util::path_to_url(
config().make_path_relative(absolute_lambda_path).string());
parameter_type = fmt::format("{}(lambda at {}{}",
parameter_type.substr(0, lambda_begin), relative_lambda_path,
@@ -1972,7 +1973,7 @@ void translation_unit_visitor::process_field(
// Add the template instantiation object to the diagram if it
// matches the include pattern
if (add_template_instantiation_to_diagram)
diagram().add_class(std::move(template_specialization_ptr));
add_class(std::move(template_specialization_ptr));
}
}
@@ -2004,7 +2005,7 @@ void translation_unit_visitor::add_incomplete_forward_declarations()
{
for (auto &[id, c] : forward_declarations_) {
if (diagram().should_include(c->full_name(false))) {
diagram().add_class(std::move(c));
add_class(std::move(c));
}
}
forward_declarations_.clear();
@@ -2092,4 +2093,61 @@ bool translation_unit_visitor::has_processed_template_class(
return util::contains(processed_template_qualified_names_, qualified_name);
}
void translation_unit_visitor::add_class(std::unique_ptr<class_> &&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.string(), common::model::path_type::kFilesystem};
p.pop_back();
diagram().add(p, std::move(c));
}
else {
diagram().add(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.string(), common::model::path_type::kFilesystem};
p.pop_back();
diagram().add(p, std::move(e));
}
else {
diagram().add(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.string(), common::model::path_type::kFilesystem};
p.pop_back();
diagram().add(p, std::move(c));
}
else {
diagram().add(c->path(), std::move(c));
}
}
} // namespace clanguml::class_diagram::visitor

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_method;
using clanguml::class_diagram::model::class_parent;
using clanguml::class_diagram::model::concept_;
using clanguml::class_diagram::model::diagram;
using clanguml::class_diagram::model::enum_;
using clanguml::class_diagram::model::method_parameter;
@@ -118,6 +119,14 @@ public:
*/
void finalize();
common::visitor::ast_id_mapper &id_mapper() const { return id_mapper_; }
void add_class(std::unique_ptr<class_> &&c);
void add_enum(std::unique_ptr<enum_> &&e);
void add_concept(std::unique_ptr<concept_> &&c);
void ensure_lambda_type_is_relative(std::string &parameter_type) const;
private:
bool should_include(const clang::NamedDecl *decl);
@@ -182,8 +191,6 @@ private:
const found_relationships_t &relationships,
bool break_on_first_aggregation = false);
void ensure_lambda_type_is_relative(std::string &parameter_type) const;
void process_record_parent(
clang::RecordDecl *cls, class_ &c, const namespace_ &ns);
@@ -218,8 +225,6 @@ private:
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_; }
// Reference to the output diagram model

View File

@@ -754,4 +754,30 @@ std::vector<std::string> tokenize_unexposed_template_parameter(
return result;
}
bool parse_source_location(const std::string &location_str, std::string &file,
unsigned &line, unsigned &column)
{
auto tokens = util::split(location_str, ":");
if (tokens.size() < 3)
return false;
file = tokens.at(0);
try {
line = std::stoi(tokens.at(1));
}
catch (std::invalid_argument &e) {
line = 0;
}
try {
column = std::stoi(tokens.at(2));
}
catch (std::invalid_argument &e) {
column = 0;
}
return true;
}
} // namespace clanguml::common

View File

@@ -172,6 +172,9 @@ void if_dyn_cast(P pointer, F &&func)
}
}
bool parse_source_location(const std::string &location_str, std::string &file,
unsigned &line, unsigned &column);
bool is_type_parameter(const std::string &t);
bool is_qualifier(const std::string &q);

View File

@@ -39,7 +39,7 @@ public:
virtual diagram_t type() const = 0;
virtual common::optional_ref<clanguml::common::model::diagram_element> get(
virtual opt_ref<clanguml::common::model::diagram_element> get(
const std::string &full_name) const = 0;
virtual common::optional_ref<clanguml::common::model::diagram_element> get(

View File

@@ -61,21 +61,21 @@ template <>
const clanguml::common::optional_ref<class_diagram::model::class_> get(
const class_diagram::model::diagram &d, const std::string &full_name)
{
return d.get_class(full_name);
return d.find<class_diagram::model::class_>(full_name);
}
template <>
const clanguml::common::optional_ref<common::model::package> get(
const package_diagram::model::diagram &d, const std::string &full_name)
{
return d.get_package(full_name);
return d.find<package>(full_name);
}
template <>
const clanguml::common::optional_ref<common::model::source_file> get(
const include_diagram::model::diagram &d, const std::string &full_name)
{
return d.get_file(full_name);
return d.find<source_file>(full_name);
}
template <>
@@ -259,7 +259,7 @@ tvl::value_t subclass_filter::match(const diagram &d, const element &e) const
clanguml::common::reference_set<class_diagram::model::class_> parents;
const auto &fn = e.full_name(false);
auto class_ref = cd.get_class(fn);
auto class_ref = cd.find<class_diagram::model::class_>(fn);
if (!class_ref.has_value())
return false;
@@ -308,7 +308,7 @@ tvl::value_t parents_filter::match(const diagram &d, const element &e) const
clanguml::common::reference_set<class_diagram::model::class_> parents;
for (const auto &child : children_) {
auto child_ref = cd.get_class(child);
auto child_ref = cd.find<class_diagram::model::class_>(child);
if (!child_ref.has_value())
continue;
parents.emplace(child_ref.value());
@@ -370,8 +370,8 @@ tvl::value_t context_filter::match(const diagram &d, const element &e) const
return tvl::any_of(context_.begin(), context_.end(),
[&e, &d](const auto &context_root_name) {
const auto &context_root =
static_cast<const class_diagram::model::diagram &>(d).get_class(
context_root_name);
static_cast<const class_diagram::model::diagram &>(d)
.find<class_diagram::model::class_>(context_root_name);
if (context_root.has_value()) {
// This is a direct match to the context root
@@ -430,8 +430,8 @@ paths_filter::paths_filter(filter_t type, const std::filesystem::path &root,
absolute_path = path;
try {
absolute_path =
std::filesystem::canonical(absolute_path.lexically_normal());
absolute_path = absolute(absolute_path);
absolute_path = canonical(absolute_path.lexically_normal());
}
catch (std::filesystem::filesystem_error &e) {
LOG_WARN("Cannot add non-existent path {} to paths filter",
@@ -539,7 +539,7 @@ void diagram_filter::init_filters(const config::diagram &c)
filter_t::kInclusive, c.include().access));
add_inclusive_filter(std::make_unique<paths_filter>(
filter_t::kInclusive, c.relative_to(), c.include().paths));
filter_t::kInclusive, c.root_directory(), c.include().paths));
// Include any of these matches even if one them does not match
std::vector<std::unique_ptr<filter_visitor>> element_filters;
@@ -623,7 +623,7 @@ void diagram_filter::init_filters(const config::diagram &c)
filter_t::kExclusive, c.exclude().namespaces));
add_exclusive_filter(std::make_unique<paths_filter>(
filter_t::kExclusive, c.relative_to(), c.exclude().paths));
filter_t::kExclusive, c.root_directory(), c.exclude().paths));
add_exclusive_filter(std::make_unique<element_filter>(
filter_t::kExclusive, c.exclude().elements));

View File

@@ -0,0 +1,70 @@
/**
* src/common/model/element_view.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/types.h"
namespace clanguml::common::model {
/**
* Provides type based views over elements in a diagram.
*
* @tparam T Type of diagram element
*/
template <typename T> class element_view {
public:
/**
* @brief Add reference to diagram element
*
* @param element Reference to diagram element of specific type
*/
void add(std::reference_wrapper<T> element)
{
elements_.emplace_back(std::move(element));
}
/**
* @brief Get collection of reference to diagram elements
*
* @return
*/
const reference_vector<T> &view() const { return elements_; }
/**
* @brief Get typed diagram element by id
* @param id Global id of a diagram element
*
* @return
*/
common::optional_ref<T> get(
clanguml::common::model::diagram_element::id_t id) const
{
for (const auto &e : elements_) {
if (e.get().id() == id) {
return {e};
}
}
return {};
}
private:
reference_vector<T> elements_;
};
} // namespace clanguml::common::model

View File

@@ -29,7 +29,7 @@ struct ns_path_separator {
static constexpr std::string_view value = "::";
};
using namespace_ = path<ns_path_separator>;
using namespace_ = path;
}

View File

@@ -70,7 +70,9 @@ public:
if (parent && dynamic_cast<nested_trait<T, Path> *>(&parent.value()))
return dynamic_cast<nested_trait<T, Path> &>(parent.value())
.template add_element<V>(std::move(p));
spdlog::info("No parent element found at: {}", path.to_string());
LOG_INFO("No parent element found at: {}", path.to_string());
throw std::runtime_error(
"No parent element found for " + path.to_string());
}
@@ -135,7 +137,29 @@ public:
elements_.end();
}
bool is_empty() const { return elements_.empty(); }
template <typename F> bool all_of(F &&f) const
{
return std::all_of(
elements_.cbegin(), elements_.cend(), [f](const auto &e) {
const auto *package_ptr =
dynamic_cast<nested_trait<T, Path> *>(e.get());
if (package_ptr != nullptr)
return package_ptr->all_of(f);
return f(*e);
});
}
bool is_empty() const
{
return elements_.empty() ||
std::all_of(elements_.cbegin(), elements_.cend(), [](auto &e) {
const auto *package_ptr =
dynamic_cast<nested_trait<T, Path> *>(e.get());
return package_ptr != nullptr && package_ptr->is_empty();
});
}
auto begin() { return elements_.begin(); }
auto end() { return elements_.end(); }

View File

@@ -19,6 +19,7 @@
#include "common/model/element.h"
#include "common/model/nested_trait.h"
#include "common/model/path.h"
#include "common/model/stylable_element.h"
#include "common/types.h"
#include "util/util.h"
@@ -33,9 +34,9 @@ namespace clanguml::common::model {
class package : public element,
public stylable_element,
public nested_trait<element, namespace_> {
public nested_trait<element, path> {
public:
package(const common::model::namespace_ &using_namespace);
package(const common::model::path &using_namespace);
package(const package &) = delete;
package(package &&) = default;

View File

@@ -25,41 +25,83 @@
namespace clanguml::common::model {
template <typename Sep> class path {
enum class path_type { kNamespace, kFilesystem };
class path {
const char *separator() const
{
switch (path_type_) {
case path_type::kNamespace:
return "::";
case path_type::kFilesystem:
#ifdef _WIN32
return "\\";
#else
return "/";
#endif
}
return "::";
}
public:
using container_type = std::vector<std::string>;
path() = default;
path(path_type pt = path_type::kNamespace)
: path_type_{pt}
{
}
explicit path(const std::string &ns)
path(const std::string &ns, path_type pt = path_type::kNamespace)
: path_type_{pt}
{
if (ns.empty())
return;
path_ = util::split(ns, Sep::value);
path_ = util::split(ns, separator());
}
virtual ~path() = default;
path(container_type::const_iterator begin,
container_type::const_iterator end)
container_type::const_iterator end,
path_type pt = path_type::kNamespace)
: path(pt)
{
if (begin == end)
return;
std::copy(begin, end, std::back_inserter(path_));
}
path(const path &right)
: path_{right.path_}
{
}
path(const path &right) = default;
path &operator=(const path &right) = default;
path &operator=(const path &right)
{
if (&right == this)
return *this;
if (path_type_ != right.path_type_)
throw std::runtime_error("");
path_type_ = right.path_type_;
path_ = right.path_;
return *this;
}
path(path &&right) noexcept = default;
path &operator=(path &&right) noexcept = default;
path(std::initializer_list<std::string> ns)
path(std::initializer_list<std::string> ns,
path_type pt = path_type::kNamespace)
: path(pt)
{
if ((ns.size() == 1) && util::contains(*ns.begin(), Sep::value)) {
path_ = util::split(*ns.begin(), Sep::value);
if ((ns.size() == 1) &&
util::contains(*ns.begin(), std::string{separator()})) {
path_ = util::split(*ns.begin(), separator());
}
else if ((ns.size() == 1) && ns.begin()->empty()) {
}
@@ -67,10 +109,13 @@ public:
path_ = ns;
}
explicit path(const std::vector<std::string> &ns)
explicit path(const std::vector<std::string> &ns,
path_type pt = path_type::kNamespace)
: path(pt)
{
if ((ns.size() == 1) && util::contains(*ns.begin(), Sep::value)) {
path_ = util::split(*ns.begin(), Sep::value);
if ((ns.size() == 1) &&
util::contains(*ns.begin(), std::string{separator()})) {
path_ = util::split(*ns.begin(), separator());
}
else if ((ns.size() == 1) && ns.begin()->empty()) {
}
@@ -78,19 +123,19 @@ public:
path_ = ns;
}
friend bool operator==(const path<Sep> &left, const path<Sep> &right)
friend bool operator==(const path &left, const path &right)
{
return left.path_ == right.path_;
}
friend bool operator<(const path<Sep> &left, const path<Sep> &right)
friend bool operator<(const path &left, const path &right)
{
return std::hash<path<Sep>>{}(left) < std::hash<path<Sep>>{}(right);
return left.to_string() < right.to_string();
}
std::string to_string() const
{
return fmt::format("{}", fmt::join(path_, Sep::value));
return fmt::format("{}", fmt::join(path_, std::string{separator()}));
}
bool is_empty() const { return path_.empty(); }
@@ -115,9 +160,9 @@ public:
void operator|=(const std::string &right) { append(right); }
std::string &operator[](const int index) { return path_[index]; }
std::string &operator[](const unsigned int index) { return path_[index]; }
const std::string &operator[](const int index) const
const std::string &operator[](const unsigned int index) const
{
return path_[index];
}
@@ -190,7 +235,7 @@ public:
return name;
auto res = name;
auto ns_prefix = to_string() + std::string{Sep::value};
auto ns_prefix = to_string() + std::string{separator()};
auto it = res.find(ns_prefix);
while (it != std::string::npos) {
@@ -220,7 +265,10 @@ public:
path::container_type::const_iterator begin() const { return path_.begin(); }
path::container_type::const_iterator end() const { return path_.end(); }
path_type type() const { return path_type_; }
private:
path_type path_type_;
container_type path_;
};

View File

@@ -46,7 +46,7 @@ struct fs_path_sep {
#endif
};
using filesystem_path = common::model::path<fs_path_sep>;
using filesystem_path = common::model::path;
class source_file
: public common::model::diagram_element,
@@ -60,7 +60,7 @@ public:
{
auto preferred = p;
preferred.make_preferred();
set_path({preferred.parent_path().string()});
set_path({preferred.parent_path().string(), path_type::kFilesystem});
set_name(preferred.filename().string());
is_absolute_ = preferred.is_absolute();
set_id(common::to_id(preferred));
@@ -126,7 +126,7 @@ public:
}
private:
filesystem_path path_;
filesystem_path path_{path_type::kFilesystem};
source_file_t type_{source_file_t::kDirectory};
bool is_absolute_{false};
};
@@ -134,21 +134,24 @@ private:
namespace std {
template <> struct hash<clanguml::common::model::filesystem_path> {
std::size_t operator()(
const clanguml::common::model::filesystem_path &key) const
{
using clanguml::common::model::path;
/*
template <> struct hash<clanguml::common::model::filesystem_path> {
std::size_t operator()(
const clanguml::common::model::filesystem_path &key) const
{
using clanguml::common::model::path;
std::size_t seed = key.size();
for (const auto &ns : key) {
seed ^=
std::hash<std::string>{}(ns) + clanguml::util::hash_seed(seed);
}
std::size_t seed = key.size();
for (const auto &ns : key) {
seed ^=
std::hash<std::string>{}(ns) +
clanguml::util::hash_seed(seed);
}
return seed;
}
};
return seed;
}
};
*/
} // namespace std

View File

@@ -129,6 +129,8 @@ private:
T *value_{nullptr};
};
template <typename T> using opt_ref = optional_ref<T>;
template <typename T>
using reference_vector = std::vector<std::reference_wrapper<T>>;

View File

@@ -85,14 +85,36 @@ void translation_unit_visitor::set_source_location(
const clang::SourceLocation &location,
clanguml::common::model::source_location &element)
{
std::string file;
unsigned line{};
[[maybe_unused]] unsigned column{};
if (location.isValid()) {
element.set_file(source_manager_.getFilename(location).str());
element.set_file_relative(util::path_to_url(
std::filesystem::relative(element.file(), relative_to_path_)
.string()));
element.set_line(source_manager_.getSpellingLineNumber(location));
element.set_location_id(location.getHashValue());
file = source_manager_.getFilename(location).str();
line = source_manager_.getSpellingLineNumber(location);
column = source_manager_.getSpellingColumnNumber(location);
if (file.empty()) {
// Why do I have to do this?
parse_source_location(
location.printToString(source_manager()), file, line, column);
}
}
else {
auto success = parse_source_location(
location.printToString(source_manager()), file, line, column);
if (!success) {
LOG_DBG("Failed to extract source location for element from {}",
location.printToString(source_manager_));
return;
}
}
element.set_file(file);
element.set_file_relative(util::path_to_url(
std::filesystem::relative(element.file(), relative_to_path_).string()));
element.set_line(line);
element.set_location_id(location.getHashValue());
}
} // namespace clanguml::common::visitor

View File

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

View File

@@ -107,6 +107,8 @@ void inheritable_diagram_options::inherit(
exclude.override(parent.exclude);
puml.override(parent.puml);
generate_method_arguments.override(parent.generate_method_arguments);
generate_packages.override(parent.generate_packages);
package_type.override(parent.package_type);
generate_links.override(parent.generate_links);
generate_system_headers.override(parent.generate_system_headers);
git.override(parent.git);
@@ -136,14 +138,20 @@ std::vector<std::string> diagram::get_translation_units() const
{
std::vector<std::string> translation_units{};
LOG_DBG("Looking for translation units in {}",
std::filesystem::current_path().string());
for (const auto &g : glob()) {
std::string glob_path =
fmt::format("{}/{}", relative_to().string(), g.c_str());
fmt::format("{}/{}", root_directory().string(), g.c_str());
LOG_DBG("Searching glob path {}", glob_path);
auto matches = glob::glob(glob_path, true, false);
for (const auto &match : matches) {
const auto path = std::filesystem::canonical(relative_to() / match);
const auto path =
std::filesystem::canonical(root_directory() / match);
translation_units.emplace_back(path.string());
}
}
@@ -151,6 +159,17 @@ std::vector<std::string> diagram::get_translation_units() const
return translation_units;
}
std::filesystem::path diagram::root_directory() const
{
return canonical(absolute(base_directory() / relative_to()));
}
std::filesystem::path diagram::make_path_relative(
const std::filesystem::path &p) const
{
return relative(p, root_directory()).lexically_normal().string();
}
std::optional<std::string> diagram::get_together_group(
const std::string &full_name) const
{

View File

@@ -37,6 +37,8 @@ namespace config {
enum class method_arguments { full, abbreviated, none };
enum class package_type_t { kNamespace, kDirectory };
std::string to_string(method_arguments ma);
enum class comment_parser_t { plain, clang };
@@ -162,10 +164,17 @@ struct inheritable_diagram_options {
option<method_arguments> generate_method_arguments{
"generate_method_arguments", method_arguments::full};
option<bool> generate_packages{"generate_packages", false};
option<package_type_t> package_type{
"package_type", package_type_t::kNamespace};
option<generate_links_config> generate_links{"generate_links"};
option<git_config> git{"git"};
option<layout_hints> layout{"layout"};
// This is the absolute filesystem path to the directory containing
// the current .clang-uml config file - it is set automatically
option<std::filesystem::path> base_directory{"__parent_path"};
// This is the relative path with respect to the `base_directory`,
// against which all matches are made, if not provided it defaults to the
// `base_directory`
option<std::filesystem::path> relative_to{"relative_to"};
option<bool> generate_system_headers{"generate_system_headers", false};
option<relationship_hints_t> relationship_hints{"relationship_hints"};
@@ -190,6 +199,11 @@ struct diagram : public inheritable_diagram_options {
std::vector<std::string> get_translation_units() const;
std::filesystem::path make_path_relative(
const std::filesystem::path &p) const;
std::filesystem::path root_directory() const;
std::optional<std::string> get_together_group(
const std::string &full_name) const;

View File

@@ -34,6 +34,7 @@ using clanguml::config::layout_hint;
using clanguml::config::location_t;
using clanguml::config::method_arguments;
using clanguml::config::package_diagram;
using clanguml::config::package_type_t;
using clanguml::config::plantuml;
using clanguml::config::relationship_hint_t;
using clanguml::config::sequence_diagram;
@@ -88,6 +89,22 @@ void get_option<method_arguments>(
}
}
template <>
void get_option<package_type_t>(
const Node &node, clanguml::config::option<package_type_t> &option)
{
if (node[option.name]) {
const auto &val = node[option.name].as<std::string>();
if (val == "namespace")
option.set(package_type_t::kNamespace);
else if (val == "directory")
option.set(package_type_t::kDirectory);
else
throw std::runtime_error(
"Invalid generate_method_arguments value: " + val);
}
}
template <>
void get_option<clanguml::config::comment_parser_t>(const Node &node,
clanguml::config::option<clanguml::config::comment_parser_t> &option)
@@ -403,6 +420,7 @@ template <> struct convert<class_diagram> {
get_option(node, rhs.include_relations_also_as_members);
get_option(node, rhs.generate_method_arguments);
get_option(node, rhs.generate_packages);
get_option(node, rhs.package_type);
get_option(node, rhs.relationship_hints);
get_option(node, rhs.type_aliases);
get_option(node, rhs.relative_to);
@@ -451,6 +469,13 @@ template <> struct convert<package_diagram> {
return false;
get_option(node, rhs.layout);
get_option(node, rhs.relative_to);
get_option(node, rhs.package_type);
// Ensure relative_to has a value
if (!rhs.relative_to.has_value)
rhs.relative_to.set(
std::filesystem::current_path().lexically_normal());
return true;
}
@@ -597,6 +622,7 @@ template <> struct convert<config> {
get_option(node, rhs.puml);
get_option(node, rhs.generate_method_arguments);
get_option(node, rhs.generate_packages);
get_option(node, rhs.package_type);
get_option(node, rhs.generate_links);
get_option(node, rhs.generate_system_headers);
get_option(node, rhs.git);

View File

@@ -31,19 +31,19 @@ common::model::diagram_t diagram::type() const
common::optional_ref<common::model::diagram_element> diagram::get(
const std::string &full_name) const
{
return get_file(full_name);
return find<source_file>(full_name);
}
common::optional_ref<common::model::diagram_element> diagram::get(
const common::model::diagram_element::id_t id) const
{
return get_file(id);
return find<source_file>(id);
}
void diagram::add_file(std::unique_ptr<common::model::source_file> &&f)
{
// Don't add the same file more than once
if (get_file(f->id()))
if (find<source_file>(f->id()))
return;
LOG_DBG("Adding source file: {}, {}", f->name(), f->fs_path().string());
@@ -53,14 +53,15 @@ void diagram::add_file(std::unique_ptr<common::model::source_file> &&f)
assert(!ff.name().empty());
assert(ff.id() != 0);
files_.emplace_back(ff);
element_view<source_file>::add(ff);
auto p = ff.path();
if (!f->path().is_empty()) {
// If the parent path is not empty, ensure relative parent directories
// of this source_file are in the diagram
common::model::filesystem_path parent_path_so_far;
common::model::filesystem_path parent_path_so_far{
common::model::path_type::kFilesystem};
for (const auto &directory : f->path()) {
auto source_file_path = parent_path_so_far | directory;
if (parent_path_so_far.is_empty())
@@ -83,37 +84,11 @@ void diagram::add_file(std::unique_ptr<common::model::source_file> &&f)
}
}
assert(p.type() == common::model::path_type::kFilesystem);
add_element(p, std::move(f));
}
common::optional_ref<common::model::source_file> diagram::get_file(
const std::string &name) const
{
// Convert the name to the OS preferred path
std::filesystem::path namePath{name};
namePath.make_preferred();
for (const auto &p : files_) {
if (p.get().full_name(false) == namePath.string()) {
return {p};
}
}
return {};
}
common::optional_ref<common::model::source_file> diagram::get_file(
const common::model::diagram_element::id_t id) const
{
for (const auto &p : files_) {
if (p.get().id() == id) {
return {p};
}
}
return {};
}
std::string diagram::to_alias(const std::string &full_name) const
{
LOG_DBG("Looking for alias for {}", full_name);
@@ -136,7 +111,7 @@ std::string diagram::to_alias(const std::string &full_name) const
const common::reference_vector<common::model::source_file> &
diagram::files() const
{
return files_;
return element_view<source_file>::view();
}
common::optional_ref<clanguml::common::model::diagram_element>

View File

@@ -18,6 +18,7 @@
#pragma once
#include "common/model/diagram.h"
#include "common/model/element_view.h"
#include "common/model/package.h"
#include "common/model/source_file.h"
#include "common/types.h"
@@ -27,9 +28,13 @@
namespace clanguml::include_diagram::model {
using clanguml::common::opt_ref;
using clanguml::common::model::diagram_element;
using clanguml::common::model::source_file;
class diagram : public clanguml::common::model::diagram,
public clanguml::common::model::nested_trait<
clanguml::common::model::source_file,
public clanguml::common::model::element_view<source_file>,
public clanguml::common::model::nested_trait<source_file,
clanguml::common::model::filesystem_path> {
public:
diagram() = default;
@@ -41,33 +46,58 @@ public:
common::model::diagram_t type() const override;
common::optional_ref<common::model::diagram_element> get(
const std::string &full_name) const override;
opt_ref<diagram_element> get(const std::string &full_name) const override;
common::optional_ref<common::model::diagram_element> get(
common::model::diagram_element::id_t id) const override;
opt_ref<diagram_element> get(diagram_element::id_t id) const override;
void add_file(std::unique_ptr<common::model::source_file> &&f);
common::optional_ref<common::model::source_file> get_file(
const std::string &name) const;
template <typename ElementT>
opt_ref<ElementT> find(const std::string &name) const;
common::optional_ref<common::model::source_file> get_file(
common::model::diagram_element::id_t id) const;
template <typename ElementT>
opt_ref<ElementT> find(diagram_element::id_t id) const;
std::string to_alias(const std::string &full_name) const;
const common::reference_vector<common::model::source_file> &files() const;
const common::reference_vector<source_file> &files() const;
common::optional_ref<clanguml::common::model::diagram_element>
get_with_namespace(const std::string &name,
opt_ref<diagram_element> get_with_namespace(const std::string &name,
const common::model::namespace_ &ns) const override;
inja::json context() const override;
private:
common::reference_vector<common::model::source_file> files_;
};
template <typename ElementT>
opt_ref<ElementT> diagram::find(const std::string &name) const
{
// Convert the name to the OS preferred path
std::filesystem::path namePath{name};
namePath.make_preferred();
for (const auto &element : element_view<ElementT>::view()) {
const auto full_name = element.get().full_name(false);
if (full_name == namePath.string()) {
return {element};
}
}
return {};
}
template <typename ElementT>
opt_ref<ElementT> diagram::find(diagram_element::id_t id) const
{
for (const auto &element : element_view<ElementT>::view()) {
if (element.get().id() == id) {
return {element};
}
}
return {};
}
} // namespace clanguml::include_diagram::model
namespace clanguml::common::model {

View File

@@ -19,7 +19,6 @@
#include "cli/cli_handler.h"
#include "common/compilation_database.h"
#include "common/generators/generators.h"
#include "include_diagram/generators/plantuml/include_diagram_generator.h"
#include "util/query_driver_output_extractor.h"
#include "util/util.h"
@@ -33,8 +32,6 @@
#include <spdlog/spdlog.h>
#include <cstring>
#include <iostream>
#include <util/thread_pool_executor.h>
#ifdef ENABLE_BACKWARD_CPP
namespace backward {
@@ -55,6 +52,12 @@ int main(int argc, const char *argv[])
if (res == cli::cli_flow_t::kError)
return 1;
#if !defined(NDEBUG)
// Catch invalid logger message formats, e.g. missing arguments
spdlog::set_error_handler(
[](const std::string & /*msg*/) { assert(0 == 1); });
#endif
try {
const auto db =
clanguml::common::compilation_database::auto_detect_from_directory(

View File

@@ -31,55 +31,21 @@ common::model::diagram_t diagram::type() const
const common::reference_vector<clanguml::common::model::package> &
diagram::packages() const
{
return packages_;
}
void diagram::add_package(std::unique_ptr<common::model::package> &&p)
{
LOG_DBG("Adding package: {}, {}", p->name(), p->full_name(true));
auto ns = p->get_relative_namespace();
packages_.emplace_back(*p);
add_element(ns, std::move(p));
}
common::optional_ref<common::model::package> diagram::get_package(
const std::string &name) const
{
for (const auto &p : packages_) {
auto p_full_name = p.get().full_name(false);
if (p_full_name == name) {
return {p};
}
}
return {};
}
common::optional_ref<common::model::package> diagram::get_package(
const clanguml::common::model::diagram_element::id_t id) const
{
for (const auto &p : packages_) {
if (p.get().id() == id) {
return {p};
}
}
return {};
return element_view<package>::view();
}
common::optional_ref<clanguml::common::model::diagram_element> diagram::get(
const std::string &full_name) const
{
return get_package(full_name);
return find<package>(full_name);
}
common::optional_ref<clanguml::common::model::diagram_element> diagram::get(
const clanguml::common::model::diagram_element::id_t id) const
{
return get_package(id);
LOG_DBG("Looking for package with id {}", id);
return find<package>(id);
}
std::string diagram::to_alias(
@@ -87,10 +53,9 @@ std::string diagram::to_alias(
{
LOG_DBG("Looking for alias for {}", id);
for (const auto &p : packages_) {
if (p.get().id() == id)
return p.get().alias();
}
auto p = find<package>(id);
if (p.has_value() && p.value().id() == id)
return p.value().alias();
return {};
}

View File

@@ -18,6 +18,7 @@
#pragma once
#include "common/model/diagram.h"
#include "common/model/element_view.h"
#include "common/model/package.h"
#include <string>
@@ -25,7 +26,13 @@
namespace clanguml::package_diagram::model {
using clanguml::common::opt_ref;
using clanguml::common::model::diagram_element;
using clanguml::common::model::package;
using clanguml::common::model::path;
class diagram : public clanguml::common::model::diagram,
public clanguml::common::model::element_view<package>,
public clanguml::common::model::nested_trait<
clanguml::common::model::element,
clanguml::common::model::namespace_> {
@@ -39,31 +46,109 @@ public:
common::model::diagram_t type() const override;
const common::reference_vector<clanguml::common::model::package> &
packages() const;
const common::reference_vector<package> &packages() const;
common::optional_ref<clanguml::common::model::diagram_element> get(
const std::string &full_name) const override;
opt_ref<diagram_element> get(const std::string &full_name) const override;
common::optional_ref<clanguml::common::model::diagram_element> get(
clanguml::common::model::diagram_element::id_t id) const override;
opt_ref<diagram_element> get(diagram_element::id_t id) const override;
void add_package(std::unique_ptr<common::model::package> &&p);
template <typename ElementT>
opt_ref<ElementT> find(const std::string &name) const;
common::optional_ref<clanguml::common::model::package> get_package(
const std::string &name) const;
template <typename ElementT>
opt_ref<ElementT> find(diagram_element::id_t id) const;
common::optional_ref<common::model::package> get_package(
clanguml::common::model::diagram_element::id_t id) const;
template <typename ElementT>
bool add(const path &parent_path, std::unique_ptr<ElementT> &&e)
{
if (parent_path.type() == common::model::path_type::kNamespace) {
return add_with_namespace_path(std::move(e));
}
std::string to_alias(
clanguml::common::model::diagram_element::id_t /*id*/) const;
return add_with_filesystem_path(parent_path, std::move(e));
}
template <typename ElementT>
bool add_with_namespace_path(std::unique_ptr<ElementT> &&e);
template <typename ElementT>
bool add_with_filesystem_path(
const common::model::path &parent_path, std::unique_ptr<ElementT> &&e);
std::string to_alias(diagram_element::id_t /*id*/) const;
inja::json context() const override;
private:
common::reference_vector<clanguml::common::model::package> packages_;
};
template <typename ElementT>
opt_ref<ElementT> diagram::find(const std::string &name) const
{
for (const auto &element : element_view<ElementT>::view()) {
const auto full_name = element.get().full_name(false);
if (full_name == name) {
return {element};
}
}
return {};
}
template <typename ElementT>
opt_ref<ElementT> diagram::find(diagram_element::id_t id) const
{
for (const auto &element : element_view<ElementT>::view()) {
if (element.get().id() == id) {
return {element};
}
}
return {};
}
template <typename ElementT>
bool diagram::add_with_namespace_path(std::unique_ptr<ElementT> &&p)
{
LOG_DBG(
"Adding package: {}, {}, [{}]", p->name(), p->full_name(true), p->id());
auto ns = p->get_relative_namespace();
auto p_ref = std::ref(*p);
auto res = add_element(ns, std::move(p));
if (res)
element_view<ElementT>::add(p_ref);
return res;
}
template <typename ElementT>
bool diagram::add_with_filesystem_path(
const common::model::path &parent_path, std::unique_ptr<ElementT> &&p)
{
LOG_DBG("Adding package: {}, {}", p->name(), p->full_name(true));
// 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>(p->using_namespace());
pkg->set_name(*it);
auto ns = common::model::path(parent_path.begin(), it);
pkg->set_namespace(ns);
pkg->set_id(common::to_id(pkg->full_name(false)));
add_with_filesystem_path(ns, std::move(pkg));
}
auto pp = std::ref(*p);
auto res = add_element(parent_path, std::move(p));
if (res)
element_view<ElementT>::add(pp);
return res;
}
} // namespace clanguml::package_diagram::model
namespace clanguml::common::model {

View File

@@ -45,6 +45,9 @@ bool translation_unit_visitor::VisitNamespaceDecl(clang::NamespaceDecl *ns)
{
assert(ns != nullptr);
if (config().package_type() == config::package_type_t::kDirectory)
return true;
if (ns->isAnonymousNamespace() || ns->isInline())
return true;
@@ -90,7 +93,7 @@ bool translation_unit_visitor::VisitNamespaceDecl(clang::NamespaceDecl *ns)
}
if (!p->skip()) {
diagram().add_package(std::move(p));
diagram().add(p->path(), std::move(p));
}
}
@@ -144,24 +147,92 @@ bool translation_unit_visitor::VisitCXXRecordDecl(clang::CXXRecordDecl *cls)
return true;
}
void translation_unit_visitor::add_relationships(
clang::DeclContext *cls, found_relationships_t &relationships)
{
int64_t current_package_id{0};
const auto *namespace_context = cls->getEnclosingNamespaceContext();
if (namespace_context != nullptr && namespace_context->isNamespace()) {
current_package_id =
common::to_id(*llvm::cast<clang::NamespaceDecl>(namespace_context));
bool translation_unit_visitor::VisitRecordDecl(clang::RecordDecl *decl)
{
assert(decl != nullptr);
// Skip system headers
if (source_manager().isInSystemHeader(decl->getSourceRange().getBegin()))
return true;
found_relationships_t relationships;
if (decl->isCompleteDefinition()) {
process_record_children(*decl, relationships);
add_relationships(decl, relationships);
}
return true;
}
bool translation_unit_visitor::VisitEnumDecl(clang::EnumDecl *decl)
{
assert(decl != nullptr);
// Skip system headers
if (source_manager().isInSystemHeader(decl->getSourceRange().getBegin()))
return true;
found_relationships_t relationships;
if (decl->isCompleteDefinition()) {
add_relationships(decl, relationships);
}
return true;
}
bool translation_unit_visitor::VisitClassTemplateDecl(
clang::ClassTemplateDecl *decl)
{
assert(decl != nullptr);
// Skip system headers
if (source_manager().isInSystemHeader(decl->getSourceRange().getBegin()))
return true;
found_relationships_t relationships;
process_class_declaration(*decl->getTemplatedDecl(), relationships);
add_relationships(decl, relationships);
return true;
}
void translation_unit_visitor::add_relationships(
clang::Decl *cls, found_relationships_t &relationships)
{
// If this diagram has directory packages, first make sure that the
// package for current directory is already in the model
if (config().package_type() == config::package_type_t::kDirectory) {
auto file = source_manager().getFilename(cls->getLocation()).str();
auto relative_file = config().make_path_relative(file);
relative_file.make_preferred();
common::model::path parent_path{
relative_file.string(), common::model::path_type::kFilesystem};
parent_path.pop_back();
auto pkg_name = parent_path.name();
parent_path.pop_back();
auto pkg = std::make_unique<common::model::package>(
config().using_namespace());
pkg->set_name(pkg_name);
pkg->set_id(get_package_id(cls));
diagram().add(parent_path, std::move(pkg));
}
auto current_package_id = get_package_id(cls);
if (current_package_id == 0)
// These are relationships to a global namespace, and we don't care
// about those
return;
assert(current_package_id != 0);
auto current_package = diagram().get(current_package_id);
if (current_package) {
@@ -175,6 +246,31 @@ void translation_unit_visitor::add_relationships(
}
}
common::model::diagram_element::id_t translation_unit_visitor::get_package_id(
const clang::Decl *cls)
{
if (config().package_type() == config::package_type_t::kNamespace) {
const auto *namespace_context =
cls->getDeclContext()->getEnclosingNamespaceContext();
if (namespace_context != nullptr && namespace_context->isNamespace()) {
return common::to_id(
*llvm::cast<clang::NamespaceDecl>(namespace_context));
}
return {};
}
auto file =
source_manager().getFilename(cls->getSourceRange().getBegin()).str();
auto relative_file = config().make_path_relative(file);
relative_file.make_preferred();
common::model::path parent_path{
relative_file.string(), common::model::path_type::kFilesystem};
parent_path.pop_back();
return common::to_id(parent_path.to_string());
}
void translation_unit_visitor::process_class_declaration(
const clang::CXXRecordDecl &cls, found_relationships_t &relationships)
{
@@ -255,6 +351,44 @@ void translation_unit_visitor::process_method(
}
}
void translation_unit_visitor::process_record_children(
const clang::RecordDecl &cls, found_relationships_t &relationships)
{
if (const auto *decl_context =
clang::dyn_cast_or_null<clang::DeclContext>(&cls);
decl_context != nullptr) {
// Iterate over class template methods
for (auto const *decl_iterator : decl_context->decls()) {
auto const *method_template =
llvm::dyn_cast_or_null<clang::FunctionTemplateDecl>(
decl_iterator);
if (method_template == nullptr)
continue;
process_template_method(*method_template, relationships);
}
}
// Iterate over regular class fields
for (const auto *field : cls.fields()) {
if (field != nullptr)
process_field(*field, relationships);
}
// Static fields have to be processed by iterating over variable
// declarations
for (const auto *decl : cls.decls()) {
if (decl->getKind() == clang::Decl::Var) {
const clang::VarDecl *variable_declaration{
clang::dyn_cast_or_null<clang::VarDecl>(decl)};
if ((variable_declaration != nullptr) &&
variable_declaration->isStaticDataMember()) {
process_static_field(*variable_declaration, relationships);
}
}
}
}
void translation_unit_visitor::process_template_method(
const clang::FunctionTemplateDecl &method,
found_relationships_t &relationships)
@@ -336,13 +470,22 @@ bool translation_unit_visitor::find_relationships(const clang::QualType &type,
else if (type->isEnumeralType()) {
if (const auto *enum_type = type->getAs<clang::EnumType>();
enum_type != nullptr) {
relationships.emplace_back(
common::to_id(*enum_type), relationship_hint);
if (const auto *enum_decl = enum_type->getDecl();
enum_decl != nullptr)
relationships.emplace_back(
get_package_id(enum_decl), relationship_hint);
}
}
else if (const auto *template_specialization_type =
type->getAs<clang::TemplateSpecializationType>()) {
if (template_specialization_type != nullptr) {
// Add dependency to template declaration
relationships.emplace_back(
get_package_id(template_specialization_type->getTemplateName()
.getAsTemplateDecl()),
relationship_hint);
// Add dependencies to template arguments
for (const auto &template_argument :
template_specialization_type->template_arguments()) {
const auto template_argument_kind = template_argument.getKind();
@@ -388,20 +531,48 @@ bool translation_unit_visitor::find_relationships(const clang::QualType &type,
}
}
}
else if (type->isRecordType() && type->getAsCXXRecordDecl()) {
const auto *namespace_context =
type->getAsCXXRecordDecl()->getEnclosingNamespaceContext();
if (namespace_context != nullptr && namespace_context->isNamespace()) {
const auto *namespace_declaration =
clang::cast<clang::NamespaceDecl>(namespace_context);
else if (type->isRecordType()) {
if (const auto *cxxrecord_decl = type->getAsCXXRecordDecl();
cxxrecord_decl != nullptr) {
if (config().package_type() == config::package_type_t::kNamespace) {
const auto *namespace_context =
cxxrecord_decl->getEnclosingNamespaceContext();
if (namespace_context != nullptr &&
namespace_context->isNamespace()) {
const auto *namespace_declaration =
clang::cast<clang::NamespaceDecl>(namespace_context);
if (namespace_declaration != nullptr &&
diagram().should_include(
common::get_qualified_name(*namespace_declaration))) {
const auto target_id = common::to_id(
*clang::cast<clang::NamespaceDecl>(namespace_context));
relationships.emplace_back(target_id, relationship_hint);
result = true;
if (namespace_declaration != nullptr &&
diagram().should_include(common::get_qualified_name(
*namespace_declaration))) {
const auto target_id = get_package_id(cxxrecord_decl);
relationships.emplace_back(
target_id, relationship_hint);
result = true;
}
}
}
else {
if (diagram().should_include(common::get_qualified_name(
*type->getAsCXXRecordDecl()))) {
const auto target_id =
get_package_id(type->getAsCXXRecordDecl());
relationships.emplace_back(target_id, relationship_hint);
result = true;
}
}
}
else if (const auto *record_decl = type->getAsRecordDecl();
record_decl != nullptr) {
// This is only possible for plain C translation unit, so we don't
// need to consider namespaces here
if (config().package_type() == config::package_type_t::kDirectory) {
if (diagram().should_include(
common::get_qualified_name(*record_decl))) {
const auto target_id = get_package_id(record_decl);
relationships.emplace_back(target_id, relationship_hint);
result = true;
}
}
}
}

View File

@@ -49,8 +49,14 @@ public:
virtual bool VisitNamespaceDecl(clang::NamespaceDecl *ns);
virtual bool VisitEnumDecl(clang::EnumDecl *decl);
virtual bool VisitCXXRecordDecl(clang::CXXRecordDecl *cls);
virtual bool VisitRecordDecl(clang::RecordDecl *cls);
virtual bool VisitClassTemplateDecl(clang::ClassTemplateDecl *decl);
virtual bool VisitFunctionDecl(clang::FunctionDecl *function_declaration);
clanguml::package_diagram::model::diagram &diagram() { return diagram_; }
@@ -60,12 +66,17 @@ public:
void finalize() { }
private:
common::model::diagram_element::id_t get_package_id(const clang::Decl *cls);
void process_class_declaration(
const clang::CXXRecordDecl &cls, found_relationships_t &relationships);
void process_class_children(
const clang::CXXRecordDecl &cls, found_relationships_t &relationships);
void process_record_children(
const clang::RecordDecl &cls, found_relationships_t &relationships);
void process_class_bases(
const clang::CXXRecordDecl &cls, found_relationships_t &relationships);
@@ -90,7 +101,7 @@ private:
common::model::relationship_t::kDependency);
void add_relationships(
clang::DeclContext *cls, found_relationships_t &relationships);
clang::Decl *cls, found_relationships_t &relationships);
// Reference to the output diagram model
clanguml::package_diagram::model::diagram &diagram_;

View File

@@ -550,8 +550,7 @@ common::id_t generator::generate_participant(
nlohmann::json j = function_participant;
j["name"] = util::path_to_url(
std::filesystem::relative(function_participant.file(),
std::filesystem::canonical(m_config.relative_to()).string()));
m_config.make_path_relative(function_participant.file()).string());
participant_id = common::to_id(function_participant.file_relative());

View File

@@ -329,11 +329,8 @@ void generator::generate_participant(
if (is_participant_generated(file_id))
return;
const auto &relative_to =
std::filesystem::canonical(m_config.relative_to());
auto participant_name = util::path_to_url(std::filesystem::relative(
std::filesystem::path{file_path}, relative_to)
std::filesystem::path{file_path}, m_config.root_directory())
.string());
ostr << "participant \"" << render_name(participant_name) << "\" as "