From 21e7c3d3a09340b0d622c60fd2eb5552d2728b40 Mon Sep 17 00:00:00 2001 From: Bartek Kryza Date: Wed, 25 Jan 2023 22:42:49 +0100 Subject: [PATCH] Refactored cx utils to clang_utils --- .../visitor/translation_unit_visitor.cc | 15 +- src/common/clang_utils.cc | 100 ++++++++++++++ src/common/clang_utils.h | 13 +- src/common/model/diagram_filter.cc | 2 +- src/common/model/diagram_filter.h | 2 +- src/cx/util.cc | 128 ------------------ src/cx/util.h | 36 ----- .../visitor/translation_unit_visitor.cc | 1 - .../visitor/translation_unit_visitor.cc | 14 +- tests/test_cases.h | 2 +- tests/test_util.cc | 4 +- 11 files changed, 129 insertions(+), 188 deletions(-) delete mode 100644 src/cx/util.cc delete mode 100644 src/cx/util.h diff --git a/src/class_diagram/visitor/translation_unit_visitor.cc b/src/class_diagram/visitor/translation_unit_visitor.cc index a1548d52..2f84b579 100644 --- a/src/class_diagram/visitor/translation_unit_visitor.cc +++ b/src/class_diagram/visitor/translation_unit_visitor.cc @@ -18,7 +18,6 @@ #include "translation_unit_visitor.h" #include "common/clang_utils.h" -#include "cx/util.h" #include #include @@ -1358,9 +1357,8 @@ void translation_unit_visitor::process_template_specialization_argument( declaration_text.find(cls->getNameAsString()) + cls->getNameAsString().size() + 1); - auto template_params = - cx::util::parse_unexposed_template_params( - declaration_text, [](const auto &t) { return t; }); + auto template_params = common::parse_unexposed_template_params( + declaration_text, [](const auto &t) { return t; }); if (template_params.size() > argument_index) type_name = template_params[argument_index].to_string( @@ -1404,9 +1402,8 @@ void translation_unit_visitor::process_template_specialization_argument( declaration_text.find(cls->getNameAsString()) + cls->getNameAsString().size() + 1); - auto template_params = - cx::util::parse_unexposed_template_params( - declaration_text, [](const auto &t) { return t; }); + auto template_params = common::parse_unexposed_template_params( + declaration_text, [](const auto &t) { return t; }); if (template_params.size() > argument_index) type_name = template_params[argument_index].to_string( @@ -1472,7 +1469,7 @@ void translation_unit_visitor:: process_unexposed_template_specialization_parameters( const std::string &type_name, template_parameter &tp, class_ &c) { - auto template_params = cx::util::parse_unexposed_template_params( + auto template_params = common::parse_unexposed_template_params( type_name, [](const std::string &t) { return t; }); found_relationships_t relationships; @@ -2211,7 +2208,7 @@ void translation_unit_visitor::process_field( [&d = diagram()](const std::string &full_name) { if (full_name.empty()) return false; - auto [ns, name] = cx::util::split_ns(full_name); + auto [ns, name] = common::split_ns(full_name); return d.should_include(ns, name); }); } diff --git a/src/common/clang_utils.cc b/src/common/clang_utils.cc index d47e05cc..57cea568 100644 --- a/src/common/clang_utils.cc +++ b/src/common/clang_utils.cc @@ -300,4 +300,104 @@ template <> id_t to_id(const clang::TemplateArgument &template_argument) throw std::runtime_error("Cannot generate id for template argument"); } + +std::pair split_ns( + const std::string &full_name) +{ + assert(!full_name.empty()); + + auto name_before_template = ::clanguml::util::split(full_name, "<")[0]; + auto ns = common::model::namespace_{ + ::clanguml::util::split(name_before_template, "::")}; + auto name = ns.name(); + ns.pop_back(); + return {ns, name}; +} + +std::vector parse_unexposed_template_params( + const std::string ¶ms, + const std::function &ns_resolve, + int depth) +{ + using common::model::template_parameter; + + std::vector res; + + auto it = params.begin(); + while (std::isspace(*it) != 0) + ++it; + + std::string type{}; + std::vector nested_params; + bool complete_class_template_argument{false}; + + while (it != params.end()) { + if (*it == '<') { + int nested_level{0}; + auto bracket_match_begin = it + 1; + auto bracket_match_end = bracket_match_begin; + while (bracket_match_end != params.end()) { + if (*bracket_match_end == '<') { + nested_level++; + } + else if (*bracket_match_end == '>') { + if (nested_level > 0) + nested_level--; + else + break; + } + else { + } + bracket_match_end++; + } + + std::string nested_params_str( + bracket_match_begin, bracket_match_end); + + nested_params = parse_unexposed_template_params( + nested_params_str, ns_resolve, depth + 1); + + if (nested_params.empty()) + nested_params.emplace_back( + template_parameter{nested_params_str}); + + it = bracket_match_end - 1; + } + else if (*it == '>') { + complete_class_template_argument = true; + if (depth == 0) { + break; + } + } + else if (*it == ',') { + complete_class_template_argument = true; + } + else { + type += *it; + } + if (complete_class_template_argument) { + template_parameter t; + t.set_type(ns_resolve(clanguml::util::trim(type))); + type = ""; + for (auto &¶m : nested_params) + t.add_template_param(std::move(param)); + + res.emplace_back(std::move(t)); + complete_class_template_argument = false; + } + it++; + } + + if (!type.empty()) { + template_parameter t; + t.set_type(ns_resolve(clanguml::util::trim(type))); + type = ""; + for (auto &¶m : nested_params) + t.add_template_param(std::move(param)); + + res.emplace_back(std::move(t)); + } + + return res; +} } // namespace clanguml::common diff --git a/src/common/clang_utils.h b/src/common/clang_utils.h index d8d75669..1c162ae5 100644 --- a/src/common/clang_utils.h +++ b/src/common/clang_utils.h @@ -17,8 +17,11 @@ */ #pragma once -#include "cx/util.h" +#include "common/model/enums.h" +#include "common/model/namespace.h" +#include "common/model/template_parameter.h" #include "types.h" +#include "util/util.h" #include @@ -131,4 +134,12 @@ template <> id_t to_id(const clang::EnumType &type); template <> id_t to_id(const clang::TemplateSpecializationType &type); template <> id_t to_id(const std::filesystem::path &type); + +std::pair split_ns( + const std::string &full_name); + +std::vector parse_unexposed_template_params( + const std::string ¶ms, + const std::function &ns_resolve, + int depth = 0); } // namespace clanguml::common diff --git a/src/common/model/diagram_filter.cc b/src/common/model/diagram_filter.cc index 34154ca5..dc14a503 100644 --- a/src/common/model/diagram_filter.cc +++ b/src/common/model/diagram_filter.cc @@ -612,7 +612,7 @@ bool diagram_filter::should_include(const std::string &name) const if (name.empty()) return false; - auto [ns, n] = cx::util::split_ns(name); + auto [ns, n] = common::split_ns(name); return should_include(ns, n); } diff --git a/src/common/model/diagram_filter.h b/src/common/model/diagram_filter.h index 7d5ceda5..49e69df3 100644 --- a/src/common/model/diagram_filter.h +++ b/src/common/model/diagram_filter.h @@ -18,12 +18,12 @@ #pragma once #include "class_diagram/model/diagram.h" +#include "common/clang_utils.h" #include "common/model/diagram.h" #include "common/model/element.h" #include "common/model/enums.h" #include "common/model/namespace.h" #include "config/config.h" -#include "cx/util.h" #include "diagram.h" #include "include_diagram/model/diagram.h" #include "source_file.h" diff --git a/src/cx/util.cc b/src/cx/util.cc deleted file mode 100644 index a7b0b69d..00000000 --- a/src/cx/util.cc +++ /dev/null @@ -1,128 +0,0 @@ -/** - * src/cx/util.cc - * - * Copyright (c) 2021-2023 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 "cx/util.h" -#include "util/util.h" - -#include - -#include - -namespace clanguml::cx::util { - -std::pair split_ns( - const std::string &full_name) -{ - assert(!full_name.empty()); - - auto name_before_template = ::clanguml::util::split(full_name, "<")[0]; - auto ns = common::model::namespace_{ - ::clanguml::util::split(name_before_template, "::")}; - auto name = ns.name(); - ns.pop_back(); - return {ns, name}; -} - -std::vector parse_unexposed_template_params( - const std::string ¶ms, - const std::function &ns_resolve, - int depth) -{ - using common::model::template_parameter; - - std::vector res; - - auto it = params.begin(); - while (std::isspace(*it) != 0) - ++it; - - std::string type{}; - std::vector nested_params; - bool complete_class_template_argument{false}; - - while (it != params.end()) { - if (*it == '<') { - int nested_level{0}; - auto bracket_match_begin = it + 1; - auto bracket_match_end = bracket_match_begin; - while (bracket_match_end != params.end()) { - if (*bracket_match_end == '<') { - nested_level++; - } - else if (*bracket_match_end == '>') { - if (nested_level > 0) - nested_level--; - else - break; - } - else { - } - bracket_match_end++; - } - - std::string nested_params_str( - bracket_match_begin, bracket_match_end); - - nested_params = parse_unexposed_template_params( - nested_params_str, ns_resolve, depth + 1); - - if (nested_params.empty()) - nested_params.emplace_back( - template_parameter{nested_params_str}); - - it = bracket_match_end - 1; - } - else if (*it == '>') { - complete_class_template_argument = true; - if (depth == 0) { - break; - } - } - else if (*it == ',') { - complete_class_template_argument = true; - } - else { - type += *it; - } - if (complete_class_template_argument) { - template_parameter t; - t.set_type(ns_resolve(clanguml::util::trim(type))); - type = ""; - for (auto &¶m : nested_params) - t.add_template_param(std::move(param)); - - res.emplace_back(std::move(t)); - complete_class_template_argument = false; - } - it++; - } - - if (!type.empty()) { - template_parameter t; - t.set_type(ns_resolve(clanguml::util::trim(type))); - type = ""; - for (auto &¶m : nested_params) - t.add_template_param(std::move(param)); - - res.emplace_back(std::move(t)); - } - - return res; -} - -} // namespace clanguml::cx::util diff --git a/src/cx/util.h b/src/cx/util.h deleted file mode 100644 index 5f54799a..00000000 --- a/src/cx/util.h +++ /dev/null @@ -1,36 +0,0 @@ -/** - * src/cx/util.h - * - * Copyright (c) 2021-2023 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/namespace.h" - -#include "common/model/template_parameter.h" - -#include - -namespace clanguml::cx::util { - -std::pair split_ns( - const std::string &full_name); - -std::vector parse_unexposed_template_params( - const std::string ¶ms, - const std::function &ns_resolve, - int depth = 0); - -} // namespace clanguml::cx::util diff --git a/src/package_diagram/visitor/translation_unit_visitor.cc b/src/package_diagram/visitor/translation_unit_visitor.cc index feb2d7f0..cf68dcb7 100644 --- a/src/package_diagram/visitor/translation_unit_visitor.cc +++ b/src/package_diagram/visitor/translation_unit_visitor.cc @@ -20,7 +20,6 @@ #include "common/clang_utils.h" #include "common/model/namespace.h" -#include "cx/util.h" #include diff --git a/src/sequence_diagram/visitor/translation_unit_visitor.cc b/src/sequence_diagram/visitor/translation_unit_visitor.cc index 32581c6b..6f5cdf3b 100644 --- a/src/sequence_diagram/visitor/translation_unit_visitor.cc +++ b/src/sequence_diagram/visitor/translation_unit_visitor.cc @@ -18,8 +18,8 @@ #include "translation_unit_visitor.h" +#include "common/clang_utils.h" #include "common/model/namespace.h" -#include "cx/util.h" namespace clanguml::sequence_diagram::visitor { @@ -1731,9 +1731,8 @@ void translation_unit_visitor::process_template_specialization_argument( declaration_text.find(cls->getNameAsString()) + cls->getNameAsString().size() + 1); - auto template_params = - cx::util::parse_unexposed_template_params( - declaration_text, [](const auto &t) { return t; }); + auto template_params = common::parse_unexposed_template_params( + declaration_text, [](const auto &t) { return t; }); if (template_params.size() > argument_index) type_name = template_params[argument_index].to_string( @@ -1789,9 +1788,8 @@ void translation_unit_visitor::process_template_specialization_argument( declaration_text.find(cls->getNameAsString()) + cls->getNameAsString().size() + 1); - auto template_params = - cx::util::parse_unexposed_template_params( - declaration_text, [](const auto &t) { return t; }); + auto template_params = common::parse_unexposed_template_params( + declaration_text, [](const auto &t) { return t; }); if (template_params.size() > argument_index) type_name = template_params[argument_index].to_string( @@ -2043,7 +2041,7 @@ void translation_unit_visitor:: const std::string &type_name, common::model::template_parameter &tp, model::class_ & /*c*/) const { - auto template_params = cx::util::parse_unexposed_template_params( + auto template_params = common::parse_unexposed_template_params( type_name, [](const std::string &t) { return t; }); for (auto ¶m : template_params) { diff --git a/tests/test_cases.h b/tests/test_cases.h index 0f693234..e4f549df 100644 --- a/tests/test_cases.h +++ b/tests/test_cases.h @@ -22,8 +22,8 @@ #include "class_diagram/generators/plantuml/class_diagram_generator.h" #include "class_diagram/model/diagram.h" #include "class_diagram/visitor/translation_unit_visitor.h" +#include "common/clang_utils.h" #include "config/config.h" -#include "cx/util.h" #include "include_diagram/generators/plantuml/include_diagram_generator.h" #include "include_diagram/visitor/translation_unit_visitor.h" #include "package_diagram/generators/plantuml/package_diagram_generator.h" diff --git a/tests/test_util.cc b/tests/test_util.cc index b4860c9f..28905eaa 100644 --- a/tests/test_util.cc +++ b/tests/test_util.cc @@ -18,7 +18,7 @@ #define CATCH_CONFIG_MAIN #include "util/util.h" -#include +#include #include @@ -96,7 +96,7 @@ TEST_CASE("Test replace_all", "[unit-test]") TEST_CASE("Test parse_unexposed_template_params", "[unit-test]") { - using namespace clanguml::cx::util; + using namespace clanguml::common; const std::string int_template_str{"ns1::ns2::class1"};