Generate enums as aliases

This commit is contained in:
Bartek Kryza
2021-05-22 14:10:50 +02:00
parent 6cd1abcd38
commit e1c6da5f2e
4 changed files with 60 additions and 7 deletions

View File

@@ -124,7 +124,7 @@ public:
} }
} }
void generate_aliases(const class_ &c, std::ostream &ostr) const void generate_alias(const class_ &c, std::ostream &ostr) const
{ {
std::string class_type{"class"}; std::string class_type{"class"};
if (c.is_abstract()) if (c.is_abstract())
@@ -135,6 +135,13 @@ public:
ostr << "\" as " << c.alias() << std::endl; ostr << "\" as " << c.alias() << std::endl;
} }
void generate_alias(const enum_ &e, std::ostream &ostr) const
{
ostr << "enum" << " \"" << e.full_name(m_config.using_namespace);
ostr << "\" as " << e.alias() << std::endl;
}
void generate(const class_ &c, std::ostream &ostr) const void generate(const class_ &c, std::ostream &ostr) const
{ {
std::string class_type{"class"}; std::string class_type{"class"};
@@ -266,7 +273,7 @@ public:
void generate(const enum_ &e, std::ostream &ostr) const void generate(const enum_ &e, std::ostream &ostr) const
{ {
ostr << "enum " << ns_relative(m_config.using_namespace, e.name) << " {" ostr << "enum " << e.alias() << " {"
<< std::endl; << std::endl;
for (const auto &enum_constant : e.constants) { for (const auto &enum_constant : e.constants) {
@@ -325,7 +332,12 @@ public:
if (m_config.should_include_entities("classes")) { if (m_config.should_include_entities("classes")) {
for (const auto &c : m_model.classes) { for (const auto &c : m_model.classes) {
generate_aliases(c, ostr); generate_alias(c, ostr);
ostr << std::endl;
}
for (const auto &e : m_model.enums) {
generate_alias(e, ostr);
ostr << std::endl; ostr << std::endl;
} }

View File

@@ -175,9 +175,9 @@ public:
void add_relationship(class_relationship &&cr) void add_relationship(class_relationship &&cr)
{ {
if (cr.destination.empty() || type_aliases.count(cr.destination) == 0) { if (cr.destination.empty()) {
LOG_WARN( LOG_WARN(
"Skipping relationship '{}' - {} - '{}' due to missing alias", "Skipping relationship '{}' - {} - '{}' due empty destination",
cr.destination, to_string(cr.type), usr); cr.destination, to_string(cr.type), usr);
return; return;
} }
@@ -240,6 +240,18 @@ struct enum_ : public element {
{ {
return l.name == r.name; return l.name == r.name;
} }
std::string full_name(
const std::vector<std::string> &using_namespaces) const
{
using namespace clanguml::util;
std::ostringstream ostr;
ostr << ns_relative(using_namespaces, name);
return ostr.str();
}
}; };
struct diagram { struct diagram {
@@ -289,6 +301,12 @@ struct diagram {
} }
} }
for (const auto &e : enums) {
if (e.full_name(using_namespaces) == full_name) {
return e.alias();
}
}
throw error::uml_alias_missing( throw error::uml_alias_missing(
fmt::format("Missing alias for {}", full_name)); fmt::format("Missing alias for {}", full_name));
} }

View File

@@ -33,6 +33,29 @@ public:
a->foo_c(); a->foo_c();
} }
private:
std::vector<A *> as;
};
//
// NOTE: libclang fails on:
//
// class D : public virtual B, public virtual C {
//
class E : virtual public B, virtual public C {
public:
void foo_a() override
{
for (auto a : as)
a->foo_a();
}
void foo_c() override
{
for (auto a : as)
a->foo_c();
}
private: private:
std::vector<A *> as; std::vector<A *> as;
}; };

View File

@@ -46,10 +46,10 @@ TEST_CASE("t00004", "[test-case][class]")
REQUIRE_THAT(puml, IsClass(_A("A"))); REQUIRE_THAT(puml, IsClass(_A("A")));
REQUIRE_THAT(puml, IsClass(_A("AA"))); REQUIRE_THAT(puml, IsClass(_A("AA")));
REQUIRE_THAT(puml, IsClass(_A("AAA"))); REQUIRE_THAT(puml, IsClass(_A("AAA")));
REQUIRE_THAT(puml, IsEnum("Lights")); REQUIRE_THAT(puml, IsEnum(_A("Lights")));
REQUIRE_THAT(puml, IsInnerClass(_A("A"), _A("AA"))); REQUIRE_THAT(puml, IsInnerClass(_A("A"), _A("AA")));
REQUIRE_THAT(puml, IsInnerClass(_A("AA"), _A("AAA"))); REQUIRE_THAT(puml, IsInnerClass(_A("AA"), _A("AAA")));
REQUIRE_THAT(puml, IsInnerClass(_A("AA"), "Lights")); REQUIRE_THAT(puml, IsInnerClass(_A("AA"), _A("Lights")));
REQUIRE_THAT(puml, (IsMethod<Public, Const>("foo"))); REQUIRE_THAT(puml, (IsMethod<Public, Const>("foo")));
REQUIRE_THAT(puml, (IsMethod<Public, Const>("foo2"))); REQUIRE_THAT(puml, (IsMethod<Public, Const>("foo2")));