Removed cppast dependency

This commit is contained in:
Bartek Kryza
2022-08-03 22:12:08 +02:00
parent 5917d341e2
commit 487e5d435b
46 changed files with 167 additions and 1230 deletions

View File

@@ -25,29 +25,23 @@
namespace clanguml::class_diagram::model {
const std::vector<std::reference_wrapper<class_>> &diagram::classes() const
const common::reference_vector<class_> &diagram::classes() const
{
return classes_;
}
const std::vector<std::reference_wrapper<enum_>> &diagram::enums() const
{
return enums_;
}
const common::reference_vector<enum_> &diagram::enums() const { return enums_; }
common::model::diagram_t diagram::type() const
{
return common::model::diagram_t::kClass;
}
std::optional<std::reference_wrapper<clanguml::common::model::diagram_element>>
diagram::get(const std::string &full_name) const
common::optional_ref<clanguml::common::model::diagram_element> diagram::get(
const std::string &full_name) const
{
std::optional<
std::reference_wrapper<clanguml::common::model::diagram_element>>
res;
res = get_class(full_name);
common::optional_ref<clanguml::common::model::diagram_element> res =
get_class(full_name);
if (res.has_value())
return res;
@@ -57,12 +51,10 @@ diagram::get(const std::string &full_name) const
return res;
}
std::optional<std::reference_wrapper<clanguml::common::model::diagram_element>>
diagram::get(const clanguml::common::model::diagram_element::id_t id) const
common::optional_ref<clanguml::common::model::diagram_element> diagram::get(
const clanguml::common::model::diagram_element::id_t id) const
{
std::optional<
std::reference_wrapper<clanguml::common::model::diagram_element>>
res;
common::optional_ref<clanguml::common::model::diagram_element> res;
res = get_class(id);
@@ -86,8 +78,7 @@ bool diagram::has_enum(const enum_ &e) const
[&e](const auto &ee) { return ee.get().full_name() == e.full_name(); });
}
std::optional<std::reference_wrapper<class_>> diagram::get_class(
const std::string &name) const
common::optional_ref<class_> diagram::get_class(const std::string &name) const
{
for (const auto &c : classes_) {
const auto full_name = c.get().full_name(false);
@@ -100,7 +91,7 @@ std::optional<std::reference_wrapper<class_>> diagram::get_class(
return {};
}
std::optional<std::reference_wrapper<class_>> diagram::get_class(
common::optional_ref<class_> diagram::get_class(
clanguml::common::model::diagram_element::id_t id) const
{
for (const auto &c : classes_) {
@@ -112,8 +103,7 @@ std::optional<std::reference_wrapper<class_>> diagram::get_class(
return {};
}
std::optional<std::reference_wrapper<enum_>> diagram::get_enum(
const std::string &name) const
common::optional_ref<enum_> diagram::get_enum(const std::string &name) const
{
for (const auto &e : enums_) {
if (e.get().full_name(false) == name) {
@@ -124,7 +114,7 @@ std::optional<std::reference_wrapper<enum_>> diagram::get_enum(
return {};
}
std::optional<std::reference_wrapper<enum_>> diagram::get_enum(
common::optional_ref<enum_> diagram::get_enum(
clanguml::common::model::diagram_element::id_t id) const
{
for (const auto &e : enums_) {
@@ -176,11 +166,9 @@ bool diagram::add_class(std::unique_ptr<class_> &&c)
auto name_and_ns = ns | name;
auto &cc = *c;
auto cc_ref = std::ref(cc);
if (!has_class(cc)) {
if (add_element(ns, std::move(c)))
classes_.push_back(std::move(cc_ref));
classes_.push_back(std::ref(cc));
const auto &el = get_element<class_>(name_and_ns).value();

View File

@@ -45,32 +45,28 @@ public:
common::model::diagram_t type() const override;
std::optional<
std::reference_wrapper<clanguml::common::model::diagram_element>>
get(const std::string &full_name) const override;
common::optional_ref<common::model::diagram_element> get(
const std::string &full_name) const override;
std::optional<
std::reference_wrapper<clanguml::common::model::diagram_element>>
get(const clanguml::common::model::diagram_element::id_t id) const override;
common::optional_ref<common::model::diagram_element> get(
const clanguml::common::model::diagram_element::id_t id) const override;
const std::vector<std::reference_wrapper<class_>> &classes() const;
const common::reference_vector<class_> &classes() const;
const std::vector<std::reference_wrapper<enum_>> &enums() const;
const common::reference_vector<enum_> &enums() const;
bool has_class(const class_ &c) const;
bool has_enum(const enum_ &e) const;
std::optional<std::reference_wrapper<class_>> get_class(
const std::string &name) const;
common::optional_ref<class_> get_class(const std::string &name) const;
std::optional<std::reference_wrapper<class_>> get_class(
common::optional_ref<class_> get_class(
clanguml::common::model::diagram_element::id_t id) const;
std::optional<std::reference_wrapper<enum_>> get_enum(
const std::string &name) const;
common::optional_ref<enum_> get_enum(const std::string &name) const;
std::optional<std::reference_wrapper<enum_>> get_enum(
common::optional_ref<enum_> get_enum(
clanguml::common::model::diagram_element::id_t id) const;
void add_type_alias(std::unique_ptr<type_alias> &&ta);
@@ -92,9 +88,9 @@ public:
const clanguml::common::model::diagram_element::id_t id) const override;
private:
std::vector<std::reference_wrapper<class_>> classes_;
common::reference_vector<class_> classes_;
std::vector<std::reference_wrapper<enum_>> enums_;
common::reference_vector<enum_> enums_;
std::map<std::string, std::unique_ptr<type_alias>> type_aliases_;
};

View File

@@ -1,57 +0,0 @@
/**
* src/class_diagram/model/visitor/element_visitor_context.cc
*
* 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.
*/
#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

@@ -1,48 +0,0 @@
/**
* src/class_diagram/model/visitor/element_visitor_context.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.
*/
#pragma once
#include "class_diagram/model/class.h"
#include "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

@@ -1,273 +0,0 @@
/**
* src/class_diagram/visitor/translation_unit_context.cc
*
* 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.
*/
#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_namespace_alias(
const std::string &full_name) const
{
bool res =
namespace_alias_index_.find(full_name) != namespace_alias_index_.end();
LOG_DBG("Alias {} {} found in index", full_name, res ? "" : "not");
return res;
}
void translation_unit_context::add_namespace_alias(const std::string &full_name,
type_safe::object_ref<const cppast::cpp_namespace> ref)
{
if (!has_namespace_alias(full_name)) {
LOG_DBG(
"Stored namespace alias: {} -> {} ", full_name, ref.get().name());
namespace_alias_index_.emplace(full_name, std::move(ref));
}
}
type_safe::object_ref<const cppast::cpp_namespace>
translation_unit_context::get_namespace_alias(
const std::string &full_name) const
{
assert(has_namespace_alias(full_name));
return namespace_alias_index_.at(full_name);
}
type_safe::object_ref<const cppast::cpp_namespace>
translation_unit_context::get_namespace_alias_final(
const cppast::cpp_namespace &ns) const
{
auto ns_full_name = cx::util::full_name({}, ns);
ns_full_name = cx::util::ns(ns) + "::" + ns_full_name;
if (has_namespace_alias(ns_full_name)) {
return get_namespace_alias_final(
namespace_alias_index_.at(ns_full_name).get());
}
return type_safe::ref(ns);
}
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 type_full_name =
cx::util::full_name(cppast::remove_cv(t), entity_index_, false);
if (has_type_alias(type_full_name)) {
const auto &alias_type = alias_index_.at(type_full_name).get();
// Prevent infinite recursion
if (type_full_name !=
cx::util::full_name(
cppast::remove_cv(alias_type), entity_index_, false))
return get_type_alias_final(alias_type);
}
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)
{
ns_ |= ns;
}
void translation_unit_context::pop_namespace() { ns_.pop_back(); }
const common::model::namespace_ &translation_unit_context::get_namespace() const
{
return ns_;
}
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_;
}
clanguml::class_diagram::model::diagram &
translation_unit_context::diagram() const
{
return diagram_;
}
void translation_unit_context::set_current_package(
type_safe::optional_ref<common::model::package> p)
{
current_package_ = p;
}
type_safe::optional_ref<common::model::package>
translation_unit_context::get_current_package() const
{
return current_package_;
}
void translation_unit_context::add_using_namespace_directive(
common::model::namespace_ ns)
{
using_ns_declarations_[ns_.to_string()].insert(std::move(ns));
}
const std::set<common::model::namespace_> &
translation_unit_context::using_namespace_directive(
const common::model::namespace_ &ns) const
{
return using_ns_declarations_.at(ns.to_string());
}
type_safe::optional<common::model::namespace_>
translation_unit_context::get_name_with_namespace(const std::string &name) const
{
using common::model::namespace_;
std::set<namespace_> possible_matches;
possible_matches.emplace(name);
possible_matches.emplace(get_namespace() | namespace_{name});
auto parent = get_namespace().parent();
while (parent.has_value()) {
possible_matches.emplace(parent.value() | namespace_{name});
parent = parent.value().parent();
}
if (using_ns_declarations_.find(get_namespace().to_string()) !=
using_ns_declarations_.end()) {
for (const auto &ns :
using_ns_declarations_.at(get_namespace().to_string())) {
possible_matches.emplace(ns | namespace_{name});
auto parent = ns.parent();
while (parent.has_value()) {
possible_matches.emplace(parent.value() | namespace_{name});
parent = parent.value().parent();
}
}
}
// Search classes
for (const auto &c : diagram_.classes()) {
auto c_ns = namespace_{c->name_and_ns()};
for (const auto &possible_match : possible_matches) {
if (c_ns == possible_match) {
return possible_match;
}
}
}
// Search enums
for (const auto &e : diagram_.enums()) {
auto e_ns = namespace_{e->name_and_ns()};
for (const auto &possible_match : possible_matches) {
if (e_ns == possible_match) {
return possible_match;
}
// Try to also match possible references to enum values
else if (possible_match.starts_with(e_ns)) {
return possible_match;
}
}
}
return {};
}
}

View File

@@ -1,127 +0,0 @@
/**
* src/class_diagram/visitor/translation_unit_context.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.
*/
#pragma once
#include "config/config.h"
#include <cppast/cpp_entity_index.hpp>
#include <cppast/cpp_namespace.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_namespace_alias(const std::string &full_name) const;
void add_namespace_alias(const std::string &full_name,
type_safe::object_ref<const cppast::cpp_namespace> ref);
type_safe::object_ref<const cppast::cpp_namespace> get_namespace_alias(
const std::string &full_name) const;
type_safe::object_ref<const cppast::cpp_namespace>
get_namespace_alias_final(const cppast::cpp_namespace &t) const;
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 common::model::namespace_ &get_namespace() const;
const cppast::cpp_entity_index &entity_index() const;
const clanguml::config::class_diagram &config() const;
clanguml::class_diagram::model::diagram &diagram();
clanguml::class_diagram::model::diagram &diagram() const;
void set_current_package(type_safe::optional_ref<common::model::package> p);
type_safe::optional_ref<common::model::package> get_current_package() const;
void add_using_namespace_directive(common::model::namespace_ ns);
const std::set<common::model::namespace_> &using_namespace_directive(
const common::model::namespace_ &ns) const;
type_safe::optional<common::model::namespace_> get_name_with_namespace(
const std::string &name) const;
private:
// Current visitor namespace
common::model::namespace_ ns_;
// A map of 'using namespace' declared within a given namespace scope
// This is necessary to properly establish the namespace of a given entity
// for instance in unexposed template parameters
// - key - namespace
// - value - set of namespaces 'imported' within this namespace scope
std::map<std::string, std::set<common::model::namespace_>>
using_ns_declarations_;
// 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 'namespace' keyword)
std::map<std::string, type_safe::object_ref<const cppast::cpp_namespace>>
namespace_alias_index_;
// 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_;
type_safe::optional_ref<common::model::package> current_package_;
};
}

View File

@@ -1193,7 +1193,7 @@ bool translation_unit_visitor::find_relationships_in_unexposed_template_params(
auto element_opt = diagram().get(type_with_namespace.value().to_string());
if (element_opt) {
relationships.emplace_back(
element_opt.value().get().id(), relationship_t::kDependency);
element_opt.value().id(), relationship_t::kDependency);
found = true;
}