Refactored call expression context in sequence diagram visitor

This commit is contained in:
Bartek Kryza
2022-12-04 12:42:36 +01:00
parent 8684bc861b
commit f07b35802a
4 changed files with 275 additions and 202 deletions

View File

@@ -20,6 +20,7 @@
#include "common/visitor/translation_unit_visitor.h"
#include "config/config.h"
#include "sequence_diagram/model/diagram.h"
#include "call_expression_context.h"
#include <clang/AST/Expr.h>
#include <clang/AST/RecursiveASTVisitor.h>
@@ -31,173 +32,6 @@ namespace clanguml::sequence_diagram::visitor {
std::string to_string(const clang::FunctionTemplateDecl *decl);
struct call_expression_context {
call_expression_context()
: current_class_decl_{nullptr}
, current_class_template_decl_{nullptr}
, current_class_template_specialization_decl_{nullptr}
, current_method_decl_{nullptr}
, current_function_decl_{nullptr}
, current_function_template_decl_{nullptr}
, current_caller_id_{0}
{
}
void reset()
{
current_caller_id_ = 0;
current_class_decl_ = nullptr;
current_class_template_decl_ = nullptr;
current_class_template_specialization_decl_ = nullptr;
current_method_decl_ = nullptr;
current_function_decl_ = nullptr;
current_function_template_decl_ = nullptr;
}
void dump()
{
LOG_DBG("current_caller_id_ = {}", current_caller_id_);
LOG_DBG("current_class_decl_ = {}", (void *)current_class_decl_);
LOG_DBG("current_class_template_decl_ = {}",
(void *)current_class_template_decl_);
LOG_DBG("current_class_template_specialization_decl_ = {}",
(void *)current_class_template_specialization_decl_);
LOG_DBG("current_method_decl_ = {}", (void *)current_method_decl_);
LOG_DBG("current_function_decl_ = {}", (void *)current_function_decl_);
LOG_DBG("current_function_template_decl_ = {}",
(void *)current_function_template_decl_);
}
bool valid() const
{
return (current_class_decl_ != nullptr) ||
(current_class_template_decl_ != nullptr) ||
(current_class_template_specialization_decl_ != nullptr) ||
(current_method_decl_ != nullptr) ||
(current_function_decl_ != nullptr) ||
(current_function_template_decl_ != nullptr);
}
clang::ASTContext *get_ast_context()
{
if (current_class_template_specialization_decl_)
return &current_class_template_specialization_decl_
->getASTContext();
if (current_class_template_decl_)
return &current_class_template_decl_->getASTContext();
if (current_class_decl_)
return &current_class_decl_->getASTContext();
if (current_function_template_decl_)
return &current_function_template_decl_->getASTContext();
return &current_function_decl_->getASTContext();
}
void update(clang::CXXRecordDecl *cls) { current_class_decl_ = cls; }
void update(clang::ClassTemplateSpecializationDecl *clst)
{
current_class_template_specialization_decl_ = clst;
}
void update(clang::ClassTemplateDecl *clst)
{
current_class_template_decl_ = clst;
}
void update(clang::CXXMethodDecl *method) { current_method_decl_ = method; }
void update(clang::FunctionDecl *function)
{
if (!function->isCXXClassMember())
reset();
current_function_decl_ = function;
// Check if this function is a part of template function declaration,
// If no - reset the current_function_template_decl_
if (current_function_template_decl_ &&
current_function_template_decl_->getQualifiedNameAsString() !=
function->getQualifiedNameAsString()) {
current_function_template_decl_ = nullptr;
}
}
void update(clang::FunctionTemplateDecl *function_template)
{
current_function_template_decl_ = function_template;
if (!function_template->isCXXClassMember())
current_class_decl_ = nullptr;
current_function_template_decl_ = function_template;
}
bool in_class_method() const { return current_class_decl_ != nullptr; }
bool in_function() const
{
return current_class_decl_ == nullptr &&
current_function_decl_ != nullptr;
}
bool in_function_template() const
{
return current_function_decl_ != nullptr &&
current_function_template_decl_ != nullptr;
}
std::int64_t caller_id() const { return current_caller_id_; }
std::int64_t lambda_caller_id() const
{
if(current_lambda_caller_id_.empty())
return 0;
return current_lambda_caller_id_.top();
}
void set_caller_id(std::int64_t id)
{
LOG_DBG("Setting current caller id to {}", id);
current_caller_id_ = id;
}
void enter_lambda_expression(std::int64_t id)
{
LOG_DBG("Setting current lambda caller id to {}", id);
assert(id != 0);
current_lambda_caller_id_.push(id);
}
void leave_lambda_expression()
{
assert(!current_lambda_caller_id_.empty());
LOG_DBG("Leaving current lambda expression id to {}",
current_lambda_caller_id_.top());
current_lambda_caller_id_.pop();
}
clang::CXXRecordDecl *current_class_decl_;
clang::ClassTemplateDecl *current_class_template_decl_;
clang::ClassTemplateSpecializationDecl
*current_class_template_specialization_decl_;
clang::CXXMethodDecl *current_method_decl_;
clang::FunctionDecl *current_function_decl_;
clang::FunctionTemplateDecl *current_function_template_decl_;
private:
std::int64_t current_caller_id_;
std::stack<std::int64_t> current_lambda_caller_id_;
};
class translation_unit_visitor
: public clang::RecursiveASTVisitor<translation_unit_visitor>,
public common::visitor::translation_unit_visitor {