Refactored class diagram visitor

This commit is contained in:
Bartek Kryza
2021-10-03 11:14:30 +02:00
parent 74315340aa
commit 3e46f2504f
10 changed files with 451 additions and 242 deletions

View File

@@ -23,7 +23,7 @@
#include "puml/class_diagram_generator.h"
#include "puml/sequence_diagram_generator.h"
#include "uml/class_diagram/model/diagram.h"
#include "uml/class_diagram_visitor.h"
#include "uml/class_diagram/visitor/translation_unit_visitor.h"
#include "uml/sequence_diagram_visitor.h"
#include "util/util.h"

View File

@@ -439,7 +439,8 @@ clanguml::class_diagram::model::diagram generate(
type_safe::ref(idx)};
// Process all matching translation units
clanguml::visitor::class_diagram::tu_visitor ctx(idx, d, diagram);
clanguml::class_diagram::visitor::translation_unit_visitor ctx(
idx, d, diagram);
cppast::parse_files(parser, translation_units, db);
for (auto &file : parser.files())
ctx(file);

View File

@@ -23,7 +23,7 @@
#include "uml/class_diagram/model/class_relationship.h"
#include "uml/class_diagram/model/diagram.h"
#include "uml/class_diagram/model/enum.h"
#include "uml/class_diagram_visitor.h"
#include "uml/class_diagram/visitor/translation_unit_visitor.h"
#include "util/util.h"
#include <cppast/cpp_entity_index.hpp>

View File

@@ -0,0 +1,57 @@
/**
* src/uml/class_diagram/model/visitor/element_visitor_context.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.
*/
#include "element_visitor_context.h"
#include "translation_unit_context.h"
namespace clanguml::class_diagram::visitor {
template <typename T>
element_visitor_context<T>::element_visitor_context(
clanguml::class_diagram::model::diagram &diagram, T &element)
: element_{element}
, diagram_{diagram}
{
}
template <typename T>
void element_visitor_context<T>::set_parent_class(
clanguml::class_diagram::model::class_ *parent_class)
{
parent_class_ = parent_class;
}
template <typename T>
clanguml::class_diagram::model::class_ *
element_visitor_context<T>::parent_class()
{
return parent_class_;
}
template <typename T> T &element_visitor_context<T>::element()
{
return element_;
}
template <typename T>
clanguml::class_diagram::model::diagram &element_visitor_context<T>::diagram()
{
return diagram_;
}
}

View File

@@ -0,0 +1,48 @@
/**
* src/uml/class_diagram/model/visitor/element_visitor_context.h
*
* 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.
*/
#pragma once
#include "uml/class_diagram/model/class.h"
#include "uml/class_diagram/model/diagram.h"
namespace clanguml::class_diagram::visitor {
class translation_unit_context;
template <typename T> class element_visitor_context {
public:
element_visitor_context(
clanguml::class_diagram::model::diagram &diagram, T &element);
void set_parent_class(clanguml::class_diagram::model::class_ *parent_class);
clanguml::class_diagram::model::class_ *parent_class();
T &element();
clanguml::class_diagram::model::diagram &diagram();
private:
translation_unit_context *ctx_;
T &element_;
clanguml::class_diagram::model::class_ *parent_class_{};
clanguml::class_diagram::model::diagram &diagram_;
};
}

View File

