Added basic class relationship handling

This commit is contained in:
Bartek Kryza
2021-02-28 19:13:15 +01:00
parent e885149cf0
commit e4d77db5c0
11 changed files with 324 additions and 69 deletions

View File

@@ -118,6 +118,11 @@ public:
CXCursorKind kind() const { return m_cursor.kind; }
std::string kind_spelling() const
{
return to_string(clang_getCursorKindSpelling(m_cursor.kind));
}
bool is_definition() const { return clang_isCursorDefinition(m_cursor); }
bool is_declaration() const { return clang_isDeclaration(kind()); }

View File

@@ -21,6 +21,8 @@
#include <clang-c/Index.h>
#include <spdlog/spdlog.h>
#include "util/util.h"
namespace clanguml {
namespace cx {
@@ -93,6 +95,11 @@ public:
CXTypeKind kind() const { return m_type.kind; }
std::string kind_spelling()
{
return to_string(clang_getTypeKindSpelling(m_type.kind));
}
CXCallingConv calling_convention() const
{
return clang_getFunctionTypeCallingConv(m_type);
@@ -116,6 +123,39 @@ public:
bool is_pod() const { return clang_isPODType(m_type); }
bool is_pointer() const { return kind() == CXType_Pointer; }
bool is_record() const { return kind() == CXType_Record; }
/**
* @brief Return final referenced type.
*
* This method allows to extract a final type in case a type consists of a
* single or multiple pointers or references.
*
* @return Referenced type.
*/
type referenced() const
{
auto t = *this;
while (t.is_pointer() || t.is_reference()) {
t = t.pointee_type();
}
return t;
}
bool is_reference() const
{
return (kind() == CXType_LValueReference) ||
(kind() == CXType_RValueReference);
}
bool is_relationship() const
{
return is_pointer() || is_record() || is_reference() || !is_pod();
}
type element_type() const { return clang_getElementType(m_type); }
long long element_count() const { return clang_getNumElements(m_type); }
@@ -157,6 +197,20 @@ public:
return clang_Type_getCXXRefQualifier(m_type);
}
std::string unqualified() const
{
auto toks = clanguml::util::split(spelling(), " ");
const std::vector<std::string> qualifiers = {
"static", "const", "volatile", "register", "mutable"};
while (toks.size() > 0 &&
std::count(qualifiers.begin(), qualifiers.end(), toks.front())) {
toks.erase(toks.begin());
}
return fmt::format("{}", fmt::join(toks, " "));
}
private:
CXType m_type;
};

View File

@@ -79,6 +79,7 @@ public:
std::string to_string(relationship_t r) const
{
switch (r) {
case relationship_t::kOwnership:
case relationship_t::kComposition:
return "*--";
case relationship_t::kAggregation:
@@ -86,7 +87,7 @@ public:
case relationship_t::kContainment:
return "+--";
case relationship_t::kAssociation:
return "--";
return "-->";
default:
return "";
}
@@ -139,7 +140,9 @@ public:
if (m.is_static)
ostr << "{static} ";
ostr << to_string(m.scope) << m.type << " " << m.name << std::endl;
ostr << to_string(m.scope)
<< ns_relative(m_config.using_namespace, m.type) << " "
<< m.name << std::endl;
}
ostr << "}" << std::endl;

View File

@@ -33,6 +33,7 @@ namespace class_diagram {
enum class scope_t { kPublic, kProtected, kPrivate };
enum class relationship_t {
kNone,
kExtension,
kComposition,
kAggregation,

View File

@@ -253,86 +253,101 @@ static enum CXChildVisitResult class_visitor(
auto t = cursor.type();
class_member m;
m.name = cursor.spelling();
m.type = cursor.type().spelling();
m.type = cursor.type().canonical().unqualified();
m.scope = cx_access_specifier_to_scope(
cursor.cxxaccess_specifier());
m.is_static = cursor.is_static();
spdlog::info("Adding member {} {}::{}", m.type,
ctx->element.name, cursor.spelling());
spdlog::info("Adding member {} {}::{} (type kind: {} | {} "
"| {} | {})",
m.type, ctx->element.name, cursor.spelling(),
t.kind_spelling(), t.pointee_type().spelling(),
t.is_pod(), cursor.type().canonical().spelling());
relationship_t relationship_type =
relationship_t::kOwnership;
relationship_t relationship_type = relationship_t::kNone;
// Parse the field declaration to determine the relationship
// type
// Skip:
// - POD
// - function variables
if (!t.is_pod() && !is_vardecl &&
config.should_include(cursor.type().spelling())) {
while (true) {
if (t.kind() == CXType_Pointer) {
relationship_type =
relationship_t::kAssociation;
t = t.pointee_type();
continue;
}
else if (t.kind() == CXType_LValueReference) {
relationship_type =
relationship_t::kAggregation;
t = t.pointee_type();
continue;
}
else if (t.kind() == CXType_RValueReference) {
relationship_type =
relationship_t::kAssociation;
t = t.pointee_type();
continue;
if (t.is_relationship() &&
config.should_include(
cursor.type().canonical().unqualified())) {
spdlog::info(
"Analazing possible relationship candidate: {}",
t.spelling());
if (t.kind() == CXType_Record) {
spdlog::info(
"Found relationship candidate record: {} | {}",
t.spelling(), t.pointee_type().spelling());
relationship_type = relationship_t::kOwnership;
}
else if (t.kind() == CXType_Pointer) {
spdlog::info(
"Found relationship candidate pointer: {}",
t.spelling());
relationship_type = relationship_t::kAssociation;
t = t.referenced();
}
else if (t.kind() == CXType_LValueReference) {
spdlog::info("Found relationship candidate "
"lvalue reference: {}",
t.spelling());
relationship_type = relationship_t::kAssociation;
t = t.referenced();
}
else if (t.kind() == CXType_RValueReference) {
spdlog::info("Found relationship candidate "
"rvalue reference: {}",
t.spelling());
relationship_type = relationship_t::kOwnership;
t = t.referenced();
}
if (relationship_type != relationship_t::kNone) {
spdlog::info(
"Found unknown candidate: {}", t.spelling());
spdlog::error("UNKNOWN CXTYPE: {}", t.kind());
class_relationship r;
auto template_argument_count =
t.template_arguments_count();
std::string name = t.canonical().unqualified();
if (template_argument_count > 0) {
std::vector<cx::type> template_arguments;
for (int i = 0; i < template_argument_count;
i++) {
auto tt = t.template_argument_type(i);
template_arguments.push_back(tt);
}
if (name.rfind("vector") == 0 ||
name.rfind("std::vector") == 0) {
r.type = relationship_t::kAggregation;
r.destination =
template_arguments[0].spelling();
}
if (name.rfind("map") == 0 ||
name.rfind("std::map") == 0) {
r.type = relationship_t::kAggregation;
r.destination =
template_arguments[1].spelling();
}
r.label = m.name;
ctx->element.relationships.emplace_back(
std::move(r));
}
else {
spdlog::error("UNKNOWN CXTYPE: {}", t.kind());
class_relationship r;
auto template_argument_count =
t.template_arguments_count();
std::string name = t.spelling();
if (template_argument_count > 0) {
std::vector<cx::type> template_arguments;
for (int i = 0; i < template_argument_count;
i++) {
auto tt = t.template_argument_type(i);
template_arguments.push_back(tt);
}
if (name.rfind("vector") == 0 ||
name.rfind("std::vector") == 0) {
r.type = relationship_t::kAggregation;
r.destination =
template_arguments[0].spelling();
}
if (name.rfind("map") == 0 ||
name.rfind("std::map") == 0) {
r.type = relationship_t::kAggregation;
r.destination =
template_arguments[1].spelling();
}
r.label = m.name;
ctx->element.relationships.emplace_back(
std::move(r));
}
else {
r.destination = name;
r.type = relationship_type;
r.label = m.name;
ctx->element.relationships.emplace_back(
std::move(r));
}
spdlog::debug("Adding relationship to: {}",
r.destination);
r.destination = name;
r.type = relationship_type;
r.label = m.name;
ctx->element.relationships.emplace_back(
std::move(r));
}
break;
spdlog::info(
"Adding relationship to: {}", r.destination);
}
}