Refactored sequence diagram

This commit is contained in:
Bartek Kryza
2021-10-03 12:19:51 +02:00
parent 3e46f2504f
commit f50e59fd51
16 changed files with 334 additions and 108 deletions

View File

@@ -24,7 +24,7 @@
#include "puml/sequence_diagram_generator.h"
#include "uml/class_diagram/model/diagram.h"
#include "uml/class_diagram/visitor/translation_unit_visitor.h"
#include "uml/sequence_diagram_visitor.h"
#include "uml/sequence_diagram/visitor/translation_unit_context.h"
#include "util/util.h"
#include <cli11/CLI11.hpp>

View File

@@ -18,15 +18,18 @@
#include "puml/sequence_diagram_generator.h"
#include "uml/sequence_diagram/visitor/translation_unit_context.h"
namespace clanguml::generators::sequence_diagram {
namespace puml {
using diagram_model = clanguml::model::sequence_diagram::diagram;
using diagram_model = clanguml::sequence_diagram::model::diagram;
using diagram_config = clanguml::config::sequence_diagram::diagram;
using clanguml::config::source_location;
using clanguml::model::sequence_diagram::activity;
using clanguml::model::sequence_diagram::message;
using clanguml::model::sequence_diagram::message_t;
using clanguml::visitor::sequence_diagram::tu_context;
using clanguml::sequence_diagram::model::activity;
using clanguml::sequence_diagram::model::message;
using clanguml::sequence_diagram::model::message_t;
using clanguml::sequence_diagram::visitor::translation_unit_context;
using namespace clanguml::util;
//
@@ -118,12 +121,12 @@ std::ostream &operator<<(std::ostream &os, const generator &g)
}
}
clanguml::model::sequence_diagram::diagram generate(
clanguml::sequence_diagram::model::diagram generate(
clanguml::cx::compilation_database &db, const std::string &name,
clanguml::config::sequence_diagram &diagram)
{
spdlog::info("Generating diagram {}.puml", name);
clanguml::model::sequence_diagram::diagram d;
clanguml::sequence_diagram::model::diagram d;
d.name = name;
// Get all translation units matching the glob from diagram
@@ -154,9 +157,10 @@ clanguml::model::sequence_diagram::diagram generate(
spdlog::debug("Cursor name: {}",
clang_getCString(clang_getCursorDisplayName(cursor)));
clanguml::visitor::sequence_diagram::tu_context ctx(d, diagram);
clanguml::sequence_diagram::visitor::translation_unit_context ctx(
d, diagram);
auto res = clang_visitChildren(cursor,
clanguml::visitor::sequence_diagram::translation_unit_visitor,
clanguml::sequence_diagram::visitor::translation_unit_visitor,
&ctx);
spdlog::debug("Processing result: {}", res);

View File

@@ -19,8 +19,8 @@
#include "config/config.h"
#include "cx/compilation_database.h"
#include "uml/sequence_diagram_model.h"
#include "uml/sequence_diagram_visitor.h"
#include "uml/sequence_diagram/model/diagram.h"
#include "uml/sequence_diagram/visitor/translation_unit_visitor.h"
#include "util/util.h"
#include <glob/glob.hpp>
@@ -35,21 +35,21 @@ namespace generators {
namespace sequence_diagram {
namespace puml {
using diagram_model = clanguml::model::sequence_diagram::diagram;
using diagram_model = clanguml::sequence_diagram::model::diagram;
class generator {
public:
generator(clanguml::config::sequence_diagram &config, diagram_model &model);
std::string to_string(clanguml::model::sequence_diagram::message_t r) const;
std::string to_string(clanguml::sequence_diagram::model::message_t r) const;
void generate_call(const clanguml::model::sequence_diagram::message &m,
void generate_call(const clanguml::sequence_diagram::model::message &m,
std::ostream &ostr) const;
void generate_return(const clanguml::model::sequence_diagram::message &m,
void generate_return(const clanguml::sequence_diagram::model::message &m,
std::ostream &ostr) const;
void generate_activity(const clanguml::model::sequence_diagram::activity &a,
void generate_activity(const clanguml::sequence_diagram::model::activity &a,
std::ostream &ostr) const;
void generate(std::ostream &ostr) const;
@@ -58,12 +58,12 @@ public:
private:
clanguml::config::sequence_diagram &m_config;
clanguml::model::sequence_diagram::diagram &m_model;
clanguml::sequence_diagram::model::diagram &m_model;
};
}
clanguml::model::sequence_diagram::diagram generate(
clanguml::sequence_diagram::model::diagram generate(
clanguml::cx::compilation_database &db, const std::string &name,
clanguml::config::sequence_diagram &diagram);

View File

@@ -1,5 +1,5 @@
/**
* src/uml/sequence_diagram_model.cc
* src/uml/sequence_diagram/model/activity.cc
*
* Copyright (c) 2021 Bartek Kryza <bkryza@gmail.com>
*
@@ -16,8 +16,8 @@
* limitations under the License.
*/
#include "sequence_diagram_visitor.h"
#include "activity.h"
namespace clanguml::model::sequence_diagram {
namespace clanguml::sequence_diagram::model {
}

View File

@@ -0,0 +1,33 @@
/**
* src/uml/sequence_diagram/model/activity.h
*
* 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.
*/
#pragma once
#include "message.h"
#include <string>
#include <vector>
namespace clanguml::sequence_diagram::model {
struct activity {
std::string usr;
std::string from;
std::vector<message> messages;
};
}

View File

@@ -0,0 +1,32 @@
/**
* src/uml/sequence_diagram/model/diagram.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.
*/
#include "diagram.h"
#include <clang-c/CXCompilationDatabase.h>
#include <clang-c/Index.h>
#include <spdlog/spdlog.h>
#include <functional>
#include <memory>
#include <string>
#include <vector>
namespace clanguml::sequence_diagram::model {
}

View File

@@ -0,0 +1,34 @@
/**
* src/uml/sequence_diagram/model/diagram.h
*
* 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.
*/
#pragma once
#include "activity.h"
#include <map>
#include <string>
namespace clanguml::sequence_diagram::model {
struct diagram {
bool started{false};
std::string name;
std::map<std::string, activity> sequences;
};
}

View File

@@ -0,0 +1,24 @@
/**
* src/uml/sequence_diagram/model/enums.h
*
* 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.
*/
#pragma once
namespace clanguml::sequence_diagram::model {
enum class message_t { kCall, kReturn };
}

View File

@@ -0,0 +1,23 @@
/**
* src/uml/sequence_diagram/model/message.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.
*/
#include "message.h"
namespace clanguml::sequence_diagram::model {
}

View File

@@ -1,5 +1,5 @@
/**
* src/uml/sequence_diagram_model.h
* src/uml/sequence_diagram/model/message.h
*
* Copyright (c) 2021 Bartek Kryza <bkryza@gmail.com>
*
@@ -17,20 +17,12 @@
*/
#pragma once
#include <clang-c/CXCompilationDatabase.h>
#include <clang-c/Index.h>
#include <spdlog/spdlog.h>
#include "enums.h"
#include <functional>
#include <memory>
#include <string>
#include <vector>
namespace clanguml {
namespace model {
namespace sequence_diagram {
enum class message_t { kCall, kReturn };
namespace clanguml::sequence_diagram::model {
struct message {
message_t type;
@@ -43,18 +35,4 @@ struct message {
unsigned int line;
};
struct activity {
std::string usr;
std::string from;
std::vector<message> messages;
};
struct diagram {
bool started{false};
std::string name;
std::map<std::string, activity> sequences;
};
}
}
}

View File

@@ -0,0 +1,68 @@
/**
* src/uml/sequence_diagram/visitor/translation_unit_context.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.
*/
#include "translation_unit_context.h"
#include <clang-c/CXCompilationDatabase.h>
#include <clang-c/Index.h>
#include <spdlog/spdlog.h>
namespace clanguml::sequence_diagram::visitor {
translation_unit_context::translation_unit_context(
clanguml::sequence_diagram::model::diagram &diagram,
const clanguml::config::sequence_diagram &config)
: diagram_{diagram}
, config_{config}
{
}
void translation_unit_context::push_namespace(const std::string &ns)
{
namespace_.push_back(ns);
}
void translation_unit_context::pop_namespace() { namespace_.pop_back(); }
const std::vector<std::string> &translation_unit_context::get_namespace() const
{
return namespace_;
}
const clanguml::config::sequence_diagram &
translation_unit_context::config() const
{
return config_;
}
clanguml::sequence_diagram::model::diagram &translation_unit_context::diagram()
{
return diagram_;
}
void translation_unit_context::set_current_method(cx::cursor method)
{
current_method_ = method;
}
cx::cursor &translation_unit_context::current_method()
{
return current_method_;
}
}

View File

@@ -0,0 +1,59 @@
/**
* src/uml/sequence_diagram/visitor/translation_unit_context.h
*
* 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.
*/
#pragma once
#include "config/config.h"
#include "cx/cursor.h"
#include "uml/sequence_diagram/model/diagram.h"
#include <functional>
#include <memory>
#include <string>
namespace clanguml::sequence_diagram::visitor {
class translation_unit_context {
public:
translation_unit_context(
clanguml::sequence_diagram::model::diagram &diagram,
const clanguml::config::sequence_diagram &config);
void push_namespace(const std::string &ns);
void pop_namespace();
const std::vector<std::string> &get_namespace() const;
const cppast::cpp_entity_index &entity_index() const;
const clanguml::config::sequence_diagram &config() const;
clanguml::sequence_diagram::model::diagram &diagram();
void set_current_method(cx::cursor method);
cx::cursor &current_method();
private:
std::vector<std::string> namespace_;
cx::cursor current_method_;
clanguml::sequence_diagram::model::diagram &diagram_;
const clanguml::config::sequence_diagram &config_;
};
}

View File

@@ -1,5 +1,5 @@
/**
* src/uml/sequence_diagram_visitor.cc
* src/uml/sequence_diagram/visitor/translation_unit_visitor.cc
*
* Copyright (c) 2021 Bartek Kryza <bkryza@gmail.com>
*
@@ -16,30 +16,21 @@
* limitations under the License.
*/
#include "sequence_diagram_visitor.h"
#include "translation_unit_visitor.h"
namespace clanguml::visitor::sequence_diagram {
#include "translation_unit_context.h"
//
// tu_context
//
tu_context::tu_context(clanguml::model::sequence_diagram::diagram &d_,
const clanguml::config::sequence_diagram &config_)
: d{d_}
, config{config_}
{
}
namespace clanguml::sequence_diagram::visitor {
enum CXChildVisitResult translation_unit_visitor(
CXCursor cx_cursor, CXCursor cx_parent, CXClientData client_data)
{
using clanguml::model::sequence_diagram::activity;
using clanguml::model::sequence_diagram::diagram;
using clanguml::model::sequence_diagram::message;
using clanguml::model::sequence_diagram::message_t;
using clanguml::sequence_diagram::model::activity;
using clanguml::sequence_diagram::model::diagram;
using clanguml::sequence_diagram::model::message;
using clanguml::sequence_diagram::model::message_t;
struct tu_context *ctx = (struct tu_context *)client_data;
auto *ctx = (struct translation_unit_context *)client_data;
enum CXChildVisitResult ret = CXChildVisit_Break;
@@ -54,7 +45,7 @@ enum CXChildVisitResult translation_unit_visitor(
case CXCursor_FunctionTemplate:
case CXCursor_CXXMethod:
case CXCursor_FunctionDecl:
ctx->current_method = cursor;
ctx->set_current_method(cursor);
ret = CXChildVisit_Recurse;
break;
case CXCursor_CallExpr: {
@@ -74,25 +65,28 @@ enum CXChildVisitResult translation_unit_visitor(
clang_getFileLocation(cursor.location(), &f, &line, &column, &offset);
std::string file{clang_getCString(clang_getFileName(f))};
auto &d = ctx->d;
auto &config = ctx->config;
auto &d = ctx->diagram();
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()
if (ctx->current_method()
.semantic_parent()
.is_translation_unit() ||
ctx->current_method.semantic_parent().is_namespace()) {
caller = ctx->current_method.semantic_parent()
ctx->current_method().semantic_parent().is_namespace()) {
caller = ctx->current_method()
.semantic_parent()
.fully_qualified() +
"::" + ctx->current_method.spelling() + "()";
"::" + ctx->current_method().spelling() + "()";
}
else {
caller =
ctx->current_method.semantic_parent().fully_qualified();
caller = ctx->current_method()
.semantic_parent()
.fully_qualified();
}
auto caller_usr = ctx->current_method.usr();
auto caller_usr = ctx->current_method().usr();
// Get called object
auto callee = referenced.semantic_parent().fully_qualified();
auto callee_usr = referenced.semantic_parent().usr();
@@ -105,8 +99,8 @@ enum CXChildVisitResult translation_unit_visitor(
"\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,
file, line, d.name, ctx->current_method().spelling(),
caller, callee, called_message, caller_usr, callee_usr,
referenced.type().result_type().spelling());
message m;
@@ -146,5 +140,4 @@ enum CXChildVisitResult translation_unit_visitor(
return ret;
}
}

View File

@@ -1,5 +1,5 @@
/**
* src/uml/sequence_diagram_visitor.h
* src/uml/sequence_diagram/visitor/translation_unit_visitor.h
*
* Copyright (c) 2021 Bartek Kryza <bkryza@gmail.com>
*
@@ -19,33 +19,11 @@
#include "config/config.h"
#include "cx/cursor.h"
#include "sequence_diagram_model.h"
#include "uml/sequence_diagram/model/diagram.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 {
struct tu_context {
tu_context(clanguml::model::sequence_diagram::diagram &d_,
const clanguml::config::sequence_diagram &config_);
std::vector<std::string> namespace_;
cx::cursor current_method;
clanguml::model::sequence_diagram::diagram &d;
const clanguml::config::sequence_diagram &config;
};
namespace clanguml::sequence_diagram::visitor {
enum CXChildVisitResult translation_unit_visitor(
CXCursor cx_cursor, CXCursor cx_parent, CXClientData client_data);
}
}
}

View File

@@ -40,7 +40,7 @@ std::pair<clanguml::config::config, compilation_database> load_config2(
return std::make_pair(std::move(config), std::move(db));
}
clanguml::model::sequence_diagram::diagram generate_sequence_diagram(
clanguml::sequence_diagram::model::diagram generate_sequence_diagram(
compilation_database &db,
std::shared_ptr<clanguml::config::diagram> diagram)
{
@@ -64,7 +64,7 @@ clanguml::class_diagram::model::diagram generate_class_diagram(
std::string generate_sequence_puml(
std::shared_ptr<clanguml::config::diagram> config,
clanguml::model::sequence_diagram::diagram &model)
clanguml::sequence_diagram::model::diagram &model)
{
using namespace clanguml::generators::sequence_diagram::puml;

View File

@@ -25,7 +25,7 @@
#include "puml/sequence_diagram_generator.h"
#include "uml/class_diagram/model/diagram.h"
#include "uml/class_diagram/visitor/translation_unit_visitor.h"
#include "uml/sequence_diagram_visitor.h"
#include "uml/sequence_diagram/visitor/translation_unit_visitor.h"
#include "util/util.h"
#define CATCH_CONFIG_RUNNER
@@ -48,7 +48,7 @@ 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(
clanguml::sequence_diagram::model::diagram generate_sequence_diagram(
compilation_database &db,
std::shared_ptr<clanguml::config::diagram> diagram);
@@ -58,7 +58,7 @@ clanguml::class_diagram::model::diagram generate_class_diagram(
std::string generate_sequence_puml(
std::shared_ptr<clanguml::config::diagram> config,
clanguml::model::sequence_diagram::diagram &model);
clanguml::sequence_diagram::model::diagram &model);
std::string generate_class_puml(
std::shared_ptr<clanguml::config::diagram> config,