Added aliased template handling

This commit is contained in:
Bartek Kryza
2021-04-17 18:12:56 +02:00
parent dd5befa89b
commit 4f0302496e
9 changed files with 363 additions and 170 deletions

View File

@@ -71,8 +71,11 @@ std::string full_name(const cppast::cpp_type &t,
if (!inside_class)
t_ns = ns(t, idx);
if (t_ns.size() > 0)
return t_ns + "::" + cppast::to_string(t);
auto t_name = cppast::to_string(t);
if (t_ns.size() > 0 &&
t_name.substr(0, t_name.find("<")).find("::") == std::string::npos)
return t_ns + "::" + t_name;
return cppast::to_string(t);
}
@@ -107,9 +110,38 @@ bool is_inside_class(const cppast::cpp_entity &e)
std::string ns(const cppast::cpp_type &t, const cppast::cpp_entity_index &idx)
{
if (t.kind() == cppast::cpp_type_kind::user_defined_t &&
(static_cast<const cppast::cpp_user_defined_type &>(t)
.entity()
.get(idx)
.size() > 0)) {
// If this is a user defined type - return the namespace of the
// entity
return ns(static_cast<const cppast::cpp_user_defined_type &>(t)
.entity()
.get(idx)[0]
.get());
}
else if (t.kind() == cppast::cpp_type_kind::template_instantiation_t) {
if (static_cast<const cppast::cpp_template_instantiation_type &>(t)
.primary_template()
.get(idx)
.size() > 0)
return ns(
static_cast<const cppast::cpp_template_instantiation_type &>(t)
.primary_template()
.get(idx)[0]
.get());
else
return "";
}
else {
auto canon = cppast::to_string(t.canonical());
auto full_name = canon.substr(0, canon.find("<"));
if (canon.find("type-parameter-") == std::string::npos) {
if (full_name.empty()) {
return "";
}
else if (canon.find("type-parameter-") == std::string::npos) {
// This is an easy case, canonical representation contains full
// namespace
auto ns_toks = clanguml::util::split(full_name, "::");
@@ -119,14 +151,15 @@ std::string ns(const cppast::cpp_type &t, const cppast::cpp_entity_index &idx)
"{}", fmt::join(ns_toks.begin(), ns_toks.end(), "::"));
}
else {
// This is a bug/feature in libclang, where canonical representation of
// a template type with incomplete specialization doesn't have a full
// namespace We have to extract it from te primary template
// This is a bug/feature in libclang, where canonical representation
// of a template type with incomplete specialization doesn't have a
// full namespace. We have to extract it from te primary template
const auto &primary_template =
static_cast<const cppast::cpp_template_instantiation_type &>(t)
.primary_template();
return ns(primary_template.get(idx)[0].get());
}
}
}
std::string fully_prefixed(const cppast::cpp_entity &e)

View File

