Merge pull request #2 from bkryza/handle-unnamed-enums

Handle unnamed enums
This commit is contained in:
Bartek Kryza
2021-05-03 20:39:49 +02:00
committed by GitHub
26 changed files with 324 additions and 25 deletions

View File

@@ -14,6 +14,7 @@
* [t00013](./test_cases/t00013.md) - Template instantiation relationships * [t00013](./test_cases/t00013.md) - Template instantiation relationships
* [t00014](./test_cases/t00014.md) - Alias template instantiation * [t00014](./test_cases/t00014.md) - Alias template instantiation
* [t00015](./test_cases/t00015.md) - Namespace fun * [t00015](./test_cases/t00015.md) - Namespace fun
* [t00016](./test_cases/t00016.md) - Unnamed enums and empty templates
## Sequence diagrams ## Sequence diagrams
* [t20001](./test_cases/t20001.md) - Basic sequence diagram * [t20001](./test_cases/t20001.md) - Basic sequence diagram
## Configuration diagrams ## Configuration diagrams

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 62 KiB

After

Width:  |  Height:  |  Size: 62 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.3 KiB

After

Width:  |  Height:  |  Size: 8.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.8 KiB

After

Width:  |  Height:  |  Size: 8.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 33 KiB

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 45 KiB

After

Width:  |  Height:  |  Size: 45 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 26 KiB

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

47
docs/test_cases/t00016.md Normal file
View File

@@ -0,0 +1,47 @@
# t00016 - Unnamed enums and empty templates
## Config
```yaml
compilation_database_dir: ..
output_directory: puml
diagrams:
t00016_class:
type: class
glob:
- ../../tests/t00016/t00016.cc
using_namespace:
- clanguml::t00016
include:
namespaces:
- clanguml::t00016
```
## Source code
```cpp
namespace clanguml {
namespace t00016 {
template <typename> struct is_numeric {
enum { value = false };
};
template <> struct is_numeric<char> {
enum { value = true };
};
template <> struct is_numeric<unsigned char> {
enum { value = true };
};
template <> struct is_numeric<int> {
enum { value = true };
};
template <> struct is_numeric<bool> {
enum { value = false };
};
}
}
```
## Generated UML diagrams
![t00016_class](./t00016_class.png "Unnamed enums and empty templates")

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View File

