Initial refactor of id_t to a separate class

This commit is contained in:
Bartek Kryza
2024-06-04 00:03:26 +02:00
parent e21c2d2b14
commit cf79b3184c
37 changed files with 374 additions and 248 deletions

View File

@@ -24,7 +24,7 @@ call_expression_context::call_expression_context() = default;
void call_expression_context::reset()
{
current_caller_id_ = 0;
current_caller_id_ = common::id_t{};
current_class_decl_ = nullptr;
current_class_template_decl_ = nullptr;
current_class_template_specialization_decl_ = nullptr;
@@ -130,33 +130,33 @@ void call_expression_context::update(
current_function_template_decl_ = function_template;
}
std::int64_t call_expression_context::caller_id() const
common::id_t call_expression_context::caller_id() const
{
if (lambda_caller_id() != 0)
return lambda_caller_id();
if (lambda_caller_id().has_value())
return *lambda_caller_id();
return current_caller_id_;
}
std::int64_t call_expression_context::lambda_caller_id() const
std::optional<common::id_t> call_expression_context::lambda_caller_id() const
{
if (current_lambda_caller_id_.empty())
return 0;
return {};
return current_lambda_caller_id_.top();
}
void call_expression_context::set_caller_id(std::int64_t id)
void call_expression_context::set_caller_id(common::id_t id)
{
LOG_DBG("Setting current caller id to {}", id);
current_caller_id_ = id;
}
void call_expression_context::enter_lambda_expression(std::int64_t id)
void call_expression_context::enter_lambda_expression(common::id_t id)
{
LOG_DBG("Setting current lambda caller id to {}", id);
assert(id != 0);
assert(id.value() != 0);
current_lambda_caller_id_.push(id);
}

View File

@@ -116,14 +116,14 @@ struct call_expression_context {
*
* @param id Set current caller id.
*/
void set_caller_id(std::int64_t id);
void set_caller_id(common::id_t id);
/**
* @brief Get current caller id
*
* @return Id of the current caller participant
*/
std::int64_t caller_id() const;
common::id_t caller_id() const;
/**
* @brief Get the id of the current lambda caller.
@@ -133,14 +133,14 @@ struct call_expression_context {
*
* @return Current lambda caller id, or 0 if current caller is not lambda.
*/
std::int64_t lambda_caller_id() const;
std::optional<common::id_t> lambda_caller_id() const;
/**
* @brief Enter a lambda expression
*
* @param id Lambda id
*/
void enter_lambda_expression(std::int64_t id);
void enter_lambda_expression(common::id_t id);
/**
* @brief Leave current lambda expression
@@ -315,8 +315,8 @@ struct call_expression_context {
clang::FunctionTemplateDecl *current_function_template_decl_{nullptr};
private:
std::int64_t current_caller_id_{0};
std::stack<std::int64_t> current_lambda_caller_id_;
common::id_t current_caller_id_{};
std::stack<common::id_t> current_lambda_caller_id_;
std::stack<callexpr_stack_t> call_expr_stack_;

View File

@@ -64,7 +64,8 @@ bool translation_unit_visitor::VisitCXXRecordDecl(
// Skip this class if it's parent template is already in the model
if (declaration->isTemplated() &&
declaration->getDescribedTemplate() != nullptr) {
if (get_unique_id(declaration->getDescribedTemplate()->getID()))
if (get_unique_id(
common::id_t{declaration->getDescribedTemplate()->getID()}))
return true;
}
@@ -335,7 +336,8 @@ bool translation_unit_visitor::VisitFunctionDecl(
if (declaration->getDescribedTemplate() != nullptr) {
// If the described templated of this function is already in the
// model skip it:
if (get_unique_id(declaration->getDescribedTemplate()->getID()))
if (get_unique_id(
common::id_t{declaration->getDescribedTemplate()->getID()}))
return true;
}
}
@@ -474,7 +476,7 @@ bool translation_unit_visitor::VisitLambdaExpr(clang::LambdaExpr *expr)
// method function would be excluded by filters
if (std::holds_alternative<clang::CallExpr *>(
context().current_callexpr()) &&
(context().lambda_caller_id() == 0)) {
(!context().lambda_caller_id().has_value())) {
using clanguml::common::model::message_t;
using clanguml::sequence_diagram::model::message;
@@ -661,7 +663,7 @@ bool translation_unit_visitor::TraverseCompoundStmt(clang::CompoundStmt *stmt)
if (current_elseifstmt->getElse() == stmt) {
const auto current_caller_id = context().caller_id();
if (current_caller_id != 0) {
if (current_caller_id.value() != 0) {
model::message m{message_t::kElse, current_caller_id};
set_source_location(*stmt, m);
diagram().add_message(std::move(m));
@@ -672,7 +674,7 @@ bool translation_unit_visitor::TraverseCompoundStmt(clang::CompoundStmt *stmt)
if (current_ifstmt->getElse() == stmt) {
const auto current_caller_id = context().caller_id();
if (current_caller_id != 0) {
if (current_caller_id.value() != 0) {
model::message m{message_t::kElse, current_caller_id};
set_source_location(*stmt, m);
diagram().add_message(std::move(m));
@@ -1185,7 +1187,7 @@ bool translation_unit_visitor::VisitCallExpr(clang::CallExpr *expr)
}
// Add message to diagram
if (m.from() > 0 && m.to() > 0) {
if (m.from().value() > 0 && m.to().value() > 0) {
m.set_comment(stripped_comment);
if (diagram().sequences().find(m.from()) ==
@@ -1269,7 +1271,7 @@ bool translation_unit_visitor::VisitCXXConstructExpr(
if (!process_construct_expression(m, expr))
return true;
if (m.from() > 0 && m.to() > 0) {
if (m.from().value() > 0 && m.to().value() > 0) {
if (diagram().sequences().find(m.from()) ==
diagram().sequences().end()) {
activity a{m.from()};
@@ -1311,10 +1313,10 @@ bool translation_unit_visitor::process_cuda_kernel_call_expression(
auto callee_name = callee_function->getQualifiedNameAsString() + "()";
const auto maybe_id = get_unique_id(callee_function->getID());
const auto maybe_id = get_unique_id(common::id_t{callee_function->getID()});
if (!maybe_id.has_value()) {
// This is hopefully not an interesting call...
m.set_to(callee_function->getID());
m.set_to(common::id_t{callee_function->getID()});
}
else {
m.set_to(maybe_id.value());
@@ -1349,16 +1351,17 @@ bool translation_unit_visitor::process_operator_call_expression(
auto lambda_name = make_lambda_name(lambda_method->getParent());
m.set_to(lambda_method->getParent()->getID());
m.set_to(common::id_t{lambda_method->getParent()->getID()});
}
else {
auto maybe_id =
get_unique_id(operator_call_expr->getCalleeDecl()->getID());
auto maybe_id = get_unique_id(
common::id_t{operator_call_expr->getCalleeDecl()->getID()});
if (maybe_id.has_value()) {
m.set_to(maybe_id.value());
}
else {
m.set_to(operator_call_expr->getCalleeDecl()->getID());
m.set_to(
common::id_t{operator_call_expr->getCalleeDecl()->getID()});
}
}
@@ -1384,19 +1387,19 @@ bool translation_unit_visitor::process_construct_expression(
constructor->getID(),
construct_expr->getBeginLoc().printToString(source_manager()));
auto maybe_id = get_unique_id(constructor->getID());
auto maybe_id = get_unique_id(common::id_t{constructor->getID()});
if (maybe_id.has_value()) {
m.set_to(maybe_id.value());
}
else {
m.set_to(constructor->getID());
m.set_to(common::id_t{constructor->getID()});
}
m.set_message_name(
fmt::format("{}::{}", constructor_parent->getQualifiedNameAsString(),
constructor_parent->getNameAsString()));
diagram().add_active_participant(constructor->getID());
diagram().add_active_participant(common::id_t{constructor->getID()});
return true;
}
@@ -1421,7 +1424,7 @@ bool translation_unit_visitor::process_class_method_call_expression(
if (!should_include(callee_decl) || !should_include(method_decl))
return false;
m.set_to(method_decl->getID());
m.set_to(common::id_t{method_decl->getID()});
m.set_message_name(method_decl->getNameAsString());
m.set_return_type(
method_call_expr->getCallReturnType(*context().get_ast_context())
@@ -1430,7 +1433,7 @@ bool translation_unit_visitor::process_class_method_call_expression(
LOG_TRACE("Set callee method id {} for method name {}", m.to(),
method_decl->getQualifiedNameAsString());
diagram().add_active_participant(method_decl->getID());
diagram().add_active_participant(common::id_t{method_decl->getID()});
return true;
}
@@ -1507,7 +1510,7 @@ bool translation_unit_visitor::process_class_template_method_call_expression(
dependent_member_callee->getMember().getAsString());
if (const auto maybe_id =
get_unique_id(template_declaration->getID());
get_unique_id(common::id_t{template_declaration->getID()});
maybe_id.has_value())
diagram().add_active_participant(maybe_id.value());
}
@@ -1544,10 +1547,10 @@ bool translation_unit_visitor::process_function_call_expression(
auto callee_name = callee_function->getQualifiedNameAsString() + "()";
const auto maybe_id = get_unique_id(callee_function->getID());
const auto maybe_id = get_unique_id(common::id_t{callee_function->getID()});
if (!maybe_id.has_value()) {
// This is hopefully not an interesting call...
m.set_to(callee_function->getID());
m.set_to(common::id_t{callee_function->getID()});
}
else {
m.set_to(maybe_id.value());
@@ -1567,7 +1570,8 @@ bool translation_unit_visitor::process_lambda_call_expression(
if (lambda_expr == nullptr)
return true;
const auto lambda_class_id = lambda_expr->getLambdaClass()->getID();
const auto lambda_class_id =
common::id_t{lambda_expr->getLambdaClass()->getID()};
const auto maybe_id = get_unique_id(lambda_class_id);
if (!maybe_id.has_value())
m.set_to(lambda_class_id);
@@ -1592,9 +1596,9 @@ bool translation_unit_visitor::process_unresolved_lookup_call_expression(
const auto *ftd =
clang::dyn_cast_or_null<clang::FunctionTemplateDecl>(decl);
const auto maybe_id = get_unique_id(ftd->getID());
const auto maybe_id = get_unique_id(common::id_t{ftd->getID()});
if (!maybe_id.has_value())
m.set_to(ftd->getID());
m.set_to(common::id_t{ftd->getID()});
else {
m.set_to(maybe_id.value());
}
@@ -1606,9 +1610,9 @@ bool translation_unit_visitor::process_unresolved_lookup_call_expression(
const auto *fd =
clang::dyn_cast_or_null<clang::FunctionDecl>(decl);
const auto maybe_id = get_unique_id(fd->getID());
const auto maybe_id = get_unique_id(common::id_t{fd->getID()});
if (!maybe_id.has_value())
m.set_to(fd->getID());
m.set_to(common::id_t{fd->getID()});
else {
m.set_to(maybe_id.value());
}
@@ -1684,18 +1688,18 @@ translation_unit_visitor::create_class_model(clang::CXXRecordDecl *cls)
assert(parent_record_decl != nullptr);
int64_t local_id = parent_record_decl->getID();
const common::id_t ast_id{parent_record_decl->getID()};
// First check if the parent has been added to the diagram as
// regular class
id_opt = get_unique_id(local_id);
id_opt = get_unique_id(ast_id);
// If not, check if the parent template declaration is in the model
if (!id_opt &&
(parent_record_decl->getDescribedTemplate() != nullptr)) {
parent_record_decl->getDescribedTemplate()->getID();
if (parent_record_decl->getDescribedTemplate() != nullptr)
id_opt = get_unique_id(local_id);
id_opt = get_unique_id(ast_id);
}
if (!id_opt)
@@ -1778,12 +1782,13 @@ void translation_unit_visitor::set_unique_id(
}
std::optional<common::id_t> translation_unit_visitor::get_unique_id(
int64_t local_id) const
common::id_t local_id) const
{
if (local_ast_id_map_.find(local_id) == local_ast_id_map_.end())
if (local_ast_id_map_.find(local_id.ast_local_value()) ==
local_ast_id_map_.end())
return {};
return local_ast_id_map_.at(local_id);
return local_ast_id_map_.at(local_id.ast_local_value());
}
std::unique_ptr<model::function_template>
@@ -1927,13 +1932,15 @@ std::string translation_unit_visitor::make_lambda_name(
if (context().lambda_caller_id() != 0) {
// Parent is also a lambda (this id points to a lambda operator())
std::string parent_lambda_class_name{"()"};
if (diagram().get_participant<model::method>(
context().lambda_caller_id())) {
auto parent_lambda_class_id = diagram()
.get_participant<model::method>(
context().lambda_caller_id())
.value()
.class_id();
if (context().lambda_caller_id() &&
diagram().get_participant<model::method>(
context().lambda_caller_id().value())) {
auto parent_lambda_class_id =
diagram()
.get_participant<model::method>(
context().lambda_caller_id().value())
.value()
.class_id();
if (diagram().get_participant<model::class_>(
parent_lambda_class_id)) {
@@ -2054,8 +2061,11 @@ void translation_unit_visitor::resolve_ids_to_global()
// Change all active participants AST local ids to diagram global ids
for (auto id : diagram().active_participants()) {
if (local_ast_id_map_.find(id) != local_ast_id_map_.end()) {
active_participants_unique.emplace(local_ast_id_map_.at(id));
if (!id.is_global() &&
local_ast_id_map_.find(id.ast_local_value()) !=
local_ast_id_map_.end()) {
active_participants_unique.emplace(
local_ast_id_map_.at(id.ast_local_value()));
}
else {
active_participants_unique.emplace(id);
@@ -2067,8 +2077,10 @@ void translation_unit_visitor::resolve_ids_to_global()
// Change all message callees AST local ids to diagram global ids
for (auto &[id, activity] : diagram().sequences()) {
for (auto &m : activity.messages()) {
if (local_ast_id_map_.find(m.to()) != local_ast_id_map_.end()) {
m.set_to(local_ast_id_map_.at(m.to()));
if (!id.is_global() &&
local_ast_id_map_.find(m.to().ast_local_value()) !=
local_ast_id_map_.end()) {
m.set_to(local_ast_id_map_.at(m.to().ast_local_value()));
}
}
}
@@ -2280,7 +2292,7 @@ bool translation_unit_visitor::should_include(
std::optional<std::string> translation_unit_visitor::get_expression_comment(
const clang::SourceManager &sm, const clang::ASTContext &context,
const int64_t caller_id, const clang::Stmt *stmt)
const common::id_t caller_id, const clang::Stmt *stmt)
{
const auto *raw_comment =
clanguml::common::get_expression_raw_comment(sm, context, stmt);
@@ -2288,7 +2300,8 @@ std::optional<std::string> translation_unit_visitor::get_expression_comment(
if (raw_comment == nullptr)
return {};
if (!processed_comments_by_caller_id_.emplace(caller_id, raw_comment)
if (!processed_comments_by_caller_id_
.emplace(caller_id.ast_local_value(), raw_comment)
.second) {
return {};
}

View File

@@ -166,7 +166,7 @@ public:
{
assert(decl != nullptr);
auto unique_participant_id = get_unique_id(decl->getID());
auto unique_participant_id = get_unique_id(common::id_t{decl->getID()});
if (!unique_participant_id.has_value())
return {};
@@ -185,7 +185,7 @@ public:
{
assert(decl != nullptr);
auto unique_participant_id = get_unique_id(decl->getID());
auto unique_participant_id = get_unique_id(common::id_t{decl->getID()});
if (!unique_participant_id.has_value())
return {};
@@ -243,7 +243,7 @@ public:
* @param local_id AST local element id
* @return Global diagram element id
*/
std::optional<common::id_t> get_unique_id(int64_t local_id) const;
std::optional<common::id_t> get_unique_id(common::id_t local_id) const;
/**
* @brief Finalize diagram model for this translation unit
@@ -469,7 +469,7 @@ private:
std::optional<std::string> get_expression_comment(
const clang::SourceManager &sm, const clang::ASTContext &context,
int64_t caller_id, const clang::Stmt *stmt);
common::id_t caller_id, const clang::Stmt *stmt);
/**
* @brief Initializes model message from comment call directive