Enabled subclass filter to generate class inheritance diagram
This commit is contained in:
@@ -355,14 +355,18 @@ void generator::generate(const package &p, std::ostream &ostr,
|
||||
generate(sp, ostr, relationships_ostr);
|
||||
}
|
||||
else if (dynamic_cast<class_ *>(subpackage.get())) {
|
||||
generate_alias(dynamic_cast<class_ &>(*subpackage), ostr);
|
||||
generate(
|
||||
dynamic_cast<class_ &>(*subpackage), ostr, relationships_ostr);
|
||||
if (m_model.should_include(*subpackage)) {
|
||||
generate_alias(dynamic_cast<class_ &>(*subpackage), ostr);
|
||||
generate(dynamic_cast<class_ &>(*subpackage), ostr,
|
||||
relationships_ostr);
|
||||
}
|
||||
}
|
||||
else if (dynamic_cast<enum_ *>(subpackage.get())) {
|
||||
generate_alias(dynamic_cast<enum_ &>(*subpackage), ostr);
|
||||
generate(
|
||||
dynamic_cast<enum_ &>(*subpackage), ostr, relationships_ostr);
|
||||
if (m_model.should_include(*subpackage)) {
|
||||
generate_alias(dynamic_cast<enum_ &>(*subpackage), ostr);
|
||||
generate(dynamic_cast<enum_ &>(*subpackage), ostr,
|
||||
relationships_ostr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -392,12 +396,16 @@ void generator::generate(std::ostream &ostr) const
|
||||
generate(sp, ostr, relationships_ostr);
|
||||
}
|
||||
else if (dynamic_cast<class_ *>(p.get())) {
|
||||
generate_alias(dynamic_cast<class_ &>(*p), ostr);
|
||||
generate(dynamic_cast<class_ &>(*p), ostr, relationships_ostr);
|
||||
if (m_model.should_include(*p)) {
|
||||
generate_alias(dynamic_cast<class_ &>(*p), ostr);
|
||||
generate(dynamic_cast<class_ &>(*p), ostr, relationships_ostr);
|
||||
}
|
||||
}
|
||||
else if (dynamic_cast<enum_ *>(p.get())) {
|
||||
generate_alias(dynamic_cast<enum_ &>(*p), ostr);
|
||||
generate(dynamic_cast<enum_ &>(*p), ostr, relationships_ostr);
|
||||
if (m_model.should_include(*p)) {
|
||||
generate_alias(dynamic_cast<enum_ &>(*p), ostr);
|
||||
generate(dynamic_cast<enum_ &>(*p), ostr, relationships_ostr);
|
||||
}
|
||||
}
|
||||
ostr << '\n';
|
||||
}
|
||||
|
||||
@@ -124,14 +124,12 @@ void diagram::get_parents(
|
||||
{
|
||||
bool found_new = false;
|
||||
for (const auto &parent : parents) {
|
||||
for (const auto &rel : parent.get().relationships()) {
|
||||
if (rel.type() == common::model::relationship_t::kExtension) {
|
||||
const auto p = get_class(rel.destination());
|
||||
if (p.has_value()) {
|
||||
auto [it, found] = parents.emplace(p.value());
|
||||
if (found)
|
||||
found_new = true;
|
||||
}
|
||||
for (const auto &pp : parent.get().parents()) {
|
||||
const auto p = get_class(pp.name());
|
||||
if (p.has_value()) {
|
||||
auto [it, found] = parents.emplace(p.value());
|
||||
if (found)
|
||||
found_new = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -184,7 +184,8 @@ clanguml::class_diagram::model::diagram &translation_unit_context::diagram()
|
||||
return diagram_;
|
||||
}
|
||||
|
||||
clanguml::class_diagram::model::diagram &translation_unit_context::diagram() const
|
||||
clanguml::class_diagram::model::diagram &
|
||||
translation_unit_context::diagram() const
|
||||
{
|
||||
return diagram_;
|
||||
}
|
||||
|
||||
@@ -241,13 +241,13 @@ void translation_unit_visitor::process_namespace(
|
||||
|
||||
auto usn = ctx.config().using_namespace();
|
||||
|
||||
if (ctx.diagram().should_include(package_path)) {
|
||||
auto p = std::make_unique<common::model::package>(usn);
|
||||
package_path = package_path.relative_to(usn);
|
||||
auto p = std::make_unique<common::model::package>(usn);
|
||||
package_path = package_path.relative_to(usn);
|
||||
|
||||
p->set_name(e.name());
|
||||
p->set_namespace(package_parent);
|
||||
p->set_name(e.name());
|
||||
p->set_namespace(package_parent);
|
||||
|
||||
if (ctx.diagram().should_include(*p)) {
|
||||
if (e.comment().has_value())
|
||||
p->set_comment(e.comment().value());
|
||||
|
||||
@@ -580,7 +580,8 @@ void translation_unit_visitor::process_class_bases(
|
||||
cx::util::ns(base.type(), ctx.entity_index())};
|
||||
base_ns = base_ns | common::model::namespace_{base.name()}.name();
|
||||
cp.set_name(
|
||||
base_ns.relative_to(ctx.config().using_namespace()).to_string());
|
||||
// base_ns.relative_to(ctx.config().using_namespace()).to_string());
|
||||
base_ns.to_string());
|
||||
cp.is_virtual(base.is_virtual());
|
||||
|
||||
switch (base.access_specifier()) {
|
||||
|
||||
@@ -225,14 +225,15 @@ void generator<C, D>::generate_link(
|
||||
|
||||
template <typename DiagramModel, typename DiagramConfig,
|
||||
typename DiagramVisitor>
|
||||
DiagramModel generate(const cppast::libclang_compilation_database &db,
|
||||
const std::string &name, DiagramConfig &config, bool verbose = false)
|
||||
std::unique_ptr<DiagramModel> generate(
|
||||
const cppast::libclang_compilation_database &db, const std::string &name,
|
||||
DiagramConfig &config, bool verbose = false)
|
||||
{
|
||||
LOG_INFO("Generating diagram {}.puml", name);
|
||||
DiagramModel diagram{};
|
||||
diagram.set_name(name);
|
||||
diagram.set_filter(
|
||||
std::make_unique<model::diagram_filter>(diagram, config));
|
||||
auto diagram = std::make_unique<DiagramModel>();
|
||||
diagram->set_name(name);
|
||||
diagram->set_filter(
|
||||
std::make_unique<model::diagram_filter>(*diagram, config));
|
||||
|
||||
// Get all translation units matching the glob from diagram
|
||||
// configuration
|
||||
@@ -251,11 +252,13 @@ DiagramModel generate(const cppast::libclang_compilation_database &db,
|
||||
type_safe::ref(idx), std::move(logger)};
|
||||
|
||||
// Process all matching translation units
|
||||
DiagramVisitor ctx(idx, diagram, config);
|
||||
DiagramVisitor ctx(idx, *diagram, config);
|
||||
cppast::parse_files(parser, translation_units, db);
|
||||
for (auto &file : parser.files())
|
||||
ctx(file);
|
||||
|
||||
diagram->set_complete(true);
|
||||
|
||||
return std::move(diagram);
|
||||
}
|
||||
|
||||
|
||||
@@ -40,28 +40,36 @@ void diagram::set_filter(std::unique_ptr<diagram_filter> filter)
|
||||
filter_ = std::move(filter);
|
||||
}
|
||||
|
||||
bool diagram::should_include(const element &e) const {
|
||||
void diagram::set_complete(bool complete) { complete_ = complete; }
|
||||
|
||||
bool diagram::complete() const { return complete_; }
|
||||
|
||||
bool diagram::should_include(const element &e) const
|
||||
{
|
||||
if (filter_.get() == nullptr)
|
||||
return true;
|
||||
|
||||
return filter_->should_include(e);
|
||||
}
|
||||
|
||||
bool diagram::should_include(const std::string &name) const {
|
||||
bool diagram::should_include(const std::string &name) const
|
||||
{
|
||||
if (filter_.get() == nullptr)
|
||||
return true;
|
||||
|
||||
return filter_->should_include(name);
|
||||
}
|
||||
|
||||
bool diagram::should_include(const relationship_t r) const {
|
||||
bool diagram::should_include(const relationship_t r) const
|
||||
{
|
||||
if (filter_.get() == nullptr)
|
||||
return true;
|
||||
|
||||
return filter_->should_include(r);
|
||||
}
|
||||
|
||||
bool diagram::should_include(const scope_t s) const {
|
||||
bool diagram::should_include(const scope_t s) const
|
||||
{
|
||||
if (filter_.get() == nullptr)
|
||||
return true;
|
||||
|
||||
|
||||
@@ -44,6 +44,9 @@ public:
|
||||
|
||||
void set_filter(std::unique_ptr<diagram_filter> filter);
|
||||
|
||||
void set_complete(bool complete);
|
||||
bool complete() const;
|
||||
|
||||
// TODO: refactor to a template method
|
||||
bool should_include(const element &e) const;
|
||||
bool should_include(const std::string &e) const;
|
||||
@@ -55,6 +58,7 @@ public:
|
||||
private:
|
||||
std::string name_;
|
||||
std::unique_ptr<diagram_filter> filter_;
|
||||
bool complete_{false};
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -20,7 +20,8 @@
|
||||
|
||||
namespace clanguml::common::model {
|
||||
|
||||
std::optional<bool> filter_visitor::match(const diagram &d, const common::model::element &e)
|
||||
std::optional<bool> filter_visitor::match(
|
||||
const diagram &d, const common::model::element &e)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
@@ -31,7 +32,8 @@ std::optional<bool> filter_visitor::match(
|
||||
return {};
|
||||
}
|
||||
|
||||
std::optional<bool> filter_visitor::match(const diagram &d, const common::model::scope_t &r)
|
||||
std::optional<bool> filter_visitor::match(
|
||||
const diagram &d, const common::model::scope_t &r)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
@@ -61,19 +61,34 @@ struct namespace_filter : public filter_visitor {
|
||||
|
||||
std::optional<bool> match(const diagram &d, const namespace_ &ns) override
|
||||
{
|
||||
if (namespaces_.empty())
|
||||
if (namespaces_.empty() || ns.is_empty())
|
||||
return {};
|
||||
|
||||
return std::any_of(namespaces_.begin(), namespaces_.end(),
|
||||
[&ns](const auto &nsit) { return ns.starts_with(nsit); });
|
||||
return std::any_of(
|
||||
namespaces_.begin(), namespaces_.end(), [&ns](const auto &nsit) {
|
||||
return ns.starts_with(nsit) || ns == nsit;
|
||||
});
|
||||
}
|
||||
|
||||
std::optional<bool> match(const diagram &d, const element &e) override
|
||||
{
|
||||
return std::any_of(
|
||||
namespaces_.begin(), namespaces_.end(), [&e](const auto &nsit) {
|
||||
return e.get_namespace().starts_with(nsit);
|
||||
});
|
||||
if (namespaces_.empty())
|
||||
return {};
|
||||
|
||||
if (dynamic_cast<const package *>(&e) != nullptr) {
|
||||
return std::any_of(
|
||||
namespaces_.begin(), namespaces_.end(), [&e](const auto &nsit) {
|
||||
return (e.get_namespace() | e.name()).starts_with(nsit) ||
|
||||
nsit.starts_with(e.get_namespace() | e.name()) ||
|
||||
(e.get_namespace() | e.name()) == nsit;
|
||||
});
|
||||
}
|
||||
else {
|
||||
return std::any_of(
|
||||
namespaces_.begin(), namespaces_.end(), [&e](const auto &nsit) {
|
||||
return e.get_namespace().starts_with(nsit);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<namespace_> namespaces_;
|
||||
@@ -114,6 +129,9 @@ struct subclass_filter : public filter_visitor {
|
||||
if (roots_.empty())
|
||||
return {};
|
||||
|
||||
if (!d.complete())
|
||||
return {};
|
||||
|
||||
const auto &cd = dynamic_cast<const class_diagram::model::diagram &>(d);
|
||||
|
||||
// First get all parents of element e
|
||||
@@ -200,11 +218,14 @@ struct context_filter : public filter_visitor {
|
||||
|
||||
std::optional<bool> match(const diagram &d, const element &r) override
|
||||
{
|
||||
if (!d.complete())
|
||||
return {};
|
||||
|
||||
if (context_.empty())
|
||||
return {};
|
||||
|
||||
return std::any_of(context_.begin(), context_.end(),
|
||||
[&r](const auto &rel) { return true; });
|
||||
[&r](const auto &rel) { return std::optional<bool>{}; });
|
||||
}
|
||||
|
||||
std::vector<std::string> context_;
|
||||
@@ -248,7 +269,7 @@ public:
|
||||
auto m = ex->match(diagram_, e);
|
||||
// Return a match if a filter is undefined for specific element
|
||||
// or it's a match
|
||||
return !m.has_value() || m.value();
|
||||
return m.has_value() && m.value();
|
||||
});
|
||||
if (exc)
|
||||
return false;
|
||||
@@ -274,18 +295,14 @@ private:
|
||||
if (c.include) {
|
||||
inclusive_.emplace_back(std::make_unique<namespace_filter>(
|
||||
filter_t::kInclusive, c.include().namespaces));
|
||||
|
||||
inclusive_.emplace_back(std::make_unique<relationship_filter>(
|
||||
filter_t::kInclusive, c.include().relationships));
|
||||
inclusive_.emplace_back(std::make_unique<scope_filter>(
|
||||
filter_t::kInclusive, c.include().scopes));
|
||||
|
||||
inclusive_.emplace_back(std::make_unique<element_filter>(
|
||||
filter_t::kInclusive, c.include().elements));
|
||||
|
||||
inclusive_.emplace_back(std::make_unique<subclass_filter>(
|
||||
filter_t::kInclusive, c.include().subclasses));
|
||||
|
||||
inclusive_.emplace_back(std::make_unique<context_filter>(
|
||||
filter_t::kInclusive, c.include().context));
|
||||
}
|
||||
@@ -308,9 +325,8 @@ private:
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<filter_visitor>> inclusive_;
|
||||
// std::vector<std::unique_ptr<filter_visitor>> inclusive_and_;
|
||||
|
||||
std::vector<std::unique_ptr<filter_visitor>> exclusive_;
|
||||
|
||||
const common::model::diagram &diagram_;
|
||||
};
|
||||
|
||||
|
||||
@@ -318,8 +318,7 @@ template <> struct convert<filter> {
|
||||
node["relationships"].as<decltype(rhs.relationships)>();
|
||||
|
||||
if (node["elements"])
|
||||
rhs.elements =
|
||||
node["elements"].as<decltype(rhs.elements)>();
|
||||
rhs.elements = node["elements"].as<decltype(rhs.elements)>();
|
||||
|
||||
if (node["scopes"])
|
||||
rhs.scopes = node["scopes"].as<decltype(rhs.scopes)>();
|
||||
|
||||
@@ -35,7 +35,6 @@
|
||||
namespace clanguml {
|
||||
namespace config {
|
||||
|
||||
|
||||
enum class diagram_type { class_diagram, sequence_diagram, package_diagram };
|
||||
enum class method_arguments { full, abbreviated, none };
|
||||
|
||||
|
||||
@@ -152,7 +152,7 @@ void generate_diagram(const std::string &od, const std::string &name,
|
||||
dynamic_cast<diagram_config &>(*diagram), verbose);
|
||||
|
||||
ofs << clanguml::class_diagram::generators::plantuml::generator(
|
||||
dynamic_cast<diagram_config &>(*diagram), model);
|
||||
dynamic_cast<diagram_config &>(*diagram), *model);
|
||||
}
|
||||
else if (diagram->type() == diagram_type::sequence_diagram) {
|
||||
using diagram_config = sequence_diagram;
|
||||
@@ -167,7 +167,7 @@ void generate_diagram(const std::string &od, const std::string &name,
|
||||
|
||||
ofs << clanguml::sequence_diagram::generators::plantuml::generator(
|
||||
dynamic_cast<clanguml::config::sequence_diagram &>(*diagram),
|
||||
model);
|
||||
*model);
|
||||
}
|
||||
else if (diagram->type() == diagram_type::package_diagram) {
|
||||
using diagram_config = package_diagram;
|
||||
@@ -181,7 +181,7 @@ void generate_diagram(const std::string &od, const std::string &name,
|
||||
dynamic_cast<diagram_config &>(*diagram), verbose);
|
||||
|
||||
ofs << clanguml::package_diagram::generators::plantuml::generator(
|
||||
dynamic_cast<diagram_config &>(*diagram), model);
|
||||
dynamic_cast<diagram_config &>(*diagram), *model);
|
||||
}
|
||||
|
||||
LOG_INFO("Written {} diagram to {}", name, path.string());
|
||||
|
||||
@@ -95,18 +95,18 @@ void translation_unit_visitor::operator()(const cppast::cpp_entity &file)
|
||||
auto package_path = package_parent | e.name();
|
||||
auto usn = ctx.config().using_namespace();
|
||||
|
||||
if (ctx.diagram().should_include(package_path)) {
|
||||
auto p = std::make_unique<package>(usn);
|
||||
package_path = package_path.relative_to(usn);
|
||||
auto p = std::make_unique<package>(usn);
|
||||
package_path = package_path.relative_to(usn);
|
||||
|
||||
if (e.location().has_value()) {
|
||||
p->set_file(e.location().value().file);
|
||||
p->set_line(e.location().value().line);
|
||||
}
|
||||
if (e.location().has_value()) {
|
||||
p->set_file(e.location().value().file);
|
||||
p->set_line(e.location().value().line);
|
||||
}
|
||||
|
||||
p->set_name(e.name());
|
||||
p->set_namespace(package_parent);
|
||||
p->set_name(e.name());
|
||||
p->set_namespace(package_parent);
|
||||
|
||||
if (ctx.diagram().should_include(*p)) {
|
||||
if (ns_declaration.comment().has_value())
|
||||
p->add_decorators(decorators::parse(
|
||||
ns_declaration.comment().value()));
|
||||
|
||||
Reference in New Issue
Block a user