Ported template instantiation handling to cppast

This commit is contained in:
Bartek Kryza
2021-03-28 22:21:41 +02:00
parent 1daf611329
commit b61143a5b2
12 changed files with 642 additions and 622 deletions

View File

@@ -44,7 +44,7 @@ std::string full_name(const cppast::cpp_entity &e);
std::string fully_prefixed(const cppast::cpp_entity &e);
const cppast::cpp_type& unreferenced(const cppast::cpp_type &t);
const cppast::cpp_type &unreferenced(const cppast::cpp_type &t);
} // namespace util
} // namespace cx

View File

@@ -50,8 +50,6 @@ std::string to_string(relationship_t r)
return "invalid";
}
}
}
}
}

View File

@@ -77,8 +77,8 @@ void tu_visitor::operator()(const cppast::cpp_entity &file)
}
if (e.kind() == cppast::cpp_entity_kind::class_t) {
spdlog::debug("'{}' - {}", cx::util::full_name(e),
cppast::to_string(e.kind()));
spdlog::debug("========== Visiting '{}' - {}",
cx::util::full_name(e), cppast::to_string(e.kind()));
auto &cls = static_cast<const cppast::cpp_class &>(e);
@@ -86,8 +86,8 @@ void tu_visitor::operator()(const cppast::cpp_entity &file)
process_class_declaration(cls);
}
else if (e.kind() == cppast::cpp_entity_kind::enum_t) {
spdlog::debug("'{}' - {}", cx::util::full_name(e),
cppast::to_string(e.kind()));
spdlog::debug("========== Visiting '{}' - {}",
cx::util::full_name(e), cppast::to_string(e.kind()));
auto &enm = static_cast<const cppast::cpp_enum &>(e);
@@ -146,6 +146,7 @@ void tu_visitor::process_class_declaration(const cppast::cpp_class &cls)
}
else if (child.kind() == cppast::cpp_entity_kind::member_variable_t) {
auto &mv = static_cast<const cppast::cpp_member_variable &>(child);
spdlog::debug("Found member variable {}", mv.name());
process_field(mv, c, last_access_specifier);
}
else if (child.kind() == cppast::cpp_entity_kind::variable_t) {
@@ -171,8 +172,7 @@ void tu_visitor::process_class_declaration(const cppast::cpp_class &cls)
else if (child.kind() == cppast::cpp_entity_kind::friend_t) {
auto &fr = static_cast<const cppast::cpp_friend &>(child);
spdlog::debug("Found friend declaration: {}, {}",
child.name(),
spdlog::debug("Found friend declaration: {}, {}", child.name(),
child.scope_name() ? child.scope_name().value().name()
: "<no-scope>");
@@ -187,8 +187,8 @@ void tu_visitor::process_class_declaration(const cppast::cpp_class &cls)
process_friend(fr, c);
}
else {
spdlog::debug("Found some other class child: {} ({})",
child.name(), cppast::to_string(child.kind()));
spdlog::debug("Found some other class child: {} ({})", child.name(),
cppast::to_string(child.kind()));
}
}
@@ -217,10 +217,13 @@ void tu_visitor::process_class_declaration(const cppast::cpp_class &cls)
// Process class template arguments
if (cppast::is_templated(cls)) {
spdlog::debug("Processing class template parameters...");
auto scope = cppast::cpp_scope_name(type_safe::ref(cls));
for (const auto &tp : scope.template_parameters()) {
if (tp.kind() ==
cppast::cpp_entity_kind::template_type_parameter_t) {
spdlog::debug(
"Processing template type parameter {}", tp.name());
process_template_type_parameter(
static_cast<const cppast::cpp_template_type_parameter &>(
tp),
@@ -228,6 +231,8 @@ void tu_visitor::process_class_declaration(const cppast::cpp_class &cls)
}
else if (tp.kind() ==
cppast::cpp_entity_kind::non_type_template_parameter_t) {
spdlog::debug(
"Processing template nontype parameter {}", tp.name());
process_template_nontype_parameter(
static_cast<
const cppast::cpp_non_type_template_parameter &>(tp),
@@ -235,6 +240,8 @@ void tu_visitor::process_class_declaration(const cppast::cpp_class &cls)
}
else if (tp.kind() ==
cppast::cpp_entity_kind::template_template_parameter_t) {
spdlog::debug(
"Processing template template parameter {}", tp.name());
process_template_template_parameter(
static_cast<
const cppast::cpp_template_template_parameter &>(tp),
@@ -281,7 +288,14 @@ void tu_visitor::process_field(const cppast::cpp_member_variable &mv, class_ &c,
m.is_static = false;
const auto &tr = cx::util::unreferenced(mv.type());
spdlog::debug(
"Processing field with unreferenced type of kind {}", tr.kind());
if (tr.kind() == cppast::cpp_type_kind::template_instantiation_t) {
spdlog::debug("Processing field with template instatiation type {}",
cppast::to_string(tr));
const auto &template_instantiation_type =
static_cast<const cppast::cpp_template_instantiation_type &>(tr);
if (template_instantiation_type.primary_template()
@@ -325,6 +339,11 @@ void tu_visitor::process_field(const cppast::cpp_member_variable &mv, class_ &c,
}
}
}
else if (tr.kind() == cppast::cpp_type_kind::unexposed_t) {
spdlog::debug(
"Processing field with unexposed type {}", cppast::to_string(tr));
// TODO
}
if (mv.type().kind() != cppast::cpp_type_kind::builtin_t) {
std::vector<std::pair<std::string, relationship_t>> relationships;
@@ -614,6 +633,7 @@ void tu_visitor::find_relationships(const cppast::cpp_type &t_,
}
else {
for (const auto &arg : args) {
if (arg.type())
find_relationships(
arg.type().value(), relationships, relationship_type);
}
@@ -675,6 +695,11 @@ class_ tu_visitor::build_template_instantiation(const cppast::cpp_entity &e,
ct.type =
static_cast<const cppast::cpp_literal_expression &>(exp)
.value();
else if (exp.kind() == cppast::cpp_expression_kind::unexposed_t)
ct.type =
static_cast<const cppast::cpp_unexposed_expression &>(exp)
.expression()
.as_string();
}
spdlog::debug("Adding template argument '{}'", ct.type);

View File

@@ -85,8 +85,7 @@ static enum CXChildVisitResult translation_unit_visitor(
unsigned int line{};
unsigned int column{};
unsigned int offset{};
clang_getFileLocation(
cursor.location(), &f, &line, &column, &offset);
clang_getFileLocation(cursor.location(), &f, &line, &column, &offset);
std::string file{clang_getCString(clang_getFileName(f))};
auto &d = ctx->d;
@@ -103,27 +102,25 @@ static enum CXChildVisitResult translation_unit_visitor(
"::" + ctx->current_method.spelling() + "()";
}
else {
caller = ctx->current_method.semantic_parent()
.fully_qualified();
caller =
ctx->current_method.semantic_parent().fully_qualified();
}
auto caller_usr = ctx->current_method.usr();
// Get called object
auto callee =
referenced.semantic_parent().fully_qualified();
auto callee = referenced.semantic_parent().fully_qualified();
auto callee_usr = referenced.semantic_parent().usr();
// Get called method
auto called_message = cursor.spelling();
// Found method call: CXCursorKind () const
spdlog::debug(
"Adding method call at line {}:{} to diagram {}"
spdlog::debug("Adding method call at line {}:{} to diagram {}"
"\n\tCURRENT_METHOD: {}\n\tFROM: '{}'\n\tTO: "
"{}\n\tMESSAGE: {}\n\tFROM_USR: {}\n\tTO_USR: "
"{}\n\tRETURN_TYPE: {}",
file, line, d.name, ctx->current_method.spelling(),
caller, callee, called_message, caller_usr, callee_usr,
file, line, d.name, ctx->current_method.spelling(), caller,
callee, called_message, caller_usr, callee_usr,
referenced.type().result_type().spelling());
message m;

View File

@@ -11449,11 +11449,10 @@ public:
case Colour::BrightRed:
return setTextAttribute(FOREGROUND_INTENSITY | FOREGROUND_RED);
case Colour::BrightGreen:
return setTextAttribute(
FOREGROUND_INTENSITY | FOREGROUND_GREEN);
return setTextAttribute(FOREGROUND_INTENSITY | FOREGROUND_GREEN);
case Colour::BrightWhite:
return setTextAttribute(FOREGROUND_INTENSITY |
FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE);
return setTextAttribute(FOREGROUND_INTENSITY | FOREGROUND_GREEN |
FOREGROUND_RED | FOREGROUND_BLUE);
case Colour::BrightYellow:
return setTextAttribute(
FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
@@ -12966,8 +12965,8 @@ bool WithinUlpsMatcher::match(double const &matchee) const
{
switch (m_type) {
case FloatingPointKind::Float:
return almostEqualUlps<float>(static_cast<float>(matchee),
static_cast<float>(m_target), m_ulps);
return almostEqualUlps<float>(
static_cast<float>(matchee), static_cast<float>(m_target), m_ulps);
case FloatingPointKind::Double:
return almostEqualUlps<double>(matchee, m_target, m_ulps);
default:

View File

@@ -56,7 +56,8 @@ TEST_CASE("t00003", "[test-case][class]")
REQUIRE_THAT(puml, IsField(Public("int public_member")));
REQUIRE_THAT(puml, IsField(Protected("int protected_member")));
REQUIRE_THAT(puml, IsField(Private("int private_member")));
REQUIRE_THAT(puml, IsField(Static(Public("unsigned long const auto_member"))));
REQUIRE_THAT(
puml, IsField(Static(Public("unsigned long const auto_member"))));
REQUIRE_THAT(puml, IsField(Private("int a")));
REQUIRE_THAT(puml, IsField(Private("int b")));

View File

@@ -48,8 +48,7 @@ TEST_CASE("t00009", "[test-case][class]")
REQUIRE_THAT(puml, IsField(Public("T value")));
REQUIRE_THAT(puml, IsField(Public("A<int> aint")));
REQUIRE_THAT(puml, IsField(Public("A<std::string>* astring")));
REQUIRE_THAT(
puml, IsField(Public("A<std::vector<std::string>>& avector")));
REQUIRE_THAT(puml, IsField(Public("A<std::vector<std::string>>& avector")));
REQUIRE_THAT(puml, IsInstantiation(_A("A<T>"), _A("A<int>")));
REQUIRE_THAT(puml, IsInstantiation(_A("A<T>"), _A("A<std::string>")));

View File

@@ -47,7 +47,7 @@ TEST_CASE("t00011", "[test-case][class]")
REQUIRE_THAT(puml, IsClass(_A("D<T>")));
REQUIRE_THAT(puml, IsFriend(_A("A"), _A("B")));
//REQUIRE_THAT(puml, IsFriend(_A("A"), _A("D<T>")));
// REQUIRE_THAT(puml, IsFriend(_A("A"), _A("D<T>")));
save_puml(
"./" + config.output_directory + "/" + diagram->name + ".puml", puml);

View File

@@ -3,6 +3,7 @@
#include <numeric>
#include <string>
#include <variant>
#include <vector>
namespace clanguml {
namespace t00012 {

View File

@@ -50,7 +50,7 @@ TEST_CASE("t00012", "[test-case][class]")
puml, IsInstantiation(_A("B<int Is...>"), _A("B<1, 1, 1, 1>")));
REQUIRE_THAT(puml,
IsInstantiation(_A("C<T, int Is...>"),
_A("C<std::map<int, "
_A("C<std::map<int,"
"std::vector<std::vector<std::vector<std::string>>>>, 3, 3, "
"3>")));