Added method type diagram filter (#145)

This commit is contained in:
Bartek Kryza
2023-06-02 01:05:37 +02:00
parent e7f738c6dc
commit e40dc3a60c
20 changed files with 433 additions and 9 deletions

View File

@@ -186,7 +186,7 @@ void generator::generate(const class_ &c, std::ostream &ostr) const
ostr << "__\n";
for (const auto &m : members) {
if (!m_model.should_include(m.access()))
if (!m_model.should_include(m))
continue;
if (!m_config.include_relations_also_as_members() &&
@@ -232,7 +232,7 @@ void generator::generate_methods(
sort_class_elements(sorted_methods);
for (const auto &m : sorted_methods) {
if (!m_model.should_include(m.access()))
if (!m_model.should_include(m))
continue;
generate_method(m, ostr);
@@ -250,7 +250,7 @@ generator::method_groups_t generator::group_methods(
std::vector<class_method> filtered_methods;
std::copy_if(methods.cbegin(), methods.cend(),
std::back_inserter(filtered_methods),
[this](auto &m) { return m_model.should_include(m.access()); });
[this](auto &m) { return m_model.should_include(m); });
for (const auto &g : method_groups_) {
result[g] = {};

View File

@@ -18,11 +18,22 @@
#include "diagram.h"
#include "common/model/diagram_filter.h"
#include "util/error.h"
#include "util/util.h"
namespace clanguml::class_diagram::model {
bool diagram::should_include(const class_member &m) const
{
return filter().should_include(m);
}
bool diagram::should_include(const class_method &m) const
{
return filter().should_include(m);
}
const common::reference_vector<class_> &diagram::classes() const
{
return element_view<class_>::view();

View File

@@ -58,6 +58,12 @@ public:
diagram_t type() const override;
using common::model::diagram::should_include;
bool should_include(const class_member &m) const;
bool should_include(const class_method &m) const;
opt_ref<diagram_element> get(const std::string &full_name) const override;
opt_ref<diagram_element> get(diagram_element::id_t id) const override;

View File

@@ -1354,10 +1354,13 @@ void translation_unit_visitor::process_method(
process_function_parameter_find_relationships_in_autotype(c, atsp);
}
LOG_DBG("Adding method: {}", method.name());
if (diagram().should_include(method)) {
LOG_DBG("Adding method: {}", method.name());
c.add_method(std::move(method));
c.add_method(std::move(method));
}
}
void translation_unit_visitor::process_method_properties(
const clang::CXXMethodDecl &mf, const class_ &c,
const std::string &method_name, class_method &method) const
@@ -1471,9 +1474,11 @@ void translation_unit_visitor::process_template_method(
process_function_parameter(*param, method, c);
}
LOG_DBG("Adding method: {}", method.name());
if (diagram().should_include(method)) {
LOG_DBG("Adding method: {}", method.name());
c.add_method(std::move(method));
c.add_method(std::move(method));
}
}
bool translation_unit_visitor::find_relationships(const clang::QualType &type,

View File

@@ -59,6 +59,7 @@ public:
std::string name() const;
void set_filter(std::unique_ptr<diagram_filter> filter);
const diagram_filter &filter() const { return *filter_; }
void set_complete(bool complete);
bool complete() const;

View File

@@ -127,6 +127,18 @@ tvl::value_t filter_visitor::match(
return {};
}
tvl::value_t filter_visitor::match(
const diagram &d, const class_diagram::model::class_method &m) const
{
return match(d, m.access());
}
tvl::value_t filter_visitor::match(
const diagram &d, const class_diagram::model::class_member &m) const
{
return match(d, m.access());
}
bool filter_visitor::is_inclusive() const
{
return type_ == filter_t::kInclusive;
@@ -236,6 +248,37 @@ tvl::value_t element_type_filter::match(
});
}
method_type_filter::method_type_filter(
filter_t type, std::vector<config::method_type> method_types)
: filter_visitor{type}
, method_types_{std::move(method_types)}
{
}
tvl::value_t method_type_filter::match(
const diagram &d, const class_diagram::model::class_method &m) const
{
return tvl::any_of(
method_types_.begin(), method_types_.end(), [&m](auto mt) {
switch (mt) {
case config::method_type::constructor:
return m.is_constructor() || m.is_destructor();
case config::method_type::assignment:
return m.is_copy_assignment() || m.is_move_assignment();
case config::method_type::operator_:
return m.is_operator();
case config::method_type::defaulted:
return m.is_defaulted();
case config::method_type::deleted:
return m.is_deleted();
case config::method_type::static_:
return m.is_static();
}
return false;
});
}
subclass_filter::subclass_filter(filter_t type, std::vector<std::string> roots)
: filter_visitor{type}
, roots_{std::move(roots)}
@@ -494,6 +537,36 @@ tvl::value_t paths_filter::match(
return false;
}
class_method_filter::class_method_filter(filter_t type,
std::unique_ptr<access_filter> af, std::unique_ptr<method_type_filter> mtf)
: filter_visitor{type}
, access_filter_{std::move(af)}
, method_type_filter_{std::move(mtf)}
{
}
tvl::value_t class_method_filter::match(
const diagram &d, const class_diagram::model::class_method &m) const
{
tvl::value_t res = tvl::or_(
access_filter_->match(d, m.access()), method_type_filter_->match(d, m));
return res;
}
class_member_filter::class_member_filter(
filter_t type, std::unique_ptr<access_filter> af)
: filter_visitor{type}
, access_filter_{std::move(af)}
{
}
tvl::value_t class_member_filter::match(
const diagram &d, const class_diagram::model::class_member &m) const
{
return access_filter_->match(d, m.access());
}
diagram_filter::diagram_filter(
const common::model::diagram &d, const config::diagram &c)
: diagram_{d}
@@ -541,6 +614,18 @@ void diagram_filter::init_filters(const config::diagram &c)
add_inclusive_filter(std::make_unique<paths_filter>(
filter_t::kInclusive, c.root_directory(), c.include().paths));
add_inclusive_filter(
std::make_unique<class_method_filter>(filter_t::kInclusive,
std::make_unique<access_filter>(
filter_t::kInclusive, c.include().access),
std::make_unique<method_type_filter>(
filter_t::kInclusive, c.include().method_types)));
add_inclusive_filter(
std::make_unique<class_member_filter>(filter_t::kInclusive,
std::make_unique<access_filter>(
filter_t::kInclusive, c.include().access)));
// Include any of these matches even if one them does not match
std::vector<std::unique_ptr<filter_visitor>> element_filters;
@@ -637,6 +722,18 @@ void diagram_filter::init_filters(const config::diagram &c)
add_exclusive_filter(std::make_unique<access_filter>(
filter_t::kExclusive, c.exclude().access));
add_exclusive_filter(
std::make_unique<class_method_filter>(filter_t::kExclusive,
std::make_unique<access_filter>(
filter_t::kExclusive, c.exclude().access),
std::make_unique<method_type_filter>(
filter_t::kExclusive, c.exclude().method_types)));
add_exclusive_filter(
std::make_unique<class_member_filter>(filter_t::kExclusive,
std::make_unique<access_filter>(
filter_t::kExclusive, c.exclude().access)));
add_exclusive_filter(std::make_unique<subclass_filter>(
filter_t::kExclusive, c.exclude().subclasses));

View File

@@ -17,6 +17,8 @@
*/
#pragma once
#include "class_diagram/model/class_member.h"
#include "class_diagram/model/class_method.h"
#include "class_diagram/model/diagram.h"
#include "common/clang_utils.h"
#include "common/model/diagram.h"
@@ -78,6 +80,12 @@ public:
virtual tvl::value_t match(
const diagram &d, const common::model::source_location &f) const;
virtual tvl::value_t match(
const diagram &d, const class_diagram::model::class_method &m) const;
virtual tvl::value_t match(
const diagram &d, const class_diagram::model::class_member &m) const;
bool is_inclusive() const;
bool is_exclusive() const;
@@ -138,6 +146,19 @@ private:
std::vector<std::string> element_types_;
};
struct method_type_filter : public filter_visitor {
method_type_filter(
filter_t type, std::vector<config::method_type> method_types);
~method_type_filter() override = default;
tvl::value_t match(const diagram &d,
const class_diagram::model::class_method &e) const override;
private:
std::vector<config::method_type> method_types_;
};
struct subclass_filter : public filter_visitor {
subclass_filter(filter_t type, std::vector<std::string> roots);
@@ -354,6 +375,32 @@ private:
std::filesystem::path root_;
};
struct class_method_filter : public filter_visitor {
class_method_filter(filter_t type, std::unique_ptr<access_filter> af,
std::unique_ptr<method_type_filter> mtf);
~class_method_filter() override = default;
tvl::value_t match(const diagram &d,
const class_diagram::model::class_method &m) const override;
private:
std::unique_ptr<access_filter> access_filter_;
std::unique_ptr<method_type_filter> method_type_filter_;
};
struct class_member_filter : public filter_visitor {
class_member_filter(filter_t type, std::unique_ptr<access_filter> af);
~class_member_filter() override = default;
tvl::value_t match(const diagram &d,
const class_diagram::model::class_member &m) const override;
private:
std::unique_ptr<access_filter> access_filter_;
};
class diagram_filter {
public:
diagram_filter(const common::model::diagram &d, const config::diagram &c);

View File

@@ -24,6 +24,31 @@ namespace clanguml::common::model::tvl {
using value_t = std::optional<bool>;
inline value_t and_(const value_t &l, const value_t &r)
{
if (!l.has_value())
return r;
if (!r.has_value())
return l;
return r.value() && l.value();
}
inline value_t or_(const value_t &l, const value_t &r)
{
if (!l.has_value() && !r.has_value())
return {};
if (l.has_value() && l.value())
return true;
if (r.has_value() && r.value())
return true;
return false;
}
inline bool is_true(const value_t &v) { return v.has_value() && v.value(); }
inline bool is_false(const value_t &v) { return v.has_value() && !v.value(); }

View File

@@ -62,6 +62,27 @@ std::string to_string(const method_arguments ma)
}
}
std::string to_string(method_type mt)
{
switch (mt) {
case method_type::constructor:
return "constructor";
case method_type::assignment:
return "assignment";
case method_type::operator_:
return "operator";
case method_type::defaulted:
return "defaulted";
case method_type::deleted:
return "deleted";
case method_type::static_:
return "static";
default:
assert(false);
return "";
}
}
std::string to_string(const comment_parser_t cp)
{
switch (cp) {

View File

@@ -37,6 +37,17 @@ namespace config {
enum class method_arguments { full, abbreviated, none };
enum class method_type {
constructor,
assignment,
operator_,
defaulted,
deleted,
static_
};
std::string to_string(method_type mt);
enum class package_type_t { kNamespace, kDirectory };
enum class member_order_t { lexical, as_is };
@@ -96,6 +107,8 @@ struct filter {
std::vector<std::string> context;
std::vector<std::filesystem::path> paths;
std::vector<method_type> method_types;
};
enum class hint_t { up, down, left, right, together, row, column };

View File

@@ -34,6 +34,7 @@ using clanguml::config::layout_hint;
using clanguml::config::location_t;
using clanguml::config::member_order_t;
using clanguml::config::method_arguments;
using clanguml::config::method_type;
using clanguml::config::package_diagram;
using clanguml::config::package_type_t;
using clanguml::config::plantuml;
@@ -221,6 +222,32 @@ template <> struct convert<access_t> {
}
};
//
// config method_type decoder
//
template <> struct convert<method_type> {
static bool decode(const Node &node, method_type &rhs)
{
const auto &val = node.as<std::string>();
if (val == to_string(method_type::constructor))
rhs = method_type::constructor;
else if (val == to_string(method_type::assignment))
rhs = method_type::assignment;
else if (val == to_string(method_type::operator_))
rhs = method_type::operator_;
else if (val == to_string(method_type::defaulted))
rhs = method_type::defaulted;
else if (val == to_string(method_type::deleted))
rhs = method_type::deleted;
else if (val == to_string(method_type::static_))
rhs = method_type::static_;
else
return false;
return true;
}
};
//
// config relationship_t decoder
//
@@ -337,6 +364,10 @@ template <> struct convert<filter> {
rhs.element_types =
node["element_types"].as<decltype(rhs.element_types)>();
if (node["method_types"])
rhs.method_types =
node["method_types"].as<decltype(rhs.method_types)>();
if (node["access"])
rhs.access = node["access"].as<decltype(rhs.access)>();
@@ -515,7 +546,8 @@ template <> struct convert<include_diagram> {
rhs.relative_to.set(std::filesystem::current_path());
// Convert the path in relative_to to an absolute path, with respect
// to the directory where the `.clang-uml` configuration file is located
// to the directory where the `.clang-uml` configuration file is
// located
if (rhs.relative_to) {
auto absolute_relative_to =
std::filesystem::path{node["__parent_path"].as<std::string>()} /

View File

@@ -45,6 +45,13 @@ YAML::Emitter &operator<<(YAML::Emitter &out, const diagram_t &d)
} // namespace clanguml::common::model
namespace clanguml::config {
YAML::Emitter &operator<<(YAML::Emitter &out, const method_type &m)
{
out << to_string(m);
return out;
}
YAML::Emitter &operator<<(YAML::Emitter &out, const filter &f)
{
out << YAML::BeginMap;
@@ -62,6 +69,8 @@ YAML::Emitter &operator<<(YAML::Emitter &out, const filter &f)
out << YAML::Key << "elements" << YAML::Value << f.elements;
if (!f.element_types.empty())
out << YAML::Key << "element_types" << YAML::Value << f.element_types;
if (!f.method_types.empty())
out << YAML::Key << "method_types" << YAML::Value << f.method_types;
if (!f.paths.empty())
out << YAML::Key << "paths" << YAML::Value << f.paths;
if (!f.relationships.empty())

View File

@@ -64,5 +64,5 @@ private:
};
int A::static_int = 1;
} // namespace t00003
} // namespace t00066
} // namespace clanguml

18
tests/t00067/.clang-uml Normal file
View File

@@ -0,0 +1,18 @@
compilation_database_dir: ..
output_directory: puml
diagrams:
t00067_class:
type: class
glob:
- ../../tests/t00067/t00067.cc
include:
namespaces:
- clanguml::t00067
exclude:
method_types:
- constructor
- operator
- assignment
- static
using_namespace:
- clanguml::t00067

67
tests/t00067/t00067.cc Normal file
View File

@@ -0,0 +1,67 @@
#include <functional>
#include <string>
namespace clanguml {
namespace t00067 {
class A {
public:
A() = default;
A(int i)
: private_member{i}
{
}
A(A &&) = default;
A(const A &) = delete;
virtual ~A() = default;
void basic_method() { }
static int static_method() { return 0; }
void const_method() const { }
auto auto_method() { return 1; }
A &operator++()
{
private_member++;
return *this;
}
A &operator=(A &&other) noexcept { return *this; }
A &operator=(A &other) noexcept { return *this; }
std::size_t size() const { return private_member; }
auto double_int(const int i) { return 2 * i; }
auto sum(const double a, const double b) { return a_ + b_ + c_; }
auto default_int(int i = 12) { return i + 10; }
std::string default_string(int i, std::string s = "abc")
{
return s + std::to_string(i);
}
static A create_from_int(int i) { return A(i); }
int public_member;
static int static_int;
static const int static_const_int = 1;
static const auto auto_member{10UL};
protected:
void protected_method() { }
int protected_member;
std::function<bool(const int)> compare = [this](const int v) {
return private_member > v;
};
private:
void private_method() { }
int private_member;
int a_, b_, c_;
};
int A::static_int = 1;
}
}

58
tests/t00067/test_case.h Normal file
View File

@@ -0,0 +1,58 @@
/**
* tests/t00067/test_case.h
*
* Copyright (c) 2021-2023 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("t00067", "[test-case][class]")
{
auto [config, db] = load_config("t00067");
auto diagram = config.diagrams["t00067_class"];
REQUIRE(diagram->name == "t00067_class");
auto model = generate_class_diagram(*db, diagram);
REQUIRE(model->name() == "t00067_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, !(IsMethod<Public, Default>("A")));
REQUIRE_THAT(puml, !(IsMethod<Public, Default>("A", "void", "A &&")));
REQUIRE_THAT(
puml, !(IsMethod<Public, Deleted>("A", "void", "const A &")));
REQUIRE_THAT(puml, !(IsMethod<Public, Default>("~A")));
REQUIRE_THAT(puml, !(IsMethod<Public, Default>("~A")));
save_puml(
config.output_directory() + "/" + diagram->name + ".puml", puml);
}
{
auto j = generate_class_json(diagram, *model);
using namespace json;
save_json(config.output_directory() + "/" + diagram->name + ".json", j);
}
}

View File

@@ -305,6 +305,7 @@ using namespace clanguml::test::matchers;
#include "t00065/test_case.h"
#endif
#include "t00066/test_case.h"
#include "t00067/test_case.h"
///
/// Sequence diagram tests

View File

@@ -194,6 +194,10 @@ test_cases:
description:
- name: t00066
title: Class fields and methods without grouping and sorting
description:
- name: t00067
title: Class method type filter test case
description:
Sequence diagrams:
- name: t20001
title: Basic sequence diagram test case

View File

@@ -26,6 +26,7 @@ TEST_CASE("Test config simple", "[unit-test]")
{
using clanguml::common::model::access_t;
using clanguml::common::model::relationship_t;
using clanguml::config::method_type;
using clanguml::util::contains;
auto cfg = clanguml::config::load("./test_config_data/simple.yml");
@@ -72,6 +73,10 @@ TEST_CASE("Test config simple", "[unit-test]")
CHECK(contains(diagram.exclude().relationships, relationship_t::kNone));
CHECK(contains(diagram.exclude().method_types, method_type::operator_));
CHECK(contains(diagram.exclude().method_types, method_type::constructor));
CHECK(contains(diagram.exclude().method_types, method_type::deleted));
CHECK(diagram.relationship_hints().at("std::vector").get(0) ==
relationship_t::kComposition);
CHECK(diagram.relationship_hints().at("std::tuple").get(10) ==

View File

@@ -34,6 +34,10 @@ diagrams:
exclude:
relationships:
- none
method_types:
- operator
- constructor
- deleted
relationship_hints:
std::vector: composition
std::map: