Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
bkryza committed Jan 21, 2025
1 parent e8a744e commit da01e4d
Show file tree
Hide file tree
Showing 17 changed files with 342 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,10 @@ void generator::generate_activity(
case message_t::kConditionalEnd:
process_end_conditional_message();
break;
case message_t::kNone:
case message_t::kReturn:; // noop
case message_t::kReturn:
process_return_message(m);
case message_t::kNone:;

}
}
}
Expand Down Expand Up @@ -316,6 +318,10 @@ void generator::process_call_message(
visited.pop_back();
}

void generator::process_return_message(const message &m) const {

}

void generator::process_while_message(const message &m) const
{
nlohmann::json while_block;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ class generator : public common_generator<diagram_config, diagram_model> {
void process_call_message(
const model::message &m, std::vector<eid_t> &visited) const;

/**
* @brief Process return message
*
* @param m Message model
*/
void process_return_message(const model::message &m) const;

/**
* @brief Process `if` statement message
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,23 +171,46 @@ void generator::generate_call(const message &m, std::ostream &ostr) const

void generator::generate_return(const message &m, std::ostream &ostr) const
{
// Add return activity only for messages between different actors and
// only if the return type is different than void
const auto &from = model().get_participant<model::participant>(m.from());
const auto &to = model().get_participant<model::function>(m.to());
if ((m.from() != m.to()) && !to.value().is_void()) {
assert(m.type() == message_t::kReturn);

// Add return activity only for messages between different actors
// and only if the return type is different than void
if (m.from() == m.to())
return;

const auto &from = model().get_participant<model::function>(m.from());
const auto &to = model().get_participant<model::participant>(m.to());
if (to.has_value() && from.has_value() && !from.value().is_void()) {
const std::string from_alias = generate_alias(from.value());

const std::string to_alias = generate_alias(to.value());

ostr << indent(1) << to_alias << " "
ostr << indent(1) << from_alias << " "
<< common::generators::mermaid::to_mermaid(message_t::kReturn)
<< " " << from_alias << " : ";
<< " " << to_alias << " : ";

if (config().generate_return_types()) {
ostr << m.return_type();
ostr << " : " << m.return_type();
}
else if (false) { // if(config().generate_return_values())
ostr << " : " << m.message_name();
}

ostr << '\n';
}
else if (from.has_value() && !from.value().is_void() &&
(from.value().type_name() == "method" ||
from.value().type_name() == "objc_method" ||
config().combine_free_functions_into_file_participants())) {
const std::string from_alias = generate_alias(from.value());

ostr << indent(1) << from_alias << " "
<< common::generators::mermaid::to_mermaid(message_t::kReturn)
<< " * : ";

if (config().generate_return_types())
ostr << from.value().return_type();

ostr << '\n';
}
}
Expand Down Expand Up @@ -247,12 +270,19 @@ void generator::generate_activity(
LOG_DBG("Skipping activity {} --> {} - missing sequence {}",
m.from(), m.to(), m.to());

generate_return(m, ostr);

ostr << indent(1) << "deactivate " << to_alias << '\n';

visited.pop_back();
}
else if (m.type() == message_t::kReturn) {
print_debug(m, ostr);
generate_message_comment(ostr, m);
auto return_message = m;
if (!visited.empty()) {
return_message.set_to(visited.back());
}
generate_return(return_message, ostr);
}
else if (m.type() == message_t::kIf) {
print_debug(m, ostr);
generate_message_comment(ostr, m);
Expand Down Expand Up @@ -575,6 +605,7 @@ void generator::generate_from_sequences(std::ostream &ostr) const
// which method relates to the first activity for this 'start_from'
// condition
if (from.value().type_name() == "method" ||
from.value().type_name() == "objc_method" ||
config().combine_free_functions_into_file_participants()) {
ostr << indent(1) << "* "
<< common::generators::mermaid::to_mermaid(message_t::kCall)
Expand All @@ -586,22 +617,6 @@ void generator::generate_from_sequences(std::ostream &ostr) const

generate_activity(from_id, ostr, visited_participants);

if (from.value().type_name() == "method" ||
config().combine_free_functions_into_file_participants()) {

if (!from.value().is_void()) {
ostr << indent(1) << from_alias << " "
<< common::generators::mermaid::to_mermaid(
message_t::kReturn)
<< " *" << " : ";

if (config().generate_return_types())
ostr << from.value().return_type();

ostr << '\n';
}
}

ostr << indent(1) << "deactivate " << from_alias << '\n';
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,26 +128,42 @@ void generator::generate_call(const message &m, std::ostream &ostr) const

void generator::generate_return(const message &m, std::ostream &ostr) const
{
assert(m.type() == message_t::kReturn);

// Add return activity only for messages between different actors
// and only if the return type is different than void
if (m.from() == m.to())
return;

const auto &from = model().get_participant<model::participant>(m.from());
const auto &to = model().get_participant<model::function>(m.to());
if (to.has_value() && !to.value().is_void()) {
const auto &from = model().get_participant<model::function>(m.from());
const auto &to = model().get_participant<model::participant>(m.to());
if (to.has_value() && from.has_value() && !from.value().is_void()) {
const std::string from_alias = generate_alias(from.value());

const std::string to_alias = generate_alias(to.value());

ostr << to_alias << " "
ostr << from_alias << " "
<< common::generators::plantuml::to_plantuml(message_t::kReturn)
<< " " << from_alias;
<< " " << to_alias;

if (config().generate_return_types()) {
ostr << " : //" << m.return_type() << "//";
}
else if (false) { // if(config().generate_return_values())
ostr << " : //" << m.message_name() << "//";
}

ostr << '\n';
}
else if (from.has_value() && !from.value().is_void() &&
(from.value().type_name() == "method" ||
from.value().type_name() == "objc_method" ||
config().combine_free_functions_into_file_participants())) {
const std::string from_alias = generate_alias(from.value());

ostr << "[<--" << " " << from_alias;
if (config().generate_return_types())
ostr << " : //" << from.value().return_type() << "//";

ostr << '\n';
}
Expand Down Expand Up @@ -210,12 +226,19 @@ void generator::generate_activity(
LOG_DBG("Skipping activity {} --> {} - missing sequence {}",
m.from(), m.to(), m.to());

generate_return(m, ostr);

ostr << "deactivate " << to_alias << '\n';

visited.pop_back();
}
else if (m.type() == message_t::kReturn) {
print_debug(m, ostr);
generate_message_comment(ostr, m);
auto return_message = m;
if (!visited.empty()) {
return_message.set_to(visited.back());
}
generate_return(return_message, ostr);
}
else if (m.type() == message_t::kIf) {
print_debug(m, ostr);
generate_message_comment(ostr, m);
Expand Down Expand Up @@ -606,20 +629,6 @@ void generator::generate_from_sequences(std::ostream &ostr) const

generate_activity(from_id, ostr, visited_participants);

if (from.value().type_name() == "method" ||
from.value().type_name() == "objc_method" ||
config().combine_free_functions_into_file_participants()) {

if (!from.value().is_void()) {
ostr << "[<--" << " " << from_alias;

if (config().generate_return_types())
ostr << " : //" << from.value().return_type() << "//";

ostr << '\n';
}
}

ostr << "deactivate " << from_alias << '\n';
}
}
Expand Down
28 changes: 26 additions & 2 deletions src/sequence_diagram/model/diagram.cc
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,26 @@ void diagram::print() const

const auto &from_participant = *participants_.at(message.from());

if (participants_.find(message.to()) == participants_.end()) {
if (message.type() == common::model::message_t::kReturn) {
if (message.to() == 0)
LOG_TRACE(
" Return from={}, from_id={}, name={}, type={}",
from_participant.full_name(false),
from_participant.id(), message.message_name(),
to_string(message.type()));
else {
const auto &to_participant =
*participants_.at(message.to());

LOG_TRACE(" Return from={}, from_id={}, "
"to={}, to_id={}, name={}, type={}",
from_participant.full_name(false),
from_participant.id(), to_participant.full_name(false),
message.to(), message.message_name(),
to_string(message.type()));
}
}
else if (participants_.find(message.to()) == participants_.end()) {
LOG_TRACE(" Message from={}, from_id={}, "
"to={}, to_id={}, name={}, type={}",
from_participant.full_name(false), from_participant.id(),
Expand Down Expand Up @@ -603,6 +622,10 @@ void diagram::fold_or_end_block_statement(message &&m,
if (rit->type() == statement_begin) {
break;
}
if (rit->type() == common::model::message_t::kReturn) {
is_empty_statement = false;
break;
}
if (rit->type() == common::model::message_t::kCall) {
is_empty_statement = false;
break;
Expand Down Expand Up @@ -667,7 +690,8 @@ void diagram::finalize()
// on the previous stack
if (std::count_if(block_message_stack.back().begin(),
block_message_stack.back().end(), [](auto &m) {
return m.type() == message_t::kCall;
return (m.type() == message_t::kCall) ||
(m.type() == message_t::kReturn);
}) > 0) {
std::copy(block_message_stack.back().begin(),
block_message_stack.back().end(),
Expand Down
5 changes: 5 additions & 0 deletions src/sequence_diagram/visitor/call_expression_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,11 @@ void call_expression_context::enter_callexpr(clang::ObjCMessageExpr *expr)
call_expr_stack_.emplace(expr);
}

void call_expression_context::enter_callexpr(clang::ReturnStmt *stmt)
{
call_expr_stack_.emplace(stmt);
}

void call_expression_context::leave_callexpr()
{
if (!call_expr_stack_.empty()) {
Expand Down
3 changes: 2 additions & 1 deletion src/sequence_diagram/visitor/call_expression_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct call_expression_context {
* to add to the call stack either type.
*/
using callexpr_stack_t = std::variant<std::monostate, clang::CallExpr *,
clang::CXXConstructExpr *, clang::ObjCMessageExpr *>;
clang::CXXConstructExpr *, clang::ObjCMessageExpr *, clang::ReturnStmt *>;

call_expression_context();

Expand Down Expand Up @@ -282,6 +282,7 @@ struct call_expression_context {
*/
void enter_callexpr(clang::CXXConstructExpr *expr);
void enter_callexpr(clang::ObjCMessageExpr *expr);
void enter_callexpr(clang::ReturnStmt *stmt);

/**
* @brief Leave call expression
Expand Down
Loading

0 comments on commit da01e4d

Please sign in to comment.