Merge pull request #2 from bkryza/handle-unnamed-enums
Handle unnamed enums
@@ -14,6 +14,7 @@
|
||||
* [t00013](./test_cases/t00013.md) - Template instantiation relationships
|
||||
* [t00014](./test_cases/t00014.md) - Alias template instantiation
|
||||
* [t00015](./test_cases/t00015.md) - Namespace fun
|
||||
* [t00016](./test_cases/t00016.md) - Unnamed enums and empty templates
|
||||
## Sequence diagrams
|
||||
* [t20001](./test_cases/t20001.md) - Basic sequence diagram
|
||||
## Configuration diagrams
|
||||
|
||||
|
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 15 KiB |
|
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 14 KiB |
|
Before Width: | Height: | Size: 10 KiB After Width: | Height: | Size: 10 KiB |
|
Before Width: | Height: | Size: 30 KiB After Width: | Height: | Size: 30 KiB |
|
Before Width: | Height: | Size: 62 KiB After Width: | Height: | Size: 62 KiB |
|
Before Width: | Height: | Size: 8.3 KiB After Width: | Height: | Size: 8.3 KiB |
|
Before Width: | Height: | Size: 18 KiB After Width: | Height: | Size: 18 KiB |
|
Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 16 KiB |
|
Before Width: | Height: | Size: 15 KiB After Width: | Height: | Size: 15 KiB |
|
Before Width: | Height: | Size: 8.8 KiB After Width: | Height: | Size: 8.8 KiB |
|
Before Width: | Height: | Size: 33 KiB After Width: | Height: | Size: 33 KiB |
|
Before Width: | Height: | Size: 45 KiB After Width: | Height: | Size: 45 KiB |
|
Before Width: | Height: | Size: 26 KiB After Width: | Height: | Size: 26 KiB |
|
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 11 KiB |
47
docs/test_cases/t00016.md
Normal 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
|
||||

|
||||
BIN
docs/test_cases/t00016_class.png
Normal file
|
After Width: | Height: | Size: 14 KiB |
|
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 14 KiB |
|
Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 16 KiB |
@@ -97,6 +97,18 @@ void tu_visitor::operator()(const cppast::cpp_entity &file)
|
||||
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) {
|
||||
LOG_DBG("========== Visiting '{}' - {}",
|
||||
cx::util::full_name(ctx.namespace_, e),
|
||||
@@ -112,6 +124,7 @@ void tu_visitor::operator()(const cppast::cpp_entity &file)
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (ctx.config.should_include(
|
||||
cx::util::fully_prefixed(ctx.namespace_, 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)
|
||||
{
|
||||
if (enm.name().empty()) {
|
||||
// Anonymous enum values should be rendered as class fields
|
||||
// with type enum
|
||||
return;
|
||||
}
|
||||
|
||||
enum_ e;
|
||||
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()) {
|
||||
// find nearest parent class, if any
|
||||
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));
|
||||
}
|
||||
|
||||
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;
|
||||
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);
|
||||
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) {
|
||||
auto &fr = static_cast<const cppast::cpp_friend &>(child);
|
||||
|
||||
@@ -296,33 +324,134 @@ void tu_visitor::process_class_declaration(const cppast::cpp_class &cls)
|
||||
if (cppast::is_templated(cls)) {
|
||||
LOG_DBG("Processing class template parameters...");
|
||||
auto scope = cppast::cpp_scope_name(type_safe::ref(cls));
|
||||
// Even if this is a template the scope.is_templated() returns
|
||||
// false when the template parameter list is empty
|
||||
if (scope.is_templated()) {
|
||||
for (const auto &tp : scope.template_parameters()) {
|
||||
if (tp.kind() ==
|
||||
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),
|
||||
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());
|
||||
LOG_DBG(
|
||||
"Processing template nontype parameter {}", tp.name());
|
||||
process_template_nontype_parameter(
|
||||
static_cast<
|
||||
const cppast::cpp_non_type_template_parameter &>(tp),
|
||||
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());
|
||||
LOG_DBG(
|
||||
"Processing template template parameter {}", tp.name());
|
||||
process_template_template_parameter(
|
||||
static_cast<
|
||||
const cppast::cpp_template_template_parameter &>(tp),
|
||||
const cppast::cpp_template_template_parameter &>(
|
||||
tp),
|
||||
c);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
LOG_WARN("Class {} is templated but it's scope {} is not - "
|
||||
"probably this is a specialization",
|
||||
cls.name(), scope.name());
|
||||
|
||||
// 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 {
|
||||
LOG_DBG("Skipping template class declaration which has only "
|
||||
"unexposed arguments but no tspec provided");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Find if class is contained in another class
|
||||
for (auto cur = cls.parent(); cur; cur = cur.value().parent()) {
|
||||
@@ -476,6 +605,21 @@ void tu_visitor::process_field(const cppast::cpp_member_variable &mv, class_ &c,
|
||||
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,
|
||||
cppast::cpp_access_specifier_kind as)
|
||||
{
|
||||
|
||||
@@ -149,10 +149,16 @@ public:
|
||||
|
||||
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_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,
|
||||
clanguml::model::class_diagram::class_ &c,
|
||||
cppast::cpp_access_specifier_kind as);
|
||||
|
||||
12
tests/t00016/.clanguml
Normal 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
@@ -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
@@ -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);
|
||||
}
|
||||
@@ -119,6 +119,7 @@ using namespace clanguml::test::matchers;
|
||||
#include "t00013/test_case.h"
|
||||
#include "t00014/test_case.h"
|
||||
#include "t00015/test_case.h"
|
||||
#include "t00016/test_case.h"
|
||||
|
||||
//
|
||||
// Sequence diagram tests
|
||||
|
||||
@@ -42,6 +42,9 @@ test_cases:
|
||||
- name: t00015
|
||||
title: Namespace fun
|
||||
description:
|
||||
- name: t00016
|
||||
title: Unnamed enums and empty templates
|
||||
description:
|
||||
Sequence diagrams:
|
||||
- name: t20001
|
||||
title: Basic sequence diagram
|
||||
|
||||