Added generation of dependency relations based on method params

This commit is contained in:
Bartek Kryza
2021-03-18 23:14:40 +01:00
parent d55e57eb05
commit 2322833897
8 changed files with 133 additions and 14 deletions

View File

@@ -92,6 +92,8 @@ public:
return "..|>";
case relationship_t::kFriendship:
return "<..";
case relationship_t::kDependency:
return "..>";
default:
return "";
}

View File

@@ -45,7 +45,8 @@ enum class relationship_t {
kOwnership,
kAssociation,
kInstantiation,
kFriendship
kFriendship,
kDependency
};
class element {

View File

@@ -65,6 +65,7 @@ template <typename T> struct element_visitor_context {
tu_context *ctx;
T &element;
class_ *parent_class{};
diagram &d;
};
@@ -220,26 +221,26 @@ static enum CXChildVisitResult method_parameter_visitor(
spdlog::debug(
"Analyzing method parameter: {}, {}", cursor, cursor.type());
auto t = cursor.type();
method_parameter mp;
mp.name = cursor.spelling();
mp.type = cursor.type().spelling();
mp.type = t.spelling();
mp.default_value = cursor.default_value();
ctx->element.parameters.emplace_back(std::move(mp));
/* TODO: handle dependency relationships based
* on method arguments
*
if (ctx->ctx->config.should_include(
cursor.type().referenced().fully_qualified())) {
if (t.is_relationship() &&
ctx->ctx->config.should_include(t.referenced().spelling()) &&
(t.referenced().spelling() != ctx->parent_class->name)) {
class_relationship r;
r.type = relationship_t::kDependency;
r.destination = cursor.type().referenced().usr();
r.destination = t.referenced().spelling();
ctx->element.relationships.emplace_back(std::move(r));
assert(ctx->parent_class != nullptr);
ctx->parent_class->relationships.emplace_back(std::move(r));
}
*/
ret = CXChildVisit_Continue;
} break;
@@ -448,6 +449,7 @@ static enum CXChildVisitResult class_visitor(
auto method_ctx =
element_visitor_context<class_method>(ctx->d, m);
method_ctx.ctx = ctx->ctx;
method_ctx.parent_class = &ctx->element;
clang_visitChildren(
cursor.get(), method_parameter_visitor, &method_ctx);

12
tests/t00013/.clanguml Normal file
View File

@@ -0,0 +1,12 @@
compilation_database_dir: ..
output_directory: puml
diagrams:
t00013_class:
type: class
glob:
- ../../tests/t00013/t00013.cc
using_namespace:
- clanguml::t00013
include:
namespaces:
- clanguml::t00013

38
tests/t00013/t00013.cc Normal file
View File

@@ -0,0 +1,38 @@
#include <algorithm>
#include <map>
#include <numeric>
#include <string>
#include <variant>
namespace clanguml {
namespace t00013 {
struct A {
int a;
};
struct B {
int b;
};
struct C {
int c;
};
class R;
struct D {
int d;
void print(R *r) {}
};
class R {
public:
int get_a(A *a) { return a->a; }
int get_b(B &b) { return b.b; }
// TODO: int get_b(const B &b) { return b.b; }
int get_c(C c) { return c.c; }
int get_d(D &&d) { return d.d; }
};
}
}

57
tests/t00013/test_case.h Normal file
View File

@@ -0,0 +1,57 @@
/**
* tests/t00013/test_case.cc
*
* Copyright (c) 2021 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("t00013", "[test-case][class]")
{
auto [config, db] = load_config("t00013");
auto diagram = config.diagrams["t00013_class"];
REQUIRE(diagram->name == "t00013_class");
REQUIRE(diagram->include.namespaces.size() == 1);
REQUIRE_THAT(diagram->include.namespaces,
VectorContains(std::string{"clanguml::t00013"}));
REQUIRE(diagram->exclude.namespaces.size() == 0);
REQUIRE(diagram->should_include("clanguml::t00013::A"));
REQUIRE(diagram->should_include("clanguml::t00013::B"));
auto model = generate_class_diagram(db, diagram);
REQUIRE(model.name == "t00013_class");
auto puml = generate_class_puml(diagram, model);
AliasMatcher _A(puml);
REQUIRE_THAT(puml, StartsWith("@startuml"));
REQUIRE_THAT(puml, EndsWith("@enduml\n"));
REQUIRE_THAT(puml, IsClass(_A("A")));
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("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")));
save_puml(
"./" + config.output_directory + "/" + diagram->name + ".puml", puml);
}

View File

@@ -15,8 +15,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//#define CATCH_CONFIG_MAIN
#include "test_cases.h"
#include <spdlog/spdlog.h>
@@ -105,6 +103,7 @@ using clanguml::test::matchers::IsBaseClass;
using clanguml::test::matchers::IsClass;
using clanguml::test::matchers::IsClassTemplate;
using clanguml::test::matchers::IsComposition;
using clanguml::test::matchers::IsDependency;
using clanguml::test::matchers::IsEnum;
using clanguml::test::matchers::IsField;
using clanguml::test::matchers::IsFriend;
@@ -129,6 +128,7 @@ using clanguml::test::matchers::Static;
#include "t00010/test_case.h"
#include "t00011/test_case.h"
#include "t00012/test_case.h"
#include "t00013/test_case.h"
//
// Sequence diagram tests

View File

@@ -304,6 +304,13 @@ ContainsMatcher IsInstantiation(std::string const &from, std::string const &to,
CasedString(fmt::format("{} ..|> {}", to, from), caseSensitivity));
}
ContainsMatcher IsDependency(std::string const &from, std::string const &to,
CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes)
{
return ContainsMatcher(
CasedString(fmt::format("{} ..> {}", from, to), caseSensitivity));
}
ContainsMatcher IsMethod(std::string const &name,
CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes)
{