diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 8bb913d0..6d7e26bf 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -8,8 +8,6 @@ jobs: steps: - name: Checkout repository uses: actions/checkout@v2 - with: - submodules: recursive - name: Update package database run: sudo apt -y update - name: Install deps diff --git a/.gitmodules b/.gitmodules index 25a47558..e69de29b 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,4 +0,0 @@ -[submodule "thirdparty/cppast"] - path = thirdparty/cppast - url = https://github.com/bkryza/cppast - branch = handle-exposed-template-arguments diff --git a/CMakeLists.txt b/CMakeLists.txt index 461e712f..861350f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,7 +44,6 @@ message(STATUS "Using CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}") # Thirdparty sources set(THIRDPARTY_HEADERS_DIR ${PROJECT_SOURCE_DIR}/thirdparty/) -add_subdirectory(thirdparty/cppast) find_package(LLVM REQUIRED CONFIG) set(CLANG_INCLUDE_DIRS "llvm/clang/include") @@ -76,9 +75,6 @@ include_directories(${CLANG_UML_INSTALL_INCLUDE_DIR}) include_directories(${YAML_CPP_INCLUDE_DIR}) include_directories(${UML_HEADERS_DIR}) include_directories(${THIRDPARTY_HEADERS_DIR}) -include_directories(${THIRDPARTY_HEADERS_DIR}/cppast/include) -include_directories(${THIRDPARTY_HEADERS_DIR}/cppast/external/type_safe/include) -include_directories(${THIRDPARTY_HEADERS_DIR}/cppast/external/type_safe/external/debug_assert) include_directories(${PROJECT_SOURCE_DIR}/src/) include_directories(${PROJECT_BINARY_DIR}/src/version) @@ -122,7 +118,6 @@ target_link_libraries(clang-uml ${LIBCLANG_LIBRARIES} ${YAML_CPP_LIBRARIES} ${LIBTOOLING_LIBS} - cppast clang-umllib Threads::Threads) diff --git a/README.md b/README.md index 7dcbcdb4..ad874fe2 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,6 @@ Then proceed with building the sources: ```bash git clone https://github.com/bkryza/clang-uml cd clang-uml -make submodules # Please note that top level Makefile is just a convenience wrapper for CMake make release release/clang-uml --help @@ -466,8 +465,7 @@ The build-in test cases used for unit testing of the `clang-uml`, can be browsed ## Acknowledgements This project relies on the following great tools: - * [libclang](https://clang.llvm.org/) - a C/C++ frontend for LLVM - * [cppast](https://github.com/foonathan/cppast) - high-level C++ API for libclang + * [clang](https://clang.llvm.org/) - a C/C++ frontend for LLVM * [PlantUML](https://plantuml.com/) - language and diagram for generating UML diagrams * [Catch2](https://github.com/catchorg/Catch2) - C++ unit test framework * [glob](https://github.com/p-ranav/glob) - Unix style path expansion for C++ diff --git a/src/class_diagram/model/diagram.cc b/src/class_diagram/model/diagram.cc index 59c73841..7c6afe9f 100644 --- a/src/class_diagram/model/diagram.cc +++ b/src/class_diagram/model/diagram.cc @@ -25,29 +25,23 @@ namespace clanguml::class_diagram::model { -const std::vector> &diagram::classes() const +const common::reference_vector &diagram::classes() const { return classes_; } -const std::vector> &diagram::enums() const -{ - return enums_; -} +const common::reference_vector &diagram::enums() const { return enums_; } common::model::diagram_t diagram::type() const { return common::model::diagram_t::kClass; } -std::optional> -diagram::get(const std::string &full_name) const +common::optional_ref diagram::get( + const std::string &full_name) const { - std::optional< - std::reference_wrapper> - res; - - res = get_class(full_name); + common::optional_ref 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> -diagram::get(const clanguml::common::model::diagram_element::id_t id) const +common::optional_ref diagram::get( + const clanguml::common::model::diagram_element::id_t id) const { - std::optional< - std::reference_wrapper> - res; + common::optional_ref 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> diagram::get_class( - const std::string &name) const +common::optional_ref 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> diagram::get_class( return {}; } -std::optional> diagram::get_class( +common::optional_ref diagram::get_class( clanguml::common::model::diagram_element::id_t id) const { for (const auto &c : classes_) { @@ -112,8 +103,7 @@ std::optional> diagram::get_class( return {}; } -std::optional> diagram::get_enum( - const std::string &name) const +common::optional_ref 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> diagram::get_enum( return {}; } -std::optional> diagram::get_enum( +common::optional_ref 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 &&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(name_and_ns).value(); diff --git a/src/class_diagram/model/diagram.h b/src/class_diagram/model/diagram.h index 26cfb29c..0f3d033e 100644 --- a/src/class_diagram/model/diagram.h +++ b/src/class_diagram/model/diagram.h @@ -45,32 +45,28 @@ public: common::model::diagram_t type() const override; - std::optional< - std::reference_wrapper> - get(const std::string &full_name) const override; + common::optional_ref get( + const std::string &full_name) const override; - std::optional< - std::reference_wrapper> - get(const clanguml::common::model::diagram_element::id_t id) const override; + common::optional_ref get( + const clanguml::common::model::diagram_element::id_t id) const override; - const std::vector> &classes() const; + const common::reference_vector &classes() const; - const std::vector> &enums() const; + const common::reference_vector &enums() const; bool has_class(const class_ &c) const; bool has_enum(const enum_ &e) const; - std::optional> get_class( - const std::string &name) const; + common::optional_ref get_class(const std::string &name) const; - std::optional> get_class( + common::optional_ref get_class( clanguml::common::model::diagram_element::id_t id) const; - std::optional> get_enum( - const std::string &name) const; + common::optional_ref get_enum(const std::string &name) const; - std::optional> get_enum( + common::optional_ref get_enum( clanguml::common::model::diagram_element::id_t id) const; void add_type_alias(std::unique_ptr &&ta); @@ -92,9 +88,9 @@ public: const clanguml::common::model::diagram_element::id_t id) const override; private: - std::vector> classes_; + common::reference_vector classes_; - std::vector> enums_; + common::reference_vector enums_; std::map> type_aliases_; }; diff --git a/src/class_diagram/visitor/element_visitor_context.cc.bak b/src/class_diagram/visitor/element_visitor_context.cc.bak deleted file mode 100644 index ed81432d..00000000 --- a/src/class_diagram/visitor/element_visitor_context.cc.bak +++ /dev/null @@ -1,57 +0,0 @@ -/** - * src/class_diagram/model/visitor/element_visitor_context.cc - * - * Copyright (c) 2021-2022 Bartek Kryza - * - * 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 -element_visitor_context::element_visitor_context( - clanguml::class_diagram::model::diagram &diagram, T &element) - : element_{element} - , diagram_{diagram} -{ -} - -template -void element_visitor_context::set_parent_class( - clanguml::class_diagram::model::class_ *parent_class) -{ - parent_class_ = parent_class; -} - -template -clanguml::class_diagram::model::class_ * -element_visitor_context::parent_class() -{ - return parent_class_; -} - -template T &element_visitor_context::element() -{ - return element_; -} - -template -clanguml::class_diagram::model::diagram &element_visitor_context::diagram() -{ - return diagram_; -} -} diff --git a/src/class_diagram/visitor/element_visitor_context.h b/src/class_diagram/visitor/element_visitor_context.h deleted file mode 100644 index cf6bd31f..00000000 --- a/src/class_diagram/visitor/element_visitor_context.h +++ /dev/null @@ -1,48 +0,0 @@ -/** - * src/class_diagram/model/visitor/element_visitor_context.h - * - * Copyright (c) 2021-2022 Bartek Kryza - * - * 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 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_; -}; - -} diff --git a/src/class_diagram/visitor/translation_unit_context.cc.bak b/src/class_diagram/visitor/translation_unit_context.cc.bak deleted file mode 100644 index 83655608..00000000 --- a/src/class_diagram/visitor/translation_unit_context.cc.bak +++ /dev/null @@ -1,273 +0,0 @@ -/** - * src/class_diagram/visitor/translation_unit_context.cc - * - * Copyright (c) 2021-2022 Bartek Kryza - * - * 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 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 -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 -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 &&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 -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 -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 &&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 -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 p) -{ - current_package_ = p; -} - -type_safe::optional_ref -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 & -translation_unit_context::using_namespace_directive( - const common::model::namespace_ &ns) const -{ - return using_ns_declarations_.at(ns.to_string()); -} - -type_safe::optional -translation_unit_context::get_name_with_namespace(const std::string &name) const -{ - using common::model::namespace_; - - std::set 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 {}; -} - -} diff --git a/src/class_diagram/visitor/translation_unit_context.h b/src/class_diagram/visitor/translation_unit_context.h deleted file mode 100644 index 62cb7293..00000000 --- a/src/class_diagram/visitor/translation_unit_context.h +++ /dev/null @@ -1,127 +0,0 @@ -/** - * src/class_diagram/visitor/translation_unit_context.h - * - * Copyright (c) 2021-2022 Bartek Kryza - * - * 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 -#include -#include -#include - -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 ref); - - type_safe::object_ref get_namespace_alias( - const std::string &full_name) const; - - type_safe::object_ref - 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 &&ref); - - type_safe::object_ref get_type_alias( - const std::string &full_name) const; - - type_safe::object_ref 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 &&ref); - - type_safe::object_ref 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 p); - - type_safe::optional_ref get_current_package() const; - - void add_using_namespace_directive(common::model::namespace_ ns); - - const std::set &using_namespace_directive( - const common::model::namespace_ &ns) const; - - type_safe::optional 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> - 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> - namespace_alias_index_; - - // Map of discovered aliases (declared with 'using' keyword) - std::map> - alias_index_; - - // Map of discovered template aliases (declared with 'using' keyword) - std::map> - alias_template_index_; - - type_safe::optional_ref current_package_; -}; - -} diff --git a/src/class_diagram/visitor/translation_unit_visitor.cc b/src/class_diagram/visitor/translation_unit_visitor.cc index da5f3125..81c8c4e2 100644 --- a/src/class_diagram/visitor/translation_unit_visitor.cc +++ b/src/class_diagram/visitor/translation_unit_visitor.cc @@ -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; } diff --git a/src/common/generators/plantuml/generator.h b/src/common/generators/plantuml/generator.h index 2a699c17..71de76ca 100644 --- a/src/common/generators/plantuml/generator.h +++ b/src/common/generators/plantuml/generator.h @@ -163,9 +163,9 @@ void generator::generate_config_layout_hints(std::ostream &ostr) const if (!element_opt || !hint_element_opt) continue; - hint_str << element_opt.value().get().alias() << " -[hidden]" + hint_str << element_opt.value().alias() << " -[hidden]" << clanguml::config::to_string(hint.hint) << "- " - << hint_element_opt.value().get().alias() << '\n'; + << hint_element_opt.value().alias() << '\n'; ostr << hint_str.str(); } catch (clanguml::error::uml_alias_missing &e) { @@ -196,8 +196,7 @@ void generator::generate_plantuml_directives( if (element_opt) directive.replace(std::get<1>(alias_match), - std::get<2>(alias_match), - element_opt.value().get().alias()); + std::get<2>(alias_match), element_opt.value().alias()); else { LOG_ERROR( "CANNOT FIND ALIAS TO ELEMENT {}", full_name.to_string()); @@ -426,7 +425,7 @@ template void generator::init_env() m_config.using_namespace() | args[0]->get(); auto element_opt = m_model.get(alias_match.to_string()); - return element_opt.value().get().alias(); + return element_opt.value().alias(); }); m_env.add_callback("comment", 1, [this](inja::Arguments &args) { @@ -441,7 +440,7 @@ template void generator::init_env() } if (element.has_value()) { - auto comment = element.value().get().comment(); + auto comment = element.value().comment(); if (comment.has_value()) res = comment.value(); diff --git a/src/common/model/diagram.h b/src/common/model/diagram.h index 0d58506a..4483072d 100644 --- a/src/common/model/diagram.h +++ b/src/common/model/diagram.h @@ -39,13 +39,11 @@ public: virtual diagram_t type() const = 0; - virtual std::optional< - std::reference_wrapper> - get(const std::string &full_name) const = 0; + virtual common::optional_ref get( + const std::string &full_name) const = 0; - virtual std::optional< - std::reference_wrapper> - get(const diagram_element::id_t id) const = 0; + virtual common::optional_ref get( + const diagram_element::id_t id) const = 0; diagram(const diagram &) = delete; diagram(diagram &&); diff --git a/src/common/model/diagram_filter.cc b/src/common/model/diagram_filter.cc index 6fa3b616..6d6452f5 100644 --- a/src/common/model/diagram_filter.cc +++ b/src/common/model/diagram_filter.cc @@ -295,25 +295,25 @@ tvl::value_t context_filter::match(const diagram &d, const element &e) const if (context_root.has_value()) { // This is a direct match to the context root - if (context_root.value().get().id() == e.id()) + if (context_root.value().id() == e.id()) return true; // Return a positive match if the element e is in a direct // relationship with any of the context_root's for (const relationship &rel : - context_root.value().get().relationships()) { + context_root.value().relationships()) { if (rel.destination() == e.id()) return true; } for (const relationship &rel : e.relationships()) { - if (rel.destination() == context_root.value().get().id()) + if (rel.destination() == context_root.value().id()) return true; } // Return a positive match if the context_root is a parent // of the element for (const class_diagram::model::class_parent &p : - context_root.value().get().parents()) { + context_root.value().parents()) { if (p.name() == e.full_name(false)) return true; } @@ -322,8 +322,7 @@ tvl::value_t context_filter::match(const diagram &d, const element &e) const for (const class_diagram::model::class_parent &p : static_cast(e) .parents()) { - if (p.name() == - context_root.value().get().full_name(false)) + if (p.name() == context_root.value().full_name(false)) return true; } } diff --git a/src/common/model/diagram_filter.h b/src/common/model/diagram_filter.h index aa8ddd9c..e0e86e6c 100644 --- a/src/common/model/diagram_filter.h +++ b/src/common/model/diagram_filter.h @@ -221,7 +221,7 @@ private: while (parent.has_value()) { parents.emplace(std::ref(parent.value())); parent = detail::get( - cd, parent.value().get().path().to_string()); + cd, parent.value().path().to_string()); } }); diff --git a/src/common/model/namespace.h b/src/common/model/namespace.h index 9ba3faad..1a255b8c 100644 --- a/src/common/model/namespace.h +++ b/src/common/model/namespace.h @@ -19,8 +19,8 @@ #include "path.h" +#include #include -#include #include namespace clanguml::common::model { diff --git a/src/common/model/nested_trait.h b/src/common/model/nested_trait.h index bd134a7f..e902ad0b 100644 --- a/src/common/model/nested_trait.h +++ b/src/common/model/nested_trait.h @@ -19,9 +19,8 @@ #include "util/util.h" -#include - #include +#include #include #include @@ -84,7 +83,7 @@ public: if (path.is_empty() || !has_element(path[0])) { LOG_DBG("Nested element {} not found in element", path.to_string()); - return type_safe::optional_ref{}; + return optional_ref{}; } if (path.size() == 1) { @@ -94,13 +93,13 @@ public: auto p = get_element(path[0]); if (!p) - return type_safe::optional_ref{}; + return optional_ref{}; if (dynamic_cast *>(&p.value())) return dynamic_cast &>(p.value()) .get_element(Path{path.begin() + 1, path.end()}); - return type_safe::optional_ref{}; + return optional_ref{}; } template auto get_element_parent(const T &element) const @@ -109,10 +108,10 @@ public: auto parent = get_element(path); if (parent.has_value()) - return type_safe::optional_ref{ - type_safe::ref(dynamic_cast(parent.value()))}; + return optional_ref{ + std::ref(dynamic_cast(parent.value()))}; - return type_safe::optional_ref{}; + return optional_ref{}; } template auto get_element(const std::string &name) const @@ -123,15 +122,14 @@ public: [&](const auto &p) { return name == p->name(); }); if (it == elements_.end()) - return type_safe::optional_ref{type_safe::nullopt}; + return optional_ref{}; assert(it->get() != nullptr); if (dynamic_cast(it->get())) - return type_safe::optional_ref{ - type_safe::ref(dynamic_cast(*it->get()))}; + return optional_ref{std::ref(dynamic_cast(*it->get()))}; - return type_safe::optional_ref{type_safe::nullopt}; + return optional_ref{}; } bool has_element(const std::string &name) const diff --git a/src/common/model/package.h b/src/common/model/package.h index 6ae4c702..911fbab0 100644 --- a/src/common/model/package.h +++ b/src/common/model/package.h @@ -24,7 +24,6 @@ #include "util/util.h" #include -#include #include #include diff --git a/src/common/model/path.h b/src/common/model/path.h index 912d66f5..18e8f414 100644 --- a/src/common/model/path.h +++ b/src/common/model/path.h @@ -19,8 +19,8 @@ #include "util/util.h" +#include #include -#include #include namespace clanguml::common::model { @@ -130,7 +130,7 @@ public: void pop_back() { path_.pop_back(); } - type_safe::optional parent() const + std::optional parent() const { if (size() <= 1) { return {}; diff --git a/src/common/model/source_file.h b/src/common/model/source_file.h index 3c0d5c41..092e57b5 100644 --- a/src/common/model/source_file.h +++ b/src/common/model/source_file.h @@ -27,7 +27,6 @@ #include "util/util.h" #include -#include #include #include diff --git a/src/common/types.h b/src/common/types.h index ad6ef5ae..f25ad344 100644 --- a/src/common/types.h +++ b/src/common/types.h @@ -17,6 +17,7 @@ */ #pragma once +#include #include #include #include @@ -26,8 +27,89 @@ namespace clanguml::common { using id_t = int64_t; -template -using optional_ref = std::optional>; +template class optional_ref { +public: + using optional_type = T; + + optional_ref() + : value_{nullptr} + { + } + + optional_ref(T &value) { value_ = &value; } + + optional_ref(const T &value) { value_ = &value; } + + optional_ref(optional_ref &right) { value_ = right.get(); } + + template || + std::is_same_v>> + optional_ref(const V &t) + { + value_ = t.get(); + } + + template || + std::is_same_v>> + optional_ref(V &&t) + { + value_ = t.get(); + t.reset(); + } + + template >> + optional_ref(const std::reference_wrapper &t) + { + value_ = &t.get(); + } + + optional_ref &operator=(const optional_ref &right) + { + if (this == &right) + return *this; + + value_ = right.value_; + return *this; + } + + optional_ref &operator=(optional_ref &&right) noexcept + { + if (this == &right) + return *this; + + value_ = right.value_; + right.reset(); + return *this; + } + + bool has_value() const noexcept { return value_ != nullptr; } + + operator bool() const noexcept { return has_value(); } + + const T &value() const + { + assert(value_ != nullptr); + return *value_; + } + + T &value() + { + assert(value_ != nullptr); + return *value_; + } + + void reset() { value_ = nullptr; } + + T *get() const { return value_; } + +private: + T *value_; +}; template using reference_vector = std::vector>; diff --git a/src/cx/util.cc b/src/cx/util.cc index 1b069205..7f80566e 100644 --- a/src/cx/util.cc +++ b/src/cx/util.cc @@ -19,112 +19,12 @@ #include "cx/util.h" #include "util/util.h" -#include -#include -#include -#include #include #include #include -namespace clanguml { -namespace cx { -namespace util { - -std::string full_name( - const common::model::namespace_ ¤t_ns, const cppast::cpp_entity &e) -{ - if (e.name().empty()) - return ""; - else if (cppast::is_parameter(e.kind())) - // parameters don't have a full name - return e.name(); - - std::vector fn; - - for (const auto &ns : current_ns) { - if (!ns.empty()) - fn.push_back(ns); - } - - fn.push_back(e.name()); - - return fmt::format("{}", fmt::join(fn, "::")); -} - -std::string full_name(const cppast::cpp_type &t, - const cppast::cpp_entity_index &idx, bool inside_class) -{ - std::string t_ns; - if (!inside_class) { - t_ns = ns(cppast::remove_cv(unreferenced(t)), idx); - } - - auto t_name = cppast::to_string(t); - - if (t_ns.size() > 0 && - t_name.substr(0, t_name.find("<")).find("::") == std::string::npos) - return t_ns + "::" + t_name; - - return cppast::to_string(t); -} - -std::string ns(const cppast::cpp_entity &e) -{ - std::vector res{}; - - auto it = e.parent(); - while (it) { - if (it.value().kind() == cppast::cpp_entity_kind::namespace_t) { - const auto &ns = - static_cast(it.value()); - if (!ns.name().empty() && !ns.is_inline()) - res.push_back(it.value().name()); - } - it = it.value().parent(); - } - - if (res.empty()) - return ""; - - std::reverse(res.begin(), res.end()); - - return fmt::format("{}", fmt::join(res, "::")); -} - -type_safe::optional_ref entity_ns( - const cppast::cpp_entity &e) -{ - std::vector res{}; - - if (e.kind() == cppast::cpp_entity_kind::namespace_t) - return type_safe::optional_ref( - static_cast(e)); - - auto it = e.parent(); - while (it) { - if (it.value().kind() == cppast::cpp_entity_kind::namespace_t) { - return type_safe::optional_ref( - static_cast(it.value())); - } - it = it.value().parent(); - } - - return {}; -} - -bool is_inside_class(const cppast::cpp_entity &e) -{ - auto it = e.parent(); - while (it) { - if (it.value().kind() == cppast::cpp_entity_kind::class_t) { - return true; - } - it = it.value().parent(); - } - return false; -} +namespace clanguml::cx::util { std::pair split_ns( const std::string &full_name) @@ -139,123 +39,6 @@ std::pair split_ns( return {ns, name}; } -std::string ns(const cppast::cpp_type &t, const cppast::cpp_entity_index &idx) -{ - if (t.kind() == cppast::cpp_type_kind::user_defined_t && - (static_cast(t) - .entity() - .get(idx) - .size() > 0)) { - // If this is a user defined type - return the namespace of the - // entity - return ns(static_cast(t) - .entity() - .get(idx)[0] - .get()); - } - else if (t.kind() == cppast::cpp_type_kind::template_instantiation_t) { - if (static_cast(t) - .primary_template() - .get(idx) - .size() > 0) { - return ns( - static_cast(t) - .primary_template() - .get(idx)[0] - .get()); - } - else { - return {}; - } - } - else { - auto canon = cppast::to_string(t.canonical()); - auto full_name = canon.substr(0, canon.find("<")); - if (full_name.empty()) { - return ""; - } - else if (canon.find("type-parameter-") == std::string::npos) { - // This is an easy case, canonical representation contains full - // namespace - auto ns_toks = clanguml::util::split(full_name, "::"); - if (ns_toks.size() > 0) - ns_toks.pop_back(); - return fmt::format( - "{}", fmt::join(ns_toks.begin(), ns_toks.end(), "::")); - } - else if (canon.find("type-parameter-") == 0) { - return ""; - } - else { - // This is a bug/feature in libclang, where canonical representation - // of a template type with incomplete specialization doesn't have a - // full namespace. We have to extract it from the primary template - const auto &primary_template = - static_cast(t) - .primary_template(); - if (!primary_template.is_overloaded()) { - LOG_DBG( - "Cannot establish namespace for ", cppast::to_string(t)); - return ""; - } - return ns(primary_template.get(idx)[0].get()); - } - } -} - -std::string fully_prefixed( - const common::model::namespace_ ¤t_ns, const cppast::cpp_entity &e) -{ - if (e.name().find("::") != std::string::npos) { - // the name already contains namespace, but it could be not - // absolute, i.e. relative to some supernamespace of current - // namespace context - std::list res; - - for (const auto &n : clanguml::util::split(e.name(), "::")) - res.push_back(n); - - std::list prefix_ns; - for (const auto &n : current_ns) { - if (!n.empty() && n != res.front()) - prefix_ns.push_back(n); - else - break; - } - - prefix_ns.reverse(); - for (const auto &n : prefix_ns) - res.push_front(n); - - return fmt::format("{}", fmt::join(res, "::")); - } - - std::vector res{e.name()}; - - auto it = e.parent(); - while (it) { - if (it.value().kind() == cppast::cpp_entity_kind::namespace_t) { - if (!it.value().name().empty()) - res.push_back(it.value().name()); - } - it = it.value().parent(); - } - - return fmt::format("{}", fmt::join(res.rbegin(), res.rend(), "::")); -} - -const cppast::cpp_type &unreferenced(const cppast::cpp_type &t) -{ - if (t.kind() == cppast::cpp_type_kind::pointer_t) - return unreferenced( - static_cast(t).pointee()); - else if (t.kind() == cppast::cpp_type_kind::reference_t) - return unreferenced( - static_cast(t).referee()); - - return t; -} - std::vector parse_unexposed_template_params(const std::string ¶ms, std::function ns_resolve) @@ -334,6 +117,4 @@ parse_unexposed_template_params(const std::string ¶ms, return res; } -} // namespace util -} // namespace cx -} // namespace clanguml +} // namespace clanguml::cx::util diff --git a/src/cx/util.h b/src/cx/util.h index 6a052095..c64ae55a 100644 --- a/src/cx/util.h +++ b/src/cx/util.h @@ -21,43 +21,17 @@ #include #include -#include -#include #include #include -namespace clanguml { -namespace cx { -namespace util { - -std::string full_name( - const common::model::namespace_ ¤t_ns, const cppast::cpp_entity &e); - -std::string full_name(const cppast::cpp_type &t, - const cppast::cpp_entity_index &idx, bool inside_class); - -std::string fully_prefixed( - const common::model::namespace_ ¤t_ns, const cppast::cpp_entity &e); - -const cppast::cpp_type &unreferenced(const cppast::cpp_type &t); - -std::string ns(const cppast::cpp_entity &e); - -type_safe::optional_ref entity_ns( - const cppast::cpp_entity &e); - -std::string ns(const cppast::cpp_type &t, const cppast::cpp_entity_index &idx); +namespace clanguml::cx::util { std::pair split_ns( const std::string &full_name); -bool is_inside_class(const cppast::cpp_entity &e); - std::vector parse_unexposed_template_params(const std::string ¶ms, std::function ns_resolve); -} // namespace util -} // namespace cx -} // namespace clanguml +} // namespace clanguml::cx::util diff --git a/src/include_diagram/generators/plantuml/include_diagram_generator.cc b/src/include_diagram/generators/plantuml/include_diagram_generator.cc index 3676031d..0156c160 100644 --- a/src/include_diagram/generators/plantuml/include_diagram_generator.cc +++ b/src/include_diagram/generators/plantuml/include_diagram_generator.cc @@ -49,13 +49,12 @@ void generator::generate_relationships( [this](const auto &r) { return m_model.should_include(r.type()) && util::contains(m_generated_aliases, - m_model.get(r.destination()).value().get().alias()); + m_model.get(r.destination()).value().alias()); }, [&f, &ostr, this](const auto &r) { ostr << f.alias() << " " << plantuml_common::to_plantuml(r.type(), r.style()) << " " - << m_model.get(r.destination()).value().get().alias() - << '\n'; + << m_model.get(r.destination()).value().alias() << '\n'; }); } } diff --git a/src/include_diagram/generators/plantuml/include_diagram_generator.h b/src/include_diagram/generators/plantuml/include_diagram_generator.h index 3c1f1d7b..c4ff08e5 100644 --- a/src/include_diagram/generators/plantuml/include_diagram_generator.h +++ b/src/include_diagram/generators/plantuml/include_diagram_generator.h @@ -26,9 +26,6 @@ #include "include_diagram/visitor/translation_unit_visitor.h" #include "util/util.h" -#include -#include - #include #include #include diff --git a/src/include_diagram/visitor/element_visitor_context.h b/src/include_diagram/visitor/element_visitor_context.h deleted file mode 100644 index 7dfc7e7a..00000000 --- a/src/include_diagram/visitor/element_visitor_context.h +++ /dev/null @@ -1,42 +0,0 @@ -/** - * src/include_diagram/model/visitor/element_visitor_context.h - * - * Copyright (c) 2021-2022 Bartek Kryza - * - * 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 "include_diagram/model/diagram.h" - -namespace clanguml::include_diagram::visitor { - -class translation_unit_context; - -template class element_visitor_context { -public: - element_visitor_context( - clanguml::include_diagram::model::diagram &diagram, T &element); - - T &element(); - - clanguml::include_diagram::model::diagram &diagram(); - -private: - translation_unit_context *ctx_; - - T &element_; - clanguml::include_diagram::model::diagram &diagram_; -}; - -} diff --git a/src/include_diagram/visitor/translation_unit_context.h b/src/include_diagram/visitor/translation_unit_context.h deleted file mode 100644 index 5d72912a..00000000 --- a/src/include_diagram/visitor/translation_unit_context.h +++ /dev/null @@ -1,62 +0,0 @@ -/** - * src/include_diagram/visitor/translation_unit_context.h - * - * Copyright (c) 2021-2022 Bartek Kryza - * - * 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 "common/model/package.h" -#include "config/config.h" -#include "include_diagram/model/diagram.h" - -#include -#include -#include -#include - -namespace clanguml::include_diagram::visitor { - -class translation_unit_context { -public: - translation_unit_context(cppast::cpp_entity_index &idx, - clanguml::include_diagram::model::diagram &diagram, - const clanguml::config::include_diagram &config); - - const cppast::cpp_entity_index &entity_index() const; - - const clanguml::config::include_diagram &config() const; - - clanguml::include_diagram::model::diagram &diagram(); - - void set_current_file( - type_safe::optional_ref p); - - type_safe::optional_ref - get_current_file() const; - -private: - // Reference to the cppast entity index - cppast::cpp_entity_index &entity_index_; - - // Reference to the output diagram model - clanguml::include_diagram::model::diagram &diagram_; - - // Reference to class diagram config - const clanguml::config::include_diagram &config_; - - type_safe::optional_ref current_file_; -}; - -} diff --git a/src/include_diagram/visitor/translation_unit_visitor.cc b/src/include_diagram/visitor/translation_unit_visitor.cc index 1dc6a628..34f52984 100644 --- a/src/include_diagram/visitor/translation_unit_visitor.cc +++ b/src/include_diagram/visitor/translation_unit_visitor.cc @@ -132,7 +132,6 @@ translation_unit_visitor::include_visitor::process_internal_header( diagram() .get(current_file_id) .value() - .get() .add_relationship(common::model::relationship{ relationship_type, include_file.id()}); } @@ -161,7 +160,6 @@ translation_unit_visitor::include_visitor::process_external_system_header( diagram() .get(current_file_id) .value() - .get() .add_relationship(common::model::relationship{ common::model::relationship_t::kDependency, f_id}); } diff --git a/src/include_diagram/visitor/translation_unit_visitor.h b/src/include_diagram/visitor/translation_unit_visitor.h index c044353d..586c854b 100644 --- a/src/include_diagram/visitor/translation_unit_visitor.h +++ b/src/include_diagram/visitor/translation_unit_visitor.h @@ -21,7 +21,6 @@ #include "common/model/package.h" #include "config/config.h" #include "include_diagram/model/diagram.h" -#include "include_diagram/visitor/translation_unit_context.h" #include #include diff --git a/src/main.cc b/src/main.cc index 34012dd0..e15af019 100644 --- a/src/main.cc +++ b/src/main.cc @@ -25,7 +25,6 @@ #include "version.h" #include -#include #include #include diff --git a/src/package_diagram/generators/plantuml/package_diagram_generator.h b/src/package_diagram/generators/plantuml/package_diagram_generator.h index f4eaeec5..13324f0f 100644 --- a/src/package_diagram/generators/plantuml/package_diagram_generator.h +++ b/src/package_diagram/generators/plantuml/package_diagram_generator.h @@ -25,9 +25,6 @@ #include "package_diagram/visitor/translation_unit_visitor.h" #include "util/util.h" -#include -#include - #include #include #include diff --git a/src/package_diagram/model/diagram.cc b/src/package_diagram/model/diagram.cc index 96803b9b..3f633682 100644 --- a/src/package_diagram/model/diagram.cc +++ b/src/package_diagram/model/diagram.cc @@ -28,7 +28,7 @@ common::model::diagram_t diagram::type() const return common::model::diagram_t::kPackage; } -const std::vector> & +const common::reference_vector & diagram::packages() const { return packages_; @@ -45,8 +45,8 @@ void diagram::add_package(std::unique_ptr &&p) add_element(ns, std::move(p)); } -std::optional> -diagram::get_package(const std::string &name) const +common::optional_ref diagram::get_package( + const std::string &name) const { for (const auto &p : packages_) { auto p_full_name = p.get().full_name(false); @@ -58,8 +58,7 @@ diagram::get_package(const std::string &name) const return {}; } -std::optional> -diagram::get_package( +common::optional_ref diagram::get_package( const clanguml::common::model::diagram_element::id_t id) const { for (const auto &p : packages_) { @@ -71,14 +70,14 @@ diagram::get_package( return {}; } -std::optional> -diagram::get(const std::string &full_name) const +common::optional_ref diagram::get( + const std::string &full_name) const { return get_package(full_name); } -std::optional> -diagram::get(const clanguml::common::model::diagram_element::id_t id) const +common::optional_ref diagram::get( + const clanguml::common::model::diagram_element::id_t id) const { return get_package(id); } diff --git a/src/package_diagram/model/diagram.h b/src/package_diagram/model/diagram.h index e7209ea0..dacb6b2c 100644 --- a/src/package_diagram/model/diagram.h +++ b/src/package_diagram/model/diagram.h @@ -39,31 +39,28 @@ public: common::model::diagram_t type() const override; - const std::vector> - &packages() const; + const common::reference_vector & + packages() const; - std::optional< - std::reference_wrapper> - get(const std::string &full_name) const override; + common::optional_ref get( + const std::string &full_name) const override; - std::optional< - std::reference_wrapper> - get(const clanguml::common::model::diagram_element::id_t id) const override; + common::optional_ref get( + const clanguml::common::model::diagram_element::id_t id) const override; void add_package(std::unique_ptr &&p); - std::optional> - get_package(const std::string &name) const; + common::optional_ref get_package( + const std::string &name) const; - std::optional> get_package( + common::optional_ref get_package( const clanguml::common::model::diagram_element::id_t id) const; std::string to_alias( const clanguml::common::model::diagram_element::id_t) const; private: - std::vector> - packages_; + common::reference_vector packages_; }; } diff --git a/src/package_diagram/visitor/element_visitor_context.h b/src/package_diagram/visitor/element_visitor_context.h deleted file mode 100644 index bcf7650e..00000000 --- a/src/package_diagram/visitor/element_visitor_context.h +++ /dev/null @@ -1,42 +0,0 @@ -/** - * src/package_diagram/model/visitor/element_visitor_context.h - * - * Copyright (c) 2021-2022 Bartek Kryza - * - * 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 "package_diagram/model/diagram.h" - -namespace clanguml::package_diagram::visitor { - -class translation_unit_context; - -template class element_visitor_context { -public: - element_visitor_context( - clanguml::package_diagram::model::diagram &diagram, T &element); - - T &element(); - - clanguml::package_diagram::model::diagram &diagram(); - -private: - translation_unit_context *ctx_; - - T &element_; - clanguml::package_diagram::model::diagram &diagram_; -}; - -} diff --git a/src/package_diagram/visitor/translation_unit_context.h b/src/package_diagram/visitor/translation_unit_context.h deleted file mode 100644 index fbb075a6..00000000 --- a/src/package_diagram/visitor/translation_unit_context.h +++ /dev/null @@ -1,111 +0,0 @@ -/** - * src/package_diagram/visitor/translation_unit_context.h - * - * Copyright (c) 2021-2022 Bartek Kryza - * - * 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 "common/model/package.h" -#include "config/config.h" -#include "package_diagram/model/diagram.h" - -#include -#include -#include -#include - -namespace clanguml::package_diagram::visitor { - -class translation_unit_context { -public: - translation_unit_context(cppast::cpp_entity_index &idx, - clanguml::package_diagram::model::diagram &diagram, - const clanguml::config::package_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 ref); - - type_safe::object_ref get_namespace_alias( - const std::string &full_name) const; - - type_safe::object_ref - 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 &&ref); - - type_safe::object_ref get_type_alias( - const std::string &full_name) const; - - type_safe::object_ref 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 &&ref); - - type_safe::object_ref 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::package_diagram &config() const; - - clanguml::package_diagram::model::diagram &diagram(); - - void set_current_package(type_safe::optional_ref p); - - type_safe::optional_ref get_current_package() const; - -private: - // Current visitor namespace - common::model::namespace_ namespace_; - - // Reference to the cppast entity index - cppast::cpp_entity_index &entity_index_; - - // Reference to the output diagram model - clanguml::package_diagram::model::diagram &diagram_; - - // Reference to class diagram config - const clanguml::config::package_diagram &config_; - - // Map of discovered aliases (declared with 'namespace' keyword) - std::map> - namespace_alias_index_; - - // Map of discovered type aliases (declared with 'using' keyword) - std::map> - alias_index_; - - // Map of discovered template aliases (declared with 'using' keyword) - std::map> - alias_template_index_; - - type_safe::optional_ref current_package_; -}; - -} diff --git a/src/package_diagram/visitor/translation_unit_visitor.cc b/src/package_diagram/visitor/translation_unit_visitor.cc index 4ac33684..d71e1d4f 100644 --- a/src/package_diagram/visitor/translation_unit_visitor.cc +++ b/src/package_diagram/visitor/translation_unit_visitor.cc @@ -164,7 +164,7 @@ void translation_unit_visitor::add_relationships( const auto destination_id = std::get<0>(dependency); relationship r{relationship_t::kDependency, destination_id}; if (destination_id != current_package_id) - current_package.value().get().add_relationship(std::move(r)); + current_package.value().add_relationship(std::move(r)); } } } diff --git a/src/package_diagram/visitor/translation_unit_visitor.h b/src/package_diagram/visitor/translation_unit_visitor.h index 0e760013..0be26d7a 100644 --- a/src/package_diagram/visitor/translation_unit_visitor.h +++ b/src/package_diagram/visitor/translation_unit_visitor.h @@ -19,7 +19,6 @@ #include "config/config.h" #include "package_diagram/model/diagram.h" -#include "package_diagram/visitor/translation_unit_context.h" #include #include diff --git a/src/sequence_diagram/generators/plantuml/sequence_diagram_generator.cc b/src/sequence_diagram/generators/plantuml/sequence_diagram_generator.cc index fc98907d..a90b1983 100644 --- a/src/sequence_diagram/generators/plantuml/sequence_diagram_generator.cc +++ b/src/sequence_diagram/generators/plantuml/sequence_diagram_generator.cc @@ -18,18 +18,12 @@ #include "sequence_diagram_generator.h" -#include "sequence_diagram/visitor/translation_unit_context.h" - -#include -#include - namespace clanguml::sequence_diagram::generators::plantuml { using clanguml::common::model::message_t; using clanguml::config::source_location; using clanguml::sequence_diagram::model::activity; using clanguml::sequence_diagram::model::message; -using clanguml::sequence_diagram::visitor::translation_unit_context; using namespace clanguml::util; // diff --git a/src/sequence_diagram/generators/plantuml/sequence_diagram_generator.h b/src/sequence_diagram/generators/plantuml/sequence_diagram_generator.h index 9b36eede..30e9ef63 100644 --- a/src/sequence_diagram/generators/plantuml/sequence_diagram_generator.h +++ b/src/sequence_diagram/generators/plantuml/sequence_diagram_generator.h @@ -23,7 +23,6 @@ #include "sequence_diagram/visitor/translation_unit_visitor.h" #include "util/util.h" -#include #include #include diff --git a/src/sequence_diagram/visitor/translation_unit_context.h b/src/sequence_diagram/visitor/translation_unit_context.h deleted file mode 100644 index 6a71ab3d..00000000 --- a/src/sequence_diagram/visitor/translation_unit_context.h +++ /dev/null @@ -1,58 +0,0 @@ -/** - * src/sequence_diagram/visitor/translation_unit_context.h - * - * Copyright (c) 2021-2022 Bartek Kryza - * - * 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 "sequence_diagram/model/diagram.h" - -#include - -#include -#include -#include - -namespace clanguml::sequence_diagram::visitor { - -class translation_unit_context { -public: - translation_unit_context(cppast::cpp_entity_index &idx, - clanguml::sequence_diagram::model::diagram &diagram, - const clanguml::config::sequence_diagram &config); - - void push_namespace(const std::string &ns); - - void pop_namespace(); - - const std::vector &get_namespace() const; - - const cppast::cpp_entity_index &entity_index() const; - - const clanguml::config::sequence_diagram &config() const; - - clanguml::sequence_diagram::model::diagram &diagram(); - -private: - // Reference to the cppast entity index - cppast::cpp_entity_index &entity_index_; - clanguml::sequence_diagram::model::diagram &diagram_; - const clanguml::config::sequence_diagram &config_; - - std::vector namespace_; -}; - -} diff --git a/src/sequence_diagram/visitor/translation_unit_visitor.cc b/src/sequence_diagram/visitor/translation_unit_visitor.cc index 6559ef9a..76f99b05 100644 --- a/src/sequence_diagram/visitor/translation_unit_visitor.cc +++ b/src/sequence_diagram/visitor/translation_unit_visitor.cc @@ -20,7 +20,6 @@ #include "common/model/namespace.h" #include "cx/util.h" -#include "translation_unit_context.h" namespace clanguml::sequence_diagram::visitor { @@ -36,16 +35,6 @@ translation_unit_visitor::translation_unit_visitor(clang::SourceManager &sm, /* void translation_unit_visitor::process_activities(const cppast::cpp_function &e) { - using clanguml::common::model::message_t; - using clanguml::sequence_diagram::model::activity; - using clanguml::sequence_diagram::model::diagram; - using clanguml::sequence_diagram::model::message; - using cppast::cpp_entity; - using cppast::cpp_entity_kind; - using cppast::cpp_function; - using cppast::cpp_member_function; - using cppast::cpp_member_function_call; - using cppast::visitor_info; for (const auto &function_call_ptr : e.function_calls()) { const auto &function_call = @@ -129,12 +118,6 @@ void translation_unit_visitor::process_activities(const cppast::cpp_function &e) void translation_unit_visitor::operator()(const cppast::cpp_entity &file) { - using cppast::cpp_entity; - using cppast::cpp_entity_kind; - using cppast::cpp_function; - using cppast::cpp_member_function; - using cppast::cpp_member_function_call; - using cppast::visitor_info; cppast::visit(file, [&, this](const cpp_entity &e, visitor_info) { if (e.kind() == cpp_entity_kind::function_t) { diff --git a/src/sequence_diagram/visitor/translation_unit_visitor.h b/src/sequence_diagram/visitor/translation_unit_visitor.h index 043d083d..b8d8107f 100644 --- a/src/sequence_diagram/visitor/translation_unit_visitor.h +++ b/src/sequence_diagram/visitor/translation_unit_visitor.h @@ -19,7 +19,6 @@ #include "config/config.h" #include "sequence_diagram/model/diagram.h" -#include "sequence_diagram/visitor/translation_unit_context.h" #include #include diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 28b9cef5..d634e70b 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -14,7 +14,6 @@ file(GLOB_RECURSE TEST_CONFIG_YMLS test_config_data/*.yml) set(CLANG_UML_TEST_LIBRARIES clang-umllib - cppast ${YAML_CPP_LIBRARIES} ${LIBTOOLING_LIBS} ${LIBCLANG_LIBRARIES} diff --git a/tests/t40001/include/t40001_include1.h b/tests/t40001/include/t40001_include1.h index f43f00eb..a4872688 100644 --- a/tests/t40001/include/t40001_include1.h +++ b/tests/t40001/include/t40001_include1.h @@ -2,7 +2,7 @@ #include "lib1/lib1.h" -#include +#include #include diff --git a/tests/t40001/test_case.h b/tests/t40001/test_case.h index 2f089af5..51589942 100644 --- a/tests/t40001/test_case.h +++ b/tests/t40001/test_case.h @@ -41,7 +41,7 @@ TEST_CASE("t40001", "[test-case][package]") REQUIRE_THAT(puml, IsFile("t40001_include1.h")); REQUIRE_THAT(puml, IsFile("string")); - REQUIRE_THAT(puml, IsFile("cppast/cpp_preprocessor.hpp")); + REQUIRE_THAT(puml, IsFile("clang/Lex/Lexer.h")); REQUIRE_THAT(puml, IsAssociation(_A("t40001.cc"), _A("t40001_include1.h"))); REQUIRE_THAT(puml, IsAssociation(_A("t40001_include1.h"), _A("lib1.h"))); diff --git a/thirdparty/cppast b/thirdparty/cppast deleted file mode 160000 index 79b8d563..00000000 --- a/thirdparty/cppast +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 79b8d56391b8e40a7b51bd1d567df300ba39a77a