Added basic test case for sequence diagrams with class template specializations

This commit is contained in:
Bartek Kryza
2022-11-27 12:51:02 +01:00
parent c7bfcbd66f
commit e586c9d062
5 changed files with 137 additions and 113 deletions

View File

@@ -201,7 +201,7 @@ bool translation_unit_visitor::VisitClassTemplateSpecializationDecl(
if (!diagram().should_include(cls->getQualifiedNameAsString())) if (!diagram().should_include(cls->getQualifiedNameAsString()))
return true; return true;
LOG_DBG("= Visiting template specialization declaration {} at {}", LOG_DBG("##### Visiting template specialization declaration {} at {}",
cls->getQualifiedNameAsString(), cls->getQualifiedNameAsString(),
cls->getLocation().printToString(source_manager())); cls->getLocation().printToString(source_manager()));
@@ -251,6 +251,10 @@ bool translation_unit_visitor::VisitCXXMethodDecl(clang::CXXMethodDecl *m)
nullptr) nullptr)
return true; return true;
LOG_DBG("= Processing method {} in class {} [{}]",
m->getQualifiedNameAsString(),
m->getParent()->getQualifiedNameAsString(), (void *)m->getParent());
call_expression_context_.update(m); call_expression_context_.update(m);
auto m_ptr = std::make_unique<sequence_diagram::model::method>( auto m_ptr = std::make_unique<sequence_diagram::model::method>(
@@ -262,45 +266,24 @@ bool translation_unit_visitor::VisitCXXMethodDecl(clang::CXXMethodDecl *m)
ns.pop_back(); ns.pop_back();
m_ptr->set_name(ns.name()); m_ptr->set_name(ns.name());
ns.pop_back(); ns.pop_back();
// m_ptr->set_namespace(ns);
if (call_expression_context_.current_class_decl_) { clang::Decl *parent_decl = m->getParent();
const auto &current_class =
diagram()
.get_participant<model::class_>(get_ast_local_id(
call_expression_context_.current_class_decl_->getID())
.value())
.value();
m_ptr->set_class_id(current_class.id()); if (call_expression_context_.current_class_template_decl_)
m_ptr->set_class_full_name(current_class.full_name(false)); parent_decl = call_expression_context_.current_class_template_decl_;
}
else if (call_expression_context_.current_class_template_decl_) {
const auto &current_template_class =
diagram()
.get_participant<model::class_>(
get_ast_local_id(call_expression_context_
.current_class_template_decl_->getID())
.value())
.value();
m_ptr->set_class_id(current_template_class.id()); call_expression_context_.dump();
m_ptr->set_class_full_name(current_template_class.full_name(false));
}
else {
const auto &current_template_specialization_class =
diagram()
.get_participant<model::class_>(get_ast_local_id(
call_expression_context_
.current_class_template_specialization_decl_->getID())
.value())
.value();
m_ptr->set_class_id(current_template_specialization_class.id()); LOG_DBG("Getting method's class with local id {}", parent_decl->getID());
m_ptr->set_class_full_name(
current_template_specialization_class.full_name(false));
}
const auto &method_class =
diagram()
.get_participant<model::class_>(
get_ast_local_id(parent_decl->getID()).value())
.value();
m_ptr->set_class_id(method_class.id());
m_ptr->set_class_full_name(method_class.full_name(false));
m_ptr->set_name( m_ptr->set_name(
diagram().participants.at(m_ptr->class_id())->full_name_no_ns() + diagram().participants.at(m_ptr->class_id())->full_name_no_ns() +
"::" + m->getNameAsString()); "::" + m->getNameAsString());
@@ -434,8 +417,6 @@ bool translation_unit_visitor::VisitCallExpr(clang::CallExpr *expr)
if (clang::dyn_cast_or_null<clang::ImplicitCastExpr>(expr)) if (clang::dyn_cast_or_null<clang::ImplicitCastExpr>(expr))
return true; return true;
call_expression_context_.dump();
if (!call_expression_context_.valid()) if (!call_expression_context_.valid())
return true; return true;
@@ -480,27 +461,6 @@ bool translation_unit_visitor::VisitCallExpr(clang::CallExpr *expr)
LOG_DBG("Callee is a template specialization declaration {}", LOG_DBG("Callee is a template specialization declaration {}",
callee_template_specialization->getQualifiedNameAsString()); callee_template_specialization->getQualifiedNameAsString());
if (!get_ast_local_id(callee_template_specialization->getID())) {
call_expression_context context_backup =
call_expression_context_;
// Since this visitor will overwrite the
// call_expression_context_ we need to back it up and restore it
// later
VisitClassTemplateSpecializationDecl(
const_cast<clang::ClassTemplateSpecializationDecl *>(
callee_template_specialization));
call_expression_context_ = context_backup;
diagram()
.get_participant<model::class_>(get_ast_local_id(
callee_template_specialization->getID())
.value())
.value()
.set_implicit(true);
}
const auto &participant = const auto &participant =
diagram() diagram()
.get_participant<model::class_>(get_ast_local_id( .get_participant<model::class_>(get_ast_local_id(
@@ -509,6 +469,7 @@ bool translation_unit_visitor::VisitCallExpr(clang::CallExpr *expr)
.value(); .value();
if (participant.is_implicit()) { if (participant.is_implicit()) {
/*
const auto *parent_template = const auto *parent_template =
callee_template_specialization->getSpecializedTemplate(); callee_template_specialization->getSpecializedTemplate();
@@ -525,6 +486,32 @@ bool translation_unit_visitor::VisitCallExpr(clang::CallExpr *expr)
m.to = common::to_id(parent_method_name); m.to = common::to_id(parent_method_name);
m.message_name = participant.full_name_no_ns() + m.message_name = participant.full_name_no_ns() +
"::" + method_decl->getNameAsString(); "::" + method_decl->getNameAsString();
*/
const auto specialization_method_name =
participant.full_name(false) +
"::" + method_decl->getNameAsString();
m.to = common::to_id(method_name);
m.message_name = method_decl->getNameAsString();
// Since this is an implicit instantiation it might not exist
// so we have to create this participant here and it to the
// diagram
if (!diagram()
.get_participant<model::class_>(m.to)
.has_value()) {
auto m_ptr =
std::make_unique<sequence_diagram::model::method>(
config().using_namespace());
m_ptr->set_id(m.to);
m_ptr->set_method_name(method_decl->getNameAsString());
m_ptr->set_name(method_decl->getNameAsString());
m_ptr->set_class_id(participant.id());
m_ptr->set_class_full_name(participant.full_name(false));
set_ast_local_id(method_decl->getID(), m_ptr->id());
diagram().add_active_participant(m_ptr->id());
diagram().add_participant(std::move(m_ptr));
}
} }
else { else {
const auto &specialization_participant = const auto &specialization_participant =
@@ -676,19 +663,14 @@ bool translation_unit_visitor::VisitCallExpr(clang::CallExpr *expr)
callee_function->getTemplateSpecializationArgs()->size() > 0) { callee_function->getTemplateSpecializationArgs()->size() > 0) {
f_ptr = build_function_template_instantiation(*callee_function); f_ptr = build_function_template_instantiation(*callee_function);
callee_name = f_ptr->full_name(false);
f_ptr->set_id(common::to_id(f_ptr->full_name(false))); f_ptr->set_id(common::to_id(f_ptr->full_name(false)));
set_ast_local_id(callee_function->getID(), f_ptr->id()); set_ast_local_id(callee_function->getID(), f_ptr->id());
} }
if (is_implicit) if (is_implicit) {
LOG_DBG("Processing implicit template specialization {}", LOG_DBG("Processing implicit template specialization {}",
f_ptr->full_name(false)); f_ptr->full_name(false));
// m.to_name =
// callee_function->getQualifiedNameAsString();
if (is_implicit) {
// If this is an implicit template specialization/instantiation // If this is an implicit template specialization/instantiation
// for now we just redirect the call to it's primary template // for now we just redirect the call to it's primary template
// (TODO: this is not correct in a general case) // (TODO: this is not correct in a general case)
@@ -1061,29 +1043,28 @@ void translation_unit_visitor::
// template arguments // template arguments
if (arg.getAsType()->getAs<clang::FunctionType>()) { if (arg.getAsType()->getAs<clang::FunctionType>()) {
for (const auto &param_type : // for (const auto &param_type :
arg.getAsType()->getAs<clang::FunctionProtoType>()->param_types()) { // arg.getAsType()->getAs<clang::FunctionProtoType>()->param_types()) {
//
if (!param_type->getAs<clang::RecordType>()) // if (!param_type->getAs<clang::RecordType>())
continue; // continue;
//
// auto classTemplateSpecialization = // auto classTemplateSpecialization =
// llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>( // llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
// param_type->getAsRecordDecl()); // param_type->getAsRecordDecl());
//
// if (classTemplateSpecialization) { // if (classTemplateSpecialization) {
// // Read arg info as needed. // // Read arg info as needed.
// auto nested_template_instantiation = // auto nested_template_instantiation =
// build_template_instantiation_from_class_template_specialization( // build_template_instantiation_from_class_template_specialization(
// *classTemplateSpecialization, // *classTemplateSpecialization,
// *param_type->getAs<clang::RecordType>(), // *param_type->getAs<clang::RecordType>(),
// diagram().should_include( // diagram().should_include(
// full_template_specialization_name) // full_template_specialization_name)
// ? // ? std::make_optional(&template_instantiation)
// std::make_optional(&template_instantiation) // : parent);
// : parent); // }
// } // }
}
} }
else if (arg.getAsType()->getAs<clang::TemplateSpecializationType>()) { else if (arg.getAsType()->getAs<clang::TemplateSpecializationType>()) {
const auto *nested_template_type = const auto *nested_template_type =

View File

@@ -122,10 +122,6 @@ struct call_expression_context {
function->getQualifiedNameAsString()) { function->getQualifiedNameAsString()) {
current_function_template_decl_ = nullptr; current_function_template_decl_ = nullptr;
} }
// else {
// call_expression_context_.current_class_method_ =
// process_class_method(method);
// }
} }
void update(clang::FunctionTemplateDecl *function_template) void update(clang::FunctionTemplateDecl *function_template)
@@ -180,6 +176,10 @@ public:
clanguml::sequence_diagram::model::diagram &diagram, clanguml::sequence_diagram::model::diagram &diagram,
const clanguml::config::sequence_diagram &config); const clanguml::config::sequence_diagram &config);
bool shouldVisitTemplateInstantiations() {
return true;
}
virtual bool VisitCallExpr(clang::CallExpr *expr); virtual bool VisitCallExpr(clang::CallExpr *expr);
virtual bool VisitCXXMethodDecl(clang::CXXMethodDecl *method); virtual bool VisitCXXMethodDecl(clang::CXXMethodDecl *method);

View File

@@ -32,27 +32,24 @@ TEST_CASE("t20004", "[test-case][sequence]")
AliasMatcher _A(puml); AliasMatcher _A(puml);
REQUIRE_THAT(puml, StartsWith("@startuml")); REQUIRE_THAT(puml, StartsWith("@startuml"));
REQUIRE_THAT(puml, HasCall(_A("main()"), _A("m1<float>()"), "m1<float>")); REQUIRE_THAT(puml, HasCall(_A("main()"), _A("m1<float>()"), "m1"));
REQUIRE_THAT(puml, !HasCall(_A("m1<float>()"), _A("m1<float>()"), "m2<T>")); REQUIRE_THAT(puml, !HasCall(_A("m1<float>()"), _A("m1<float>()"), "m2"));
REQUIRE_THAT( REQUIRE_THAT(puml, !HasCall(_A("m1<float>()"), _A("m1<float>()"), "m2"));
puml, !HasCall(_A("m1<float>()"), _A("m1<float>()"), "m2<float>"));
REQUIRE_THAT(puml, REQUIRE_THAT(puml, HasCall(_A("main()"), _A("m1<unsigned long>()"), "m1"));
HasCall(_A("main()"), _A("m1<unsigned long>()"), "m1<unsigned long>"));
REQUIRE_THAT(puml, REQUIRE_THAT(puml,
HasCall(_A("m1<unsigned long>()"), _A("m4<unsigned long>()"), HasCall(_A("m1<unsigned long>()"), _A("m4<unsigned long>()"),
"m4<unsigned long>")); "m4"));
REQUIRE_THAT(puml, REQUIRE_THAT(puml, HasCall(_A("main()"), _A("m1<std::string>()"), "m1"));
HasCall(_A("main()"), _A("m1<std::string>()"), "m1<std::string>"));
REQUIRE_THAT(puml, REQUIRE_THAT(puml,
HasCall(_A("m1<std::string>()"), _A("m2<std::string>()"), HasCall(_A("m1<std::string>()"), _A("m2<std::string>()"),
"m2<std::string>")); "m2"));
REQUIRE_THAT(puml, HasCall(_A("main()"), _A("m1<T>()"), "m1<int>")); REQUIRE_THAT(puml, HasCall(_A("main()"), _A("m1<int>()"), "m1"));
REQUIRE_THAT(puml, HasCall(_A("m1<T>()"), _A("m2<T>()"), "m2<T>")); REQUIRE_THAT(puml, HasCall(_A("m1<int>()"), _A("m2<int>()"), "m2"));
REQUIRE_THAT(puml, HasCall(_A("m2<T>()"), _A("m3<T>()"), "m3<T>")); REQUIRE_THAT(puml, HasCall(_A("m2<int>()"), _A("m3<int>()"), "m3"));
REQUIRE_THAT(puml, HasCall(_A("m3<T>()"), _A("m4<T>()"), "m4<T>")); REQUIRE_THAT(puml, HasCall(_A("m3<int>()"), _A("m4<int>()"), "m4"));
REQUIRE_THAT(puml, EndsWith("@enduml\n")); REQUIRE_THAT(puml, EndsWith("@enduml\n"));
save_puml( save_puml(

View File

@@ -4,20 +4,41 @@ namespace clanguml {
namespace t20006 { namespace t20006 {
template <typename T> struct A { template <typename T> struct A {
T a_int(T arg) { return arg + 1; } T a1(T arg) { return arg; }
T a_string(T arg) { return arg + "_string"; } T a2(T arg) { return arg + arg; }
}; };
template <typename T> struct B { template <typename T> struct B {
T b(T arg) { return a_.a_int(arg); } T b(T arg) { return a_.a1(arg); }
A<T> a_; A<T> a_;
}; };
template <> struct B<std::string> { template <> struct B<std::string> {
std::string b(std::string arg) { return a_.a_string(arg); } std::string b(std::string arg) { return a_.a2(arg); }
A<std::string> a_; A<std::string> a_;
}; };
template <typename T> struct AA {
void aa1(T t) { }
void aa2(T t) { }
};
template <typename T, typename F> struct BB {
void bb1(T t, F f) { aa_.aa1(t); }
void bb2(T t, F f) { aa_.aa2(t); }
AA<T> aa_;
};
template <typename T> struct BB<T, std::string> {
void bb1(T t, std::string f) { aa_.aa2(t); }
void bb2(T t, std::string f) { aa_.aa1(t); }
AA<T> aa_;
};
void tmain() void tmain()
{ {
B<int> bint; B<int> bint;
@@ -25,6 +46,15 @@ void tmain()
bint.b(1); bint.b(1);
bstring.b("bstring"); bstring.b("bstring");
BB<int, int> bbint;
BB<int, std::string> bbstring;
bbint.bb1(1, 1);
bbint.bb2(2, 2);
bbstring.bb1(1, "calling aa2");
bbstring.bb2(1, "calling aa1");
} }
} }
} }

View File

@@ -35,12 +35,28 @@ TEST_CASE("t20006", "[test-case][sequence]")
REQUIRE_THAT(puml, EndsWith("@enduml\n")); REQUIRE_THAT(puml, EndsWith("@enduml\n"));
// Check if all calls exist // Check if all calls exist
REQUIRE_THAT(puml, HasCall(_A("tmain()"), _A("B<T>"), "B<int>::b")); REQUIRE_THAT(puml, HasCall(_A("tmain()"), _A("B<int>"), "b"));
REQUIRE_THAT(puml, HasCall(_A("B<T>"), _A("A<T>"), "a_int")); REQUIRE_THAT(puml, HasCall(_A("B<int>"), _A("A<int>"), "a1"));
REQUIRE_THAT(puml, HasCall(_A("tmain()"), _A("B<std::string>"), "b")); REQUIRE_THAT(puml, HasCall(_A("tmain()"), _A("B<std::string>"), "b"));
REQUIRE_THAT(puml, REQUIRE_THAT(
HasCall(_A("B<std::string>"), _A("A<T>"), "A<std::string>::a_string")); puml, HasCall(_A("B<std::string>"), _A("A<std::string>"), "a2"));
REQUIRE_THAT(puml, HasCall(_A("tmain()"), _A("BB<int,int>"), "bb1"));
REQUIRE_THAT(puml, HasCall(_A("BB<int,int>"), _A("AA<int>"), "aa1"));
REQUIRE_THAT(puml, HasCall(_A("tmain()"), _A("BB<int,int>"), "bb2"));
REQUIRE_THAT(puml, HasCall(_A("BB<int,int>"), _A("AA<int>"), "aa2"));
REQUIRE_THAT(
puml, HasCall(_A("tmain()"), _A("BB<int,std::string>"), "bb1"));
REQUIRE_THAT(
puml, HasCall(_A("BB<int,std::string>"), _A("AA<int>"), "aa2"));
REQUIRE_THAT(
puml, HasCall(_A("tmain()"), _A("BB<int,std::string>"), "bb2"));
REQUIRE_THAT(
puml, HasCall(_A("BB<int,std::string>"), _A("AA<int>"), "aa1"));
save_puml( save_puml(
"./" + config.output_directory() + "/" + diagram->name + ".puml", puml); "./" + config.output_directory() + "/" + diagram->name + ".puml", puml);