From fcafef4b850fe726d73ef77a18bf4be6c715b925 Mon Sep 17 00:00:00 2001 From: Bartek Kryza Date: Thu, 18 Mar 2021 19:58:27 +0100 Subject: [PATCH] Added basic method parameter handling --- src/puml/class_diagram_generator.h | 12 ++++++- src/uml/class_diagram_model.h | 13 +++++-- src/uml/class_diagram_visitor.h | 56 ++++++++++++++++++++++++++++++ tests/t00003/t00003.cc | 3 ++ 4 files changed, 81 insertions(+), 3 deletions(-) diff --git a/src/puml/class_diagram_generator.h b/src/puml/class_diagram_generator.h index 7a2a1bf7..a95ff172 100644 --- a/src/puml/class_diagram_generator.h +++ b/src/puml/class_diagram_generator.h @@ -130,7 +130,17 @@ public: if (m.type != "void") type = m.type + " "; - ostr << to_string(m.scope) << type << m.name + "()"; + ostr << to_string(m.scope) << type << m.name; + + ostr << "("; + if (true) { // TODO: add option to disable parameter generation + std::vector params; + std::transform(m.parameters.begin(), m.parameters.end(), + std::back_inserter(params), + [](const auto &mp) { return mp.to_string(); }); + ostr << fmt::format("{}", fmt::join(params, ", ")); + } + ostr << ")"; if (m.is_const) ostr << " const"; diff --git a/src/uml/class_diagram_model.h b/src/uml/class_diagram_model.h index fbac4341..9968d55e 100644 --- a/src/uml/class_diagram_model.h +++ b/src/uml/class_diagram_model.h @@ -77,13 +77,22 @@ struct class_member : public class_element { bool is_static{false}; }; -struct method_argument { +struct method_parameter { std::string type; std::string name; + std::string default_value; + + std::string to_string() const + { + if (default_value.empty()) + return fmt::format("{} {}", type, name); + + return fmt::format("{} {} = {}", type, name, default_value); + } }; struct class_method : public class_element { - std::vector arguments; + std::vector parameters; bool is_pure_virtual{false}; bool is_virtual{false}; bool is_const{false}; diff --git a/src/uml/class_diagram_visitor.h b/src/uml/class_diagram_visitor.h index 7b1e2be3..f17cd3ad 100644 --- a/src/uml/class_diagram_visitor.h +++ b/src/uml/class_diagram_visitor.h @@ -40,6 +40,7 @@ using clanguml::model::class_diagram::class_relationship; using clanguml::model::class_diagram::class_template; using clanguml::model::class_diagram::diagram; using clanguml::model::class_diagram::enum_; +using clanguml::model::class_diagram::method_parameter; using clanguml::model::class_diagram::relationship_t; using clanguml::model::class_diagram::scope_t; @@ -201,6 +202,54 @@ static enum CXChildVisitResult enum_visitor( return ret; } +static enum CXChildVisitResult method_parameter_visitor( + CXCursor cx_cursor, CXCursor cx_parent, CXClientData client_data) +{ + auto ctx = (element_visitor_context *)client_data; + + cx::cursor cursor{std::move(cx_cursor)}; + cx::cursor parent{std::move(cx_parent)}; + + spdlog::debug("Visiting method declaration {}: {} - {}:{}", + ctx->element.name, cursor.spelling(), cursor.kind(), + cursor.referenced()); + + enum CXChildVisitResult ret = CXChildVisit_Break; + switch (cursor.kind()) { + case CXCursor_ParmDecl: { + spdlog::debug( + "Analyzing method parameter: {}, {}", cursor, cursor.type()); + + method_parameter mp; + mp.name = cursor.spelling(); + mp.type = cursor.type().spelling(); + + ctx->element.parameters.emplace_back(std::move(mp)); + + /* TODO: handle dependency relationships based + * on method arguments + * + if (ctx->ctx->config.should_include( + cursor.type().referenced().fully_qualified())) { + + class_relationship r; + r.type = relationship_t::kDependency; + r.destination = cursor.type().referenced().usr(); + + ctx->element.relationships.emplace_back(std::move(r)); + } + */ + + ret = CXChildVisit_Continue; + } break; + default: + ret = CXChildVisit_Continue; + break; + } + + return ret; +} + static enum CXChildVisitResult friend_class_visitor( CXCursor cx_cursor, CXCursor cx_parent, CXClientData client_data) { @@ -395,6 +444,13 @@ static enum CXChildVisitResult class_visitor( m.scope = cx_access_specifier_to_scope(cursor.cxxaccess_specifier()); + auto method_ctx = + element_visitor_context(ctx->d, m); + method_ctx.ctx = ctx->ctx; + + clang_visitChildren( + cursor.get(), method_parameter_visitor, &method_ctx); + spdlog::debug("Adding method {} {}::{}()", m.type, ctx->element.name, cursor.spelling()); diff --git a/tests/t00003/t00003.cc b/tests/t00003/t00003.cc index 96c603af..120976eb 100644 --- a/tests/t00003/t00003.cc +++ b/tests/t00003/t00003.cc @@ -15,6 +15,9 @@ public: void const_method() const {} auto auto_method() { return 1; } + auto double_int(const int i) { return 2 * i; } + auto sum(const double a, const double b) { return a + b; } + int public_member; static int static_int; static const int static_const_int = 1;