Added template method specialization sequence diagram test case

This commit is contained in:
Bartek Kryza
2022-12-08 00:41:54 +01:00
parent de4e88a92d
commit 0a0b2a3d35
10 changed files with 157 additions and 29 deletions

View File

@@ -616,23 +616,28 @@ bool translation_unit_visitor::VisitCallExpr(clang::CallExpr *expr)
}
}
else {
if (!process_function_call_expression(m, expr))
return true;
}
if (!process_function_call_expression(m, expr)) {
// expr->dump();
LOG_DBG("Skipping call to unsupported type of call expression "
"at: {}",
expr->getBeginLoc().printToString(source_manager()));
//
// This crashes on LLVM <= 12, for now just return empty type
//
// const auto &return_type =
// function_call_expr->getCallReturnType(current_ast_context);
// m.return_type = return_type.getAsString();
m.return_type = "";
return true;
}
}
}
//
// This crashes on LLVM <= 12, for now just return empty type
//
// const auto &return_type =
// function_call_expr->getCallReturnType(current_ast_context);
// m.return_type = return_type.getAsString();
m.return_type = "";
if (m.from > 0 && m.to > 0) {
if (diagram().sequences.find(m.from) == diagram().sequences.end()) {
activity a;
// a.usr = m.from;
a.from = m.from;
diagram().sequences.insert({m.from, std::move(a)});
}
@@ -778,6 +783,10 @@ bool translation_unit_visitor::process_class_template_method_call_expression(
diagram().add_active_participant(
get_unique_id(template_declaration->getID()).value());
}
else {
LOG_WARN("Cannot generate call due to unresolvable "
"CXXDependentScopeMemberExpr");
}
return true;
}
@@ -845,14 +854,23 @@ bool translation_unit_visitor::process_unresolved_lookup_call_expression(
}
bool translation_unit_visitor::is_callee_valid_template_specialization(
const clang::CXXDependentScopeMemberExpr *dependent_member_callee) const
const clang::CXXDependentScopeMemberExpr *dependent_member_expr) const
{
return !dependent_member_callee->getBaseType().isNull() &&
dependent_member_callee->getBaseType()
->getAs<clang::TemplateSpecializationType>() &&
!dependent_member_callee->getBaseType()
const bool base_type_is_not_null =
!dependent_member_expr->getBaseType().isNull();
const bool base_type_is_specialization_type =
dependent_member_expr->getBaseType()
->getAs<clang::TemplateSpecializationType>() != nullptr;
const bool base_type_is_not_pointer_type =
base_type_is_specialization_type &&
!dependent_member_expr->getBaseType()
->getAs<clang::TemplateSpecializationType>()
->isPointerType();
return (base_type_is_not_null && base_type_is_specialization_type &&
base_type_is_not_pointer_type);
}
bool translation_unit_visitor::is_smart_pointer(
@@ -898,8 +916,8 @@ translation_unit_visitor::create_class_declaration(clang::CXXRecordDecl *cls)
int64_t local_id =
static_cast<const clang::RecordDecl *>(parent)->getID();
// First check if the parent has been added to the diagram as regular
// class
// First check if the parent has been added to the diagram as
// regular class
id_opt = get_unique_id(local_id);
// If not, check if the parent template declaration is in the model

View File

@@ -206,7 +206,7 @@ private:
bool is_smart_pointer(const clang::TemplateDecl *primary_template) const;
bool is_callee_valid_template_specialization(
const clang::CXXDependentScopeMemberExpr *dependent_member_callee)
const clang::CXXDependentScopeMemberExpr *dependent_member_expr)
const;
bool process_operator_call_expression(model::message &m,

View File

@@ -1,5 +1,7 @@
#include <algorithm>
#include <functional>
#include <memory>
#include <optional>
#include <utility>
namespace clanguml {
@@ -18,6 +20,8 @@ struct B {
void bb() { bbb(); }
void bbb() { }
void eb() { }
};
struct C {
@@ -32,6 +36,14 @@ struct D {
int add5(int arg) const { return arg + 5; }
};
class E {
std::optional<std::shared_ptr<B>> maybe_b;
std::shared_ptr<A> a;
public:
template <typename F> void setup(F &&f) { f(maybe_b); }
};
template <typename F> struct R {
R(F &&f)
: f_{std::move(f)}
@@ -79,6 +91,14 @@ void tmain()
std::vector<int> ints{0, 1, 2, 3, 4};
std::transform(ints.begin(), ints.end(), ints.begin(),
[&d](auto i) { return d.add5(i); });
// TODO: Fix naming function call arguments which are lambdas
// E e;
//
// e.setup([](auto &&arg) mutable {
// // We cannot know here what 'arg' might be
// arg.value()->eb();
// });
}
}
}

View File

@@ -36,38 +36,39 @@ TEST_CASE("t20012", "[test-case][sequence]")
// Check if all calls exist
REQUIRE_THAT(puml,
HasCall(_A("tmain()"), _A("tmain()::(lambda t20012.cc:54:20)"),
HasCall(_A("tmain()"), _A("tmain()::(lambda t20012.cc:66:20)"),
"operator()()"));
REQUIRE_THAT(
puml, HasCall(_A("tmain()::(lambda t20012.cc:54:20)"), _A("A"), "a()"));
puml, HasCall(_A("tmain()::(lambda t20012.cc:66:20)"), _A("A"), "a()"));
REQUIRE_THAT(puml, HasCall(_A("A"), _A("A"), "aa()"));
REQUIRE_THAT(puml, HasCall(_A("A"), _A("A"), "aaa()"));
REQUIRE_THAT(
puml, HasCall(_A("tmain()::(lambda t20012.cc:54:20)"), _A("B"), "b()"));
puml, HasCall(_A("tmain()::(lambda t20012.cc:66:20)"), _A("B"), "b()"));
REQUIRE_THAT(puml, HasCall(_A("B"), _A("B"), "bb()"));
REQUIRE_THAT(puml, HasCall(_A("B"), _A("B"), "bbb()"));
REQUIRE_THAT(
puml, HasCall(_A("tmain()::(lambda t20012.cc:67:20)"), _A("C"), "c()"));
puml, HasCall(_A("tmain()::(lambda t20012.cc:79:20)"), _A("C"), "c()"));
REQUIRE_THAT(puml, HasCall(_A("C"), _A("C"), "cc()"));
REQUIRE_THAT(puml, HasCall(_A("C"), _A("C"), "ccc()"));
REQUIRE_THAT(puml,
HasCall(_A("tmain()::(lambda t20012.cc:67:20)"),
_A("tmain()::(lambda t20012.cc:54:20)"), "operator()()"));
HasCall(_A("tmain()::(lambda t20012.cc:79:20)"),
_A("tmain()::(lambda t20012.cc:66:20)"), "operator()()"));
REQUIRE_THAT(puml, HasCall(_A("C"), _A("C"), "ccc()"));
REQUIRE_THAT(puml,
HasCall(_A("tmain()"), _A("R<R::(lambda t20012.cc:73:9)>"), "r()"));
HasCall(_A("tmain()"), _A("R<R::(lambda t20012.cc:85:9)>"), "r()"));
REQUIRE_THAT(puml,
HasCall(_A("R<R::(lambda t20012.cc:73:9)>"),
_A("tmain()::(lambda t20012.cc:73:9)"), "operator()()"));
HasCall(_A("R<R::(lambda t20012.cc:85:9)>"),
_A("tmain()::(lambda t20012.cc:85:9)"), "operator()()"));
REQUIRE_THAT(
puml, HasCall(_A("tmain()::(lambda t20012.cc:73:9)"), _A("C"), "c()"));
puml, HasCall(_A("tmain()::(lambda t20012.cc:85:9)"), _A("C"), "c()"));
REQUIRE_THAT(puml, HasCall(_A("tmain()"), _A("D"), "add5(int)"));
save_puml(
"./" + config.output_directory() + "/" + diagram->name + ".puml", puml);
}