@@ -168,8 +168,7 @@ public:
void add_type_alias(type_alias &&ta)
{
spdlog::debug(
"Adding class alias: {} -> {}", ta.alias, ta.underlying_type);
LOG_DBG("Adding class alias: {} -> {}", ta.alias, ta.underlying_type);
type_aliases[ta.alias] = std::move(ta);
}
@@ -210,7 +209,7 @@ public:
return fmt::format("{}", fmt::join(res, " "));
});
ostr << fmt::format("<{}>", fmt::join(tnames, ", "));
ostr << fmt::format("<{}>", fmt::join(tnames, ","));
}
return ostr.str();
@@ -249,29 +248,28 @@ struct diagram {
void add_type_alias(type_alias &&ta)
{
spdlog::debug(
"Adding global alias: {} -> {}", ta.alias, ta.underlying_type);
LOG_DBG("Adding global alias: {} -> {}", ta.alias, ta.underlying_type);
type_aliases[ta.alias] = std::move(ta);
}
void add_class(class_ &&c)
{
spdlog::debug("Adding class: {}, {}", c.name, c.usr);
LOG_DBG("Adding class: {}, {}", c.name, c.usr);
if (!has_class(c.usr))
classes.emplace_back(std::move(c));
else
spdlog::debug("Class {} already in the model", c.name);
LOG_DBG("Class {} already in the model", c.name);
}
void add_enum(enum_ &&e)
{
spdlog::debug("Adding enum: {}", e.name);
LOG_DBG("Adding enum: {}", e.name);
auto it = std::find(enums.begin(), enums.end(), e);
if (it == enums.end())
enums.emplace_back(std::move(e));
else
spdlog::debug("Enum {} already in the model", e.name);
LOG_DBG("Enum {} already in the model", e.name);
}
std::string to_alias(const std::vector<std::string> &using_namespaces,

File diff suppressed because it is too large Load Diff

View File

@@ -30,8 +30,10 @@
#include <cppast/cpp_template_parameter.hpp>
#include <cppast/cpp_type.hpp>
#include <cppast/visitor.hpp>
#include <type_safe/reference.hpp>
#include <functional>
#include <map>
#include <memory>
#include <string>
@@ -49,10 +51,78 @@ struct tu_context {
{
}
bool has_type_alias(const std::string &full_name) const
{
bool res = alias_index.find(full_name) != alias_index.end();
LOG_DBG("Alias {} {} found in index", full_name, res ? "" : "not");
return res;
}
void add_type_alias(const std::string &full_name,
type_safe::object_ref<const cppast::cpp_type> &&ref)
{
if (!has_type_alias(full_name)) {
LOG_DBG("Stored type alias: {} -> {} ", full_name,
cppast::to_string(ref.get()));
alias_index.emplace(full_name, std::move(ref));
}
}
type_safe::object_ref<const cppast::cpp_type> get_type_alias(
const std::string &full_name) const
{
assert(has_type_alias(full_name));
return alias_index.at(full_name);
}
type_safe::object_ref<const cppast::cpp_type> get_type_alias_final(
const cppast::cpp_type &t) const
{
const auto fn =
cx::util::full_name(cppast::remove_cv(t), entity_index, false);
if (has_type_alias(fn)) {
return get_type_alias_final(alias_index.at(fn).get());
}
return type_safe::ref(t);
}
bool has_type_alias_template(const std::string &full_name) const
{
bool res =
alias_template_index.find(full_name) != alias_template_index.end();
LOG_DBG(
"Alias template {} {} found in index", full_name, res ? "" : "not");
return res;
}
void add_type_alias_template(const std::string &full_name,
type_safe::object_ref<const cppast::cpp_type> &&ref)
{
if (!has_type_alias_template(full_name)) {
LOG_DBG("Stored type alias template for: {} ", full_name);
alias_template_index.emplace(full_name, std::move(ref));
}
}
type_safe::object_ref<const cppast::cpp_type> get_type_alias_template(
const std::string &full_name) const
{
assert(has_type_alias_template(full_name));
return alias_template_index.at(full_name);
}
std::vector<std::string> namespace_;
cppast::cpp_entity_index &entity_index;
clanguml::model::class_diagram::diagram &d;
const clanguml::config::class_diagram &config;
std::map<std::string, type_safe::object_ref<const cppast::cpp_type>>
alias_index;
std::map<std::string, type_safe::object_ref<const cppast::cpp_type>>
alias_template_index;
};
template <typename T> struct element_visitor_context {
@@ -87,6 +157,10 @@ public:
clanguml::model::class_diagram::class_ &c,
cppast::cpp_access_specifier_kind as);
void process_field_with_template_instantiation(
const cppast::cpp_member_variable &mv, const cppast::cpp_type &tr,
clanguml::model::class_diagram::class_ &c);
void process_static_field(const cppast::cpp_variable &mv,
clanguml::model::class_diagram::class_ &c,
cppast::cpp_access_specifier_kind as);
@@ -138,9 +212,10 @@ public:
private:
clanguml::model::class_diagram::class_ build_template_instantiation(
const cppast::cpp_entity &e,
const cppast::cpp_template_instantiation_type &t);
const cppast::cpp_type &resolve_alias(const cppast::cpp_type &t);
tu_context ctx;
};
}

