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

View File

@@ -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 <clang/AST/RecursiveASTVisitor.h>
@@ -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<common::model::namespace_, std::string> split_ns(
const std::string &full_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 = 0);
} // namespace clanguml::common

View File

@@ -612,7 +612,7 @@ bool diagram_filter::should_include<std::string>(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);
}

View File

@@ -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"