Added initial seqence diagram support
This commit is contained in:
@@ -44,13 +44,13 @@ struct class_visitor_context : element_visitor_context<class_> {
|
||||
};
|
||||
|
||||
struct tu_context {
|
||||
tu_context(diagram &d)
|
||||
: diagram(d)
|
||||
tu_context(diagram &d_)
|
||||
: d{d_}
|
||||
{
|
||||
}
|
||||
|
||||
std::vector<std::string> namespace_;
|
||||
diagram &diagram;
|
||||
class_diagram::diagram &d;
|
||||
};
|
||||
|
||||
enum CXChildVisitResult visit_if_cursor_valid(
|
||||
@@ -310,7 +310,7 @@ static enum CXChildVisitResult translation_unit_visitor(
|
||||
|
||||
clang_visitChildren(cursor, class_visitor, &c);
|
||||
|
||||
ctx->diagram.classes.emplace_back(std::move(c));
|
||||
ctx->d.classes.emplace_back(std::move(c));
|
||||
});
|
||||
|
||||
ret = CXChildVisit_Continue;
|
||||
@@ -327,7 +327,7 @@ static enum CXChildVisitResult translation_unit_visitor(
|
||||
|
||||
clang_visitChildren(cursor, enum_visitor, &e);
|
||||
|
||||
ctx->diagram.enums.emplace_back(std::move(e));
|
||||
ctx->d.enums.emplace_back(std::move(e));
|
||||
});
|
||||
ret = CXChildVisit_Continue;
|
||||
|
||||
|
||||
265
src/uml/cx.h
Normal file
265
src/uml/cx.h
Normal file
@@ -0,0 +1,265 @@
|
||||
#pragma once
|
||||
|
||||
#include <clang-c/CXCompilationDatabase.h>
|
||||
#include <clang-c/Index.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
namespace clanguml {
|
||||
namespace cx {
|
||||
|
||||
namespace detail {
|
||||
std::string to_string(CXString &&cxs)
|
||||
{
|
||||
std::string r{clang_getCString(cxs)};
|
||||
clang_disposeString(cxs);
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
using detail::to_string;
|
||||
|
||||
class type {
|
||||
public:
|
||||
type(CXType &&t)
|
||||
: m_type{std::move(t)}
|
||||
{
|
||||
}
|
||||
~type() = default;
|
||||
|
||||
std::string spelling() const
|
||||
{
|
||||
return to_string(clang_getTypeSpelling(m_type));
|
||||
}
|
||||
|
||||
bool operator==(const type &b) const
|
||||
{
|
||||
return clang_equalTypes(m_type, b.get());
|
||||
}
|
||||
|
||||
type canonical() const { return {clang_getCanonicalType(m_type)}; }
|
||||
|
||||
bool is_const_qualified() const
|
||||
{
|
||||
return clang_isConstQualifiedType(m_type);
|
||||
}
|
||||
|
||||
bool is_volatile_qualified() const
|
||||
{
|
||||
return clang_isVolatileQualifiedType(m_type);
|
||||
}
|
||||
|
||||
bool is_restricted_qualified() const
|
||||
{
|
||||
return clang_isRestrictQualifiedType(m_type);
|
||||
}
|
||||
|
||||
std::string typedef_name() const
|
||||
{
|
||||
return to_string(clang_getTypedefName(m_type));
|
||||
}
|
||||
|
||||
type pointee_type() const { return {clang_getPointeeType(m_type)}; }
|
||||
|
||||
/*
|
||||
*cursor type_declaration() const
|
||||
*{
|
||||
* return {clang_getTypeDeclaration(m_type)};
|
||||
*}
|
||||
*/
|
||||
|
||||
/*
|
||||
*std::string type_kind_spelling() const
|
||||
*{
|
||||
* return clang_getTypeKindSpelling(kind());
|
||||
*}
|
||||
*/
|
||||
|
||||
CXCallingConv calling_convention() const
|
||||
{
|
||||
return clang_getFunctionTypeCallingConv(m_type);
|
||||
}
|
||||
|
||||
type result_type() const { return clang_getResultType(m_type); }
|
||||
|
||||
int exception_specification_type() const
|
||||
{
|
||||
return clang_getExceptionSpecificationType(m_type);
|
||||
}
|
||||
|
||||
int argument_type_count() const { return clang_getNumArgTypes(m_type); }
|
||||
|
||||
type argument_type(int i) const { return {clang_getArgType(m_type, i)}; }
|
||||
|
||||
bool is_function_variadic() const
|
||||
{
|
||||
return clang_isFunctionTypeVariadic(m_type);
|
||||
}
|
||||
|
||||
bool is_pod() const { return clang_isPODType(m_type); }
|
||||
|
||||
type element_type() const { return clang_getElementType(m_type); }
|
||||
|
||||
long long element_count() const { return clang_getNumElements(m_type); }
|
||||
|
||||
type array_element_type() const
|
||||
{
|
||||
return clang_getArrayElementType(m_type);
|
||||
}
|
||||
|
||||
type named_type() const { return clang_Type_getNamedType(m_type); }
|
||||
|
||||
CXTypeNullabilityKind nullability() const
|
||||
{
|
||||
return clang_Type_getNullability(m_type);
|
||||
}
|
||||
|
||||
type class_type() const { return clang_Type_getClassType(m_type); }
|
||||
|
||||
long long size_of() const { return clang_Type_getSizeOf(m_type); }
|
||||
|
||||
type modified_type() const { return clang_Type_getModifiedType(m_type); }
|
||||
|
||||
type value_type() const { return clang_Type_getValueType(m_type); }
|
||||
|
||||
int template_arguments_count() const
|
||||
{
|
||||
return clang_Type_getNumTemplateArguments(m_type);
|
||||
}
|
||||
|
||||
type template_argument_type(int i) const
|
||||
{
|
||||
return clang_Type_getTemplateArgumentAsType(m_type, i);
|
||||
}
|
||||
|
||||
const CXType &get() const { return m_type; }
|
||||
|
||||
CXRefQualifierKind cxxref_qualifier() const
|
||||
{
|
||||
return clang_Type_getCXXRefQualifier(m_type);
|
||||
}
|
||||
|
||||
private:
|
||||
CXType m_type;
|
||||
};
|
||||
|
||||
class cursor {
|
||||
public:
|
||||
cursor()
|
||||
: m_cursor{clang_getNullCursor()}
|
||||
{
|
||||
}
|
||||
|
||||
cursor(CXCursor &&c)
|
||||
: m_cursor{std::move(c)}
|
||||
{
|
||||
}
|
||||
|
||||
cursor(const CXCursor &c)
|
||||
: m_cursor{c}
|
||||
{
|
||||
}
|
||||
|
||||
cursor(const cursor &c)
|
||||
: m_cursor{c.get()}
|
||||
{
|
||||
}
|
||||
|
||||
~cursor() = default;
|
||||
|
||||
bool operator==(const cursor &b) const
|
||||
{
|
||||
return clang_equalCursors(m_cursor, b.get());
|
||||
}
|
||||
|
||||
cx::type type() const { return {clang_getCursorType(m_cursor)}; }
|
||||
|
||||
std::string display_name() const
|
||||
{
|
||||
return to_string(clang_getCursorDisplayName(m_cursor));
|
||||
}
|
||||
|
||||
std::string spelling() const
|
||||
{
|
||||
return to_string(clang_getCursorSpelling(m_cursor));
|
||||
}
|
||||
|
||||
std::string fully_qualified() const
|
||||
{
|
||||
std::list<std::string> res;
|
||||
cursor iterator{m_cursor};
|
||||
while (iterator.kind() != CXCursor_TranslationUnit) {
|
||||
auto name = iterator.spelling();
|
||||
if (!name.empty())
|
||||
res.push_front(iterator.spelling());
|
||||
iterator = iterator.semantic_parent();
|
||||
}
|
||||
|
||||
return fmt::format("{}", fmt::join(res, "::"));
|
||||
}
|
||||
|
||||
cursor referenced() const
|
||||
{
|
||||
return cx::cursor{clang_getCursorReferenced(m_cursor)};
|
||||
}
|
||||
|
||||
cursor semantic_parent() const
|
||||
{
|
||||
return {clang_getCursorSemanticParent(m_cursor)};
|
||||
}
|
||||
|
||||
cursor lexical_parent() const
|
||||
{
|
||||
return {clang_getCursorLexicalParent(m_cursor)};
|
||||
}
|
||||
|
||||
CXCursorKind kind() const { return m_cursor.kind; }
|
||||
|
||||
bool is_definition() const { return clang_isCursorDefinition(m_cursor); }
|
||||
|
||||
bool is_declaration() const { return clang_isDeclaration(kind()); }
|
||||
|
||||
bool is_invalid_declaration() const
|
||||
{
|
||||
return clang_isInvalidDeclaration(m_cursor);
|
||||
}
|
||||
|
||||
CXSourceLocation location() const
|
||||
{
|
||||
return clang_getCursorLocation(m_cursor);
|
||||
}
|
||||
|
||||
bool is_reference() const { return clang_isReference(kind()); }
|
||||
|
||||
bool is_expression() const { return clang_isExpression(kind()); }
|
||||
|
||||
bool is_statement() const { return clang_isStatement(kind()); }
|
||||
|
||||
bool is_attribute() const { return clang_isAttribute(kind()); }
|
||||
|
||||
bool has_attrs() const { return clang_Cursor_hasAttrs(m_cursor); }
|
||||
|
||||
bool is_invalid() const { return clang_isInvalid(kind()); }
|
||||
|
||||
bool is_translation_unit() const { return clang_isTranslationUnit(kind()); }
|
||||
|
||||
bool is_preprocessing() const { return clang_isPreprocessing(kind()); }
|
||||
|
||||
CXVisibilityKind visibitity() const
|
||||
{
|
||||
return clang_getCursorVisibility(m_cursor);
|
||||
}
|
||||
|
||||
CXAvailabilityKind availability() const
|
||||
{
|
||||
return clang_getCursorAvailability(m_cursor);
|
||||
}
|
||||
|
||||
std::string usr() const { return to_string(clang_getCursorUSR(m_cursor)); }
|
||||
|
||||
const CXCursor &get() const { return m_cursor; }
|
||||
|
||||
private:
|
||||
CXCursor m_cursor;
|
||||
};
|
||||
}
|
||||
}
|
||||
44
src/uml/sequence_diagram_model.h
Normal file
44
src/uml/sequence_diagram_model.h
Normal file
@@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include <clang-c/CXCompilationDatabase.h>
|
||||
#include <clang-c/Index.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace clanguml {
|
||||
namespace model {
|
||||
namespace sequence_diagram {
|
||||
|
||||
enum class message_t { kCall, kReturn };
|
||||
|
||||
struct message {
|
||||
message_t type;
|
||||
std::string from;
|
||||
std::string from_usr;
|
||||
std::string to;
|
||||
std::string message;
|
||||
unsigned int line;
|
||||
};
|
||||
|
||||
struct diagram {
|
||||
std::string name;
|
||||
std::vector<message> sequence;
|
||||
|
||||
void sort()
|
||||
{
|
||||
std::sort(sequence.begin(), sequence.end(),
|
||||
[](const auto &a, const auto &b) -> bool {
|
||||
if (a.from_usr == b.from_usr)
|
||||
return a.line > b.line;
|
||||
|
||||
return a.from_usr > b.from_usr;
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
126
src/uml/sequence_diagram_visitor.h
Normal file
126
src/uml/sequence_diagram_visitor.h
Normal file
@@ -0,0 +1,126 @@
|
||||
#pragma once
|
||||
|
||||
#include "cx.h"
|
||||
#include "sequence_diagram_model.h"
|
||||
|
||||
#include <clang-c/CXCompilationDatabase.h>
|
||||
#include <clang-c/Index.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace clanguml {
|
||||
namespace visitor {
|
||||
namespace sequence_diagram {
|
||||
|
||||
using clanguml::model::sequence_diagram::diagram;
|
||||
using clanguml::model::sequence_diagram::message;
|
||||
using clanguml::model::sequence_diagram::message_t;
|
||||
|
||||
struct tu_context {
|
||||
tu_context(diagram &d_)
|
||||
: d{d_}
|
||||
{
|
||||
}
|
||||
|
||||
std::vector<std::string> namespace_;
|
||||
cx::cursor current_method;
|
||||
clanguml::model::sequence_diagram::diagram &d;
|
||||
};
|
||||
|
||||
static enum CXChildVisitResult translation_unit_visitor(
|
||||
CXCursor cx_cursor, CXCursor cx_parent, CXClientData client_data)
|
||||
{
|
||||
struct tu_context *ctx = (struct tu_context *)client_data;
|
||||
|
||||
enum CXChildVisitResult ret = CXChildVisit_Break;
|
||||
|
||||
cx::cursor cursor{std::move(cx_cursor)};
|
||||
cx::cursor parent{std::move(cx_parent)};
|
||||
|
||||
if (cursor.spelling().empty()) {
|
||||
return CXChildVisit_Recurse;
|
||||
}
|
||||
|
||||
switch (cursor.kind()) {
|
||||
case CXCursor_FunctionTemplate:
|
||||
case CXCursor_CXXMethod:
|
||||
case CXCursor_FunctionDecl:
|
||||
ctx->current_method = cursor;
|
||||
ret = CXChildVisit_Recurse;
|
||||
break;
|
||||
case CXCursor_CallExpr: {
|
||||
auto referenced = cursor.referenced();
|
||||
auto referenced_type = referenced.type();
|
||||
auto referenced_cursor_name = referenced.display_name();
|
||||
|
||||
auto semantic_parent = referenced.semantic_parent();
|
||||
auto sp_name = semantic_parent.fully_qualified();
|
||||
auto lexical_parent = cursor.lexical_parent();
|
||||
auto lp_name = lexical_parent.spelling();
|
||||
|
||||
CXFile f;
|
||||
unsigned int line{};
|
||||
unsigned int column{};
|
||||
unsigned int offset{};
|
||||
clang_getFileLocation(
|
||||
cursor.location(), &f, &line, &column, &offset);
|
||||
|
||||
if (referenced.kind() == CXCursor_CXXMethod) {
|
||||
if (sp_name.find("clanguml::") == 0) {
|
||||
// Get calling object
|
||||
std::string caller =
|
||||
ctx->current_method.semantic_parent().fully_qualified();
|
||||
if (caller.empty() &&
|
||||
clang_Location_isFromMainFile(cursor.location()) == 0)
|
||||
caller = "<MAIN>";
|
||||
|
||||
std::string caller_usr = ctx->current_method.usr();
|
||||
// Get called object
|
||||
std::string callee =
|
||||
cursor.referenced().semantic_parent().fully_qualified();
|
||||
|
||||
// Get called method
|
||||
std::string called_message = cursor.spelling();
|
||||
|
||||
// Found method call: CXCursorKind () const
|
||||
spdlog::debug("Found method call at line {}:{} "
|
||||
"\n\tCURRENT_METHOD: {}\n\tFROM: {}\n\tTO: "
|
||||
"{}\n\tMESSAGE: {}",
|
||||
clang_getCString(clang_getFileName(f)), line,
|
||||
ctx->current_method.spelling(), caller, callee,
|
||||
called_message);
|
||||
|
||||
message m;
|
||||
m.type = message_t::kCall;
|
||||
m.from = caller;
|
||||
m.from_usr = caller_usr;
|
||||
m.line = line;
|
||||
m.to = callee;
|
||||
m.message = called_message;
|
||||
|
||||
ctx->d.sequence.emplace_back(std::move(m));
|
||||
}
|
||||
}
|
||||
else if (referenced.kind() == CXCursor_FunctionDecl) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
ret = CXChildVisit_Continue;
|
||||
break;
|
||||
}
|
||||
case CXCursor_Namespace: {
|
||||
ret = CXChildVisit_Recurse;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
ret = CXChildVisit_Recurse;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user