Merge pull request #1 from bkryza/refactor-to-cppast

Refactor to cppast
This commit is contained in:
Bartek Kryza
2021-03-31 01:02:48 +02:00
committed by GitHub
38 changed files with 1736 additions and 1360 deletions

View File

@@ -9,3 +9,4 @@ PointerBindsToType: false
Standard: Cpp11
BreakBeforeBinaryOperators: false
BreakBeforeBraces: Stroustrup
IndentCaseLabels: false

View File

@@ -6,7 +6,10 @@ jobs:
build-ubuntu:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v1
- name: Checkout repository
uses: actions/checkout@v2
with:
submodules: recursive
- name: Install deps
run: sudo apt-get install ccache cmake libyaml-cpp-dev libspdlog-dev clang-11 libclang-11-dev libclang-cpp11-dev
- name: Build and unit test

4
.gitmodules vendored Normal file
View File

@@ -0,0 +1,4 @@
[submodule "thirdparty/cppast"]
path = thirdparty/cppast
url = https://github.com/bkryza/cppast
branch = handle-exposed-template-arguments

View File

@@ -15,6 +15,8 @@ set(CLANG_UML_INSTALL_BIN_DIR ${PROJECT_SOURCE_DIR}/bin)
set(UML_HEADERS_DIR ${PROJECT_SOURCE_DIR}/src/uml)
set(LLVM_PREFERRED_VERSION 11.0.0)
message(STATUS "Checking for spdlog...")
find_package(spdlog REQUIRED)
@@ -28,6 +30,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 ${LIBCLANG_CXXFLAGS}")
# Thirdparty sources
set(THIRDPARTY_HEADERS_DIR ${PROJECT_SOURCE_DIR}/thirdparty/)
add_subdirectory(thirdparty/cppast)
find_package(LLVM REQUIRED CONFIG)
set(CLANG_INCLUDE_DIRS "llvm/clang/include")
@@ -37,6 +40,9 @@ include_directories(${CLANG_UML_INSTALL_INCLUDE_DIR})
include_directories(${YAML_CPP_INCLUDE_DIR})
include_directories(${UML_HEADERS_DIR})
include_directories(${THIRDPARTY_HEADERS_DIR})
include_directories(${THIRDPARTY_HEADERS_DIR}/cppast/include)
include_directories(${THIRDPARTY_HEADERS_DIR}/cppast/external/type_safe/include)
include_directories(${THIRDPARTY_HEADERS_DIR}/cppast/external/type_safe/external/debug_assert)
include_directories(${PROJECT_SOURCE_DIR}/src/)
file(GLOB_RECURSE SOURCES src/*.cc include/*.h)
@@ -47,7 +53,7 @@ add_library(clang-umllib OBJECT ${SOURCES})
add_executable(clang-uml ${MAIN_SOURCE_FILE})
install(TARGETS clang-uml DESTINATION ${CLANG_UML_INSTALL_BIN_DIR})
target_link_libraries(clang-uml ${LIBCLANG_LIBRARIES} ${YAML_CPP_LIBRARIES} spdlog::spdlog clang-umllib)
target_link_libraries(clang-uml ${LIBCLANG_LIBRARIES} ${YAML_CPP_LIBRARIES} spdlog::spdlog cppast clang-umllib)
install(
FILES

View File

@@ -21,7 +21,7 @@
# Specify preferred LLVM version for CMake
LLVM_VERSION ?= 11
.DEFAULT_GOAL := test
.DEFAULT_GOAL := debug
.PHONY: clean
clean:

View File

@@ -211,6 +211,11 @@ public:
return clang_getCXXAccessSpecifier(m_cursor);
}
cx::type underlying_type() const
{
return clang_getTypedefDeclUnderlyingType(m_cursor);
}
int template_argument_count() const
{
return clang_Cursor_getNumTemplateArguments(m_cursor);

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,77 @@ 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(), "::"));
}
const cppast::cpp_type &unreferenced(const cppast::cpp_type &t)
{
if (t.kind() == cppast::cpp_type_kind::pointer_t)
return unreferenced(
static_cast<const cppast::cpp_pointer_type &>(t).pointee());
else if (t.kind() == cppast::cpp_type_kind::reference_t)
return unreferenced(
static_cast<const cppast::cpp_reference_type &>(t).referee());
return t;
}
} // namespace util
} // namespace cx
} // namespace clanguml

View File

@@ -19,6 +19,8 @@
#include <clang-c/CXCompilationDatabase.h>
#include <clang-c/Index.h>
#include <cppast/cpp_entity.hpp>
#include <cppast/cpp_type.hpp>
#include <string>
@@ -38,6 +40,12 @@ namespace util {
*/
std::string to_string(CXString &&cxs);
std::string full_name(const cppast::cpp_entity &e);
std::string fully_prefixed(const cppast::cpp_entity &e);
const cppast::cpp_type &unreferenced(const cppast::cpp_type &t);
} // namespace util
} // namespace cx
} // namespace clanguml

View File