@@ -0,0 +1,135 @@
/**
* src/uml/class_diagram/visitor/translation_unit_context.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.
*/
#include "translation_unit_context.h"
#include "cx/util.h"
namespace clanguml::class_diagram::visitor {
translation_unit_context::translation_unit_context(
cppast::cpp_entity_index &idx,
clanguml::class_diagram::model::diagram &diagram,
const clanguml::config::class_diagram &config)
: entity_index_{idx}
, diagram_{diagram}
, config_{config}
{
}
bool translation_unit_context::has_type_alias(
const std::string &full_name) const
{
bool res = alias_index_.find(full_name) != alias_index_.end();
LOG_DBG("Alias {} {} found in index", full_name, res ? "" : "not");
return res;
}
void translation_unit_context::add_type_alias(const std::string &full_name,
type_safe::object_ref<const cppast::cpp_type> &&ref)
{
if (!has_type_alias(full_name)) {
LOG_DBG("Stored type alias: {} -> {} ", full_name,
cppast::to_string(ref.get()));
alias_index_.emplace(full_name, std::move(ref));
}
}
type_safe::object_ref<const cppast::cpp_type>
translation_unit_context::get_type_alias(const std::string &full_name) const
{
assert(has_type_alias(full_name));
return alias_index_.at(full_name);
}
type_safe::object_ref<const cppast::cpp_type>
translation_unit_context::get_type_alias_final(const cppast::cpp_type &t) const
{
const auto fn =
cx::util::full_name(cppast::remove_cv(t), entity_index_, false);
if (has_type_alias(fn)) {
return get_type_alias_final(alias_index_.at(fn).get());
}
return type_safe::ref(t);
}
bool translation_unit_context::has_type_alias_template(
const std::string &full_name) const
{
bool res =
alias_template_index_.find(full_name) != alias_template_index_.end();
LOG_DBG("Alias template {} {} found in index", full_name, res ? "" : "not");
return res;
}
void translation_unit_context::add_type_alias_template(
const std::string &full_name,
type_safe::object_ref<const cppast::cpp_type> &&ref)
{
if (!has_type_alias_template(full_name)) {
LOG_DBG("Stored type alias template for: {} ", full_name);
alias_template_index_.emplace(full_name, std::move(ref));
}
}
type_safe::object_ref<const cppast::cpp_type>
translation_unit_context::get_type_alias_template(
const std::string &full_name) const
{
assert(has_type_alias_template(full_name));
return alias_template_index_.at(full_name);
}
void translation_unit_context::push_namespace(const std::string &ns)
{
namespace_.push_back(ns);
}
void translation_unit_context::pop_namespace() { namespace_.pop_back(); }
const std::vector<std::string> &translation_unit_context::get_namespace() const
{
return namespace_;
}
const cppast::cpp_entity_index &translation_unit_context::entity_index() const
{
return entity_index_;
}
const clanguml::config::class_diagram &translation_unit_context::config() const
{
return config_;
}
clanguml::class_diagram::model::diagram &translation_unit_context::diagram()
{
return diagram_;
}
}

View File

@@ -0,0 +1,87 @@
/**
* src/uml/class_diagram/visitor/translation_unit_context.h
*
* 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.
*/
#pragma once
#include "config/config.h"
#include <cppast/cpp_entity_index.hpp>
#include <cppast/cpp_type.hpp>
#include <type_safe/reference.hpp>
namespace clanguml::class_diagram::visitor {
class translation_unit_context {
public:
translation_unit_context(cppast::cpp_entity_index &idx,
clanguml::class_diagram::model::diagram &diagram,
const clanguml::config::class_diagram &config);
bool has_type_alias(const std::string &full_name) const;
void add_type_alias(const std::string &full_name,
type_safe::object_ref<const cppast::cpp_type> &&ref);
type_safe::object_ref<const cppast::cpp_type> get_type_alias(
const std::string &full_name) const;
type_safe::object_ref<const cppast::cpp_type> get_type_alias_final(
const cppast::cpp_type &t) const;
bool has_type_alias_template(const std::string &full_name) const;
void add_type_alias_template(const std::string &full_name,
type_safe::object_ref<const cppast::cpp_type> &&ref);
type_safe::object_ref<const cppast::cpp_type> get_type_alias_template(
const std::string &full_name) const;
void push_namespace(const std::string &ns);
void pop_namespace();
const std::vector<std::string> &get_namespace() const;
const cppast::cpp_entity_index &entity_index() const;
const clanguml::config::class_diagram &config() const;
clanguml::class_diagram::model::diagram &diagram();
private:
// Current visitor namespace
std::vector<std::string> namespace_;
// Reference to the cppast entity index
cppast::cpp_entity_index &entity_index_;
// Reference to the output diagram model
clanguml::class_diagram::model::diagram &diagram_;
// Reference to class diagram config
const clanguml::config::class_diagram &config_;
// Map of discovered aliases (declared with 'using' keyword)
std::map<std::string, type_safe::object_ref<const cppast::cpp_type>>
alias_index_;
// Map of discovered template aliases (declared with 'using' keyword)
std::map<std::string, type_safe::object_ref<const cppast::cpp_type>>
alias_template_index_;
};
}