View File

@@ -45,8 +45,8 @@ TEST_CASE("t00008", "[test-case][class]")
// TODO: add option to resolve using declared types
// REQUIRE_THAT(puml, IsClassTemplate("A", "T, P, bool (*)(int, int), int
// N"));
REQUIRE_THAT(puml, IsClassTemplate("A", "T, P, CMP, int N"));
REQUIRE_THAT(puml, IsClassTemplate("B", "T, C<>"));
REQUIRE_THAT(puml, IsClassTemplate("A", "T,P,CMP,int N"));
REQUIRE_THAT(puml, IsClassTemplate("B", "T,C<>"));
REQUIRE_THAT(puml, IsField(Public("T value")));
REQUIRE_THAT(puml, IsField(Public("T* pointer")));

View File

@@ -42,17 +42,17 @@ TEST_CASE("t00010", "[test-case][class]")
REQUIRE_THAT(puml, StartsWith("@startuml"));
REQUIRE_THAT(puml, EndsWith("@enduml\n"));
REQUIRE_THAT(puml, IsClassTemplate("A", "T, P"));
REQUIRE_THAT(puml, IsClassTemplate("A", "T,P"));
REQUIRE_THAT(puml, IsClassTemplate("B", "T"));
REQUIRE_THAT(puml, IsField(Public("A<T,std::string> astring")));
REQUIRE_THAT(puml, IsField(Public("B<int> aintstring")));
REQUIRE_THAT(puml, IsInstantiation(_A("A<T, P>"), _A("A<T, std::string>")));
REQUIRE_THAT(puml, IsInstantiation(_A("A<T,P>"), _A("A<T,std::string>")));
REQUIRE_THAT(puml, IsInstantiation(_A("B<T>"), _A("B<int>")));
REQUIRE_THAT(
puml, IsComposition(_A("B<T>"), _A("A<T, std::string>"), "astring"));
puml, IsComposition(_A("B<T>"), _A("A<T,std::string>"), "astring"));
REQUIRE_THAT(puml, IsComposition(_A("C"), _A("B<int>"), "aintstring"));
save_puml(

View File

@@ -42,17 +42,15 @@ TEST_CASE("t00012", "[test-case][class]")
REQUIRE_THAT(puml, StartsWith("@startuml"));
REQUIRE_THAT(puml, EndsWith("@enduml\n"));
REQUIRE_THAT(puml, IsClassTemplate("A", "T, Ts..."));
REQUIRE_THAT(puml, IsClassTemplate("A", "T,Ts..."));
REQUIRE_THAT(puml, IsClassTemplate("B", "int Is..."));
REQUIRE_THAT(puml, IsInstantiation(_A("B<int Is...>"), _A("B<3, 2, 1>")));
REQUIRE_THAT(
puml, IsInstantiation(_A("B<int Is...>"), _A("B<1, 1, 1, 1>")));
REQUIRE_THAT(puml, IsInstantiation(_A("B<int Is...>"), _A("B<3,2,1>")));
REQUIRE_THAT(puml, IsInstantiation(_A("B<int Is...>"), _A("B<1,1,1,1>")));
REQUIRE_THAT(puml,
IsInstantiation(_A("C<T, int Is...>"),
IsInstantiation(_A("C<T,int Is...>"),
_A("C<std::map<int,"
"std::vector<std::vector<std::vector<std::string>>>>, 3, 3, "
"3>")));
"std::vector<std::vector<std::vector<std::string>>>>,3,3,3>")));
save_puml(
"./" + config.output_directory + "/" + diagram->name + ".puml", puml);

View File

