Refactored cx utils to clang_utils

This commit is contained in:
Bartek Kryza
2023-01-25 22:42:49 +01:00
parent 9a7d66f93f
commit 21e7c3d3a0
11 changed files with 129 additions and 188 deletions

View File

@@ -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<common::model::namespace_, std::string> 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<common::model::template_parameter> parse_unexposed_template_params(
const std::string &params,
const std::function<std::string(const std::string &)> &ns_resolve,
int depth)
{
using common::model::template_parameter;
std::vector<template_parameter> res;
auto it = params.begin();
while (std::isspace(*it) != 0)
++it;
std::string type{};
std::vector<template_parameter> 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 &&param : 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 &&param : nested_params)
t.add_template_param(std::move(param));
res.emplace_back(std::move(t));
}
return res;
}
} // namespace clanguml::common