View File

@@ -1,5 +1,5 @@
/**
* src/uml/class_diagram_visitor.h
* src/uml/class_diagram/visitor/translation_unit_visitor.h
*
* Copyright (c) 2021 Bartek Kryza <bkryza@gmail.com>
*
@@ -20,6 +20,7 @@
#include "config/config.h"
#include "cx/cursor.h"
#include "uml/class_diagram/model/diagram.h"
#include "uml/class_diagram/visitor/translation_unit_context.h"
#include <clang-c/CXCompilationDatabase.h>
#include <clang-c/Index.h>
@@ -27,6 +28,7 @@
#include <cppast/cpp_function_template.hpp>
#include <cppast/cpp_member_function.hpp>
#include <cppast/cpp_member_variable.hpp>
#include <cppast/cpp_template.hpp>
#include <cppast/cpp_template_parameter.hpp>
#include <cppast/cpp_type.hpp>
#include <cppast/visitor.hpp>
@@ -37,59 +39,13 @@
#include <memory>
#include <string>
namespace clanguml {
namespace visitor {
namespace class_diagram {
namespace clanguml::class_diagram::visitor {
struct tu_context {
tu_context(cppast::cpp_entity_index &idx,
clanguml::class_diagram::model::diagram &d_,
const clanguml::config::class_diagram &config_);
bool has_type_alias(const std::string &full_name) const;
void add_type_alias(const std::string &full_name,
type_safe::object_ref<const cppast::cpp_type> &&ref);
type_safe::object_ref<const cppast::cpp_type> get_type_alias(
const std::string &full_name) const;
type_safe::object_ref<const cppast::cpp_type> get_type_alias_final(
const cppast::cpp_type &t) const;
bool has_type_alias_template(const std::string &full_name) const;
void add_type_alias_template(const std::string &full_name,
type_safe::object_ref<const cppast::cpp_type> &&ref);
type_safe::object_ref<const cppast::cpp_type> get_type_alias_template(
const std::string &full_name) const;
std::vector<std::string> namespace_;
cppast::cpp_entity_index &entity_index;
clanguml::class_diagram::model::diagram &d;
const clanguml::config::class_diagram &config;
std::map<std::string, type_safe::object_ref<const cppast::cpp_type>>
alias_index;
std::map<std::string, type_safe::object_ref<const cppast::cpp_type>>
alias_template_index;
};
template <typename T> struct element_visitor_context {
element_visitor_context(clanguml::class_diagram::model::diagram &d_, T &e);
tu_context *ctx;
T &element;
clanguml::class_diagram::model::class_ *parent_class{};
clanguml::class_diagram::model::diagram &d;
};
class tu_visitor {
class translation_unit_visitor {
public:
tu_visitor(cppast::cpp_entity_index &idx_,
clanguml::class_diagram::model::diagram &d_,
const clanguml::config::class_diagram &config_);
translation_unit_visitor(cppast::cpp_entity_index &idx,
clanguml::class_diagram::model::diagram &diagram,
const clanguml::config::class_diagram &config);
void operator()(const cppast::cpp_entity &file);
@@ -168,10 +124,13 @@ private:
const cppast::cpp_template_instantiation_type &t,
std::optional<clanguml::class_diagram::model::class_ *> parent = {});
/**
* Try to resolve a type instance into a type referenced through an alias.
* If t does not represent an alias, returns t.
*/
const cppast::cpp_type &resolve_alias(const cppast::cpp_type &t);
tu_context ctx;
// ctx allows to track current visitor context, e.g. current namespace
translation_unit_context ctx;
};
}
}
}

View File

@@ -24,7 +24,7 @@
#include "puml/class_diagram_generator.h"
#include "puml/sequence_diagram_generator.h"
#include "uml/class_diagram/model/diagram.h"
#include "uml/class_diagram_visitor.h"
#include "uml/class_diagram/visitor/translation_unit_visitor.h"
#include "uml/sequence_diagram_visitor.h"
#include "util/util.h"