View File

@@ -1,4 +1,5 @@
#include <memory>
#include <optional>
namespace clanguml {
namespace t20015 {

14
tests/t20016/.clang-uml Normal file
View File

@@ -0,0 +1,14 @@
compilation_database_dir: ..
output_directory: puml
diagrams:
t20016_sequence:
type: sequence
glob:
- ../../tests/t20016/t20016.cc
include:
namespaces:
- clanguml::t20016
using_namespace:
- clanguml::t20016
start_from:
- function: "clanguml::t20016::tmain()"

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

@@ -0,0 +1,24 @@
namespace clanguml {
namespace t20016 {
struct A {
void a1(int a) { }
template <typename T> T a2(const T &a) { return a;}
};
template <typename T> struct B {
void b1(T b) { a_.a1(1); }
template <typename F> F b2(T t) { return a_.a2(t); }
A a_;
};
void tmain() {
B<long> b;
b.b1(1);
b.b2<int>(2);
}
}
}

46
tests/t20016/test_case.h Normal file
View File

@@ -0,0 +1,46 @@
/**
* tests/t20016/test_case.h
*
* Copyright (c) 2021-2022 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("t20016", "[test-case][sequence]")
{
auto [config, db] = load_config("t20016");
auto diagram = config.diagrams["t20016_sequence"];
REQUIRE(diagram->name == "t20016_sequence");
auto model = generate_sequence_diagram(*db, diagram);
REQUIRE(model->name() == "t20016_sequence");
auto puml = generate_sequence_puml(diagram, *model);
AliasMatcher _A(puml);
REQUIRE_THAT(puml, StartsWith("@startuml"));
REQUIRE_THAT(puml, EndsWith("@enduml\n"));
// Check if all calls exist
REQUIRE_THAT(puml, HasCall(_A("tmain()"), _A("B<long>"), "b1(long)"));
REQUIRE_THAT(puml, HasCall(_A("B<long>"), _A("A"), "a1(int)"));
REQUIRE_THAT(puml, HasCall(_A("tmain()"), _A("B<long>"), "b2(long)"));
REQUIRE_THAT(puml, HasCall(_A("B<long>"), _A("A"), "a2(const long &)"));
save_puml(
"./" + config.output_directory() + "/" + diagram->name + ".puml", puml);
}

View File

@@ -262,6 +262,7 @@ using namespace clanguml::test::matchers;
#include "t20013/test_case.h"
#include "t20014/test_case.h"
#include "t20015/test_case.h"
#include "t20016/test_case.h"
///
/// Package diagram tests

View File

@@ -193,6 +193,9 @@ test_cases:
- name: t20015
title: Class exclusion by namespace in sequence diagram test case
description:
- name: t20016
title: Template method specialization sequence diagram test case
description:
Package diagrams:
- name: t30001
title: Basic package diagram test case