Refactored class visitor to cpp api
This commit is contained in:
3
Makefile
3
Makefile
@@ -44,6 +44,9 @@ release: release/CMakeLists.txt
|
|||||||
test: debug
|
test: debug
|
||||||
CTEST_OUTPUT_ON_FAILURE=1 make -C debug test
|
CTEST_OUTPUT_ON_FAILURE=1 make -C debug test
|
||||||
|
|
||||||
|
test_plantuml: test
|
||||||
|
plantuml debug/tests/puml/*.puml
|
||||||
|
|
||||||
.PHONY: clang-format
|
.PHONY: clang-format
|
||||||
clang-format:
|
clang-format:
|
||||||
docker run --rm -v $(CURDIR):/root/sources bkryza/clang-format-check:1.2
|
docker run --rm -v $(CURDIR):/root/sources bkryza/clang-format-check:1.2
|
||||||
|
|||||||
@@ -142,6 +142,11 @@ public:
|
|||||||
return clang_getCursorAvailability(m_cursor);
|
return clang_getCursorAvailability(m_cursor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CX_CXXAccessSpecifier cxxaccess_specifier() const
|
||||||
|
{
|
||||||
|
return clang_getCXXAccessSpecifier(m_cursor);
|
||||||
|
}
|
||||||
|
|
||||||
std::string usr() const { return to_string(clang_getCursorUSR(m_cursor)); }
|
std::string usr() const { return to_string(clang_getCursorUSR(m_cursor)); }
|
||||||
|
|
||||||
const CXCursor &get() const { return m_cursor; }
|
const CXCursor &get() const { return m_cursor; }
|
||||||
|
|||||||
@@ -91,6 +91,8 @@ public:
|
|||||||
*}
|
*}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
CXTypeKind kind() const { return m_type.kind; }
|
||||||
|
|
||||||
CXCallingConv calling_convention() const
|
CXCallingConv calling_convention() const
|
||||||
{
|
{
|
||||||
return clang_getFunctionTypeCallingConv(m_type);
|
return clang_getFunctionTypeCallingConv(m_type);
|
||||||
|
|||||||
@@ -72,12 +72,11 @@ struct tu_context {
|
|||||||
};
|
};
|
||||||
|
|
||||||
enum CXChildVisitResult visit_if_cursor_valid(
|
enum CXChildVisitResult visit_if_cursor_valid(
|
||||||
CXCursor cursor, std::function<void(CXCursor)> f)
|
cx::cursor cursor, std::function<void(cx::cursor)> f)
|
||||||
{
|
{
|
||||||
enum CXChildVisitResult ret = CXChildVisit_Break;
|
enum CXChildVisitResult ret = CXChildVisit_Break;
|
||||||
CXString cursorName = clang_getCursorSpelling(cursor);
|
if (cursor.is_definition()) {
|
||||||
if (clang_isCursorDefinition(cursor)) {
|
if (!cursor.spelling().empty()) {
|
||||||
if (*clang_getCString(cursorName)) {
|
|
||||||
f(cursor);
|
f(cursor);
|
||||||
ret = CXChildVisit_Continue;
|
ret = CXChildVisit_Continue;
|
||||||
}
|
}
|
||||||
@@ -92,26 +91,25 @@ enum CXChildVisitResult visit_if_cursor_valid(
|
|||||||
}
|
}
|
||||||
|
|
||||||
static enum CXChildVisitResult enum_visitor(
|
static enum CXChildVisitResult enum_visitor(
|
||||||
CXCursor cursor, CXCursor parent, CXClientData client_data)
|
CXCursor cx_cursor, CXCursor cx_parent, CXClientData client_data)
|
||||||
{
|
{
|
||||||
auto e = (struct enum_ *)client_data;
|
auto e = (struct enum_ *)client_data;
|
||||||
|
|
||||||
CXString cursorName = clang_getCursorSpelling(cursor);
|
cx::cursor cursor{std::move(cx_cursor)};
|
||||||
std::string cursor_name_str = clang_getCString(cursorName);
|
cx::cursor parent{std::move(cx_parent)};
|
||||||
|
|
||||||
spdlog::info(
|
spdlog::info("Visiting enum {}: {} - {}:{}", e->name, cursor.spelling(),
|
||||||
"Visiting enum {}: {} - {}:{}", e->name, cursor_name_str, cursor.kind);
|
cursor.kind());
|
||||||
|
|
||||||
enum CXChildVisitResult ret = CXChildVisit_Break;
|
enum CXChildVisitResult ret = CXChildVisit_Break;
|
||||||
switch (cursor.kind) {
|
switch (cursor.kind()) {
|
||||||
case CXCursor_EnumConstantDecl:
|
case CXCursor_EnumConstantDecl:
|
||||||
visit_if_cursor_valid(
|
visit_if_cursor_valid(cursor, [e](cx::cursor cursor) {
|
||||||
cursor, [e, cursor_name_str](CXCursor cursor) {
|
spdlog::info(
|
||||||
spdlog::info("Adding enum constant {}::{}", e->name,
|
"Adding enum constant {}::{}", e->name, cursor.spelling());
|
||||||
cursor_name_str);
|
|
||||||
|
|
||||||
e->constants.emplace_back(cursor_name_str);
|
e->constants.emplace_back(cursor.spelling());
|
||||||
});
|
});
|
||||||
|
|
||||||
ret = CXChildVisit_Continue;
|
ret = CXChildVisit_Continue;
|
||||||
break;
|
break;
|
||||||
@@ -124,66 +122,66 @@ static enum CXChildVisitResult enum_visitor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
static enum CXChildVisitResult class_visitor(
|
static enum CXChildVisitResult class_visitor(
|
||||||
CXCursor cursor, CXCursor parent, CXClientData client_data)
|
CXCursor cx_cursor, CXCursor cx_parent, CXClientData client_data)
|
||||||
{
|
{
|
||||||
auto c = (struct class_ *)client_data;
|
auto c = (struct class_ *)client_data;
|
||||||
|
|
||||||
CXString cursorName = clang_getCursorSpelling(cursor);
|
cx::cursor cursor{std::move(cx_cursor)};
|
||||||
std::string cursor_name_str = clang_getCString(cursorName);
|
cx::cursor parent{std::move(cx_parent)};
|
||||||
|
|
||||||
|
std::string cursor_name_str = cursor.spelling();
|
||||||
|
|
||||||
spdlog::info("Visiting {}: {} - {}:{}", c->is_struct ? "struct" : "class",
|
spdlog::info("Visiting {}: {} - {}:{}", c->is_struct ? "struct" : "class",
|
||||||
c->name, cursor_name_str, cursor.kind);
|
c->name, cursor_name_str, cursor.kind());
|
||||||
|
|
||||||
enum CXChildVisitResult ret = CXChildVisit_Break;
|
enum CXChildVisitResult ret = CXChildVisit_Break;
|
||||||
switch (cursor.kind) {
|
switch (cursor.kind()) {
|
||||||
case CXCursor_CXXMethod:
|
case CXCursor_CXXMethod:
|
||||||
case CXCursor_Constructor:
|
case CXCursor_Constructor:
|
||||||
case CXCursor_Destructor:
|
case CXCursor_Destructor:
|
||||||
case CXCursor_FunctionTemplate: {
|
case CXCursor_FunctionTemplate: {
|
||||||
visit_if_cursor_valid(
|
visit_if_cursor_valid(cursor, [c](cx::cursor cursor) {
|
||||||
cursor, [c, cursor_name_str](CXCursor cursor) {
|
class_method m;
|
||||||
class_method m;
|
m.name = cursor.spelling();
|
||||||
m.name = cursor_name_str;
|
m.type = cursor.type().spelling();
|
||||||
m.type = clang_getCString(clang_getTypeSpelling(
|
|
||||||
clang_getResultType(clang_getCursorType(cursor))));
|
|
||||||
|
|
||||||
spdlog::info("Adding method {} {}::{}()", m.type, c->name,
|
spdlog::info("Adding method {} {}::{}()", m.type, c->name,
|
||||||
cursor_name_str);
|
cursor.spelling());
|
||||||
|
|
||||||
c->methods.emplace_back(std::move(m));
|
c->methods.emplace_back(std::move(m));
|
||||||
});
|
});
|
||||||
ret = CXChildVisit_Continue;
|
ret = CXChildVisit_Continue;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case CXCursor_FieldDecl: {
|
case CXCursor_FieldDecl: {
|
||||||
visit_if_cursor_valid(cursor, [c, cursor_name_str](CXCursor cursor) {
|
visit_if_cursor_valid(cursor, [c](cx::cursor cursor) {
|
||||||
auto t = clang_getCursorType(cursor);
|
auto t = cursor.type();
|
||||||
class_member m;
|
class_member m;
|
||||||
m.name = cursor_name_str;
|
m.name = cursor.spelling();
|
||||||
m.type = clang_getCString(clang_getTypeSpelling(t));
|
m.type = cursor.type().spelling();
|
||||||
|
|
||||||
spdlog::info("Adding member {} {}::{}", m.type, c->name,
|
spdlog::info("Adding member {} {}::{}", m.type, c->name,
|
||||||
cursor_name_str);
|
cursor.spelling());
|
||||||
|
|
||||||
relationship_t relationship_type = relationship_t::kOwnership;
|
relationship_t relationship_type = relationship_t::kOwnership;
|
||||||
|
|
||||||
// Parse the field declaration to determine the relationship
|
// Parse the field declaration to determine the relationship
|
||||||
// type
|
// type
|
||||||
if (!clang_isPODType(t)) {
|
if (!t.is_pod()) {
|
||||||
while (true) {
|
while (true) {
|
||||||
if (t.kind == CXType_Pointer) {
|
if (t.kind() == CXType_Pointer) {
|
||||||
relationship_type = relationship_t::kAssociation;
|
relationship_type = relationship_t::kAssociation;
|
||||||
t = clang_getPointeeType(t);
|
t = t.pointee_type();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
else if (t.kind == CXType_LValueReference) {
|
else if (t.kind() == CXType_LValueReference) {
|
||||||
relationship_type = relationship_t::kAggregation;
|
relationship_type = relationship_t::kAggregation;
|
||||||
t = clang_getPointeeType(t);
|
t = t.pointee_type();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
else if (t.kind == CXType_RValueReference) {
|
else if (t.kind() == CXType_RValueReference) {
|
||||||
relationship_type = relationship_t::kAssociation;
|
relationship_type = relationship_t::kAssociation;
|
||||||
t = clang_getPointeeType(t);
|
t = t.pointee_type();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
/*else if(t.kind == CXType_Elaborated) {
|
/*else if(t.kind == CXType_Elaborated) {
|
||||||
@@ -191,20 +189,17 @@ static enum CXChildVisitResult class_visitor(
|
|||||||
continue;
|
continue;
|
||||||
}*/
|
}*/
|
||||||
else /*if (t.kind == CXType_Record) */ {
|
else /*if (t.kind == CXType_Record) */ {
|
||||||
spdlog::error("UNKNOWN CXTYPE: {}", t.kind);
|
spdlog::error("UNKNOWN CXTYPE: {}", t.kind());
|
||||||
class_relationship r;
|
class_relationship r;
|
||||||
auto template_argument_count =
|
auto template_argument_count =
|
||||||
clang_Type_getNumTemplateArguments(t);
|
t.template_arguments_count();
|
||||||
std::string name{
|
std::string name = t.spelling();
|
||||||
clang_getCString(clang_getTypeSpelling(t))};
|
|
||||||
|
|
||||||
if (template_argument_count > 0) {
|
if (template_argument_count > 0) {
|
||||||
std::vector<CXType> template_arguments;
|
std::vector<cx::type> template_arguments;
|
||||||
for (int i = 0; i < template_argument_count;
|
for (int i = 0; i < template_argument_count;
|
||||||
i++) {
|
i++) {
|
||||||
auto tt =
|
auto tt = t.template_argument_type(i);
|
||||||
clang_Type_getTemplateArgumentAsType(
|
|
||||||
t, i);
|
|
||||||
template_arguments.push_back(tt);
|
template_arguments.push_back(tt);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -212,15 +207,13 @@ static enum CXChildVisitResult class_visitor(
|
|||||||
name.rfind("std::vector") == 0) {
|
name.rfind("std::vector") == 0) {
|
||||||
r.type = relationship_t::kAggregation;
|
r.type = relationship_t::kAggregation;
|
||||||
r.destination =
|
r.destination =
|
||||||
clang_getCString(clang_getTypeSpelling(
|
template_arguments[0].spelling();
|
||||||
template_arguments[0]));
|
|
||||||
}
|
}
|
||||||
if (name.rfind("map") == 0 ||
|
if (name.rfind("map") == 0 ||
|
||||||
name.rfind("std::map") == 0) {
|
name.rfind("std::map") == 0) {
|
||||||
r.type = relationship_t::kAggregation;
|
r.type = relationship_t::kAggregation;
|
||||||
r.destination =
|
r.destination =
|
||||||
clang_getCString(clang_getTypeSpelling(
|
template_arguments[1].spelling();
|
||||||
template_arguments[1]));
|
|
||||||
}
|
}
|
||||||
r.label = m.name;
|
r.label = m.name;
|
||||||
c->relationships.emplace_back(std::move(r));
|
c->relationships.emplace_back(std::move(r));
|
||||||
@@ -248,16 +241,16 @@ static enum CXChildVisitResult class_visitor(
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case CXCursor_CXXBaseSpecifier: {
|
case CXCursor_CXXBaseSpecifier: {
|
||||||
CXCursor ref_cursor = clang_getCursorReferenced(cursor);
|
auto ref_cursor = cursor.referenced();
|
||||||
CXString display_name = clang_getCursorDisplayName(ref_cursor);
|
auto display_name = ref_cursor.referenced().spelling();
|
||||||
|
|
||||||
auto base_access = clang_getCXXAccessSpecifier(cursor);
|
auto base_access = cursor.cxxaccess_specifier();
|
||||||
|
|
||||||
spdlog::error("Found base specifier: {} - {}", cursor_name_str,
|
spdlog::error(
|
||||||
clang_getCString(display_name));
|
"Found base specifier: {} - {}", cursor_name_str, display_name);
|
||||||
|
|
||||||
class_parent cp;
|
class_parent cp;
|
||||||
cp.name = clang_getCString(display_name);
|
cp.name = display_name;
|
||||||
cp.is_virtual = false;
|
cp.is_virtual = false;
|
||||||
switch (base_access) {
|
switch (base_access) {
|
||||||
case CX_CXXAccessSpecifier::CX_CXXPrivate:
|
case CX_CXXAccessSpecifier::CX_CXXPrivate:
|
||||||
@@ -287,24 +280,25 @@ static enum CXChildVisitResult class_visitor(
|
|||||||
};
|
};
|
||||||
|
|
||||||
static enum CXChildVisitResult translation_unit_visitor(
|
static enum CXChildVisitResult translation_unit_visitor(
|
||||||
CXCursor cursor, CXCursor parent, CXClientData client_data)
|
CXCursor cx_cursor, CXCursor cx_parent, CXClientData client_data)
|
||||||
{
|
{
|
||||||
if (clang_Location_isFromMainFile(clang_getCursorLocation(cursor)) == 0) {
|
|
||||||
return CXChildVisit_Continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct tu_context *ctx = (struct tu_context *)client_data;
|
struct tu_context *ctx = (struct tu_context *)client_data;
|
||||||
|
|
||||||
enum CXChildVisitResult ret = CXChildVisit_Break;
|
enum CXChildVisitResult ret = CXChildVisit_Break;
|
||||||
|
|
||||||
CXString cursorName = clang_getCursorSpelling(cursor);
|
cx::cursor cursor{std::move(cx_cursor)};
|
||||||
std::string cursor_name_str = clang_getCString(cursorName);
|
cx::cursor parent{std::move(cx_parent)};
|
||||||
|
|
||||||
|
if (clang_Location_isFromMainFile(cursor.location()) == 0) {
|
||||||
|
return CXChildVisit_Continue;
|
||||||
|
}
|
||||||
|
std::string cursor_name_str = cursor.spelling();
|
||||||
|
|
||||||
spdlog::debug("Visiting cursor: {}", cursor_name_str);
|
spdlog::debug("Visiting cursor: {}", cursor_name_str);
|
||||||
|
|
||||||
bool is_struct{false};
|
bool is_struct{false};
|
||||||
auto scope{scope_t::kPrivate};
|
auto scope{scope_t::kPrivate};
|
||||||
switch (cursor.kind) {
|
switch (cursor.kind()) {
|
||||||
case CXCursor_StructDecl:
|
case CXCursor_StructDecl:
|
||||||
spdlog::debug("Found struct declaration: {}", cursor_name_str);
|
spdlog::debug("Found struct declaration: {}", cursor_name_str);
|
||||||
|
|
||||||
@@ -319,17 +313,16 @@ static enum CXChildVisitResult translation_unit_visitor(
|
|||||||
|
|
||||||
scope = scope_t::kPublic;
|
scope = scope_t::kPublic;
|
||||||
|
|
||||||
visit_if_cursor_valid(
|
visit_if_cursor_valid(cursor, [ctx, is_struct](cx::cursor cursor) {
|
||||||
cursor, [ctx, is_struct, cursor_name_str](CXCursor cursor) {
|
class_ c{};
|
||||||
class_ c{};
|
c.is_struct = is_struct;
|
||||||
c.is_struct = is_struct;
|
c.name = cursor.spelling();
|
||||||
c.name = cursor_name_str;
|
c.namespace_ = ctx->namespace_;
|
||||||
c.namespace_ = ctx->namespace_;
|
|
||||||
|
|
||||||
clang_visitChildren(cursor, class_visitor, &c);
|
clang_visitChildren(cursor.get(), class_visitor, &c);
|
||||||
|
|
||||||
ctx->d.classes.emplace_back(std::move(c));
|
ctx->d.classes.emplace_back(std::move(c));
|
||||||
});
|
});
|
||||||
|
|
||||||
ret = CXChildVisit_Continue;
|
ret = CXChildVisit_Continue;
|
||||||
break;
|
break;
|
||||||
@@ -337,16 +330,15 @@ static enum CXChildVisitResult translation_unit_visitor(
|
|||||||
case CXCursor_EnumDecl: {
|
case CXCursor_EnumDecl: {
|
||||||
spdlog::debug("Found enum declaration: {}", cursor_name_str);
|
spdlog::debug("Found enum declaration: {}", cursor_name_str);
|
||||||
|
|
||||||
visit_if_cursor_valid(
|
visit_if_cursor_valid(cursor, [ctx, is_struct](cx::cursor cursor) {
|
||||||
cursor, [ctx, is_struct, cursor_name_str](CXCursor cursor) {
|
enum_ e{};
|
||||||
enum_ e{};
|
e.name = cursor.spelling();
|
||||||
e.name = cursor_name_str;
|
e.namespace_ = ctx->namespace_;
|
||||||
e.namespace_ = ctx->namespace_;
|
|
||||||
|
|
||||||
clang_visitChildren(cursor, enum_visitor, &e);
|
clang_visitChildren(cursor.get(), enum_visitor, &e);
|
||||||
|
|
||||||
ctx->d.enums.emplace_back(std::move(e));
|
ctx->d.enums.emplace_back(std::move(e));
|
||||||
});
|
});
|
||||||
ret = CXChildVisit_Continue;
|
ret = CXChildVisit_Continue;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -81,6 +81,7 @@ std::string generate_class_puml(
|
|||||||
void save_puml(const std::string &path, const std::string &puml)
|
void save_puml(const std::string &path, const std::string &puml)
|
||||||
{
|
{
|
||||||
std::filesystem::path p{path};
|
std::filesystem::path p{path};
|
||||||
|
std::filesystem::create_directory(p.parent_path());
|
||||||
spdlog::error("PWD: {}", std::filesystem::current_path().string());
|
spdlog::error("PWD: {}", std::filesystem::current_path().string());
|
||||||
spdlog::error("SAVING TEST PWD {} DIAGRAM: {}", p.string());
|
spdlog::error("SAVING TEST PWD {} DIAGRAM: {}", p.string());
|
||||||
std::ofstream ofs;
|
std::ofstream ofs;
|
||||||
|
|||||||
Reference in New Issue
Block a user