@@ -97,6 +97,18 @@ void tu_visitor::operator()(const cppast::cpp_entity &file)
ctx.namespace_.pop_back(); ctx.namespace_.pop_back();
} }
} }
else if (e.kind() ==
cppast::cpp_entity_kind::class_template_specialization_t) {
LOG_DBG("========== Visiting '{}' - {}",
cx::util::full_name(ctx.namespace_, e),
cppast::to_string(e.kind()));
auto &tspec = static_cast<
const cppast::cpp_class_template_specialization &>(e);
process_class_declaration(
tspec.class_(), type_safe::ref(tspec));
}
else if (e.kind() == cppast::cpp_entity_kind::class_t) { else if (e.kind() == cppast::cpp_entity_kind::class_t) {
LOG_DBG("========== Visiting '{}' - {}", LOG_DBG("========== Visiting '{}' - {}",
cx::util::full_name(ctx.namespace_, e), cx::util::full_name(ctx.namespace_, e),
@@ -112,6 +124,7 @@ void tu_visitor::operator()(const cppast::cpp_entity &file)
return; return;
} }
} }
if (ctx.config.should_include( if (ctx.config.should_include(
cx::util::fully_prefixed(ctx.namespace_, cls))) cx::util::fully_prefixed(ctx.namespace_, cls)))
process_class_declaration(cls); process_class_declaration(cls);
@@ -170,6 +183,12 @@ void tu_visitor::operator()(const cppast::cpp_entity &file)
void tu_visitor::process_enum_declaration(const cppast::cpp_enum &enm) void tu_visitor::process_enum_declaration(const cppast::cpp_enum &enm)
{ {
if (enm.name().empty()) {
// Anonymous enum values should be rendered as class fields
// with type enum
return;
}
enum_ e; enum_ e;
e.name = cx::util::full_name(ctx.namespace_, enm); e.name = cx::util::full_name(ctx.namespace_, enm);
@@ -179,7 +198,7 @@ void tu_visitor::process_enum_declaration(const cppast::cpp_enum &enm)
} }
} }
// Find if class is contained in another class // Find if enum is contained in a class
for (auto cur = enm.parent(); cur; cur = cur.value().parent()) { for (auto cur = enm.parent(); cur; cur = cur.value().parent()) {
// find nearest parent class, if any // find nearest parent class, if any
if (cur.value().kind() == cppast::cpp_entity_kind::class_t) { if (cur.value().kind() == cppast::cpp_entity_kind::class_t) {
@@ -198,7 +217,8 @@ void tu_visitor::process_enum_declaration(const cppast::cpp_enum &enm)
ctx.d.add_enum(std::move(e)); ctx.d.add_enum(std::move(e));
} }
void tu_visitor::process_class_declaration(const cppast::cpp_class &cls) void tu_visitor::process_class_declaration(const cppast::cpp_class &cls,
type_safe::optional_ref<const cppast::cpp_template_specialization> tspec)
{ {
class_ c; class_ c;
c.is_struct = cls.class_kind() == cppast::cpp_class_kind::struct_t; c.is_struct = cls.class_kind() == cppast::cpp_class_kind::struct_t;
@@ -246,6 +266,14 @@ void tu_visitor::process_class_declaration(const cppast::cpp_class &cls)
auto &mc = static_cast<const cppast::cpp_destructor &>(child); auto &mc = static_cast<const cppast::cpp_destructor &>(child);
process_destructor(mc, c, last_access_specifier); process_destructor(mc, c, last_access_specifier);
} }
else if (child.kind() == cppast::cpp_entity_kind::enum_t) {
auto &en = static_cast<const cppast::cpp_enum &>(child);
if (en.name().empty()) {
// Here we only want to handle anonymous enums, regular nested
// enums are handled in the file-level visitor
process_anonymous_enum(en, c, last_access_specifier);
}
}
else if (child.kind() == cppast::cpp_entity_kind::friend_t) { else if (child.kind() == cppast::cpp_entity_kind::friend_t) {
auto &fr = static_cast<const cppast::cpp_friend &>(child); auto &fr = static_cast<const cppast::cpp_friend &>(child);
@@ -296,30 +324,131 @@ void tu_visitor::process_class_declaration(const cppast::cpp_class &cls)
if (cppast::is_templated(cls)) { if (cppast::is_templated(cls)) {
LOG_DBG("Processing class template parameters..."); LOG_DBG("Processing class template parameters...");
auto scope = cppast::cpp_scope_name(type_safe::ref(cls)); auto scope = cppast::cpp_scope_name(type_safe::ref(cls));
for (const auto &tp : scope.template_parameters()) { // Even if this is a template the scope.is_templated() returns
if (tp.kind() == // false when the template parameter list is empty
cppast::cpp_entity_kind::template_type_parameter_t) { if (scope.is_templated()) {
LOG_DBG("Processing template type parameter {}", tp.name()); for (const auto &tp : scope.template_parameters()) {
process_template_type_parameter( if (tp.kind() ==
static_cast<const cppast::cpp_template_type_parameter &>( cppast::cpp_entity_kind::template_type_parameter_t) {
tp), LOG_DBG("Processing template type parameter {}", tp.name());
c); process_template_type_parameter(
static_cast<
const cppast::cpp_template_type_parameter &>(tp),
c);
}
else if (tp.kind() ==
cppast::cpp_entity_kind::non_type_template_parameter_t) {
LOG_DBG(
"Processing template nontype parameter {}", tp.name());
process_template_nontype_parameter(
static_cast<
const cppast::cpp_non_type_template_parameter &>(
tp),
c);
}
else if (tp.kind() ==
cppast::cpp_entity_kind::template_template_parameter_t) {
LOG_DBG(
"Processing template template parameter {}", tp.name());
process_template_template_parameter(
static_cast<
const cppast::cpp_template_template_parameter &>(
tp),
c);
}
} }
else if (tp.kind() == }
cppast::cpp_entity_kind::non_type_template_parameter_t) { else {
LOG_DBG("Processing template nontype parameter {}", tp.name()); LOG_WARN("Class {} is templated but it's scope {} is not - "
process_template_nontype_parameter( "probably this is a specialization",
static_cast< cls.name(), scope.name());
const cppast::cpp_non_type_template_parameter &>(tp),
c); // Add specialization arguments
if (tspec) {
if (!tspec.value().arguments_exposed()) {
// Create template specialization with unexposed arguments
auto ua = tspec.value().unexposed_arguments().as_string();
// Naive parse of template arguments:
auto toks = util::split(ua, ",");
for (const auto &t : toks) {
class_template ct;
ct.type = t;
ct.default_value = "";
ct.is_variadic = false;
ct.name = "";
c.templates.emplace_back(std::move(ct));
const auto &primary_template_ref =
static_cast<const cppast::cpp_class_template &>(
tspec.value()
.primary_template()
.get(ctx.entity_index)[0]
.get())
.class_();
if (primary_template_ref.user_data()) {
auto base_template_usr = static_cast<const char *>(
primary_template_ref.user_data());
LOG_DBG("Primary template ref set to: {}",
base_template_usr);
// Add template specialization/instantiation
// relationship
class_relationship r;
r.type = relationship_t::kInstantiation;
r.label = "";
r.destination = base_template_usr;
c.add_relationship(std::move(r));
}
else {
LOG_WARN("No user data for base template {}",
primary_template_ref.name());
}
}
}
else {
for (auto &tp : tspec.value().parameters()) {
switch (tp.kind()) {
case cppast::cpp_entity_kind::
template_type_parameter_t: {
LOG_DBG("Processing template type parameter {}",
tp.name());
process_template_type_parameter(
static_cast<const cppast::
cpp_template_type_parameter &>(tp),
c);
} break;
case cppast::cpp_entity_kind::
non_type_template_parameter_t: {
LOG_DBG("Processing template nontype parameter {}",
tp.name());
process_template_nontype_parameter(
static_cast<const cppast::
cpp_non_type_template_parameter &>(tp),
c);
} break;
case cppast::cpp_entity_kind::
template_template_parameter_t: {
LOG_DBG("Processing template template parameter {}",
tp.name());
process_template_template_parameter(
static_cast<const cppast::
cpp_template_template_parameter &>(tp),
c);
} break;
default:
LOG_DBG("Unhandled template parameter "
"type {}",
cppast::to_string(tp.kind()));
break;
}
}
}
} }
else if (tp.kind() == else {
cppast::cpp_entity_kind::template_template_parameter_t) { LOG_DBG("Skipping template class declaration which has only "
LOG_DBG("Processing template template parameter {}", tp.name()); "unexposed arguments but no tspec provided");
process_template_template_parameter( return;
static_cast<
const cppast::cpp_template_template_parameter &>(tp),
c);
} }
} }
} }
@@ -476,6 +605,21 @@ void tu_visitor::process_field(const cppast::cpp_member_variable &mv, class_ &c,
c.members.emplace_back(std::move(m)); c.members.emplace_back(std::move(m));
} }
void tu_visitor::process_anonymous_enum(
const cppast::cpp_enum &en, class_ &c, cppast::cpp_access_specifier_kind as)
{
for (const auto &ev : en) {
if (ev.kind() == cppast::cpp_entity_kind::enum_value_t) {
class_member m;
m.name = ev.name();
m.type = "enum"; // TODO: Try to figure out real enum type
m.scope = detail::cpp_access_specifier_to_scope(as);
m.is_static = false;
c.members.emplace_back(std::move(m));
}
}
}
void tu_visitor::process_static_field(const cppast::cpp_variable &mv, class_ &c, void tu_visitor::process_static_field(const cppast::cpp_variable &mv, class_ &c,
cppast::cpp_access_specifier_kind as) cppast::cpp_access_specifier_kind as)
{ {

View File

@@ -149,10 +149,16 @@ public:
void operator()(const cppast::cpp_entity &file); void operator()(const cppast::cpp_entity &file);
void process_class_declaration(const cppast::cpp_class &cls); void process_class_declaration(const cppast::cpp_class &cls,
type_safe::optional_ref<const cppast::cpp_template_specialization>
tspec = nullptr);
void process_enum_declaration(const cppast::cpp_enum &enm); void process_enum_declaration(const cppast::cpp_enum &enm);
void process_anonymous_enum(const cppast::cpp_enum &en,
clanguml::model::class_diagram::class_ &c,
cppast::cpp_access_specifier_kind as);
void process_field(const cppast::cpp_member_variable &mv, void process_field(const cppast::cpp_member_variable &mv,
clanguml::model::class_diagram::class_ &c, clanguml::model::class_diagram::class_ &c,
cppast::cpp_access_specifier_kind as); cppast::cpp_access_specifier_kind as);

12
tests/t00016/.clanguml Normal file
View File

@@ -0,0 +1,12 @@
compilation_database_dir: ..
output_directory: puml
diagrams:
t00016_class:
type: class
glob:
- ../../tests/t00016/t00016.cc
using_namespace:
- clanguml::t00016
include:
namespaces:
- clanguml::t00016

24
tests/t00016/t00016.cc Normal file
View File

@@ -0,0 +1,24 @@
namespace clanguml {
namespace t00016 {
template <typename> struct is_numeric {
enum { value = false };
};
template <> struct is_numeric<char> {
enum { value = true };
};
template <> struct is_numeric<unsigned char> {
enum { value = true };
};
template <> struct is_numeric<int> {
enum { value = true };
};
template <> struct is_numeric<bool> {
enum { value = false };
};
}
}

61
tests/t00016/test_case.h Normal file
View File

@@ -0,0 +1,61 @@
/**
* tests/t00016/test_case.cc
*
* Copyright (c) 2021 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.
*/
TEST_CASE("t00016", "[test-case][class]")
{
auto [config, db] = load_config("t00016");
auto diagram = config.diagrams["t00016_class"];
REQUIRE(diagram->name == "t00016_class");
REQUIRE(diagram->include.namespaces.size() == 1);
REQUIRE_THAT(diagram->include.namespaces,
VectorContains(std::string{"clanguml::t00016"}));
REQUIRE(diagram->exclude.namespaces.size() == 0);
REQUIRE(diagram->should_include("clanguml::t00016::is_numeric"));
auto model = generate_class_diagram(db, diagram);
REQUIRE(model.name == "t00016_class");
auto puml = generate_class_puml(diagram, model);
AliasMatcher _A(puml);
REQUIRE_THAT(puml, StartsWith("@startuml"));
REQUIRE_THAT(puml, EndsWith("@enduml\n"));
REQUIRE_THAT(puml, IsClassTemplate("is_numeric", ""));
REQUIRE_THAT(puml, IsClassTemplate("is_numeric", "int"));
REQUIRE_THAT(puml, IsClassTemplate("is_numeric", "bool"));
REQUIRE_THAT(puml, IsClassTemplate("is_numeric", "char"));
REQUIRE_THAT(puml, IsClassTemplate("is_numeric", "unsigned char"));
REQUIRE_THAT(
puml, IsInstantiation(_A("is_numeric<>"), _A("is_numeric<int>")));
REQUIRE_THAT(
puml, IsInstantiation(_A("is_numeric<>"), _A("is_numeric<bool>")));
REQUIRE_THAT(
puml, IsInstantiation(_A("is_numeric<>"), _A("is_numeric<char>")));
REQUIRE_THAT(puml,
IsInstantiation(_A("is_numeric<>"), _A("is_numeric<unsigned char>")));
save_puml(
"./" + config.output_directory + "/" + diagram->name + ".puml", puml);
}

View File

@@ -119,6 +119,7 @@ using namespace clanguml::test::matchers;
#include "t00013/test_case.h" #include "t00013/test_case.h"
#include "t00014/test_case.h" #include "t00014/test_case.h"
#include "t00015/test_case.h" #include "t00015/test_case.h"
#include "t00016/test_case.h"
// //
// Sequence diagram tests // Sequence diagram tests

View File

@@ -42,6 +42,9 @@ test_cases:
- name: t00015 - name: t00015
title: Namespace fun title: Namespace fun
description: description:
- name: t00016
title: Unnamed enums and empty templates
description:
Sequence diagrams: Sequence diagrams:
- name: t20001 - name: t20001
title: Basic sequence diagram title: Basic sequence diagram