Added basic config option inheritance

This commit is contained in:
Bartek Kryza
2022-02-06 00:02:58 +01:00
parent bf64a59d0d
commit 8117dfefab
55 changed files with 375 additions and 399 deletions
@@ -119,7 +119,7 @@ void generator::generate_alias(const enum_ &e, std::ostream &ostr) const
void generator::generate(const class_ &c, std::ostream &ostr) const
{
const auto &uns = m_config.using_namespace;
const auto &uns = m_config.using_namespace();
std::string class_type{"class"};
if (c.is_abstract())
@@ -154,7 +154,7 @@ void generator::generate(const class_ &c, std::ostream &ostr) const
std::vector<std::string> params;
std::transform(m.parameters().cbegin(), m.parameters().cend(),
std::back_inserter(params), [this](const auto &mp) {
return mp.to_string(m_config.using_namespace);
return mp.to_string(m_config.using_namespace());
});
ostr << fmt::format("{}", fmt::join(params, ", "));
}
@@ -238,7 +238,7 @@ void generator::generate(const class_ &c, std::ostream &ostr) const
if (!m_config.should_include(m.scope()))
continue;
if (!m_config.include_relations_also_as_members &&
if (!m_config.include_relations_also_as_members() &&
rendered_relations.find(m.name()) != rendered_relations.end())
continue;
@@ -310,10 +310,10 @@ void generator::generate(const enum_ &e, std::ostream &ostr) const
destination = r.destination();
relstr << m_model.to_alias(
ns_relative(m_config.using_namespace, e.name()))
ns_relative(m_config.using_namespace(), e.name()))
<< " " << to_string(r.type()) << " "
<< m_model.to_alias(
ns_relative(m_config.using_namespace, destination));
ns_relative(m_config.using_namespace(), destination));
if (!r.label().empty())
relstr << " : " << r.label();
@@ -346,12 +346,12 @@ void generator::generate(std::ostream &ostr) const
{
ostr << "@startuml" << '\n';
for (const auto &b : m_config.puml.before) {
for (const auto &b : m_config.puml().before) {
std::string note{b};
std::tuple<std::string, size_t, size_t> alias_match;
while (util::find_element_alias(note, alias_match)) {
auto alias = m_model.to_alias(ns_relative(
m_config.using_namespace, std::get<0>(alias_match)));
m_config.using_namespace(), std::get<0>(alias_match)));
note.replace(
std::get<1>(alias_match), std::get<2>(alias_match), alias);
}
@@ -388,12 +388,12 @@ void generator::generate(std::ostream &ostr) const
}
// Process aliases in any of the puml directives
for (const auto &b : m_config.puml.after) {
for (const auto &b : m_config.puml().after) {
std::string note{b};
std::tuple<std::string, size_t, size_t> alias_match;
while (util::find_element_alias(note, alias_match)) {
auto alias = m_model.to_alias(ns_relative(
m_config.using_namespace, std::get<0>(alias_match)));
m_config.using_namespace(), std::get<0>(alias_match)));
note.replace(
std::get<1>(alias_match), std::get<2>(alias_match), alias);
}
@@ -419,7 +419,7 @@ clanguml::class_diagram::model::diagram generate(
// Get all translation units matching the glob from diagram
// configuration
std::vector<std::string> translation_units{};
for (const auto &g : diagram.glob) {
for (const auto &g : diagram.glob()) {
LOG_DBG("Processing glob: {}", g);
const auto matches = glob::rglob(g);
std::copy(matches.begin(), matches.end(),
@@ -202,7 +202,7 @@ void translation_unit_visitor::process_enum_declaration(
return;
}
enum_ e{ctx.config().using_namespace};
enum_ e{ctx.config().using_namespace()};
e.set_name(cx::util::full_name(ctx.get_namespace(), enm));
if (enm.comment().has_value())
@@ -242,7 +242,7 @@ void translation_unit_visitor::process_class_declaration(
const cppast::cpp_class &cls,
type_safe::optional_ref<const cppast::cpp_template_specialization> tspec)
{
class_ c{ctx.config().using_namespace};
class_ c{ctx.config().using_namespace()};
c.is_struct(cls.class_kind() == cppast::cpp_class_kind::struct_t);
c.set_name(cx::util::full_name(ctx.get_namespace(), cls));
@@ -1201,7 +1201,7 @@ class_ translation_unit_visitor::build_template_instantiation(
const cppast::cpp_template_instantiation_type &t,
std::optional<clanguml::class_diagram::model::class_ *> parent)
{
class_ tinst{ctx.config().using_namespace};
class_ tinst{ctx.config().using_namespace()};
std::string full_template_name;
std::deque<std::tuple<std::string, int, bool>> template_base_params{};
+77 -74
View File
@@ -53,15 +53,15 @@ std::string to_string(const diagram_type t)
bool diagram::should_include_entities(const std::string &ent)
{
for (const auto &ex : exclude.entity_types) {
for (const auto &ex : exclude().entity_types) {
if (ent == ex)
return false;
}
if (include.entity_types.empty())
if (include().entity_types.empty())
return true;
for (const auto &in : include.entity_types) {
for (const auto &in : include().entity_types) {
if (ent == in)
return true;
}
@@ -71,15 +71,15 @@ bool diagram::should_include_entities(const std::string &ent)
bool diagram::should_include_relationship(const std::string &rel)
{
for (const auto &ex : exclude.relationships) {
for (const auto &ex : exclude().relationships) {
if (rel == ex)
return false;
}
if (include.relationships.empty())
if (include().relationships.empty())
return true;
for (const auto &in : include.relationships) {
for (const auto &in : include().relationships) {
if (rel == in)
return true;
}
@@ -91,7 +91,7 @@ bool diagram::should_include(const std::string &name_) const
{
auto name = clanguml::util::unqualify(name_);
for (const auto &ex : exclude.namespaces) {
for (const auto &ex : exclude().namespaces) {
if (name.find(ex) == 0) {
LOG_DBG("Skipping from diagram: {}", name);
return false;
@@ -100,10 +100,10 @@ bool diagram::should_include(const std::string &name_) const
// If no inclusive namespaces are provided,
// allow all
if (include.namespaces.empty())
if (include().namespaces.empty())
return true;
for (const auto &in : include.namespaces) {
for (const auto &in : include().namespaces) {
if (name.find(in) == 0)
return true;
}
@@ -115,15 +115,15 @@ bool diagram::should_include(const std::string &name_) const
bool diagram::should_include(const clanguml::common::model::scope_t scope) const
{
for (const auto &s : exclude.scopes) {
for (const auto &s : exclude().scopes) {
if (s == scope)
return false;
}
if (include.scopes.empty())
if (include().scopes.empty())
return true;
for (const auto &s : include.scopes) {
for (const auto &s : include().scopes) {
if (s == scope)
return true;
}
@@ -135,8 +135,8 @@ diagram_type class_diagram::type() const { return diagram_type::class_diagram; }
bool class_diagram::has_class(std::string clazz)
{
for (const auto &c : classes) {
for (const auto &ns : using_namespace) {
for (const auto &c : classes()) {
for (const auto &ns : using_namespace()) {
std::string prefix{};
if (!ns.empty()) {
prefix = ns + "::";
@@ -159,6 +159,16 @@ diagram_type package_diagram::type() const
return diagram_type::package_diagram;
}
template <>
void append_value<std::vector<std::string>>(
std::vector<std::string> &l, const std::vector<std::string> &r)
{
l.insert(l.end(), r.begin(), r.end());
}
template <> void append_value<plantuml>(plantuml &l, const plantuml &r)
{
l.append(r);
}
}
}
@@ -171,6 +181,42 @@ using clanguml::config::package_diagram;
using clanguml::config::plantuml;
using clanguml::config::sequence_diagram;
using clanguml::config::source_location;
inline bool has_key(const YAML::Node &n, const std::string &key)
{
assert(n.Type() == NodeType::Map);
return std::count_if(n.begin(), n.end(), [&key](auto &&n) {
return n.first.template as<std::string>() == key;
}) > 0;
}
template <typename T>
void get_option(const Node &node, clanguml::config::option<T> &option)
{
if (node[option.name])
option.append(node[option.name].template as<T>());
}
std::shared_ptr<clanguml::config::diagram> parse_diagram_config(const Node &d)
{
const auto diagram_type = d["type"].as<std::string>();
if (diagram_type == "class") {
return std::make_shared<class_diagram>(d.as<class_diagram>());
}
else if (diagram_type == "sequence") {
return std::make_shared<sequence_diagram>(d.as<sequence_diagram>());
}
else if (diagram_type == "package") {
return std::make_shared<package_diagram>(d.as<package_diagram>());
}
LOG_WARN("Diagrams of type {} are not supported... ", diagram_type);
return {};
}
template <> struct convert<scope_t> {
static bool decode(const Node &node, scope_t &rhs)
{
@@ -264,21 +310,11 @@ template <> struct convert<filter> {
template <typename T> bool decode_diagram(const Node &node, T &rhs)
{
if (node["using_namespace"])
rhs.using_namespace =
node["using_namespace"].as<std::vector<std::string>>();
if (node["glob"])
rhs.glob = node["glob"].as<std::vector<std::string>>();
if (node["include"])
rhs.include = node["include"].as<decltype(rhs.include)>();
if (node["exclude"])
rhs.exclude = node["exclude"].as<decltype(rhs.exclude)>();
if (node["plantuml"])
rhs.puml = node["plantuml"].as<plantuml>();
get_option(node, rhs.glob);
get_option(node, rhs.using_namespace);
get_option(node, rhs.include);
get_option(node, rhs.exclude);
get_option(node, rhs.puml);
return true;
}
@@ -292,10 +328,8 @@ template <> struct convert<class_diagram> {
if (!decode_diagram(node, rhs))
return false;
if (node["include_relations_also_as_members"])
rhs.include_relations_also_as_members =
node["include_relations_also_as_members"]
.as<decltype(rhs.include_relations_also_as_members)>();
get_option(node, rhs.classes);
get_option(node, rhs.include_relations_also_as_members);
return true;
}
@@ -310,9 +344,7 @@ template <> struct convert<sequence_diagram> {
if (!decode_diagram(node, rhs))
return false;
if (node["start_from"])
rhs.start_from =
node["start_from"].as<std::vector<source_location>>();
get_option(node, rhs.start_from);
return true;
}
@@ -331,49 +363,19 @@ template <> struct convert<package_diagram> {
}
};
std::shared_ptr<clanguml::config::diagram> parse_diagram_config(const Node &d)
{
const auto diagram_type = d["type"].as<std::string>();
if (diagram_type == "class") {
return std::make_shared<class_diagram>(d.as<class_diagram>());
}
else if (diagram_type == "sequence") {
return std::make_shared<sequence_diagram>(d.as<sequence_diagram>());
}
else if (diagram_type == "package") {
return std::make_shared<package_diagram>(d.as<package_diagram>());
}
LOG_WARN("Diagrams of type {} are not supported... ", diagram_type);
return {};
}
inline bool has_key(const YAML::Node &n, const std::string &key)
{
assert(n.Type() == NodeType::Map);
return std::count_if(n.begin(), n.end(), [&key](auto &&n) {
return n.first.template as<std::string>() == key;
}) > 0;
}
//
// config Yaml decoder
//
template <> struct convert<config> {
static bool decode(const Node &node, config &rhs)
{
if (node["glob"])
rhs.glob = node["glob"].as<std::vector<std::string>>();
if (node["output_directory"])
rhs.output_directory = node["output_directory"].as<std::string>();
if (node["compilation_database_dir"])
rhs.compilation_database_dir =
node["compilation_database_dir"].as<std::string>();
get_option(node, rhs.glob);
get_option(node, rhs.using_namespace);
get_option(node, rhs.output_directory);
get_option(node, rhs.compilation_database_dir);
get_option(node, rhs.include_relations_also_as_members);
get_option(node, rhs.puml);
auto diagrams = node["diagrams"];
@@ -381,7 +383,7 @@ template <> struct convert<config> {
for (const auto &d : diagrams) {
auto name = d.first.as<std::string>();
std::shared_ptr<clanguml::config::diagram> diagram_config;
std::shared_ptr<clanguml::config::diagram> diagram_config{};
if (has_key(d.second, "include!")) {
YAML::Node node =
@@ -395,6 +397,7 @@ template <> struct convert<config> {
if (diagram_config) {
diagram_config->name = name;
diagram_config->inherit(rhs);
rhs.diagrams[name] = diagram_config;
}
}
+86 -15
View File
@@ -37,6 +37,12 @@ namespace config {
struct plantuml {
std::vector<std::string> before;
std::vector<std::string> after;
void append(const plantuml &r)
{
before.insert(before.end(), r.before.begin(), r.before.end());
after.insert(after.end(), r.after.begin(), r.after.end());
}
};
struct filter {
@@ -63,19 +69,84 @@ enum class diagram_type { class_diagram, sequence_diagram, package_diagram };
std::string to_string(const diagram_type t);
struct diagram {
template <typename T> void append_value(T &l, const T &r) { l = r; }
// template<> void append_value<bool>(bool &l, const bool&r) {l = r;}
template <typename T> struct option {
option(const std::string &name_)
: name{name_}
{
}
option(const std::string &name_, const T &initial_value)
: name{name_}
, value{initial_value}
, has_value{true}
{
}
std::string name;
T value;
bool has_value{false};
void append(const T &r)
{
append_value(value, r);
has_value = true;
}
option<T> &operator+=(const T &r)
{
append_value(value, r);
has_value = true;
return *this;
}
T &operator()() { return value; }
const T &operator()() const { return value; }
};
struct inheritable_diagram_options {
option<std::vector<std::string>> glob{"glob"};
option<std::vector<std::string>> using_namespace{"using_namespace"};
option<bool> include_relations_also_as_members{
"include_relations_also_as_members", true};
option<filter> include{"include"};
option<filter> exclude{"exclude"};
option<plantuml> puml{"plantuml"};
void inherit(const inheritable_diagram_options &parent)
{
if (!glob.has_value && parent.glob.has_value)
glob.append(parent.glob());
if (!using_namespace.has_value && parent.using_namespace.has_value)
using_namespace.append(parent.using_namespace());
if (!include_relations_also_as_members.has_value &&
parent.include_relations_also_as_members.has_value)
include_relations_also_as_members.append(
parent.include_relations_also_as_members());
if (!include.has_value && parent.include.has_value)
include.append(parent.include());
if (!exclude.has_value && parent.exclude.has_value)
exclude.append(parent.exclude());
if (!puml.has_value && parent.puml.has_value)
puml.append(parent.puml());
}
};
struct diagram : public inheritable_diagram_options {
virtual ~diagram() = default;
virtual diagram_type type() const = 0;
std::string name;
std::vector<std::string> glob;
std::vector<std::string> using_namespace;
filter include;
filter exclude;
plantuml puml;
bool should_include_entities(const std::string &ent);
@@ -97,8 +168,7 @@ struct class_diagram : public diagram {
diagram_type type() const override;
std::vector<std::string> classes;
bool include_relations_also_as_members{true};
option<std::vector<std::string>> classes{"classes"};
bool has_class(std::string clazz);
};
@@ -108,7 +178,7 @@ struct sequence_diagram : public diagram {
diagram_type type() const override;
std::vector<source_location> start_from;
option<std::vector<source_location>> start_from{"start_from"};
};
struct package_diagram : public diagram {
@@ -117,12 +187,13 @@ struct package_diagram : public diagram {
diagram_type type() const override;
};
struct config {
struct config : public inheritable_diagram_options {
// the glob list is additive and relative to the current
// directory
std::vector<std::string> glob;
std::string compilation_database_dir{"."};
std::string output_directory{};
option<std::string> compilation_database_dir{
"compilation_database_dir", "."};
option<std::string> output_directory{"output_directory"};
std::map<std::string, std::shared_ptr<diagram>> diagrams;
};
+3 -3
View File
@@ -89,11 +89,11 @@ int main(int argc, const char *argv[])
LOG_INFO("Loaded clang-uml config from {}", config_path);
LOG_INFO("Loading compilation database from {} directory",
config.compilation_database_dir);
config.compilation_database_dir());
cppast::libclang_compilation_database db(config.compilation_database_dir);
cppast::libclang_compilation_database db(config.compilation_database_dir());
std::string od = config.output_directory;
auto od = config.output_directory();
if (output_directory)
od = output_directory.value();
@@ -82,7 +82,7 @@ std::string generator::name(relationship_t r) const
void generator::generate_relationships(
const package &p, std::ostream &ostr) const
{
const auto &uns = m_config.using_namespace;
const auto &uns = m_config.using_namespace();
// Generate this packages relationship
if (m_config.should_include_relationship("dependency")) {
@@ -111,7 +111,7 @@ void generator::generate_relationships(
void generator::generate(const package &p, std::ostream &ostr) const
{
const auto uns = m_config.using_namespace;
const auto &uns = m_config.using_namespace();
ostr << "package [" << p.name() << "] ";
ostr << "as " << p.alias();
@@ -150,12 +150,12 @@ void generator::generate(std::ostream &ostr) const
ostr << "@startuml" << '\n';
// Process aliases in any of the puml directives
for (const auto &b : m_config.puml.before) {
for (const auto &b : m_config.puml().before) {
std::string note{b};
std::tuple<std::string, size_t, size_t> alias_match;
while (util::find_element_alias(note, alias_match)) {
auto alias = m_model.to_alias(ns_relative(
m_config.using_namespace, std::get<0>(alias_match)));
m_config.using_namespace(), std::get<0>(alias_match)));
note.replace(
std::get<1>(alias_match), std::get<2>(alias_match), alias);
}
@@ -176,12 +176,12 @@ void generator::generate(std::ostream &ostr) const
}
// Process aliases in any of the puml directives
for (const auto &b : m_config.puml.after) {
for (const auto &b : m_config.puml().after) {
std::string note{b};
std::tuple<std::string, size_t, size_t> alias_match;
while (util::find_element_alias(note, alias_match)) {
auto alias = m_model.to_alias(ns_relative(
m_config.using_namespace, std::get<0>(alias_match)));
m_config.using_namespace(), std::get<0>(alias_match)));
note.replace(
std::get<1>(alias_match), std::get<2>(alias_match), alias);
}
@@ -208,7 +208,7 @@ clanguml::package_diagram::model::diagram generate(
// Get all translation units matching the glob from diagram
// configuration
std::vector<std::string> translation_units{};
for (const auto &g : diagram.glob) {
for (const auto &g : diagram.glob()) {
LOG_DBG("Processing glob: {}", g);
const auto matches = glob::rglob(g);
std::copy(matches.begin(), matches.end(),
@@ -94,12 +94,12 @@ void translation_unit_visitor::operator()(const cppast::cpp_entity &file)
auto package_path = package_parent;
package_path.push_back(e.name());
auto usn =
util::split(ctx.config().using_namespace[0], "::");
auto usn = util::split(
ctx.config().using_namespace()[0], "::");
if (!util::starts_with(usn, package_path)) {
auto p = std::make_unique<package>(
ctx.config().using_namespace);
ctx.config().using_namespace());
util::remove_prefix(package_path, usn);
util::remove_prefix(package_parent, usn);
@@ -59,8 +59,8 @@ std::string generator::to_string(message_t r) const
void generator::generate_call(const message &m, std::ostream &ostr) const
{
const auto from = ns_relative(m_config.using_namespace, m.from);
const auto to = ns_relative(m_config.using_namespace, m.to);
const auto from = ns_relative(m_config.using_namespace(), m.from);
const auto to = ns_relative(m_config.using_namespace(), m.to);
ostr << '"' << from << "\" "
<< "->"
@@ -72,8 +72,8 @@ void generator::generate_return(const message &m, std::ostream &ostr) const
// Add return activity only for messages between different actors and
// only if the return type is different than void
if ((m.from != m.to) && (m.return_type != "void")) {
const auto from = ns_relative(m_config.using_namespace, m.from);
const auto to = ns_relative(m_config.using_namespace, m.to);
const auto from = ns_relative(m_config.using_namespace(), m.from);
const auto to = ns_relative(m_config.using_namespace(), m.to);
ostr << '"' << to << "\" "
<< "-->"
@@ -84,7 +84,7 @@ void generator::generate_return(const message &m, std::ostream &ostr) const
void generator::generate_activity(const activity &a, std::ostream &ostr) const
{
for (const auto &m : a.messages) {
const auto to = ns_relative(m_config.using_namespace, m.to);
const auto to = ns_relative(m_config.using_namespace(), m.to);
generate_call(m, ostr);
ostr << "activate " << '"' << to << '"' << std::endl;
if (m_model.sequences.find(m.to_usr) != m_model.sequences.end())
@@ -98,10 +98,10 @@ void generator::generate(std::ostream &ostr) const
{
ostr << "@startuml" << std::endl;
for (const auto &b : m_config.puml.before)
for (const auto &b : m_config.puml().before)
ostr << b << std::endl;
for (const auto &sf : m_config.start_from) {
for (const auto &sf : m_config.start_from()) {
if (sf.location_type == source_location::location_t::function) {
std::uint_least64_t start_from;
for (const auto &[k, v] : m_model.sequences) {
@@ -117,7 +117,7 @@ void generator::generate(std::ostream &ostr) const
continue;
}
}
for (const auto &a : m_config.puml.after)
for (const auto &a : m_config.puml().after)
ostr << a << std::endl;
ostr << "@enduml" << std::endl;
@@ -147,7 +147,7 @@ clanguml::sequence_diagram::model::diagram generate(
// Get all translation units matching the glob from diagram
// configuration
std::vector<std::string> translation_units{};
for (const auto &g : diagram.glob) {
for (const auto &g : diagram.glob()) {
spdlog::debug("Processing glob: {}", g);
const auto matches = glob::rglob(g);
std::copy(matches.begin(), matches.end(),