Initial refactor to cppast

This commit is contained in:
Bartek Kryza
2021-03-24 01:02:28 +01:00
parent b95bf324c2
commit 5dc841d9e6
14 changed files with 589 additions and 17 deletions

View File

@@ -18,6 +18,8 @@
#include "cx/util.h"
#include <cppast/cpp_class.hpp>
#include <cppast/cpp_entity_kind.hpp>
#include <spdlog/spdlog.h>
namespace clanguml {
@@ -30,6 +32,65 @@ std::string to_string(CXString &&cxs)
clang_disposeString(cxs);
return r;
}
std::string full_name(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::string scopes;
for (auto cur = e.parent(); cur; cur = cur.value().parent())
// prepend each scope, if there is any
if (cur.value().kind() == cppast::cpp_entity_kind::namespace_t)
type_safe::with(cur.value().scope_name(),
[&](const cppast::cpp_scope_name &cur_scope) {
scopes = cur_scope.name() + "::" + scopes;
});
if (e.kind() == cppast::cpp_entity_kind::class_t) {
auto &c = static_cast<const cppast::cpp_class &>(e);
return scopes /*+ c.semantic_scope()*/ + c.name();
}
else if (e.kind() == cppast::cpp_entity_kind::class_template_t) {
return scopes;
}
else
return scopes + e.name();
}
std::string ns(const cppast::cpp_entity &e)
{
std::vector<std::string> res{};
auto it = e.parent();
while (it) {
if (it.value().kind() == cppast::cpp_entity_kind::namespace_t) {
res.push_back(it.value().name());
}
it = it.value().parent();
}
return fmt::format("{}", fmt::join(res.rbegin(), res.rend(), "::"));
}
std::string fully_prefixed(const cppast::cpp_entity &e)
{
std::vector<std::string> res{e.name()};
auto it = e.parent();
while (it) {
if (it.value().kind() == cppast::cpp_entity_kind::namespace_t) {
res.push_back(it.value().name());
}
it = it.value().parent();
}
return fmt::format("{}", fmt::join(res.rbegin(), res.rend(), "::"));
}
} // namespace util
} // namespace cx
} // namespace clanguml