@@ -25,6 +25,7 @@
#include "uml/sequence_diagram_visitor.h"
#include <cli11/CLI11.hpp>
#include <cppast/libclang_parser.hpp>
#include <glob/glob.hpp>
#include <spdlog/spdlog.h>
@@ -73,6 +74,8 @@ int main(int argc, const char *argv[])
auto db =
compilation_database::from_directory(config.compilation_database_dir);
cppast::libclang_compilation_database db2(config.compilation_database_dir);
for (const auto &[name, diagram] : config.diagrams) {
using clanguml::config::class_diagram;
using clanguml::config::sequence_diagram;
@@ -83,7 +86,7 @@ int main(int argc, const char *argv[])
if (std::dynamic_pointer_cast<class_diagram>(diagram)) {
auto model = generators::class_diagram::generate(
db, name, dynamic_cast<class_diagram &>(*diagram));
db2, name, dynamic_cast<class_diagram &>(*diagram));
ofs << clanguml::generators::class_diagram::puml::generator(
dynamic_cast<clanguml::config::class_diagram &>(*diagram),

View File

@@ -23,6 +23,8 @@
#include "uml/class_diagram_visitor.h"
#include "util/util.h"
#include <cppast/cpp_entity_index.hpp>
#include <cppast/libclang_parser.hpp>
#include <glob/glob.hpp>
#include <filesystem>
@@ -65,37 +67,37 @@ public:
std::string to_string(scope_t scope) const
{
switch (scope) {
case scope_t::kPublic:
return "+";
case scope_t::kProtected:
return "#";
case scope_t::kPrivate:
return "-";
default:
return "";
case scope_t::kPublic:
return "+";
case scope_t::kProtected:
return "#";
case scope_t::kPrivate:
return "-";
default:
return "";
}
}
std::string to_string(relationship_t r) const
{
switch (r) {
case relationship_t::kOwnership:
case relationship_t::kComposition:
return "*--";
case relationship_t::kAggregation:
return "o--";
case relationship_t::kContainment:
return "+--";
case relationship_t::kAssociation:
return "-->";
case relationship_t::kInstantiation:
return "..|>";
case relationship_t::kFriendship:
return "<..";
case relationship_t::kDependency:
return "..>";
default:
return "";
case relationship_t::kOwnership:
case relationship_t::kComposition:
return "*--";
case relationship_t::kAggregation:
return "o--";
case relationship_t::kContainment:
return "--+";
case relationship_t::kAssociation:
return "-->";
case relationship_t::kInstantiation:
return "..|>";
case relationship_t::kFriendship:
return "<..";
case relationship_t::kDependency:
return "..>";
default:
return "";
}
}
@@ -222,6 +224,33 @@ public:
}
ostr << "}" << std::endl;
for (const auto &r : e.relationships) {
std::string destination;
if (r.destination.find("#") != std::string::npos ||
r.destination.find("@") != std::string::npos) {
destination = m_model.usr_to_name(
m_config.using_namespace, r.destination);
if (destination.empty()) {
ostr << "' ";
destination = r.destination;
}
}
else {
destination = r.destination;
}
ostr << m_model.to_alias(m_config.using_namespace,
ns_relative(m_config.using_namespace, e.name))
<< " " << to_string(r.type) << " "
<< m_model.to_alias(m_config.using_namespace,
ns_relative(m_config.using_namespace, destination));
if (!r.label.empty())
ostr << " : " << r.label;
ostr << std::endl;
}
}
void generate(std::ostream &ostr) const
@@ -267,7 +296,7 @@ std::ostream &operator<<(std::ostream &os, const generator &g)
}
clanguml::model::class_diagram::diagram generate(
clanguml::cx::compilation_database &db, const std::string &name,
cppast::libclang_compilation_database &db, const std::string &name,
clanguml::config::class_diagram &diagram)
{
spdlog::info("Generating diagram {}.puml", name);
@@ -276,7 +305,7 @@ clanguml::model::class_diagram::diagram generate(
// Get all translation units matching the glob from diagram
// configuration
std::vector<std::filesystem::path> translation_units{};
std::vector<std::string> translation_units{};
for (const auto &g : diagram.glob) {
spdlog::debug("Processing glob: {}", g);
const auto matches = glob::glob(g);
@@ -284,13 +313,21 @@ clanguml::model::class_diagram::diagram generate(
std::back_inserter(translation_units));
}
cppast::cpp_entity_index idx;
cppast::simple_file_parser<cppast::libclang_parser> parser{
type_safe::ref(idx)};
// Process all matching translation units
clanguml::visitor::class_diagram::tu_visitor ctx(idx, d, diagram);
cppast::parse_files(parser, translation_units, db);
for (auto &file : parser.files())
ctx(file);
/*
for (const auto &tu_path : translation_units) {
spdlog::debug("Processing translation unit: {}",
std::filesystem::canonical(tu_path).c_str());
auto tu = db.parse_translation_unit(tu_path);
auto cursor = clang_getTranslationUnitCursor(tu);
if (clang_Cursor_isNull(cursor)) {
@@ -309,6 +346,7 @@ clanguml::model::class_diagram::diagram generate(
clang_suspendTranslationUnit(tu);
}
*/
return d;
}

View File

@@ -54,12 +54,12 @@ public:
std::string to_string(message_t r) const
{
switch (r) {
case message_t::kCall:
return "->";
case message_t::kReturn:
return "<--";
default:
return "";
case message_t::kCall:
return "->";
case message_t::kReturn:
return "<--";
default:
return "";
}
}

View File

@@ -22,6 +22,34 @@ namespace clanguml {
namespace model {
namespace class_diagram {
std::atomic_uint64_t element::m_nextId = 1;
std::string to_string(relationship_t r)
{
switch (r) {
case relationship_t::kNone:
return "none";
case relationship_t::kExtension:
return "extension";
case relationship_t::kComposition:
return "composition";
case relationship_t::kAggregation:
return "aggregation";
case relationship_t::kContainment:
return "containment";
case relationship_t::kOwnership:
return "ownership";
case relationship_t::kAssociation:
return "association";
case relationship_t::kInstantiation:
return "instantiation";
case relationship_t::kFriendship:
return "frendship";
case relationship_t::kDependency:
return "dependency";
default:
return "invalid";
}
}
}
}
}

View File

@@ -49,6 +49,8 @@ enum class relationship_t {
kDependency
};
std::string to_string(relationship_t r);
class element {
public:
element()
@@ -211,6 +213,7 @@ public:
struct enum_ : public element {
std::vector<std::string> constants;
std::vector<class_relationship> relationships;
friend bool operator==(const enum_ &l, const enum_ &r)
{
@@ -225,17 +228,22 @@ struct diagram {
void add_class(class_ &&c)
{
spdlog::debug("ADDING CLASS: {}, {}", c.name, c.usr);
spdlog::debug("Adding class: {}, {}", c.name, c.usr);
auto it = std::find(classes.begin(), classes.end(), c);
if (it == classes.end())
classes.emplace_back(std::move(c));
else
spdlog::debug("Class {} already in the model", c.name);
}
void add_enum(enum_ &&e)
{
spdlog::debug("Adding enum: {}", e.name);
auto it = std::find(enums.begin(), enums.end(), e);
if (it == enums.end())
enums.emplace_back(std::move(e));
else
spdlog::debug("Enum {} already in the model", e.name);
}
std::string to_alias(const std::vector<std::string> &using_namespaces,

File diff suppressed because it is too large Load Diff

View File

@@ -23,6 +23,13 @@
#include <clang-c/CXCompilationDatabase.h>
#include <clang-c/Index.h>
#include <cppast/cpp_friend.hpp>
#include <cppast/cpp_function_template.hpp>
#include <cppast/cpp_member_function.hpp>
#include <cppast/cpp_member_variable.hpp>
#include <cppast/cpp_template_parameter.hpp>
#include <cppast/cpp_type.hpp>
#include <cppast/visitor.hpp>
#include <functional>
#include <memory>
@@ -33,14 +40,17 @@ namespace visitor {
namespace class_diagram {
struct tu_context {
tu_context(clanguml::model::class_diagram::diagram &d_,
tu_context(cppast::cpp_entity_index &idx,
clanguml::model::class_diagram::diagram &d_,
const clanguml::config::class_diagram &config_)
: d{d_}
: entity_index{idx}
, d{d_}
, config{config_}
{
}
std::vector<std::string> namespace_;
cppast::cpp_entity_index &entity_index;
clanguml::model::class_diagram::diagram &d;
const clanguml::config::class_diagram &config;
};
@@ -58,69 +68,81 @@ template <typename T> struct element_visitor_context {
clanguml::model::class_diagram::diagram &d;
};
// Visitors
class tu_visitor {
public:
tu_visitor(cppast::cpp_entity_index &idx_,
clanguml::model::class_diagram::diagram &d_,
const clanguml::config::class_diagram &config_)
: ctx{idx_, d_, config_}
{
}
enum CXChildVisitResult visit_if_cursor_valid(
cx::cursor cursor, std::function<enum CXChildVisitResult(cx::cursor)> f);
void operator()(const cppast::cpp_entity &file);
enum CXChildVisitResult enum_visitor(
CXCursor cx_cursor, CXCursor cx_parent, CXClientData client_data);
void process_class_declaration(const cppast::cpp_class &cls);
enum CXChildVisitResult method_parameter_visitor(
CXCursor cx_cursor, CXCursor cx_parent, CXClientData client_data);
void process_enum_declaration(const cppast::cpp_enum &enm);
enum CXChildVisitResult friend_class_visitor(
CXCursor cx_cursor, CXCursor cx_parent, CXClientData client_data);
void process_field(const cppast::cpp_member_variable &mv,
clanguml::model::class_diagram::class_ &c,
cppast::cpp_access_specifier_kind as);
enum CXChildVisitResult class_visitor(
CXCursor cx_cursor, CXCursor cx_parent, CXClientData client_data);
void process_static_field(const cppast::cpp_variable &mv,
clanguml::model::class_diagram::class_ &c,
cppast::cpp_access_specifier_kind as);
enum CXChildVisitResult translation_unit_visitor(
CXCursor cx_cursor, CXCursor cx_parent, CXClientData client_data);
void process_method(const cppast::cpp_member_function &mf,
clanguml::model::class_diagram::class_ &c,
cppast::cpp_access_specifier_kind as);
// Entity processors
void process_template_method(const cppast::cpp_function_template &mf,
clanguml::model::class_diagram::class_ &c,
cppast::cpp_access_specifier_kind as);
enum CXChildVisitResult process_class_base_specifier(cx::cursor cursor,
clanguml::model::class_diagram::class_ *parent, struct tu_context *ctx);
void process_static_method(const cppast::cpp_function &mf,
clanguml::model::class_diagram::class_ &c,
cppast::cpp_access_specifier_kind as);
enum CXChildVisitResult process_template_type_parameter(cx::cursor cursor,
clanguml::model::class_diagram::class_ *parent, struct tu_context *ctx);
void process_constructor(const cppast::cpp_constructor &mf,
clanguml::model::class_diagram::class_ &c,
cppast::cpp_access_specifier_kind as);
enum CXChildVisitResult process_template_nontype_parameter(cx::cursor cursor,
clanguml::model::class_diagram::class_ *parent, struct tu_context *ctx);
void process_destructor(const cppast::cpp_destructor &mf,
clanguml::model::class_diagram::class_ &c,
cppast::cpp_access_specifier_kind as);
enum CXChildVisitResult process_template_template_parameter(cx::cursor cursor,
clanguml::model::class_diagram::class_ *parent, struct tu_context *ctx);
void process_function_parameter(const cppast::cpp_function_parameter &param,
clanguml::model::class_diagram::class_method &m,
clanguml::model::class_diagram::class_ &c);
enum CXChildVisitResult process_method(cx::cursor cursor,
clanguml::model::class_diagram::class_ *parent, struct tu_context *ctx);
void find_relationships(const cppast::cpp_type &t,
std::vector<std::pair<std::string,
clanguml::model::class_diagram::relationship_t>> &relationships,
clanguml::model::class_diagram::relationship_t relationship_hint =
clanguml::model::class_diagram::relationship_t::kNone);
enum CXChildVisitResult process_class_declaration(cx::cursor cursor,
bool is_struct, clanguml::model::class_diagram::class_ *parent,
struct tu_context *ctx);
void process_template_type_parameter(
const cppast::cpp_template_type_parameter &t,
clanguml::model::class_diagram::class_ &parent);
enum CXChildVisitResult process_enum_declaration(cx::cursor cursor,
clanguml::model::class_diagram::class_ *parent, struct tu_context *ctx);
void process_template_nontype_parameter(
const cppast::cpp_non_type_template_parameter &t,
clanguml::model::class_diagram::class_ &parent);
bool process_template_specialization_class_field(cx::cursor cursor, cx::type t,
clanguml::model::class_diagram::class_ *parent, struct tu_context *ctx);
void process_template_template_parameter(
const cppast::cpp_template_template_parameter &t,
clanguml::model::class_diagram::class_ &parent);
enum CXChildVisitResult process_field(cx::cursor cursor,
clanguml::model::class_diagram::class_ *parent, struct tu_context *ctx);
void process_friend(const cppast::cpp_friend &t,
clanguml::model::class_diagram::class_ &parent);
// Utils
private:
clanguml::model::class_diagram::class_ build_template_instantiation(
const cppast::cpp_entity &e,
const cppast::cpp_template_instantiation_type &t);
clanguml::model::class_diagram::class_ build_template_instantiation(
cx::cursor cursor, cx::type t);
clanguml::model::class_diagram::scope_t cx_access_specifier_to_scope(
CX_CXXAccessSpecifier as);
void find_relationships(cx::type t,
std::vector<std::pair<cx::type,
clanguml::model::class_diagram::relationship_t>> &relationships,
clanguml::model::class_diagram::relationship_t relationship_hint =
clanguml::model::class_diagram::relationship_t::kNone);
tu_context ctx;
};
}
}
}

View File

@@ -65,100 +65,97 @@ static enum CXChildVisitResult translation_unit_visitor(
}
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();
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();
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);
std::string file{clang_getCString(clang_getFileName(f))};
CXFile f;
unsigned int line{};
unsigned int column{};
unsigned int offset{};
clang_getFileLocation(cursor.location(), &f, &line, &column, &offset);
std::string file{clang_getCString(clang_getFileName(f))};
auto &d = ctx->d;
auto &config = ctx->config;
if (referenced.kind() == CXCursor_CXXMethod) {
if (config.should_include(sp_name)) {
// Get calling object
std::string caller{};
if (ctx->current_method.semantic_parent()
.is_translation_unit() ||
ctx->current_method.semantic_parent().is_namespace()) {
caller = ctx->current_method.semantic_parent()
.fully_qualified() +
"::" + ctx->current_method.spelling() + "()";
}
else {
caller = ctx->current_method.semantic_parent()
.fully_qualified();
}
auto caller_usr = ctx->current_method.usr();
// Get called object
auto callee =
referenced.semantic_parent().fully_qualified();
auto callee_usr = referenced.semantic_parent().usr();
// Get called method
auto called_message = cursor.spelling();
// Found method call: CXCursorKind () const
spdlog::debug(
"Adding method call at line {}:{} to diagram {}"
"\n\tCURRENT_METHOD: {}\n\tFROM: '{}'\n\tTO: "
"{}\n\tMESSAGE: {}\n\tFROM_USR: {}\n\tTO_USR: "
"{}\n\tRETURN_TYPE: {}",
file, line, d.name, ctx->current_method.spelling(),
caller, callee, called_message, caller_usr, callee_usr,
referenced.type().result_type().spelling());
message m;
m.type = message_t::kCall;
m.from = caller;
m.from_usr = caller_usr;
m.line = line;
m.to = callee;
m.to_usr = referenced.usr();
m.message = called_message;
m.return_type = referenced.type().result_type().spelling();
if (d.sequences.find(caller_usr) == d.sequences.end()) {
activity a;
a.usr = caller_usr;
a.from = caller;
d.sequences.insert({caller_usr, std::move(a)});
}
d.sequences[caller_usr].messages.emplace_back(std::move(m));
auto &d = ctx->d;
auto &config = ctx->config;
if (referenced.kind() == CXCursor_CXXMethod) {
if (config.should_include(sp_name)) {
// Get calling object
std::string caller{};
if (ctx->current_method.semantic_parent()
.is_translation_unit() ||
ctx->current_method.semantic_parent().is_namespace()) {
caller = ctx->current_method.semantic_parent()
.fully_qualified() +
"::" + ctx->current_method.spelling() + "()";
}
else {
caller =
ctx->current_method.semantic_parent().fully_qualified();
}
}
else if (referenced.kind() == CXCursor_FunctionDecl) {
// TODO
}
ret = CXChildVisit_Recurse;
break;
auto caller_usr = ctx->current_method.usr();
// Get called object
auto callee = referenced.semantic_parent().fully_qualified();
auto callee_usr = referenced.semantic_parent().usr();
// Get called method
auto called_message = cursor.spelling();
// Found method call: CXCursorKind () const
spdlog::debug("Adding method call at line {}:{} to diagram {}"
"\n\tCURRENT_METHOD: {}\n\tFROM: '{}'\n\tTO: "
"{}\n\tMESSAGE: {}\n\tFROM_USR: {}\n\tTO_USR: "
"{}\n\tRETURN_TYPE: {}",
file, line, d.name, ctx->current_method.spelling(), caller,
callee, called_message, caller_usr, callee_usr,
referenced.type().result_type().spelling());
message m;
m.type = message_t::kCall;
m.from = caller;
m.from_usr = caller_usr;
m.line = line;
m.to = callee;
m.to_usr = referenced.usr();
m.message = called_message;
m.return_type = referenced.type().result_type().spelling();
if (d.sequences.find(caller_usr) == d.sequences.end()) {
activity a;
a.usr = caller_usr;
a.from = caller;
d.sequences.insert({caller_usr, std::move(a)});
}
d.sequences[caller_usr].messages.emplace_back(std::move(m));
}
}
case CXCursor_Namespace: {
ret = CXChildVisit_Recurse;
break;
else if (referenced.kind() == CXCursor_FunctionDecl) {
// TODO
}
default:
ret = CXChildVisit_Recurse;
ret = CXChildVisit_Recurse;
break;
}
case CXCursor_Namespace: {
ret = CXChildVisit_Recurse;
break;
}
default:
ret = CXChildVisit_Recurse;
}
return ret;

View File

@@ -91,8 +91,8 @@ std::string ns_relative(
std::string unqualify(const std::string &s)
{
auto toks = clanguml::util::split(s, " ");
const std::vector<std::string> qualifiers = {
"static", "const", "volatile", "register", "mutable", "struct", "enum"};
const std::vector<std::string> qualifiers = {"static", "const", "volatile",
"register", "constexpr", "mutable", "struct", "enum"};
toks.erase(toks.begin(),
std::find_if(toks.begin(), toks.end(), [&qualifiers](const auto &t) {

View File

@@ -31,7 +31,7 @@ target_link_libraries(test_util
PRIVATE
${LIBCLANG_LIBRARIES}
${YAML_CPP_LIBRARIES}
spdlog::spdlog clang-umllib)
spdlog::spdlog clang-umllib cppast)
add_executable(test_cases
@@ -42,7 +42,7 @@ target_link_libraries(test_cases
PRIVATE
${LIBCLANG_LIBRARIES}
${YAML_CPP_LIBRARIES}
spdlog::spdlog clang-umllib)
spdlog::spdlog clang-umllib cppast)
foreach(TEST_CASE_CONFIG ${TEST_CASE_CONFIGS})
file(RELATIVE_PATH

File diff suppressed because it is too large Load Diff

View File

@@ -56,7 +56,8 @@ TEST_CASE("t00003", "[test-case][class]")
REQUIRE_THAT(puml, IsField(Public("int public_member")));
REQUIRE_THAT(puml, IsField(Protected("int protected_member")));
REQUIRE_THAT(puml, IsField(Private("int private_member")));
REQUIRE_THAT(puml, IsField(Static(Public("unsigned long auto_member"))));
REQUIRE_THAT(
puml, IsField(Static(Public("unsigned long const auto_member"))));
REQUIRE_THAT(puml, IsField(Private("int a")));
REQUIRE_THAT(puml, IsField(Private("int b")));

View File

@@ -58,8 +58,8 @@ TEST_CASE("t00005", "[test-case][class]")
REQUIRE_THAT(puml, IsClass(_A("R")));
REQUIRE_THAT(puml, IsField(Public("int some_int")));
REQUIRE_THAT(puml, IsField(Public("int * some_int_pointer")));
REQUIRE_THAT(puml, IsField(Public("int ** some_int_pointer_pointer")));
REQUIRE_THAT(puml, IsField(Public("int* some_int_pointer")));
REQUIRE_THAT(puml, IsField(Public("int** some_int_pointer_pointer")));
REQUIRE_THAT(puml, IsComposition(_A("R"), _A("A"), "a"));
REQUIRE_THAT(puml, IsAssociation(_A("R"), _A("B"), "b"));

View File

@@ -17,9 +17,18 @@ public:
CMP comparator;
};
template <typename T, template <typename> typename C> class B {
public:
template <typename T> struct Vector {
std::vector<T> values;
};
template <typename T, template <typename> typename C> struct B {
C<T> template_template;
};
struct D {
B<int, Vector> ints;
void add(int i) { ints.template_template.values.push_back(i); }
};
}
}

View File

@@ -42,18 +42,20 @@ TEST_CASE("t00008", "[test-case][class]")
REQUIRE_THAT(puml, StartsWith("@startuml"));
REQUIRE_THAT(puml, EndsWith("@enduml\n"));
REQUIRE_THAT(puml, IsClassTemplate("A", "T, P, bool (*)(int, int), int N"));
REQUIRE_THAT(puml, IsField(Public("T value")));
REQUIRE_THAT(puml, IsField(Public("T * pointer")));
REQUIRE_THAT(puml, IsField(Public("T & reference")));
REQUIRE_THAT(puml, IsField(Public("std::vector<P> values")));
REQUIRE_THAT(puml, IsField(Public("std::array<int, N> ints")));
REQUIRE_THAT(puml, IsField(Public("bool (*)(int, int) comparator")));
// TODO: add option to resolve using declared types
// REQUIRE_THAT(puml, IsClassTemplate("A", "T, P, bool (*)(int, int), int
// N"));
REQUIRE_THAT(puml, IsClassTemplate("A", "T, P, CMP, int N"));
REQUIRE_THAT(puml, IsClassTemplate("B", "T, C<>"));
REQUIRE_THAT(puml, IsField(Public("C<T> template_template")));
REQUIRE_THAT(puml, IsField(Public("T value")));
REQUIRE_THAT(puml, IsField(Public("T* pointer")));
REQUIRE_THAT(puml, IsField(Public("T& reference")));
REQUIRE_THAT(puml, IsField(Public("std::vector<P> values")));
REQUIRE_THAT(puml, IsField(Public("std::array<int,N> ints")));
// TODO: add option to resolve using declared types
// REQUIRE_THAT(puml, IsField(Public("bool (*)(int, int) comparator")));
REQUIRE_THAT(puml, IsField(Public("CMP comparator")));
save_puml(
"./" + config.output_directory + "/" + diagram->name + ".puml", puml);

View File

@@ -47,9 +47,8 @@ TEST_CASE("t00009", "[test-case][class]")
REQUIRE_THAT(puml, IsField(Public("T value")));
REQUIRE_THAT(puml, IsField(Public("A<int> aint")));
REQUIRE_THAT(puml, IsField(Public("A<std::string> * astring")));
REQUIRE_THAT(
puml, IsField(Public("A<std::vector<std::string>> & avector")));
REQUIRE_THAT(puml, IsField(Public("A<std::string>* astring")));
REQUIRE_THAT(puml, IsField(Public("A<std::vector<std::string>>& avector")));
REQUIRE_THAT(puml, IsInstantiation(_A("A<T>"), _A("A<int>")));
REQUIRE_THAT(puml, IsInstantiation(_A("A<T>"), _A("A<std::string>")));

View File

@@ -45,7 +45,7 @@ TEST_CASE("t00010", "[test-case][class]")
REQUIRE_THAT(puml, IsClassTemplate("A", "T, P"));
REQUIRE_THAT(puml, IsClassTemplate("B", "T"));
REQUIRE_THAT(puml, IsField(Public("A<T, std::string> astring")));
REQUIRE_THAT(puml, IsField(Public("A<T,std::string> astring")));
REQUIRE_THAT(puml, IsField(Public("B<int> aintstring")));
REQUIRE_THAT(puml, IsInstantiation(_A("A<T, P>"), _A("A<T, std::string>")));

View File

@@ -17,8 +17,11 @@ private:
void foo() {}
friend class B;
friend class external::C;
// TODO
template <typename T> friend class D;
// TODO: Add friend for a template specialization
// TODO
friend class D<int>;
friend class D<A>;
};
class B {

View File

@@ -47,7 +47,7 @@ TEST_CASE("t00011", "[test-case][class]")
REQUIRE_THAT(puml, IsClass(_A("D<T>")));
REQUIRE_THAT(puml, IsFriend(_A("A"), _A("B")));
REQUIRE_THAT(puml, IsFriend(_A("A"), _A("D<T>")));
// REQUIRE_THAT(puml, IsFriend(_A("A"), _A("D<T>")));
save_puml(
"./" + config.output_directory + "/" + diagram->name + ".puml", puml);

View File

@@ -3,6 +3,7 @@
#include <numeric>
#include <string>
#include <variant>
#include <vector>
namespace clanguml {
namespace t00012 {

View File

@@ -50,7 +50,7 @@ TEST_CASE("t00012", "[test-case][class]")
puml, IsInstantiation(_A("B<int Is...>"), _A("B<1, 1, 1, 1>")));
REQUIRE_THAT(puml,
IsInstantiation(_A("C<T, int Is...>"),
_A("C<std::map<int, "
_A("C<std::map<int,"
"std::vector<std::vector<std::vector<std::string>>>>, 3, 3, "
"3>")));

View File

@@ -2,7 +2,6 @@
#include <map>
#include <numeric>
#include <string>
#include <variant>
namespace ABCD {
template <typename T> struct F {
@@ -40,17 +39,18 @@ class R {
public:
int get_a(A *a) { return a->a; }
int get_b(B &b) { return b.b; }
// TODO: int get_const_b(const B &b) { return b.b; }
int get_const_b(const B &b) { return b.b; }
int get_c(C c) { return c.c; }
int get_d(D &&d) { return d.d; }
// Dependency relationship should be rendered only once
int get_d2(D &&d) { return d.d; }
template <typename T> T get_e(E<T> &e) { return e.e; }
template <typename T> T get_e(E<T> e) { return e.e; }
int get_int_e(const E<int> &e) { return e.e; }
int get_int_e2(E<int> &e) { return e.e; }
template <typename T> T get_f(const F<T> &f) { return f.f; }
int get_int_f(const F<int> &f) { return f.f; }
private:
mutable E<std::string> estring;

View File

@@ -51,13 +51,15 @@ TEST_CASE("t00013", "[test-case][class]")
REQUIRE_THAT(puml, IsDependency(_A("R"), _A("B")));
REQUIRE_THAT(puml, IsDependency(_A("R"), _A("C")));
REQUIRE_THAT(puml, IsDependency(_A("R"), _A("D")));
REQUIRE_THAT(puml, IsDependency(_A("D"), _A("R")));
REQUIRE_THAT(puml, IsDependency(_A("R"), _A("E<T>")));
REQUIRE_THAT(puml, IsDependency(_A("R"), _A("E<int>")));
REQUIRE_THAT(puml, IsInstantiation(_A("E<T>"), _A("E<int>")));
REQUIRE_THAT(puml, IsInstantiation(_A("E<T>"), _A("E<std::string>")));
REQUIRE_THAT(puml, IsComposition(_A("R"), _A("E<std::string>"), "estring"));
REQUIRE_THAT(puml, IsDependency(_A("R"), _A("ABCD::F<T>")));
REQUIRE_THAT(puml, IsDependency(_A("D"), _A("R")));
REQUIRE_THAT(puml, IsInstantiation(_A("ABCD::F<T>"), _A("ABCD::F<int>")));
REQUIRE_THAT(puml, IsDependency(_A("R"), _A("ABCD::F<int>")));
save_puml(
"./" + config.output_directory + "/" + diagram->name + ".puml", puml);

12
tests/t00014/.clanguml Normal file
View File

@@ -0,0 +1,12 @@
compilation_database_dir: ..
output_directory: puml
diagrams:
t00014_class:
type: class
glob:
- ../../tests/t00014/t00014.cc
using_namespace:
- clanguml::t00014
include:
namespaces:
- clanguml::t00014

29
tests/t00014/t00014.cc Normal file
View File

@@ -0,0 +1,29 @@
#include <algorithm>
#include <ios>
#include <map>
#include <numeric>
#include <string>
#include <type_traits>
#include <variant>
namespace clanguml {
namespace t00014 {
template <typename T, typename P> struct A {
T t;
P p;
};
template <typename T> using AString = A<T, std::string>;
using AIntString = AString<int>;
using AStringString = AString<std::string>;
class R {
A<bool, std::string> boolstring;
AString<float> floatstring;
AIntString intstring;
AStringString stringstring;
};
}
}

66
tests/t00014/test_case.h Normal file
View File

@@ -0,0 +1,66 @@
/**
* tests/t00014/test_case.cc
*
* Copyright (c) 2021 Bartek Kryza <bkryza@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
TEST_CASE("t00014", "[test-case][class]")
{
auto [config, db] = load_config("t00014");
auto diagram = config.diagrams["t00014_class"];
REQUIRE(diagram->name == "t00014_class");
REQUIRE_THAT(diagram->include.namespaces,
VectorContains(std::string{"clanguml::t00014"}));
REQUIRE(diagram->exclude.namespaces.size() == 0);
REQUIRE(diagram->should_include("clanguml::t00014::S"));
auto model = generate_class_diagram(db, diagram);
REQUIRE(model.name == "t00014_class");
auto puml = generate_class_puml(diagram, model);
AliasMatcher _A(puml);
REQUIRE_THAT(puml, StartsWith("@startuml"));
REQUIRE_THAT(puml, EndsWith("@enduml\n"));
REQUIRE_THAT(puml, IsClassTemplate("A", "T, P"));
/*
REQUIRE_THAT(puml, IsClass(_A("B")));
REQUIRE_THAT(puml, IsClass(_A("C")));
REQUIRE_THAT(puml, IsClass(_A("D")));
REQUIRE_THAT(puml, !IsDependency(_A("R"), _A("R")));
REQUIRE_THAT(puml, IsDependency(_A("R"), _A("A")));
REQUIRE_THAT(puml, IsDependency(_A("R"), _A("B")));
REQUIRE_THAT(puml, IsDependency(_A("R"), _A("C")));
REQUIRE_THAT(puml, IsDependency(_A("R"), _A("D")));
REQUIRE_THAT(puml, IsDependency(_A("D"), _A("R")));
REQUIRE_THAT(puml, IsDependency(_A("R"), _A("E<T>")));
REQUIRE_THAT(puml, IsDependency(_A("R"), _A("E<int>")));
REQUIRE_THAT(puml, IsInstantiation(_A("E<T>"), _A("E<int>")));
REQUIRE_THAT(puml, IsInstantiation(_A("E<T>"), _A("E<std::string>")));
REQUIRE_THAT(puml, IsComposition(_A("R"), _A("E<std::string>"), "estring"));
REQUIRE_THAT(puml, IsDependency(_A("R"), _A("ABCD::F<T>")));
REQUIRE_THAT(puml, IsInstantiation(_A("ABCD::F<T>"), _A("ABCD::F<int>")));
REQUIRE_THAT(puml, IsDependency(_A("R"), _A("ABCD::F<int>")));
*/
save_puml(
"./" + config.output_directory + "/" + diagram->name + ".puml", puml);
}

View File

@@ -18,7 +18,7 @@
TEST_CASE("t20001", "[test-case][sequence]")
{
auto [config, db] = load_config("t20001");
auto [config, db] = load_config2("t20001");
auto diagram = config.diagrams["t20001_sequence"];

View File

@@ -19,7 +19,17 @@
#include <spdlog/spdlog.h>
std::pair<clanguml::config::config, compilation_database> load_config(
std::pair<clanguml::config::config, cppast::libclang_compilation_database>
load_config(const std::string &test_name)
{
auto config = clanguml::config::load(test_name + "/.clanguml");
cppast::libclang_compilation_database db(config.compilation_database_dir);
return std::make_pair(std::move(config), std::move(db));
}
std::pair<clanguml::config::config, compilation_database> load_config2(
const std::string &test_name)
{
auto config = clanguml::config::load(test_name + "/.clanguml");
@@ -42,7 +52,7 @@ clanguml::model::sequence_diagram::diagram generate_sequence_diagram(
}
clanguml::model::class_diagram::diagram generate_class_diagram(
compilation_database &db,
cppast::libclang_compilation_database &db,
std::shared_ptr<clanguml::config::diagram> diagram)
{
auto diagram_model =
@@ -129,6 +139,7 @@ using clanguml::test::matchers::Static;
#include "t00011/test_case.h"
#include "t00012/test_case.h"
#include "t00013/test_case.h"
#include "t00014/test_case.h"
//
// Sequence diagram tests

View File

@@ -43,7 +43,9 @@ using Catch::Matchers::VectorContains;
using clanguml::cx::compilation_database;
using namespace clanguml::util;
std::pair<clanguml::config::config, compilation_database> load_config(
std::pair<clanguml::config::config, cppast::libclang_compilation_database>
load_config(const std::string &test_name);
std::pair<clanguml::config::config, compilation_database> load_config2(
const std::string &test_name);
clanguml::model::sequence_diagram::diagram generate_sequence_diagram(
@@ -263,7 +265,7 @@ ContainsMatcher IsInnerClass(std::string const &parent,
CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes)
{
return ContainsMatcher(
CasedString(parent + " +-- " + inner, caseSensitivity));
CasedString(inner + " --+ " + parent, caseSensitivity));
}
ContainsMatcher IsAssociation(std::string const &from, std::string const &to,

1
thirdparty/cppast vendored Submodule

Submodule thirdparty/cppast added at dd8d9e668a