Added variadic class template sequence diagram test case

This commit is contained in:
Bartek Kryza
2022-11-27 14:24:12 +01:00
parent e586c9d062
commit b36355352d
6 changed files with 95 additions and 32 deletions

View File

@@ -641,45 +641,18 @@ bool translation_unit_visitor::VisitCallExpr(clang::CallExpr *expr)
if (!callee_function) if (!callee_function)
return true; return true;
bool is_implicit = false;
auto callee_name = auto callee_name =
callee_function->getQualifiedNameAsString() + "()"; callee_function->getQualifiedNameAsString() + "()";
std::unique_ptr<model::function_template> f_ptr; std::unique_ptr<model::function_template> f_ptr;
// if(!get_ast_local_id(callee_function->getID()).has_value()) {
// The target template function is implicit if it's // This is hopefully not an interesting call...
// specialization/instantiation was not explicitly defined return true;
// (i.e. it was not added to the diagram by visitor methods)
//
is_implicit =
!get_ast_local_id(callee_function->getID()).has_value();
//
// If the callee is a specialization of a function template,
// build it's instantiation model to get the id
//
if (callee_function->getTemplateSpecializationArgs() &&
callee_function->getTemplateSpecializationArgs()->size() > 0) {
f_ptr = build_function_template_instantiation(*callee_function);
f_ptr->set_id(common::to_id(f_ptr->full_name(false)));
set_ast_local_id(callee_function->getID(), f_ptr->id());
} }
else {
if (is_implicit) {
LOG_DBG("Processing implicit template specialization {}",
f_ptr->full_name(false));
// If this is an implicit template specialization/instantiation
// for now we just redirect the call to it's primary template
// (TODO: this is not correct in a general case)
m.to = get_ast_local_id(
callee_function->getPrimaryTemplate()->getID())
.value();
}
else
m.to = get_ast_local_id(callee_function->getID()).value(); m.to = get_ast_local_id(callee_function->getID()).value();
}
auto message_name = callee_name; auto message_name = callee_name;
m.message_name = message_name.substr(0, message_name.size() - 2); m.message_name = message_name.substr(0, message_name.size() - 2);

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

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

25
tests/t20007/t20007.cc Normal file
View File

@@ -0,0 +1,25 @@
#include <string>
#include <utility>
namespace clanguml {
namespace t20007 {
template <typename First, typename... Args> struct Adder {
First add(First &&arg, Args &&...args) { return (arg + ... + args); }
};
void tmain()
{
using namespace std::string_literals;
Adder<int, int> adder1;
Adder<int, float, double> adder2;
Adder<std::string, std::string, std::string> adder3;
[[maybe_unused]] auto res1 = adder1.add(2, 2);
[[maybe_unused]] auto res2 = adder2.add(1, 2.0, 3.0);
[[maybe_unused]] auto res3 = adder3.add("one"s, "two"s, "three"s);
}
}
}

47
tests/t20007/test_case.h Normal file
View File

@@ -0,0 +1,47 @@
/**
* tests/t20007/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("t20007", "[test-case][sequence]")
{
auto [config, db] = load_config("t20007");
auto diagram = config.diagrams["t20007_sequence"];
REQUIRE(diagram->name == "t20007_sequence");
auto model = generate_sequence_diagram(*db, diagram);
REQUIRE(model->name() == "t20007_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("Adder<int,int>"), "add"));
REQUIRE_THAT(
puml, HasCall(_A("tmain()"), _A("Adder<int,float,double>"), "add"));
REQUIRE_THAT(puml,
HasCall(_A("tmain()"), _A("Adder<std::string,std::string,std::string>"),
"add"));
save_puml(
"./" + config.output_directory() + "/" + diagram->name + ".puml", puml);
}

View File

@@ -253,6 +253,7 @@ using namespace clanguml::test::matchers;
#include "t20004/test_case.h" #include "t20004/test_case.h"
#include "t20005/test_case.h" #include "t20005/test_case.h"
#include "t20006/test_case.h" #include "t20006/test_case.h"
#include "t20007/test_case.h"
/// ///
/// Package diagram tests /// Package diagram tests

View File

@@ -166,6 +166,9 @@ test_cases:
- name: t20006 - name: t20006
title: Class template specialization basic sequence diagram title: Class template specialization basic sequence diagram
description: description:
- name: t20007
title: Class template variadic argument list sequence diagram
description:
Package diagrams: Package diagrams:
- name: t30001 - name: t30001
title: Basic package diagram test case title: Basic package diagram test case