@@ -5,7 +5,12 @@
#include <string>
#include <type_traits>
#include <variant>
#include <vector>
/**
* These should not be include as they are not
* in ns clanguml::t00014
*/
template <typename T> struct clanguml_t00014_A {
T value;
};
@@ -22,15 +27,25 @@ template <typename T, typename P> struct A {
template <typename T> using AString = A<T, std::string>;
struct B {
std::string value;
};
using BVector = std::vector<B>;
using BVector2 = BVector;
using AIntString = AString<int>;
using AStringString = AString<std::string>;
using BStringString = AStringString;
class R {
using BStringString = AStringString;
//clang-uml: tinst A<T, std::string>
A<bool, std::string> boolstring;
AString<float> floatstring;
AIntString intstring;
BStringString stringstring;
AStringString stringstring;
BVector bs;
BVector2 bs2;
};
}
}

View File

@@ -40,26 +40,23 @@ TEST_CASE("t00014", "[test-case][class]")
REQUIRE_THAT(puml, StartsWith("@startuml"));
REQUIRE_THAT(puml, EndsWith("@enduml\n"));
REQUIRE_THAT(puml, IsClassTemplate("A", "T, P"));
/*
REQUIRE_THAT(puml, IsClass(_A("B")));
REQUIRE_THAT(puml, IsClass(_A("C")));
REQUIRE_THAT(puml, IsClass(_A("D")));
REQUIRE_THAT(puml, !IsDependency(_A("R"), _A("R")));
REQUIRE_THAT(puml, IsDependency(_A("R"), _A("A")));
REQUIRE_THAT(puml, IsDependency(_A("R"), _A("B")));
REQUIRE_THAT(puml, IsDependency(_A("R"), _A("C")));
REQUIRE_THAT(puml, IsDependency(_A("R"), _A("D")));
REQUIRE_THAT(puml, IsDependency(_A("D"), _A("R")));
REQUIRE_THAT(puml, IsDependency(_A("R"), _A("E<T>")));
REQUIRE_THAT(puml, IsDependency(_A("R"), _A("E<int>")));
REQUIRE_THAT(puml, IsInstantiation(_A("E<T>"), _A("E<int>")));
REQUIRE_THAT(puml, IsInstantiation(_A("E<T>"), _A("E<std::string>")));
REQUIRE_THAT(puml, IsComposition(_A("R"), _A("E<std::string>"), "estring"));
REQUIRE_THAT(puml, IsDependency(_A("R"), _A("ABCD::F<T>")));
REQUIRE_THAT(puml, IsInstantiation(_A("ABCD::F<T>"), _A("ABCD::F<int>")));
REQUIRE_THAT(puml, IsDependency(_A("R"), _A("ABCD::F<int>")));
*/
REQUIRE_THAT(puml, IsClassTemplate("A", "T,P"));
REQUIRE_THAT(puml, IsClassTemplate("A", "T,std::string"));
REQUIRE_THAT(puml, IsClassTemplate("A", "bool,std::string"));
REQUIRE_THAT(puml, IsClassTemplate("AString", "float"));
REQUIRE_THAT(puml, IsInstantiation(_A("A<T,P>"), _A("A<T,std::string>")));
// TODO
// REQUIRE_THAT(puml, IsInstantiation(_A("A<T,std::string>"),
// _A("A<bool,std::string>")));
REQUIRE_THAT(
puml, IsInstantiation(_A("A<T,std::string>"), _A("AString<float>")));
REQUIRE_THAT(
puml, IsComposition(_A("R"), _A("A<bool,std::string>"), "boolstring"));
REQUIRE_THAT(
puml, IsComposition(_A("R"), _A("AString<float>"), "floatstring"));
REQUIRE_THAT(puml, IsComposition(_A("R"), _A("B"), "bs"));
REQUIRE_THAT(puml, IsComposition(_A("R"), _A("B"), "bs2"));
save_puml(
"./" + config.output_directory + "/" + diagram->name + ".puml", puml);