From 7f25fa58f556c0b3a7f4082e63ba09bcf7221fad Mon Sep 17 00:00:00 2001 From: Bartek Kryza Date: Sun, 11 Feb 2024 19:33:03 +0100 Subject: [PATCH] Continued refactor of template building code to template_builder --- .../visitor/translation_unit_visitor.cc | 184 +-- .../visitor/translation_unit_visitor.h | 83 +- src/common/visitor/template_builder.cc | 1120 -------------- src/common/visitor/template_builder.h | 1342 ++++++++++++++++- .../visitor/translation_unit_visitor.cc | 168 --- src/common/visitor/translation_unit_visitor.h | 225 ++- .../visitor/translation_unit_visitor.cc | 16 +- .../visitor/translation_unit_visitor.h | 27 +- .../visitor/translation_unit_visitor.cc | 15 +- .../visitor/translation_unit_visitor.h | 26 +- .../visitor/translation_unit_visitor.cc | 685 +-------- .../visitor/translation_unit_visitor.h | 110 +- tests/t20040/.clang-uml | 11 + tests/t20040/t20040.cc | 32 + tests/t20040/test_case.h | 62 + tests/test_cases.cc | 1 + 16 files changed, 1738 insertions(+), 2369 deletions(-) create mode 100644 tests/t20040/.clang-uml create mode 100644 tests/t20040/t20040.cc create mode 100644 tests/t20040/test_case.h diff --git a/src/class_diagram/visitor/translation_unit_visitor.cc b/src/class_diagram/visitor/translation_unit_visitor.cc index e15d6d26..704abacf 100644 --- a/src/class_diagram/visitor/translation_unit_visitor.cc +++ b/src/class_diagram/visitor/translation_unit_visitor.cc @@ -29,32 +29,19 @@ namespace clanguml::class_diagram::visitor { translation_unit_visitor::translation_unit_visitor(clang::SourceManager &sm, clanguml::class_diagram::model::diagram &diagram, const clanguml::config::class_diagram &config) - : common::visitor::translation_unit_visitor{sm, config} - , diagram_{diagram} - , config_{config} - , template_builder_{diagram_, config_, *this, - [uns = config_.using_namespace()](const clang::NamedDecl *decl) { - auto cls = std::make_unique(uns); - cls->is_struct(common::is_struct(decl)); - return cls; - }, - [this](common::model::template_element &template_instantiation_base, - const std::string &full_name, common::id_t templated_decl_id) { - find_instantiation_relationships( - template_instantiation_base, full_name, templated_decl_id); - }, - [](clanguml::common::model::template_element &tinst, - clanguml::common::id_t id, const std::string &full_name) { - model::class_parent cp; - cp.set_access(common::model::access_t::kPublic); - cp.set_name(full_name); - cp.set_id(id); - - dynamic_cast(tinst).add_parent(std::move(cp)); - }} + : visitor_specialization_t{sm, diagram, config} + , template_builder_{diagram, config, *this} { } +std::unique_ptr translation_unit_visitor::create_element( + const clang::NamedDecl *decl) const +{ + auto cls = std::make_unique(config().using_namespace()); + cls->is_struct(common::is_struct(decl)); + return cls; +} + bool translation_unit_visitor::VisitNamespaceDecl(clang::NamespaceDecl *ns) { assert(ns != nullptr); @@ -126,7 +113,7 @@ bool translation_unit_visitor::VisitEnumDecl(clang::EnumDecl *enm) enm->getQualifiedNameAsString(), enm->getLocation().printToString(source_manager())); - auto e_ptr = std::make_unique(config_.using_namespace()); + auto e_ptr = std::make_unique(config().using_namespace()); auto &e = *e_ptr; std::string qualified_name = common::get_qualified_name(*enm); @@ -274,7 +261,7 @@ bool translation_unit_visitor::VisitTypeAliasTemplateDecl( auto template_specialization_ptr = std::make_unique(config().using_namespace()); - tbuilder().build( + tbuilder().build_from_template_specialization_type( *template_specialization_ptr, cls, *template_type_specialization_ptr); if (diagram().should_include(*template_specialization_ptr)) { @@ -309,11 +296,10 @@ bool translation_unit_visitor::VisitClassTemplateDecl( add_processed_template_class(cls->getQualifiedNameAsString()); + tbuilder().build_from_template_declaration(*c_ptr, *cls, *c_ptr); + // Override the id with the template id, for now we don't care about the // underlying templated class id - - process_template_parameters(*cls, *c_ptr, *c_ptr); - const auto cls_full_name = c_ptr->full_name(false); const auto id = common::to_id(cls_full_name); @@ -420,7 +406,7 @@ bool translation_unit_visitor::TraverseConceptDecl(clang::ConceptDecl *cpt) id_mapper().add(cpt->getID(), concept_id); - process_template_parameters(*cpt, *concept_model); + tbuilder().build_from_template_declaration(*concept_model, *cpt); constexpr auto kMaxConstraintCount = 24U; llvm::SmallVector constraints{}; @@ -774,7 +760,7 @@ translation_unit_visitor::create_concept_declaration(clang::ConceptDecl *cpt) return {}; auto concept_ptr{ - std::make_unique(config_.using_namespace())}; + std::make_unique(config().using_namespace())}; auto &concept_model = *concept_ptr; auto ns = common::get_template_namespace(*cpt); @@ -803,7 +789,7 @@ std::unique_ptr translation_unit_visitor::create_record_declaration( if (!should_include(rec)) return {}; - auto record_ptr{std::make_unique(config_.using_namespace())}; + auto record_ptr{std::make_unique(config().using_namespace())}; auto &record = *record_ptr; process_record_parent(rec, record, namespace_{}); @@ -849,7 +835,7 @@ std::unique_ptr translation_unit_visitor::create_class_declaration( if (!should_include(cls)) return {}; - auto c_ptr{std::make_unique(config_.using_namespace())}; + auto c_ptr{std::make_unique(config().using_namespace())}; auto &c = *c_ptr; auto ns{common::get_tag_namespace(*cls)}; @@ -909,11 +895,11 @@ void translation_unit_visitor::process_record_parent( } } - if (id_opt && diagram_.find(*id_opt)) { + if (id_opt && diagram().find(*id_opt)) { // Here we have 2 options, either: // - the parent is a regular C++ class/struct // - the parent is a class template declaration/specialization - auto parent_class = diagram_.find(*id_opt); + auto parent_class = diagram().find(*id_opt); c.set_namespace(parent_ns); const auto cls_name = cls->getNameAsString(); @@ -963,102 +949,6 @@ void translation_unit_visitor::process_class_declaration( c.complete(true); } -bool translation_unit_visitor::process_template_parameters( - const clang::TemplateDecl &template_declaration, - common::model::template_trait &c, - common::optional_ref templated_element) -{ - LOG_DBG("Processing {} template parameters...", - common::get_qualified_name(template_declaration)); - - if (template_declaration.getTemplateParameters() == nullptr) - return false; - - for (const auto *parameter : - *template_declaration.getTemplateParameters()) { - if (clang::dyn_cast_or_null(parameter) != - nullptr) { - const auto *template_type_parameter = - clang::dyn_cast_or_null(parameter); - - std::optional default_arg; - if (template_type_parameter->hasDefaultArgument()) { - default_arg = - template_type_parameter->getDefaultArgument().getAsString(); - } - - auto parameter_name = template_type_parameter->getNameAsString(); - if (parameter_name.empty()) - parameter_name = "typename"; - - auto ct = template_parameter::make_template_type(parameter_name, - default_arg, template_type_parameter->isParameterPack()); - - if (template_type_parameter->getTypeConstraint() != nullptr) { - util::if_not_null(template_type_parameter->getTypeConstraint() - ->getNamedConcept(), - [this, &ct, &templated_element]( - const clang::ConceptDecl *named_concept) mutable { - ct.set_concept_constraint( - named_concept->getQualifiedNameAsString()); - if (templated_element && - should_include(named_concept)) { - templated_element.value().add_relationship( - {relationship_t::kConstraint, - id_mapper() - .get_global_id(named_concept->getID()) - .value(), - access_t::kNone, ct.name().value()}); - } - }); - } - - c.add_template(std::move(ct)); - } - else if (clang::dyn_cast_or_null( - parameter) != nullptr) { - const auto *template_nontype_parameter = - clang::dyn_cast_or_null( - parameter); - - std::optional default_arg; - - if (template_nontype_parameter->hasDefaultArgument()) - default_arg = common::to_string( - template_nontype_parameter->getDefaultArgument()); - - auto ct = template_parameter::make_non_type_template( - template_nontype_parameter->getType().getAsString(), - template_nontype_parameter->getNameAsString(), default_arg, - template_nontype_parameter->isParameterPack()); - - c.add_template(std::move(ct)); - } - else if (clang::dyn_cast_or_null( - parameter) != nullptr) { - const auto *template_template_parameter = - clang::dyn_cast_or_null( - parameter); - std::optional default_arg; - if (template_template_parameter->hasDefaultArgument()) { - default_arg = common::to_string( - template_template_parameter->getDefaultArgument() - .getArgument()); - } - auto ct = template_parameter::make_template_template_type( - template_template_parameter->getNameAsString(), default_arg, - template_template_parameter->isParameterPack()); - - c.add_template(std::move(ct)); - } - else { - // pass - } - } - - return false; -} - void translation_unit_visitor::process_class_bases( const clang::CXXRecordDecl *cls, class_ &c) { @@ -1074,7 +964,8 @@ void translation_unit_visitor::process_class_bases( tsp != nullptr) { auto template_specialization_ptr = std::make_unique(config().using_namespace()); - tbuilder().build(*template_specialization_ptr, cls, *tsp, {}); + tbuilder().build_from_template_specialization_type( + *template_specialization_ptr, cls, *tsp, {}); cp.set_id(template_specialization_ptr->id()); cp.set_name(template_specialization_ptr->full_name(false)); @@ -1330,7 +1221,8 @@ void translation_unit_visitor::process_method( if (unaliased_type != nullptr) { auto template_specialization_ptr = std::make_unique(config().using_namespace()); - tbuilder().build(*template_specialization_ptr, + tbuilder().build_from_template_specialization_type( + *template_specialization_ptr, unaliased_type->getTemplateName().getAsTemplateDecl(), *unaliased_type, &c); @@ -1476,14 +1368,13 @@ void translation_unit_visitor::process_template_method( // Is there a better way to do this? method_name = method_name.substr(0, method_name.find('<')); } - util::if_not_null( clang::dyn_cast(mf.getTemplatedDecl()), [&](const auto *decl) { process_method_properties(*decl, c, method_name, method); }); - process_template_parameters(mf, method); + tbuilder().build_from_template_declaration(method, mf); process_comment(mf, method); @@ -1705,7 +1596,8 @@ void translation_unit_visitor::process_function_parameter( templ != nullptr) { auto template_specialization_ptr = std::make_unique(config().using_namespace()); - tbuilder().build(*template_specialization_ptr, + tbuilder().build_from_template_specialization_type( + *template_specialization_ptr, templ->getTemplateName().getAsTemplateDecl(), *templ, &c); if (diagram().should_include(*template_specialization_ptr)) { @@ -1933,7 +1825,8 @@ void translation_unit_visitor::process_field( // Build the template instantiation for the field type auto template_specialization_ptr = std::make_unique(config().using_namespace()); - tbuilder().build(*template_specialization_ptr, + tbuilder().build_from_template_specialization_type( + *template_specialization_ptr, field_type->getAs() ->getTemplateName() .getAsTemplateDecl(), @@ -2100,25 +1993,6 @@ void translation_unit_visitor::extract_constrained_template_param_name( } } -bool translation_unit_visitor::should_include(const clang::NamedDecl *decl) -{ - if (decl == nullptr) - return false; - - if (source_manager().isInSystemHeader(decl->getSourceRange().getBegin())) - return false; - - auto should_include_namespace = - diagram().should_include(namespace_{decl->getQualifiedNameAsString()}); - - const auto decl_file = decl->getLocation().printToString(source_manager()); - - const auto should_include_decl_file = - diagram().should_include(common::model::source_file{decl_file}); - - return should_include_namespace && should_include_decl_file; -} - void translation_unit_visitor::add_processed_template_class( std::string qualified_name) { diff --git a/src/class_diagram/visitor/translation_unit_visitor.h b/src/class_diagram/visitor/translation_unit_visitor.h index ac96229b..f7fbd525 100644 --- a/src/class_diagram/visitor/translation_unit_visitor.h +++ b/src/class_diagram/visitor/translation_unit_visitor.h @@ -54,6 +54,10 @@ using clanguml::common::model::template_trait; using clanguml::common::visitor::found_relationships_t; using clanguml::common::visitor::template_builder; +using visitor_specialization_t = + common::visitor::translation_unit_visitor; + /** * @brief Class diagram translation unit visitor * @@ -62,8 +66,13 @@ using clanguml::common::visitor::template_builder; */ class translation_unit_visitor : public clang::RecursiveASTVisitor, - public common::visitor::translation_unit_visitor { + public visitor_specialization_t { public: + using visitor_specialization_t::config_t; + using visitor_specialization_t::diagram_t; + + using template_builder_t = template_builder; + /** * @brief Constructor. * @@ -102,30 +111,6 @@ public: virtual bool TraverseConceptDecl(clang::ConceptDecl *cpt); /** @} */ - /** - * @brief Get diagram model reference - * - * @return Reference to diagram model created by the visitor - */ - clanguml::class_diagram::model::diagram &diagram() { return diagram_; } - - /** - * @brief Get diagram model reference - * - * @return Reference to diagram model created by the visitor - */ - const clanguml::class_diagram::model::diagram &diagram() const - { - return diagram_; - } - - /** - * @brief Get diagram config instance - * - * @return Reference to config instance - */ - const clanguml::config::class_diagram &config() const { return config_; } - /** * @brief Finalize diagram model * @@ -157,15 +142,16 @@ public: */ void add_concept(std::unique_ptr &&c); -private: - /** - * @brief Check if the diagram should include a declaration. - * - * @param decl Clang declaration. - * @return True, if the entity should be included in the diagram. - */ - bool should_include(const clang::NamedDecl *decl); + void add_diagram_element( + std::unique_ptr element) override; + std::unique_ptr create_element(const clang::NamedDecl *decl) const; + + void find_instantiation_relationships( + common::model::template_element &template_instantiation_base, + const std::string &full_name, common::id_t templated_decl_id); + +private: /** * @brief Create class element model from class declaration * @@ -244,20 +230,6 @@ private: void process_template_specialization_children( const clang::ClassTemplateSpecializationDecl *cls, class_ &c); - /** - * @brief Process template parameters - * - * @param template_declaration Template declaration - * @param t `template_trait` instance to which the parameters should be - * added - * @param templated_element Optional templated diagram element (e.g. class_) - * @return Ignored - */ - bool process_template_parameters( - const clang::TemplateDecl &template_declaration, - clanguml::common::model::template_trait &t, - common::optional_ref templated_element = {}); - /** * @brief Process class method * @@ -451,27 +423,14 @@ private: */ bool has_processed_template_class(const std::string &qualified_name) const; - void add_diagram_element( - std::unique_ptr element) override; - - void find_instantiation_relationships( - common::model::template_element &template_instantiation_base, - const std::string &full_name, common::id_t templated_decl_id); - /** * @brief Get template builder reference * * @return Reference to 'template_builder' instance */ - template_builder &tbuilder() { return template_builder_; } + template_builder_t &tbuilder() { return template_builder_; } - // Reference to the output diagram model - clanguml::class_diagram::model::diagram &diagram_; - - // Reference to class diagram config - const clanguml::config::class_diagram &config_; - - template_builder template_builder_; + template_builder_t template_builder_; std::map> diff --git a/src/common/visitor/template_builder.cc b/src/common/visitor/template_builder.cc index eee5ed7c..1b232357 100644 --- a/src/common/visitor/template_builder.cc +++ b/src/common/visitor/template_builder.cc @@ -18,535 +18,9 @@ #include "template_builder.h" #include "common/clang_utils.h" -#include "translation_unit_visitor.h" #include namespace clanguml::common::visitor { - -template_builder::template_builder(clanguml::common::model::diagram &diagram_, - const clanguml::config::diagram &config_, - clanguml::common::visitor::translation_unit_visitor &visitor, - element_factory_t element_factory, - find_instantiation_relationships_t find_instantiation_relationships, - on_argument_base_found_t on_argument_base_found) - : diagram_{diagram_} - , config_{config_} - , id_mapper_{visitor.id_mapper()} - , source_manager_{visitor.source_manager()} - , visitor_{visitor} - , element_factory_{std::move(element_factory)} - , find_instantiation_relationships_{std::move( - find_instantiation_relationships)} - , on_argument_base_found_{std::move(on_argument_base_found)} -{ -} - -common::model::diagram &template_builder::diagram() { return diagram_; } - -const config::diagram &template_builder::config() const { return config_; } - -const namespace_ &template_builder::using_namespace() const -{ - return config_.using_namespace(); -} - -common::visitor::ast_id_mapper &template_builder::id_mapper() -{ - return id_mapper_; -} - -clang::SourceManager &template_builder::source_manager() const -{ - return source_manager_; -} - -bool template_builder::simplify_system_template( - template_parameter &ct, const std::string &full_name) const -{ - auto simplified = config().simplify_template_type(full_name); - - if (simplified != full_name) { - ct.set_type(simplified); - ct.set_id(common::to_id(simplified)); - ct.clear_params(); - return true; - } - - return false; -} - -void template_builder::build( - clanguml::common::model::template_element &template_instantiation, - const clang::NamedDecl *cls, - const clang::TemplateSpecializationType &template_type_decl, - std::optional parent) -{ - // - // Here we'll hold the template base class params to replace with the - // instantiated values - // - std::deque> - template_base_params{}; - - const auto *template_type_ptr = &template_type_decl; - - if (template_type_decl.isTypeAlias()) { - if (const auto *tsp = - template_type_decl.getAliasedType() - ->template getAs(); - tsp != nullptr) - template_type_ptr = tsp; - } - - const auto &template_type = *template_type_ptr; - - template_instantiation.is_template(true); - - std::string full_template_specialization_name = common::to_string( - template_type.desugar(), - template_type.getTemplateName().getAsTemplateDecl()->getASTContext()); - - auto *template_decl{template_type.getTemplateName().getAsTemplateDecl()}; - - auto template_decl_qualified_name = - template_decl->getQualifiedNameAsString(); - - auto *class_template_decl{ - clang::dyn_cast(template_decl)}; - - if ((class_template_decl != nullptr) && - (class_template_decl->getTemplatedDecl() != nullptr) && - (class_template_decl->getTemplatedDecl()->getParent() != nullptr) && - class_template_decl->getTemplatedDecl()->getParent()->isRecord()) { - - namespace_ ns{ - common::get_tag_namespace(*class_template_decl->getTemplatedDecl() - ->getParent() - ->getOuterLexicalRecordContext())}; - - std::string ns_str = ns.to_string(); - std::string name = template_decl->getQualifiedNameAsString(); - if (!ns_str.empty()) { - name = name.substr(ns_str.size() + 2); - } - - util::replace_all(name, "::", "##"); - template_instantiation.set_name(name); - - template_instantiation.set_namespace(ns); - } - else { - namespace_ ns{template_decl_qualified_name}; - ns.pop_back(); - template_instantiation.set_name(template_decl->getNameAsString()); - template_instantiation.set_namespace(ns); - } - - // TODO: Refactor handling of base parameters to a separate method - - // We need this to match any possible base classes coming from template - // arguments - std::vector< - std::pair> - template_parameter_names{}; - - for (const auto *parameter : *template_decl->getTemplateParameters()) { - if (parameter->isTemplateParameter() && - (parameter->isTemplateParameterPack() || - parameter->isParameterPack())) { - template_parameter_names.emplace_back( - parameter->getNameAsString(), true); - } - else - template_parameter_names.emplace_back( - parameter->getNameAsString(), false); - } - - // Check if the primary template has any base classes - int base_index = 0; - - const auto *templated_class_decl = - clang::dyn_cast_or_null( - template_decl->getTemplatedDecl()); - - if ((templated_class_decl != nullptr) && - templated_class_decl->hasDefinition()) - for (const auto &base : templated_class_decl->bases()) { - const auto base_class_name = common::to_string( - base.getType(), templated_class_decl->getASTContext(), false); - - LOG_DBG("Found template instantiation base: {}, {}", - base_class_name, base_index); - - // Check if any of the primary template arguments has a - // parameter equal to this type - auto it = std::find_if(template_parameter_names.begin(), - template_parameter_names.end(), - [&base_class_name]( - const auto &p) { return p.first == base_class_name; }); - - if (it != template_parameter_names.end()) { - const auto ¶meter_name = it->first; - const bool is_variadic = it->second; - // Found base class which is a template parameter - LOG_DBG("Found base class which is a template parameter " - "{}, {}, {}", - parameter_name, is_variadic, - std::distance(template_parameter_names.begin(), it)); - - template_base_params.emplace_back(parameter_name, - std::distance(template_parameter_names.begin(), it), - is_variadic); - } - else { - // This is a regular base class - it is handled by - // process_template - } - base_index++; - } - - process_template_arguments(parent, cls, template_base_params, - template_type.template_arguments(), template_instantiation, - template_decl); - - find_instantiation_relationships(template_instantiation, - template_type.getTemplateName().getAsTemplateDecl()->getID(), - full_template_specialization_name); - - template_instantiation.set_id( - common::to_id(template_instantiation.full_name(false))); - - visitor_.set_source_location(*cls, template_instantiation); -} - -void template_builder::build_from_class_template_specialization( - clanguml::common::model::template_element &template_instantiation, - const clang::ClassTemplateSpecializationDecl &template_specialization, - std::optional parent) -{ - // - // Here we'll hold the template base params to replace with the - // instantiated values - // - std::deque> - template_base_params{}; - - const clang::ClassTemplateDecl *template_decl = - template_specialization.getSpecializedTemplate(); - - auto qualified_name = template_decl->getQualifiedNameAsString(); - - namespace_ ns{qualified_name}; - ns.pop_back(); - template_instantiation.set_name(template_decl->getNameAsString()); - template_instantiation.set_namespace(ns); - - process_template_arguments(parent, &template_specialization, - template_base_params, - template_specialization.getTemplateArgs().asArray(), - template_instantiation, template_decl); - - // Update the id after the template parameters are processed - template_instantiation.set_id( - common::to_id(template_instantiation.full_name(false))); - - find_instantiation_relationships(template_instantiation, - template_specialization.getID(), qualified_name); - - visitor_.set_source_location(*template_decl, template_instantiation); -} - -void template_builder::find_instantiation_relationships( - common::model::template_element &template_instantiation, common::id_t id, - const std::string &qualified_name) const -{ - if (find_instantiation_relationships_) { - find_instantiation_relationships_( - template_instantiation, qualified_name, id); - } -} - -void template_builder::process_template_arguments( - std::optional &parent, - const clang::NamedDecl *cls, - std::deque> &template_base_params, - const clang::ArrayRef &template_args, - clanguml::common::model::template_element &template_instantiation, - const clang::TemplateDecl *template_decl) -{ - auto arg_index{0}; - - for (const auto &arg : template_args) { - // Argument can be a parameter pack in which case it gives multiple - // arguments - std::vector arguments; - - // For now ignore the default template arguments of templates which - // do not match the inclusion filters, to make the system - // templates 'nicer' - i.e. skipping the allocators and comparators - // TODO: Change this to ignore only when the arguments are set to - // default values, and add them when they are specifically - // overridden - if (!diagram().should_include( - namespace_{template_decl->getQualifiedNameAsString()})) { - const auto *maybe_type_parm_decl = - clang::dyn_cast( - template_decl->getTemplateParameters()->getParam( - std::min(arg_index, - static_cast( - template_decl->getTemplateParameters() - ->size()) - - 1))); - if (maybe_type_parm_decl != nullptr && - maybe_type_parm_decl->hasDefaultArgument()) { - continue; - } - } - - // - // Handle the template parameter/argument based on its kind - // - argument_process_dispatch(parent, cls, template_instantiation, - template_decl, arg, arg_index, arguments); - - if (arguments.empty()) { - arg_index++; - continue; - } - - // We can figure this only when we encounter variadic param in - // the list of template params, from then this variable is true - // and we can process following template parameters as belonging - // to the variadic tuple - [[maybe_unused]] auto variadic_params{false}; - - // In case any of the template arguments are base classes, add - // them as parents of the current template instantiation class - if (!template_base_params.empty()) { - variadic_params = - add_base_classes(template_instantiation, template_base_params, - arg_index, variadic_params, arguments.front()); - } - - for (auto &argument : arguments) { - simplify_system_template( - argument, argument.to_string(using_namespace(), false, true)); - - LOG_DBG("Adding template argument {} to template " - "specialization/instantiation {}", - argument.to_string(using_namespace(), false), - template_instantiation.name()); - - template_instantiation.add_template(std::move(argument)); - } - - arg_index++; - } - - // Update id - template_instantiation.set_id( - common::to_id(template_instantiation.full_name(false))); -} - -void template_builder::argument_process_dispatch( - std::optional &parent, - const clang::NamedDecl *cls, - clanguml::common::model::template_element &template_instantiation, - const clang::TemplateDecl *template_decl, - const clang::TemplateArgument &arg, size_t argument_index, - std::vector &argument) -{ - LOG_DBG("Processing argument {} in template class: {}", argument_index, - cls->getQualifiedNameAsString()); - - switch (arg.getKind()) { - case clang::TemplateArgument::Null: - argument.push_back(process_null_argument(arg)); - break; - case clang::TemplateArgument::Template: - argument.push_back(process_template_argument(arg)); - break; - case clang::TemplateArgument::Type: { - argument.push_back(process_type_argument(parent, cls, template_decl, - arg.getAsType(), template_instantiation, argument_index)); - break; - } - case clang::TemplateArgument::Declaration: - break; - case clang::TemplateArgument::NullPtr: - argument.push_back(process_nullptr_argument(arg)); - break; - case clang::TemplateArgument::Integral: - argument.push_back(process_integral_argument(arg)); - break; - case clang::TemplateArgument::TemplateExpansion: - argument.push_back(process_template_expansion(arg)); - break; - case clang::TemplateArgument::Expression: - argument.push_back(process_expression_argument(arg)); - break; - case clang::TemplateArgument::Pack: - for (auto &a : - process_pack_argument(parent, cls, template_instantiation, - template_decl, arg, argument_index, argument)) { - argument.push_back(a); - } - break; - } -} - -template_parameter template_builder::process_template_argument( - const clang::TemplateArgument &arg) -{ - auto arg_name = common::to_string(arg); - - LOG_DBG("Processing template argument: {}", arg_name); - - return template_parameter::make_template_type(arg_name); -} - -template_parameter template_builder::process_template_expansion( - const clang::TemplateArgument &arg) -{ - auto arg_name = common::to_string(arg); - - LOG_DBG("Processing template expansion argument: {}", arg_name); - - util::if_not_null( - arg.getAsTemplate().getAsTemplateDecl(), [&arg_name](const auto *decl) { - arg_name = decl->getQualifiedNameAsString(); - }); - - auto param = template_parameter::make_template_type(arg_name); - param.is_variadic(true); - - return param; -} - -clang::QualType template_builder::consume_context( - clang::QualType type, template_parameter &tp) const -{ - auto [unqualified_type, context] = common::consume_type_context(type); - - tp.deduced_context(std::move(context)); - - return unqualified_type; -} - -template_parameter template_builder::process_type_argument( - std::optional &parent, - const clang::NamedDecl *cls, const clang::TemplateDecl *template_decl, - clang::QualType type, - clanguml::common::model::template_element &template_instantiation, - size_t argument_index) -{ - std::optional argument; - - if (type->getAs() != nullptr) { - type = type->getAs()->getNamedType(); // NOLINT - } - - auto type_name = common::to_string(type, &cls->getASTContext()); - - LOG_DBG("Processing template {} type argument {}: {}, {}, {}", - template_decl->getQualifiedNameAsString(), argument_index, type_name, - type->getTypeClassName(), - common::to_string(type, cls->getASTContext())); - - argument = try_as_function_prototype(parent, cls, template_decl, type, - template_instantiation, argument_index); - if (argument) - return *argument; - - argument = try_as_member_pointer(parent, cls, template_decl, type, - template_instantiation, argument_index); - if (argument) - return *argument; - - argument = try_as_array(parent, cls, template_decl, type, - template_instantiation, argument_index); - if (argument) - return *argument; - - argument = try_as_template_parm_type(cls, template_decl, type); - if (argument) - return *argument; - - argument = try_as_template_specialization_type(parent, cls, template_decl, - type, template_instantiation, argument_index); - if (argument) - return *argument; - - argument = try_as_decl_type(parent, cls, template_decl, type, - template_instantiation, argument_index); - if (argument) - return *argument; - - argument = try_as_typedef_type(parent, cls, template_decl, type, - template_instantiation, argument_index); - if (argument) - return *argument; - - argument = try_as_lambda(cls, template_decl, type); - if (argument) - return *argument; - - argument = try_as_record_type(parent, cls, template_decl, type, - template_instantiation, argument_index); - if (argument) - return *argument; - - argument = try_as_enum_type( - parent, cls, template_decl, type, template_instantiation); - if (argument) - return *argument; - - argument = try_as_builtin_type(parent, type, template_decl); - if (argument) - return *argument; - - // fallback - return template_parameter::make_argument(type_name); -} - -bool template_builder::find_relationships_in_unexposed_template_params( - const template_parameter &ct, found_relationships_t &relationships) -{ - const auto &type = ct.type(); - - if (!type) - return false; - - bool found{false}; - LOG_DBG("Finding relationships in user defined type: {}", - ct.to_string(config().using_namespace(), false)); - - auto type_with_namespace = - std::make_optional(type.value()); - - if (!type_with_namespace.has_value()) { - // Couldn't find declaration of this type - type_with_namespace = common::model::namespace_{type.value()}; - } - - auto element_opt = diagram().get(type_with_namespace.value().to_string()); - if (config_.generate_template_argument_dependencies() && element_opt) { - relationships.emplace_back( - element_opt.value().id(), relationship_t::kDependency); - found = true; - } - - for (const auto &nested_template_params : ct.template_params()) { - found = find_relationships_in_unexposed_template_params( - nested_template_params, relationships) || - found; - } - - return found; -} - namespace detail { std::string map_type_parameter_to_template_parameter( @@ -609,598 +83,4 @@ std::string map_type_parameter_to_template_parameter_name( return type_parameter; } -template_parameter template_builder::process_integral_argument( - const clang::TemplateArgument &arg) -{ - assert(arg.getKind() == clang::TemplateArgument::Integral); - - std::string result; - llvm::raw_string_ostream ostream(result); - arg.dump(ostream); - - return template_parameter::make_argument(result); -} - -template_parameter template_builder::process_null_argument( - const clang::TemplateArgument &arg) -{ - assert(arg.getKind() == clang::TemplateArgument::Null); - - return template_parameter::make_argument(""); -} - -template_parameter template_builder::process_nullptr_argument( - const clang::TemplateArgument &arg) -{ - assert(arg.getKind() == clang::TemplateArgument::NullPtr); - - LOG_DBG("Processing nullptr argument: {}", common::to_string(arg)); - - return template_parameter::make_argument("nullptr"); -} - -template_parameter template_builder::process_expression_argument( - const clang::TemplateArgument &arg) -{ - assert(arg.getKind() == clang::TemplateArgument::Expression); - return template_parameter::make_argument(common::get_source_text( - arg.getAsExpr()->getSourceRange(), source_manager())); -} - -std::vector template_builder::process_pack_argument( - std::optional &parent, - const clang::NamedDecl *cls, - clanguml::common::model::template_element &template_instantiation, - const clang::TemplateDecl *base_template_decl, - const clang::TemplateArgument &arg, size_t argument_index, - std::vector & /*argument*/) -{ - assert(arg.getKind() == clang::TemplateArgument::Pack); - - std::vector res; - - auto pack_argument_index = argument_index; - - for (const auto &a : arg.getPackAsArray()) { - argument_process_dispatch(parent, cls, template_instantiation, - base_template_decl, a, pack_argument_index++, res); - } - - return res; -} - -std::optional template_builder::try_as_member_pointer( - std::optional &parent, - const clang::NamedDecl *cls, const clang::TemplateDecl *template_decl, - clang::QualType &type, - clanguml::common::model::template_element &template_instantiation, - size_t argument_index) -{ - const auto *mp_type = - common::dereference(type)->getAs(); - if (mp_type == nullptr) - return {}; - - auto argument = template_parameter::make_template_type(""); - type = consume_context(type, argument); - - // Handle a pointer to a data member of a class - if (mp_type->isMemberDataPointer()) { - argument.is_member_pointer(false); - argument.is_data_pointer(true); - - auto pointee_arg = process_type_argument(parent, cls, template_decl, - mp_type->getPointeeType(), template_instantiation, argument_index); - - argument.add_template_param(std::move(pointee_arg)); - - const auto *member_class_type = mp_type->getClass(); - - if (member_class_type == nullptr) - return {}; - - auto class_type_arg = process_type_argument(parent, cls, template_decl, - mp_type->getClass()->getCanonicalTypeUnqualified(), - template_instantiation, argument_index); - - argument.add_template_param(std::move(class_type_arg)); - } - // Handle pointer to class method member - else { - argument.is_member_pointer(true); - argument.is_data_pointer(false); - - const auto *function_type = - mp_type->getPointeeType()->getAs(); - - assert(function_type != nullptr); - - auto return_type_arg = process_type_argument(parent, cls, template_decl, - function_type->getReturnType(), template_instantiation, - argument_index); - - // Add return type argument - argument.add_template_param(std::move(return_type_arg)); - - const auto *member_class_type = mp_type->getClass(); - - if (member_class_type == nullptr) - return {}; - - auto class_type_arg = process_type_argument(parent, cls, template_decl, - mp_type->getClass()->getCanonicalTypeUnqualified(), - template_instantiation, argument_index); - - // Add class type argument - argument.add_template_param(std::move(class_type_arg)); - - // Add argument types - for (const auto ¶m_type : function_type->param_types()) { - argument.add_template_param( - process_type_argument(parent, cls, template_decl, param_type, - template_instantiation, argument_index)); - } - } - - return argument; -} - -std::optional template_builder::try_as_array( - std::optional &parent, - const clang::NamedDecl *cls, const clang::TemplateDecl *template_decl, - clang::QualType &type, - clanguml::common::model::template_element &template_instantiation, - size_t argument_index) -{ - const auto *array_type = common::dereference(type)->getAsArrayTypeUnsafe(); - if (array_type == nullptr) - return {}; - - auto argument = template_parameter::make_template_type(""); - - type = consume_context(type, argument); - - argument.is_array(true); - - // Set function template return type - auto element_type = process_type_argument(parent, cls, template_decl, - array_type->getElementType(), template_instantiation, argument_index); - - argument.add_template_param(element_type); - - if (array_type->isDependentSizedArrayType() && - array_type->getDependence() == - clang::TypeDependence::DependentInstantiation) { - argument.add_template_param( - template_parameter::make_template_type(common::to_string( - ((clang::DependentSizedArrayType *)array_type) // NOLINT - ->getSizeExpr()))); - } - else if (array_type->isConstantArrayType()) { - argument.add_template_param(template_parameter::make_argument( - std::to_string(((clang::ConstantArrayType *)array_type) // NOLINT - ->getSize() - .getLimitedValue()))); - } - - // TODO: Handle variable sized arrays - - return argument; -} - -std::optional template_builder::try_as_function_prototype( - std::optional &parent, - const clang::NamedDecl *cls, const clang::TemplateDecl *template_decl, - clang::QualType &type, - clanguml::common::model::template_element &template_instantiation, - size_t argument_index) -{ - const auto *function_type = type->getAs(); - - if (function_type == nullptr && type->isFunctionPointerType()) { - function_type = - type->getPointeeType()->getAs(); - if (function_type == nullptr) - return {}; - } - - if (function_type == nullptr) - return {}; - - LOG_DBG("Template argument is a function prototype"); - - auto argument = template_parameter::make_template_type(""); - - type = consume_context(type, argument); - - argument.is_function_template(true); - - // Set function template return type - auto return_arg = process_type_argument(parent, cls, template_decl, - function_type->getReturnType(), template_instantiation, argument_index); - - argument.add_template_param(return_arg); - - // Set function template argument types - if (function_type->isVariadic() && function_type->param_types().empty()) { - auto fallback_arg = template_parameter::make_argument({}); - fallback_arg.is_ellipsis(true); - argument.add_template_param(std::move(fallback_arg)); - } - else { - for (const auto ¶m_type : function_type->param_types()) { - argument.add_template_param( - process_type_argument(parent, cls, template_decl, param_type, - template_instantiation, argument_index)); - } - } - - return argument; -} - -std::optional template_builder::try_as_decl_type( - std::optional & /*parent*/, - const clang::NamedDecl * /*cls*/, - const clang::TemplateDecl * /*template_decl*/, clang::QualType &type, - clanguml::common::model::template_element & /*template_instantiation*/, - size_t /*argument_index*/) -{ - const auto *decl_type = - common::dereference(type)->getAs(); - if (decl_type == nullptr) { - return {}; - } - - LOG_DBG("Template argument is a decltype()"); - - // TODO - return {}; -} - -std::optional template_builder::try_as_typedef_type( - std::optional &parent, - const clang::NamedDecl * /*cls*/, - const clang::TemplateDecl * /*template_decl*/, clang::QualType &type, - clanguml::common::model::template_element & /*template_instantiation*/, - size_t /*argument_index*/) -{ - const auto *typedef_type = - common::dereference(type)->getAs(); - if (typedef_type == nullptr) { - return {}; - } - - LOG_DBG("Template argument is a typedef/using"); - - // If this is a typedef/using alias to a decltype - we're not able - // to figure out anything out of it probably - if (typedef_type->getAs() != nullptr) { - // Here we need to figure out the parent context of this alias, - // it can be a: - // - class/struct - if (typedef_type->getDecl()->isCXXClassMember() && parent) { - return template_parameter::make_argument( - fmt::format("{}::{}", parent.value()->full_name(false), - typedef_type->getDecl()->getNameAsString())); - } - // - namespace - return template_parameter::make_argument( - typedef_type->getDecl()->getQualifiedNameAsString()); - } - - return {}; -} - -std::optional -template_builder::try_as_template_specialization_type( - std::optional &parent, - const clang::NamedDecl *cls, const clang::TemplateDecl *template_decl, - clang::QualType &type, - clanguml::common::model::template_element &template_instantiation, - size_t argument_index) -{ - const auto *nested_template_type = - common::dereference(type)->getAs(); - if (nested_template_type == nullptr) { - return {}; - } - - LOG_DBG("Template argument is a template specialization type"); - - auto argument = template_parameter::make_argument(""); - type = consume_context(type, argument); - - auto nested_type_name = nested_template_type->getTemplateName() - .getAsTemplateDecl() - ->getQualifiedNameAsString(); - - if (clang::dyn_cast( - nested_template_type->getTemplateName().getAsTemplateDecl()) != - nullptr) { - if (const auto *template_specialization_decl = - clang::dyn_cast(cls); - template_specialization_decl != nullptr) { - nested_type_name = - template_specialization_decl->getDescribedTemplateParams() - ->getParam(argument_index) - ->getNameAsString(); - } - else { - // fallback - nested_type_name = "template"; - } - - argument.is_template_template_parameter(true); - } - - argument.set_type(nested_type_name); - - auto nested_template_instantiation = - element_factory_(nested_template_type->getTemplateName() - .getAsTemplateDecl() - ->getTemplatedDecl()); - build(*nested_template_instantiation, cls, *nested_template_type, - diagram().should_include( - namespace_{template_decl->getQualifiedNameAsString()}) - ? std::make_optional(&template_instantiation) - : parent); - - argument.set_id(nested_template_instantiation->id()); - - for (const auto &t : nested_template_instantiation->template_params()) - argument.add_template_param(t); - - // Check if this template should be simplified (e.g. system - // template aliases such as 'std:basic_string' should - // be simply 'std::string') - simplify_system_template( - argument, argument.to_string(using_namespace(), false)); - - argument.set_id( - common::to_id(argument.to_string(using_namespace(), false))); - - const auto nested_template_instantiation_full_name = - nested_template_instantiation->full_name(false); - - if (nested_template_instantiation && - diagram().should_include( - namespace_{nested_template_instantiation_full_name})) { - if (config_.generate_template_argument_dependencies()) { - if (diagram().should_include( - namespace_{template_decl->getQualifiedNameAsString()})) { - template_instantiation.add_relationship( - {relationship_t::kDependency, - nested_template_instantiation->id()}); - } - else { - if (parent.has_value()) - parent.value()->add_relationship( - {relationship_t::kDependency, - nested_template_instantiation->id()}); - } - } - } - - if (diagram().should_include( - namespace_{nested_template_instantiation_full_name})) { - visitor_.set_source_location(*cls, *nested_template_instantiation); - visitor_.add_diagram_element(std::move(nested_template_instantiation)); - } - - return argument; -} - -std::optional template_builder::try_as_template_parm_type( - const clang::NamedDecl *cls, const clang::TemplateDecl * /*template_decl*/, - clang::QualType &type) -{ - auto is_variadic{false}; - - const auto *type_parameter = - common::dereference(type)->getAs(); - - auto type_name = common::to_string(type, &cls->getASTContext()); - - if (type_parameter == nullptr) { - if (const auto *pet = - common::dereference(type)->getAs(); - pet != nullptr) { - is_variadic = true; - type_parameter = - pet->getPattern()->getAs(); - } - } - - if (type_parameter == nullptr) - return {}; - - LOG_DBG("Template argument is a template parameter type"); - - auto argument = template_parameter::make_template_type(""); - type = consume_context(type, argument); - - auto type_parameter_name = common::to_string(type, cls->getASTContext()); - if (type_parameter_name.empty()) - type_parameter_name = "typename"; - - argument.set_name(map_type_parameter_to_template_parameter_name( - cls, type_parameter_name)); - - argument.is_variadic(is_variadic); - - common::ensure_lambda_type_is_relative(config(), type_parameter_name); - - return argument; -} - -std::optional template_builder::try_as_lambda( - const clang::NamedDecl *cls, const clang::TemplateDecl * /*template_decl*/, - clang::QualType &type) -{ - auto type_name = common::to_string(type, &cls->getASTContext()); - - if (type_name.find("(lambda at ") != 0) - return {}; - - LOG_DBG("Template argument is a lambda"); - - auto argument = template_parameter::make_argument(""); - type = consume_context(type, argument); - - common::ensure_lambda_type_is_relative(config(), type_name); - argument.set_type(type_name); - - return argument; -} - -std::optional template_builder::try_as_record_type( - std::optional &parent, - const clang::NamedDecl * /*cls*/, const clang::TemplateDecl *template_decl, - clang::QualType &type, - clanguml::common::model::template_element &template_instantiation, - size_t /*argument_index*/) -{ - const auto *record_type = - common::dereference(type)->getAs(); - if (record_type == nullptr) - return {}; - - LOG_DBG("Template argument is a c++ record"); - - auto argument = template_parameter::make_argument({}); - type = consume_context(type, argument); - - const auto type_name = config().simplify_template_type( - common::to_string(type, template_decl->getASTContext())); - - argument.set_type(type_name); - const auto type_id = common::to_id(type_name); - - argument.set_id(type_id); - - const auto *class_template_specialization = - clang::dyn_cast( - record_type->getAsRecordDecl()); - - if (class_template_specialization != nullptr) { - auto tag_argument = element_factory_(class_template_specialization); - - build_from_class_template_specialization( - *tag_argument, *class_template_specialization); - - if (tag_argument) { - argument.set_type(tag_argument->name_and_ns()); - for (const auto &p : tag_argument->template_params()) - argument.add_template_param(p); - for (auto &r : tag_argument->relationships()) { - template_instantiation.add_relationship(std::move(r)); - } - - if (config_.generate_template_argument_dependencies() && - diagram().should_include(tag_argument->get_namespace())) { - if (parent.has_value()) - parent.value()->add_relationship( - {relationship_t::kDependency, tag_argument->id()}); - visitor_.set_source_location(*template_decl, *tag_argument); - visitor_.add_diagram_element(std::move(tag_argument)); - } - } - } - else if (const auto *record_type_decl = record_type->getAsRecordDecl(); - record_type_decl != nullptr) { - if (config_.generate_template_argument_dependencies() && - diagram().should_include(namespace_{type_name})) { - // Add dependency relationship to the parent - // template - template_instantiation.add_relationship( - {relationship_t::kDependency, type_id}); - } - } - - return argument; -} - -std::optional template_builder::try_as_enum_type( - std::optional & /*parent*/, - const clang::NamedDecl * /*cls*/, const clang::TemplateDecl *template_decl, - clang::QualType &type, - clanguml::common::model::template_element &template_instantiation) -{ - const auto *enum_type = type->getAs(); - if (enum_type == nullptr) - return {}; - - LOG_DBG("Template argument is a an enum"); - - auto argument = template_parameter::make_argument({}); - type = consume_context(type, argument); - - auto type_name = common::to_string(type, template_decl->getASTContext()); - argument.set_type(type_name); - const auto type_id = common::to_id(type_name); - argument.set_id(type_id); - - if (enum_type->getAsTagDecl() != nullptr && - config_.generate_template_argument_dependencies()) { - template_instantiation.add_relationship( - {relationship_t::kDependency, type_id}); - } - - return argument; -} - -std::optional template_builder::try_as_builtin_type( - std::optional & /*parent*/, - clang::QualType &type, const clang::TemplateDecl *template_decl) -{ - const auto *builtin_type = type->getAs(); - if (builtin_type == nullptr) - return {}; - - LOG_DBG("Template argument is a builtin type"); - - auto type_name = common::to_string(type, template_decl->getASTContext()); - auto argument = template_parameter::make_argument(type_name); - - type = consume_context(type, argument); - argument.set_type(type_name); - - return argument; -} - -bool template_builder::add_base_classes( - clanguml::common::model::template_element &tinst, - std::deque> &template_base_params, - int arg_index, bool variadic_params, const template_parameter &ct) -{ - bool add_template_argument_as_base_class = false; - - if (variadic_params) { - add_template_argument_as_base_class = true; - } - else { - auto [arg_name, index, is_variadic] = template_base_params.front(); - - variadic_params = is_variadic; - if ((arg_index == index) || (is_variadic && arg_index >= index)) { - add_template_argument_as_base_class = true; - if (!is_variadic) { - // Don't remove the remaining variadic parameter - template_base_params.pop_front(); - } - } - } - - const auto maybe_id = ct.id(); - if (add_template_argument_as_base_class && maybe_id) { - LOG_DBG("Adding template argument as base class '{}'", - ct.to_string({}, false)); - if (on_argument_base_found_) - on_argument_base_found_( - tinst, maybe_id.value(), ct.to_string({}, false)); - } - - return variadic_params; -} - } // namespace clanguml::class_diagram::visitor diff --git a/src/common/visitor/template_builder.h b/src/common/visitor/template_builder.h index 25b00df9..a8f0f118 100644 --- a/src/common/visitor/template_builder.h +++ b/src/common/visitor/template_builder.h @@ -17,6 +17,7 @@ */ #pragma once +#include "class_diagram/model/diagram.h" #include "common/model/diagram.h" #include "common/model/template_element.h" #include "common/visitor/ast_id_mapper.h" @@ -32,35 +33,31 @@ using common::model::template_parameter; using found_relationships_t = std::vector< std::pair>; -class translation_unit_visitor; +namespace detail { + +std::string map_type_parameter_to_template_parameter( + const clang::ClassTemplateSpecializationDecl *decl, const std::string &tp); + +std::string map_type_parameter_to_template_parameter( + const clang::TypeAliasTemplateDecl *decl, const std::string &tp); + +} // namespace detail + +std::string map_type_parameter_to_template_parameter_name( + const clang::Decl *decl, const std::string &type_parameter); /** * @brief Class responsible for building all kinds of templates from Clang AST. */ -class template_builder { +template class template_builder { public: - using element_factory_t = - std::function( - const clang::NamedDecl *)>; - - using find_instantiation_relationships_t = std::function; - - using on_argument_base_found_t = - std::function; /** * @brief Constructor. * * @param visitor Reference to class diagram translation_unit_visitor */ template_builder(clanguml::common::model::diagram &diagram_, - const clanguml::config::diagram &config_, - clanguml::common::visitor::translation_unit_visitor &visitor, - element_factory_t element_factory, - find_instantiation_relationships_t find_instantiation_relationships = - {}, - on_argument_base_found_t on_argument_base_found = {}); + const clanguml::config::diagram &config_, VisitorT &visitor); /** * @brief Get reference to the current diagram model @@ -100,6 +97,11 @@ public: bool simplify_system_template( template_parameter &ct, const std::string &full_name) const; + void build_from_template_declaration( + clanguml::common::model::template_trait &template_model, + const clang::TemplateDecl &template_declaration, + common::optional_ref templated_element = {}); + /** * @brief Basic template class build method * @@ -108,12 +110,19 @@ public: * @param parent Optional class in which this template is contained * @return Created template class model */ - void build( + void build_from_template_specialization_type( clanguml::common::model::template_element &template_instantiation, const clang::NamedDecl *cls, const clang::TemplateSpecializationType &template_type_decl, std::optional parent = {}); + void build( + clanguml::common::model::template_element &template_instantiation, + const clang::NamedDecl *cls, const clang::TemplateDecl *template_decl, + const clang::ArrayRef template_arguments, + std::string full_template_specialization_name, + std::optional parent = {}); + /** * @brief Build template class from class template specialization decl * @@ -528,15 +537,1292 @@ private: clang::SourceManager &source_manager_; - clanguml::common::visitor::translation_unit_visitor &visitor_; - - element_factory_t element_factory_; - - find_instantiation_relationships_t find_instantiation_relationships_; - - // Callback to call when a template instantiation has arguments which are - // base classes, i.e. when template class inherits from template params - on_argument_base_found_t on_argument_base_found_; + VisitorT &visitor_; }; +template +template_builder::template_builder( + clanguml::common::model::diagram &diagram_, + const clanguml::config::diagram &config_, VisitorT &visitor) + : diagram_{diagram_} + , config_{config_} + , id_mapper_{visitor.id_mapper()} + , source_manager_{visitor.source_manager()} + , visitor_{visitor} +{ +} + +template +common::model::diagram &template_builder::diagram() +{ + return diagram_; +} + +template +const config::diagram &template_builder::config() const +{ + return config_; +} + +template +const namespace_ &template_builder::using_namespace() const +{ + return config_.using_namespace(); +} + +template +common::visitor::ast_id_mapper &template_builder::id_mapper() +{ + return id_mapper_; +} + +template +clang::SourceManager &template_builder::source_manager() const +{ + return source_manager_; +} + +template +bool template_builder::simplify_system_template( + template_parameter &ct, const std::string &full_name) const +{ + auto simplified = config().simplify_template_type(full_name); + + if (simplified != full_name) { + ct.set_type(simplified); + ct.set_id(common::to_id(simplified)); + ct.clear_params(); + return true; + } + + return false; +} + +template +void template_builder::build_from_template_declaration( + clanguml::common::model::template_trait &template_model, + const clang::TemplateDecl &template_declaration, + common::optional_ref templated_element) +{ + + LOG_DBG("Processing {} template parameters...", + common::get_qualified_name(template_declaration)); + + if (template_declaration.getTemplateParameters() == nullptr) + return; + + for (const auto *parameter : + *template_declaration.getTemplateParameters()) { + if (clang::dyn_cast_or_null(parameter) != + nullptr) { + const auto *template_type_parameter = + clang::dyn_cast_or_null(parameter); + + std::optional default_arg; + if (template_type_parameter->hasDefaultArgument()) { + default_arg = + template_type_parameter->getDefaultArgument().getAsString(); + } + + auto parameter_name = template_type_parameter->getNameAsString(); + if (parameter_name.empty()) + parameter_name = "typename"; + + auto ct = template_parameter::make_template_type(parameter_name, + default_arg, template_type_parameter->isParameterPack()); + + if constexpr (std::is_same_v) { + if (template_type_parameter->getTypeConstraint() != nullptr) { + util::if_not_null( + template_type_parameter->getTypeConstraint() + ->getNamedConcept(), + [this, &ct, &templated_element]( + const clang::ConceptDecl *named_concept) mutable { + ct.set_concept_constraint( + named_concept->getQualifiedNameAsString()); + if (templated_element && + visitor_.should_include(named_concept)) { + templated_element.value().add_relationship( + {relationship_t::kConstraint, + id_mapper() + .get_global_id( + named_concept->getID()) + .value(), + model::access_t::kNone, + ct.name().value()}); + } + }); + } + } + + template_model.add_template(std::move(ct)); + } + else if (clang::dyn_cast_or_null( + parameter) != nullptr) { + const auto *template_nontype_parameter = + clang::dyn_cast_or_null( + parameter); + + std::optional default_arg; + + if (template_nontype_parameter->hasDefaultArgument()) + default_arg = common::to_string( + template_nontype_parameter->getDefaultArgument()); + + auto ct = template_parameter::make_non_type_template( + template_nontype_parameter->getType().getAsString(), + template_nontype_parameter->getNameAsString(), default_arg, + template_nontype_parameter->isParameterPack()); + + template_model.add_template(std::move(ct)); + } + else if (clang::dyn_cast_or_null( + parameter) != nullptr) { + const auto *template_template_parameter = + clang::dyn_cast_or_null( + parameter); + std::optional default_arg; + if (template_template_parameter->hasDefaultArgument()) { + default_arg = common::to_string( + template_template_parameter->getDefaultArgument() + .getArgument()); + } + auto ct = template_parameter::make_template_template_type( + template_template_parameter->getNameAsString(), default_arg, + template_template_parameter->isParameterPack()); + + template_model.add_template(std::move(ct)); + } + else { + // pass + } + } +} + +template +void template_builder::build_from_template_specialization_type( + clanguml::common::model::template_element &template_instantiation, + const clang::NamedDecl *cls, + const clang::TemplateSpecializationType &template_type_decl, + std::optional parent) +{ + const auto *template_type_ptr = &template_type_decl; + + if (template_type_decl.isTypeAlias()) { + if (const auto *tsp = + template_type_decl.getAliasedType() + ->template getAs(); + tsp != nullptr) + template_type_ptr = tsp; + } + + const auto &template_type = *template_type_ptr; + + template_instantiation.is_template(true); + + std::string full_template_specialization_name = common::to_string( + template_type.desugar(), + template_type.getTemplateName().getAsTemplateDecl()->getASTContext()); + + auto *template_decl{template_type.getTemplateName().getAsTemplateDecl()}; + + build(template_instantiation, cls, template_decl, + template_type.template_arguments(), full_template_specialization_name, + parent); +} + +template +void template_builder::build( + clanguml::common::model::template_element &template_instantiation, + const clang::NamedDecl *cls, const clang::TemplateDecl *template_decl, + const clang::ArrayRef template_arguments, + std::string full_template_specialization_name, + std::optional parent) +{ + // + // Here we'll hold the template base class params to replace with the + // instantiated values + // + std::deque> + template_base_params{}; + + auto template_decl_qualified_name = + template_decl->getQualifiedNameAsString(); + + if (const auto *class_template_decl = + clang::dyn_cast(template_decl); + (class_template_decl != nullptr) && + (class_template_decl->getTemplatedDecl() != nullptr) && + (class_template_decl->getTemplatedDecl()->getParent() != nullptr) && + class_template_decl->getTemplatedDecl()->getParent()->isRecord()) { + + namespace_ ns{ + common::get_tag_namespace(*class_template_decl->getTemplatedDecl() + ->getParent() + ->getOuterLexicalRecordContext())}; + + std::string ns_str = ns.to_string(); + std::string name = template_decl->getQualifiedNameAsString(); + if (!ns_str.empty()) { + name = name.substr(ns_str.size() + 2); + } + + util::replace_all(name, "::", "##"); + template_instantiation.set_name(name); + + template_instantiation.set_namespace(ns); + } + else { + namespace_ ns{template_decl_qualified_name}; + ns.pop_back(); + template_instantiation.set_name(template_decl->getNameAsString()); + template_instantiation.set_namespace(ns); + } + + // TODO: Refactor handling of base parameters to a separate method + + // We need this to match any possible base classes coming from template + // arguments + std::vector< + std::pair> + template_parameter_names{}; + + for (const auto *parameter : *template_decl->getTemplateParameters()) { + if (parameter->isTemplateParameter() && + (parameter->isTemplateParameterPack() || + parameter->isParameterPack())) { + template_parameter_names.emplace_back( + parameter->getNameAsString(), true); + } + else + template_parameter_names.emplace_back( + parameter->getNameAsString(), false); + } + + // Check if the primary template has any base classes + int base_index = 0; + + const auto *templated_class_decl = + clang::dyn_cast_or_null( + template_decl->getTemplatedDecl()); + + if ((templated_class_decl != nullptr) && + templated_class_decl->hasDefinition()) + for (const auto &base : templated_class_decl->bases()) { + const auto base_class_name = common::to_string( + base.getType(), templated_class_decl->getASTContext(), false); + + LOG_DBG("Found template instantiation base: {}, {}", + base_class_name, base_index); + + // Check if any of the primary template arguments has a + // parameter equal to this type + auto it = std::find_if(template_parameter_names.begin(), + template_parameter_names.end(), + [&base_class_name]( + const auto &p) { return p.first == base_class_name; }); + + if (it != template_parameter_names.end()) { + const auto ¶meter_name = it->first; + const bool is_variadic = it->second; + // Found base class which is a template parameter + LOG_DBG("Found base class which is a template parameter " + "{}, {}, {}", + parameter_name, is_variadic, + std::distance(template_parameter_names.begin(), it)); + + template_base_params.emplace_back(parameter_name, + std::distance(template_parameter_names.begin(), it), + is_variadic); + } + else { + // This is a regular base class - it is handled by + // process_template + } + base_index++; + } + + process_template_arguments(parent, cls, template_base_params, + template_arguments, template_instantiation, template_decl); + + if constexpr (std::is_same_v) { + find_instantiation_relationships(template_instantiation, + template_decl->getID(), full_template_specialization_name); + } + + template_instantiation.set_id( + common::to_id(template_instantiation.full_name(false))); + + visitor_.set_source_location(*cls, template_instantiation); +} + +template +void template_builder::build_from_class_template_specialization( + clanguml::common::model::template_element &template_instantiation, + const clang::ClassTemplateSpecializationDecl &template_specialization, + std::optional parent) +{ + // + // Here we'll hold the template base params to replace with the + // instantiated values + // + std::deque> + template_base_params{}; + + const clang::ClassTemplateDecl *template_decl = + template_specialization.getSpecializedTemplate(); + + auto qualified_name = template_decl->getQualifiedNameAsString(); + + namespace_ ns{qualified_name}; + ns.pop_back(); + template_instantiation.set_name(template_decl->getNameAsString()); + template_instantiation.set_namespace(ns); + + process_template_arguments(parent, &template_specialization, + template_base_params, + template_specialization.getTemplateArgs().asArray(), + template_instantiation, template_decl); + + // Update the id after the template parameters are processed + template_instantiation.set_id( + common::to_id(template_instantiation.full_name(false))); + + if constexpr (std::is_same_v) { + find_instantiation_relationships(template_instantiation, + template_specialization.getID(), qualified_name); + } + + visitor_.set_source_location(*template_decl, template_instantiation); +} + +template +void template_builder::find_instantiation_relationships( + common::model::template_element &template_instantiation, common::id_t id, + const std::string &qualified_name) const +{ + visitor_.find_instantiation_relationships( + template_instantiation, qualified_name, id); +} + +template +void template_builder::process_template_arguments( + std::optional &parent, + const clang::NamedDecl *cls, + std::deque> &template_base_params, + const clang::ArrayRef &template_args, + clanguml::common::model::template_element &template_instantiation, + const clang::TemplateDecl *template_decl) +{ + auto arg_index{0}; + + for (const auto &arg : template_args) { + // Argument can be a parameter pack in which case it gives multiple + // arguments + std::vector arguments; + + // For now ignore the default template arguments of templates which + // do not match the inclusion filters, to make the system + // templates 'nicer' - i.e. skipping the allocators and comparators + // TODO: Change this to ignore only when the arguments are set to + // default values, and add them when they are specifically + // overridden + if (!diagram().should_include( + namespace_{template_decl->getQualifiedNameAsString()})) { + const auto *maybe_type_parm_decl = + clang::dyn_cast( + template_decl->getTemplateParameters()->getParam( + std::min(arg_index, + static_cast( + template_decl->getTemplateParameters() + ->size()) - + 1))); + if (maybe_type_parm_decl != nullptr && + maybe_type_parm_decl->hasDefaultArgument()) { + continue; + } + } + + // + // Handle the template parameter/argument based on its kind + // + argument_process_dispatch(parent, cls, template_instantiation, + template_decl, arg, arg_index, arguments); + + if (arguments.empty()) { + arg_index++; + continue; + } + + // We can figure this only when we encounter variadic param in + // the list of template params, from then this variable is true + // and we can process following template parameters as belonging + // to the variadic tuple + [[maybe_unused]] auto variadic_params{false}; + + if constexpr (std::is_same_v) { + // In case any of the template arguments are base classes, add + // them as parents of the current template instantiation class + if (!template_base_params.empty()) { + variadic_params = add_base_classes(template_instantiation, + template_base_params, arg_index, variadic_params, + arguments.front()); + } + } + + for (auto &argument : arguments) { + simplify_system_template( + argument, argument.to_string(using_namespace(), false, true)); + + LOG_DBG("Adding template argument {} to template " + "specialization/instantiation {}", + argument.to_string(using_namespace(), false), + template_instantiation.name()); + + template_instantiation.add_template(std::move(argument)); + } + + arg_index++; + } + + // Update id + template_instantiation.set_id( + common::to_id(template_instantiation.full_name(false))); +} + +template +void template_builder::argument_process_dispatch( + std::optional &parent, + const clang::NamedDecl *cls, + clanguml::common::model::template_element &template_instantiation, + const clang::TemplateDecl *template_decl, + const clang::TemplateArgument &arg, size_t argument_index, + std::vector &argument) +{ + LOG_DBG("Processing argument {} in template class: {}", argument_index, + cls->getQualifiedNameAsString()); + + switch (arg.getKind()) { + case clang::TemplateArgument::Null: + argument.push_back(process_null_argument(arg)); + break; + case clang::TemplateArgument::Template: + argument.push_back(process_template_argument(arg)); + break; + case clang::TemplateArgument::Type: { + argument.push_back(process_type_argument(parent, cls, template_decl, + arg.getAsType(), template_instantiation, argument_index)); + break; + } + case clang::TemplateArgument::Declaration: + break; + case clang::TemplateArgument::NullPtr: + argument.push_back(process_nullptr_argument(arg)); + break; + case clang::TemplateArgument::Integral: + argument.push_back(process_integral_argument(arg)); + break; + case clang::TemplateArgument::TemplateExpansion: + argument.push_back(process_template_expansion(arg)); + break; + case clang::TemplateArgument::Expression: + argument.push_back(process_expression_argument(arg)); + break; + case clang::TemplateArgument::Pack: + for (auto &a : + process_pack_argument(parent, cls, template_instantiation, + template_decl, arg, argument_index, argument)) { + argument.push_back(a); + } + break; + } +} + +template +template_parameter template_builder::process_template_argument( + const clang::TemplateArgument &arg) +{ + auto arg_name = common::to_string(arg); + + LOG_DBG("Processing template argument: {}", arg_name); + + return template_parameter::make_template_type(arg_name); +} + +template +template_parameter template_builder::process_template_expansion( + const clang::TemplateArgument &arg) +{ + auto arg_name = common::to_string(arg); + + LOG_DBG("Processing template expansion argument: {}", arg_name); + + util::if_not_null( + arg.getAsTemplate().getAsTemplateDecl(), [&arg_name](const auto *decl) { + arg_name = decl->getQualifiedNameAsString(); + }); + + auto param = template_parameter::make_template_type(arg_name); + param.is_variadic(true); + + return param; +} + +template +clang::QualType template_builder::consume_context( + clang::QualType type, template_parameter &tp) const +{ + auto [unqualified_type, context] = common::consume_type_context(type); + + tp.deduced_context(std::move(context)); + + return unqualified_type; +} + +template +template_parameter template_builder::process_type_argument( + std::optional &parent, + const clang::NamedDecl *cls, const clang::TemplateDecl *template_decl, + clang::QualType type, + clanguml::common::model::template_element &template_instantiation, + size_t argument_index) +{ + std::optional argument; + + if (type->getAs() != nullptr) { + type = type->getAs()->getNamedType(); // NOLINT + } + + auto type_name = common::to_string(type, &cls->getASTContext()); + + LOG_DBG("Processing template {} type argument {}: {}, {}, {}", + template_decl->getQualifiedNameAsString(), argument_index, type_name, + type->getTypeClassName(), + common::to_string(type, cls->getASTContext())); + + argument = try_as_function_prototype(parent, cls, template_decl, type, + template_instantiation, argument_index); + if (argument) + return *argument; + + argument = try_as_member_pointer(parent, cls, template_decl, type, + template_instantiation, argument_index); + if (argument) + return *argument; + + argument = try_as_array(parent, cls, template_decl, type, + template_instantiation, argument_index); + if (argument) + return *argument; + + argument = try_as_template_parm_type(cls, template_decl, type); + if (argument) + return *argument; + + argument = try_as_template_specialization_type(parent, cls, template_decl, + type, template_instantiation, argument_index); + if (argument) + return *argument; + + argument = try_as_decl_type(parent, cls, template_decl, type, + template_instantiation, argument_index); + if (argument) + return *argument; + + argument = try_as_typedef_type(parent, cls, template_decl, type, + template_instantiation, argument_index); + if (argument) + return *argument; + + argument = try_as_lambda(cls, template_decl, type); + if (argument) + return *argument; + + argument = try_as_record_type(parent, cls, template_decl, type, + template_instantiation, argument_index); + if (argument) + return *argument; + + argument = try_as_enum_type( + parent, cls, template_decl, type, template_instantiation); + if (argument) + return *argument; + + argument = try_as_builtin_type(parent, type, template_decl); + if (argument) + return *argument; + + // fallback + return template_parameter::make_argument(type_name); +} + +template +bool template_builder:: + find_relationships_in_unexposed_template_params( + const template_parameter &ct, found_relationships_t &relationships) +{ + const auto &type = ct.type(); + + if (!type) + return false; + + bool found{false}; + LOG_DBG("Finding relationships in user defined type: {}", + ct.to_string(config().using_namespace(), false)); + + auto type_with_namespace = + std::make_optional(type.value()); + + if (!type_with_namespace.has_value()) { + // Couldn't find declaration of this type + type_with_namespace = common::model::namespace_{type.value()}; + } + + auto element_opt = diagram().get(type_with_namespace.value().to_string()); + if (config_.generate_template_argument_dependencies() && element_opt) { + relationships.emplace_back( + element_opt.value().id(), relationship_t::kDependency); + found = true; + } + + for (const auto &nested_template_params : ct.template_params()) { + found = find_relationships_in_unexposed_template_params( + nested_template_params, relationships) || + found; + } + + return found; +} + +template +template_parameter template_builder::process_integral_argument( + const clang::TemplateArgument &arg) +{ + assert(arg.getKind() == clang::TemplateArgument::Integral); + + std::string result; + llvm::raw_string_ostream ostream(result); + arg.dump(ostream); + + return template_parameter::make_argument(result); +} + +template +template_parameter template_builder::process_null_argument( + const clang::TemplateArgument &arg) +{ + assert(arg.getKind() == clang::TemplateArgument::Null); + + return template_parameter::make_argument(""); +} + +template +template_parameter template_builder::process_nullptr_argument( + const clang::TemplateArgument &arg) +{ + assert(arg.getKind() == clang::TemplateArgument::NullPtr); + + LOG_DBG("Processing nullptr argument: {}", common::to_string(arg)); + + return template_parameter::make_argument("nullptr"); +} + +template +template_parameter template_builder::process_expression_argument( + const clang::TemplateArgument &arg) +{ + assert(arg.getKind() == clang::TemplateArgument::Expression); + return template_parameter::make_argument(common::get_source_text( + arg.getAsExpr()->getSourceRange(), source_manager())); +} + +template +std::vector +template_builder::process_pack_argument( + std::optional &parent, + const clang::NamedDecl *cls, + clanguml::common::model::template_element &template_instantiation, + const clang::TemplateDecl *base_template_decl, + const clang::TemplateArgument &arg, size_t argument_index, + std::vector & /*argument*/) +{ + assert(arg.getKind() == clang::TemplateArgument::Pack); + + std::vector res; + + auto pack_argument_index = argument_index; + + for (const auto &a : arg.getPackAsArray()) { + argument_process_dispatch(parent, cls, template_instantiation, + base_template_decl, a, pack_argument_index++, res); + } + + return res; +} + +template +std::optional +template_builder::try_as_member_pointer( + std::optional &parent, + const clang::NamedDecl *cls, const clang::TemplateDecl *template_decl, + clang::QualType &type, + clanguml::common::model::template_element &template_instantiation, + size_t argument_index) +{ + const auto *mp_type = + common::dereference(type)->getAs(); + if (mp_type == nullptr) + return {}; + + auto argument = template_parameter::make_template_type(""); + type = consume_context(type, argument); + + // Handle a pointer to a data member of a class + if (mp_type->isMemberDataPointer()) { + argument.is_member_pointer(false); + argument.is_data_pointer(true); + + auto pointee_arg = process_type_argument(parent, cls, template_decl, + mp_type->getPointeeType(), template_instantiation, argument_index); + + argument.add_template_param(std::move(pointee_arg)); + + const auto *member_class_type = mp_type->getClass(); + + if (member_class_type == nullptr) + return {}; + + auto class_type_arg = process_type_argument(parent, cls, template_decl, + mp_type->getClass()->getCanonicalTypeUnqualified(), + template_instantiation, argument_index); + + argument.add_template_param(std::move(class_type_arg)); + } + // Handle pointer to class method member + else { + argument.is_member_pointer(true); + argument.is_data_pointer(false); + + const auto *function_type = + mp_type->getPointeeType()->getAs(); + + assert(function_type != nullptr); + + auto return_type_arg = process_type_argument(parent, cls, template_decl, + function_type->getReturnType(), template_instantiation, + argument_index); + + // Add return type argument + argument.add_template_param(std::move(return_type_arg)); + + const auto *member_class_type = mp_type->getClass(); + + if (member_class_type == nullptr) + return {}; + + auto class_type_arg = process_type_argument(parent, cls, template_decl, + mp_type->getClass()->getCanonicalTypeUnqualified(), + template_instantiation, argument_index); + + // Add class type argument + argument.add_template_param(std::move(class_type_arg)); + + // Add argument types + for (const auto ¶m_type : function_type->param_types()) { + argument.add_template_param( + process_type_argument(parent, cls, template_decl, param_type, + template_instantiation, argument_index)); + } + } + + return argument; +} + +template +std::optional template_builder::try_as_array( + std::optional &parent, + const clang::NamedDecl *cls, const clang::TemplateDecl *template_decl, + clang::QualType &type, + clanguml::common::model::template_element &template_instantiation, + size_t argument_index) +{ + const auto *array_type = common::dereference(type)->getAsArrayTypeUnsafe(); + if (array_type == nullptr) + return {}; + + auto argument = template_parameter::make_template_type(""); + + type = consume_context(type, argument); + + argument.is_array(true); + + // Set function template return type + auto element_type = process_type_argument(parent, cls, template_decl, + array_type->getElementType(), template_instantiation, argument_index); + + argument.add_template_param(element_type); + + if (array_type->isDependentSizedArrayType() && + array_type->getDependence() == + clang::TypeDependence::DependentInstantiation) { + argument.add_template_param( + template_parameter::make_template_type(common::to_string( + ((clang::DependentSizedArrayType *)array_type) // NOLINT + ->getSizeExpr()))); + } + else if (array_type->isConstantArrayType()) { + argument.add_template_param(template_parameter::make_argument( + std::to_string(((clang::ConstantArrayType *)array_type) // NOLINT + ->getSize() + .getLimitedValue()))); + } + + // TODO: Handle variable sized arrays + + return argument; +} + +template +std::optional +template_builder::try_as_function_prototype( + std::optional &parent, + const clang::NamedDecl *cls, const clang::TemplateDecl *template_decl, + clang::QualType &type, + clanguml::common::model::template_element &template_instantiation, + size_t argument_index) +{ + const auto *function_type = type->getAs(); + + if (function_type == nullptr && type->isFunctionPointerType()) { + function_type = + type->getPointeeType()->getAs(); + if (function_type == nullptr) + return {}; + } + + if (function_type == nullptr) + return {}; + + LOG_DBG("Template argument is a function prototype"); + + auto argument = template_parameter::make_template_type(""); + + type = consume_context(type, argument); + + argument.is_function_template(true); + + // Set function template return type + auto return_arg = process_type_argument(parent, cls, template_decl, + function_type->getReturnType(), template_instantiation, argument_index); + + argument.add_template_param(return_arg); + + // Set function template argument types + if (function_type->isVariadic() && function_type->param_types().empty()) { + auto fallback_arg = template_parameter::make_argument({}); + fallback_arg.is_ellipsis(true); + argument.add_template_param(std::move(fallback_arg)); + } + else { + for (const auto ¶m_type : function_type->param_types()) { + argument.add_template_param( + process_type_argument(parent, cls, template_decl, param_type, + template_instantiation, argument_index)); + } + } + + return argument; +} + +template +std::optional template_builder::try_as_decl_type( + std::optional & /*parent*/, + const clang::NamedDecl * /*cls*/, + const clang::TemplateDecl * /*template_decl*/, clang::QualType &type, + clanguml::common::model::template_element & /*template_instantiation*/, + size_t /*argument_index*/) +{ + const auto *decl_type = + common::dereference(type)->getAs(); + if (decl_type == nullptr) { + return {}; + } + + LOG_DBG("Template argument is a decltype()"); + + // TODO + return {}; +} + +template +std::optional +template_builder::try_as_typedef_type( + std::optional &parent, + const clang::NamedDecl * /*cls*/, + const clang::TemplateDecl * /*template_decl*/, clang::QualType &type, + clanguml::common::model::template_element & /*template_instantiation*/, + size_t /*argument_index*/) +{ + const auto *typedef_type = + common::dereference(type)->getAs(); + if (typedef_type == nullptr) { + return {}; + } + + LOG_DBG("Template argument is a typedef/using"); + + // If this is a typedef/using alias to a decltype - we're not able + // to figure out anything out of it probably + if (typedef_type->getAs() != nullptr) { + // Here we need to figure out the parent context of this alias, + // it can be a: + // - class/struct + if (typedef_type->getDecl()->isCXXClassMember() && parent) { + return template_parameter::make_argument( + fmt::format("{}::{}", parent.value()->full_name(false), + typedef_type->getDecl()->getNameAsString())); + } + // - namespace + return template_parameter::make_argument( + typedef_type->getDecl()->getQualifiedNameAsString()); + } + + return {}; +} + +template +std::optional +template_builder::try_as_template_specialization_type( + std::optional &parent, + const clang::NamedDecl *cls, const clang::TemplateDecl *template_decl, + clang::QualType &type, + clanguml::common::model::template_element &template_instantiation, + size_t argument_index) +{ + const auto *nested_template_type = + common::dereference(type)->getAs(); + if (nested_template_type == nullptr) { + return {}; + } + + LOG_DBG("Template argument is a template specialization type"); + + auto argument = template_parameter::make_argument(""); + type = consume_context(type, argument); + + auto nested_type_name = nested_template_type->getTemplateName() + .getAsTemplateDecl() + ->getQualifiedNameAsString(); + + if (clang::dyn_cast( + nested_template_type->getTemplateName().getAsTemplateDecl()) != + nullptr) { + if (const auto *template_specialization_decl = + clang::dyn_cast(cls); + template_specialization_decl != nullptr) { + nested_type_name = + template_specialization_decl->getDescribedTemplateParams() + ->getParam(argument_index) + ->getNameAsString(); + } + else { + // fallback + nested_type_name = "template"; + } + + argument.is_template_template_parameter(true); + } + + argument.set_type(nested_type_name); + + auto nested_template_instantiation = + visitor_.create_element(nested_template_type->getTemplateName() + .getAsTemplateDecl() + ->getTemplatedDecl()); + build_from_template_specialization_type(*nested_template_instantiation, cls, + *nested_template_type, + diagram().should_include( + namespace_{template_decl->getQualifiedNameAsString()}) + ? std::make_optional(&template_instantiation) + : parent); + + argument.set_id(nested_template_instantiation->id()); + + for (const auto &t : nested_template_instantiation->template_params()) + argument.add_template_param(t); + + // Check if this template should be simplified (e.g. system + // template aliases such as 'std:basic_string' should + // be simply 'std::string') + simplify_system_template( + argument, argument.to_string(using_namespace(), false)); + + argument.set_id( + common::to_id(argument.to_string(using_namespace(), false))); + + const auto nested_template_instantiation_full_name = + nested_template_instantiation->full_name(false); + + if (nested_template_instantiation && + diagram().should_include( + namespace_{nested_template_instantiation_full_name})) { + if (config_.generate_template_argument_dependencies()) { + if (diagram().should_include( + namespace_{template_decl->getQualifiedNameAsString()})) { + template_instantiation.add_relationship( + {relationship_t::kDependency, + nested_template_instantiation->id()}); + } + else { + if (parent.has_value()) + parent.value()->add_relationship( + {relationship_t::kDependency, + nested_template_instantiation->id()}); + } + } + } + + if (diagram().should_include( + namespace_{nested_template_instantiation_full_name})) { + visitor_.set_source_location(*cls, *nested_template_instantiation); + visitor_.add_diagram_element(std::move(nested_template_instantiation)); + } + + return argument; +} + +template +std::optional +template_builder::try_as_template_parm_type( + const clang::NamedDecl *cls, const clang::TemplateDecl * /*template_decl*/, + clang::QualType &type) +{ + auto is_variadic{false}; + + const auto *type_parameter = + common::dereference(type)->getAs(); + + auto type_name = common::to_string(type, &cls->getASTContext()); + + if (type_parameter == nullptr) { + if (const auto *pet = + common::dereference(type)->getAs(); + pet != nullptr) { + is_variadic = true; + type_parameter = + pet->getPattern()->getAs(); + } + } + + if (type_parameter == nullptr) + return {}; + + LOG_DBG("Template argument is a template parameter type"); + + auto argument = template_parameter::make_template_type(""); + type = consume_context(type, argument); + + auto type_parameter_name = common::to_string(type, cls->getASTContext()); + if (type_parameter_name.empty()) + type_parameter_name = "typename"; + + argument.set_name(map_type_parameter_to_template_parameter_name( + cls, type_parameter_name)); + + argument.is_variadic(is_variadic); + + common::ensure_lambda_type_is_relative(config(), type_parameter_name); + + return argument; +} + +template +std::optional template_builder::try_as_lambda( + const clang::NamedDecl *cls, const clang::TemplateDecl * /*template_decl*/, + clang::QualType &type) +{ + auto type_name = common::to_string(type, &cls->getASTContext()); + + if (type_name.find("(lambda at ") != 0) + return {}; + + LOG_DBG("Template argument is a lambda"); + + auto argument = template_parameter::make_argument(""); + type = consume_context(type, argument); + + common::ensure_lambda_type_is_relative(config(), type_name); + argument.set_type(type_name); + + return argument; +} + +template +std::optional +template_builder::try_as_record_type( + std::optional &parent, + const clang::NamedDecl * /*cls*/, const clang::TemplateDecl *template_decl, + clang::QualType &type, + clanguml::common::model::template_element &template_instantiation, + size_t /*argument_index*/) +{ + const auto *record_type = + common::dereference(type)->getAs(); + if (record_type == nullptr) + return {}; + + LOG_DBG("Template argument is a c++ record"); + + auto argument = template_parameter::make_argument({}); + type = consume_context(type, argument); + + const auto type_name = config().simplify_template_type( + common::to_string(type, template_decl->getASTContext())); + + argument.set_type(type_name); + const auto type_id = common::to_id(type_name); + + argument.set_id(type_id); + + const auto *class_template_specialization = + clang::dyn_cast( + record_type->getAsRecordDecl()); + + if (class_template_specialization != nullptr) { + auto tag_argument = + visitor_.create_element(class_template_specialization); + + build_from_class_template_specialization( + *tag_argument, *class_template_specialization); + + if (tag_argument) { + argument.set_type(tag_argument->name_and_ns()); + for (const auto &p : tag_argument->template_params()) + argument.add_template_param(p); + for (auto &r : tag_argument->relationships()) { + template_instantiation.add_relationship(std::move(r)); + } + + if (config_.generate_template_argument_dependencies() && + diagram().should_include(tag_argument->get_namespace())) { + if (parent.has_value()) + parent.value()->add_relationship( + {relationship_t::kDependency, tag_argument->id()}); + visitor_.set_source_location(*template_decl, *tag_argument); + visitor_.add_diagram_element(std::move(tag_argument)); + } + } + } + else if (const auto *record_type_decl = record_type->getAsRecordDecl(); + record_type_decl != nullptr) { + if (config_.generate_template_argument_dependencies() && + diagram().should_include(namespace_{type_name})) { + // Add dependency relationship to the parent + // template + template_instantiation.add_relationship( + {relationship_t::kDependency, type_id}); + } + } + + return argument; +} + +template +std::optional template_builder::try_as_enum_type( + std::optional & /*parent*/, + const clang::NamedDecl * /*cls*/, const clang::TemplateDecl *template_decl, + clang::QualType &type, + clanguml::common::model::template_element &template_instantiation) +{ + const auto *enum_type = type->getAs(); + if (enum_type == nullptr) + return {}; + + LOG_DBG("Template argument is a an enum"); + + auto argument = template_parameter::make_argument({}); + type = consume_context(type, argument); + + auto type_name = common::to_string(type, template_decl->getASTContext()); + argument.set_type(type_name); + const auto type_id = common::to_id(type_name); + argument.set_id(type_id); + + if (enum_type->getAsTagDecl() != nullptr && + config_.generate_template_argument_dependencies()) { + template_instantiation.add_relationship( + {relationship_t::kDependency, type_id}); + } + + return argument; +} + +template +std::optional +template_builder::try_as_builtin_type( + std::optional & /*parent*/, + clang::QualType &type, const clang::TemplateDecl *template_decl) +{ + const auto *builtin_type = type->getAs(); + if (builtin_type == nullptr) + return {}; + + LOG_DBG("Template argument is a builtin type"); + + auto type_name = common::to_string(type, template_decl->getASTContext()); + auto argument = template_parameter::make_argument(type_name); + + type = consume_context(type, argument); + argument.set_type(type_name); + + return argument; +} + +template +bool template_builder::add_base_classes( + clanguml::common::model::template_element &tinst, + std::deque> &template_base_params, + int arg_index, bool variadic_params, const template_parameter &ct) +{ + bool add_template_argument_as_base_class = false; + + if (variadic_params) { + add_template_argument_as_base_class = true; + } + else { + auto [arg_name, index, is_variadic] = template_base_params.front(); + + variadic_params = is_variadic; + if ((arg_index == index) || (is_variadic && arg_index >= index)) { + add_template_argument_as_base_class = true; + if (!is_variadic) { + // Don't remove the remaining variadic parameter + template_base_params.pop_front(); + } + } + } + + const auto maybe_id = ct.id(); + if (add_template_argument_as_base_class && maybe_id) { + LOG_DBG("Adding template argument as base class '{}'", + ct.to_string({}, false)); + + class_diagram::model::class_parent cp; + cp.set_access(common::model::access_t::kPublic); + cp.set_name(ct.to_string({}, false)); + cp.set_id(maybe_id.value()); + + dynamic_cast(tinst).add_parent( + std::move(cp)); + } + + return variadic_params; +} + } // namespace clanguml::common::visitor \ No newline at end of file diff --git a/src/common/visitor/translation_unit_visitor.cc b/src/common/visitor/translation_unit_visitor.cc index 4ac9973b..0e774dcc 100644 --- a/src/common/visitor/translation_unit_visitor.cc +++ b/src/common/visitor/translation_unit_visitor.cc @@ -16,174 +16,6 @@ * limitations under the License. */ -#include "translation_unit_visitor.h" - -#include "comment/clang_visitor.h" -#include "comment/plain_visitor.h" -#include "common/clang_utils.h" - -#include -#include - namespace clanguml::common::visitor { -translation_unit_visitor::translation_unit_visitor( - clang::SourceManager &sm, const clanguml::config::diagram &config) - : source_manager_{sm} - , relative_to_path_{config.root_directory()} -{ - if (config.comment_parser() == config::comment_parser_t::plain) { - comment_visitor_ = - std::make_unique(source_manager_); - } - else if (config.comment_parser() == config::comment_parser_t::clang) { - comment_visitor_ = - std::make_unique(source_manager_); - } -} - -void translation_unit_visitor::set_tu_path( - const std::string &translation_unit_path) -{ - translation_unit_path_ = relative( - std::filesystem::path{translation_unit_path}, relative_to_path_); - translation_unit_path_.make_preferred(); -} - -const std::filesystem::path &translation_unit_visitor::tu_path() const -{ - return translation_unit_path_; -} - -clang::SourceManager &translation_unit_visitor::source_manager() const -{ - return source_manager_; -} - -void translation_unit_visitor::process_comment( - const clang::NamedDecl &decl, clanguml::common::model::decorated_element &e) -{ - assert(comment_visitor_.get() != nullptr); - - comment_visitor_->visit(decl, e); - - const auto *comment = - decl.getASTContext().getRawCommentForDeclNoCache(&decl); - - process_comment(comment, decl.getASTContext().getDiagnostics(), e); -} - -void translation_unit_visitor::process_comment(const clang::RawComment *comment, - clang::DiagnosticsEngine &de, clanguml::common::model::decorated_element &e) -{ - if (comment != nullptr) { - auto [it, inserted] = processed_comments_.emplace(comment); - - if (!inserted) - return; - - // Process clang-uml decorators in the comments - // TODO: Refactor to use standard block comments processable by clang - // comments - e.add_decorators( - decorators::parse(comment->getFormattedText(source_manager_, de))); - } -} - -void translation_unit_visitor::set_source_location( - const clang::Decl &decl, clanguml::common::model::source_location &element) -{ - set_source_location(decl.getLocation(), element); -} - -void translation_unit_visitor::set_source_location( - const clang::Expr &expr, clanguml::common::model::source_location &element) -{ - set_source_location(expr.getBeginLoc(), element); -} - -void translation_unit_visitor::set_source_location( - const clang::Stmt &stmt, clanguml::common::model::source_location &element) -{ - set_source_location(stmt.getBeginLoc(), element); -} - -void translation_unit_visitor::set_source_location( - const clang::SourceLocation &location, - clanguml::common::model::source_location &element) -{ - namespace fs = std::filesystem; - - std::string file; - unsigned line{}; - unsigned column{}; - - if (location.isValid()) { - file = source_manager_.getFilename(location).str(); - line = source_manager_.getSpellingLineNumber(location); - column = source_manager_.getSpellingColumnNumber(location); - - if (file.empty()) { - // Why do I have to do this? - parse_source_location( - location.printToString(source_manager()), file, line, column); - } - } - else { - auto success = parse_source_location( - location.printToString(source_manager()), file, line, column); - if (!success) { - LOG_DBG("Failed to extract source location for element from {}", - location.printToString(source_manager_)); - return; - } - } - - // ensure the path is absolute - fs::path file_path{file}; - if (!file_path.is_absolute()) { - file_path = fs::absolute(file_path); - } - - file_path = fs::weakly_canonical(file_path); - - file = file_path.string(); - - element.set_file(file); - - if (util::is_relative_to(file_path, relative_to_path_)) { - element.set_file_relative(util::path_to_url( - fs::relative(element.file(), relative_to_path_).string())); - } - else { - element.set_file_relative(""); - } - - element.set_translation_unit(tu_path().string()); - element.set_line(line); - element.set_column(column); - element.set_location_id(location.getHashValue()); -} - -void translation_unit_visitor::set_owning_module( - const clang::Decl &decl, clanguml::common::model::element &element) -{ - if (const clang::Module *module = decl.getOwningModule(); - module != nullptr) { - std::string module_name = module->Name; - bool is_private{false}; -#if LLVM_VERSION_MAJOR < 15 - is_private = - module->Kind == clang::Module::ModuleKind::PrivateModuleFragment; -#else - is_private = module->isPrivateModule(); -#endif - if (is_private) { - // Clang just maps private modules names to "" - module_name = module->getTopLevelModule()->Name; - } - element.set_module(module_name); - element.set_module_private(is_private); - } -} } // namespace clanguml::common::visitor \ No newline at end of file diff --git a/src/common/visitor/translation_unit_visitor.h b/src/common/visitor/translation_unit_visitor.h index 71408ed5..6f271832 100644 --- a/src/common/visitor/translation_unit_visitor.h +++ b/src/common/visitor/translation_unit_visitor.h @@ -17,14 +17,21 @@ */ #pragma once +#include "comment/clang_visitor.h" #include "comment/comment_visitor.h" +#include "comment/plain_visitor.h" +#include "common/clang_utils.h" +#include "common/model/namespace.h" +#include "common/model/source_file.h" #include "common/model/source_location.h" #include "common/model/template_element.h" #include "common/visitor/ast_id_mapper.h" #include "config/config.h" #include +#include #include +#include #include #include @@ -44,8 +51,11 @@ using found_relationships_t = std::vector< * This class provides common interface for diagram translation unit * visitors. */ -class translation_unit_visitor { +template class translation_unit_visitor { public: + using config_t = ConfigT; + using diagram_t = DiagramT; + /** * @brief Constructor * @@ -54,17 +64,39 @@ public: * instance */ explicit translation_unit_visitor( - clang::SourceManager &sm, const clanguml::config::diagram &config); + clang::SourceManager &sm, DiagramT &diagram, const ConfigT &config) + : diagram_{diagram} + , config_{config} + , source_manager_{sm} + , relative_to_path_{config.root_directory()} + { + if (config.comment_parser() == config::comment_parser_t::plain) { + comment_visitor_ = + std::make_unique(source_manager_); + } + else if (config.comment_parser() == config::comment_parser_t::clang) { + comment_visitor_ = + std::make_unique(source_manager_); + } + } virtual ~translation_unit_visitor() = default; - void set_tu_path(const std::string &translation_unit_path); + void set_tu_path(const std::string &translation_unit_path) + { + translation_unit_path_ = relative( + std::filesystem::path{translation_unit_path}, relative_to_path_); + translation_unit_path_.make_preferred(); + } /** * @brief Return relative path to current translation unit * @return Current translation unit path */ - const std::filesystem::path &tu_path() const; + const std::filesystem::path &tu_path() const + { + return translation_unit_path_; + } /** * @brief Get reference to Clang AST to clang-uml id mapper @@ -78,7 +110,7 @@ public: * @return Reference to @ref clang::SourceManager used by this translation * unit visitor */ - clang::SourceManager &source_manager() const; + clang::SourceManager &source_manager() const { return source_manager_; } /** * @brief Set source location in diagram element @@ -87,7 +119,10 @@ public: * @param element Reference to element to be updated */ void set_source_location(const clang::Decl &decl, - clanguml::common::model::source_location &element); + clanguml::common::model::source_location &element) + { + set_source_location(decl.getLocation(), element); + } /** * @brief Set source location in diagram element @@ -96,10 +131,25 @@ public: * @param element Reference to element to be updated */ void set_source_location(const clang::Expr &expr, - clanguml::common::model::source_location &element); + clanguml::common::model::source_location &element) + { + set_source_location(expr.getBeginLoc(), element); + } void set_source_location(const clang::Stmt &stmt, - clanguml::common::model::source_location &element); + clanguml::common::model::source_location &element) + { + set_source_location(stmt.getBeginLoc(), element); + } + + void set_qualified_name( + const clang::NamedDecl &decl, clanguml::common::model::element &element) + { + common::model::namespace_ ns{decl.getQualifiedNameAsString()}; + element.set_name(ns.name()); + ns.pop_back(); + element.set_namespace(ns); + } /** * @brief Set source location in diagram element @@ -108,17 +158,88 @@ public: * @param element Reference to element to be updated */ void set_source_location(const clang::SourceLocation &location, - clanguml::common::model::source_location &element); + clanguml::common::model::source_location &element) + { + namespace fs = std::filesystem; + + std::string file; + unsigned line{}; + unsigned column{}; + + if (location.isValid()) { + file = source_manager_.getFilename(location).str(); + line = source_manager_.getSpellingLineNumber(location); + column = source_manager_.getSpellingColumnNumber(location); + + if (file.empty()) { + // Why do I have to do this? + parse_source_location(location.printToString(source_manager()), + file, line, column); + } + } + else { + auto success = parse_source_location( + location.printToString(source_manager()), file, line, column); + if (!success) { + LOG_DBG("Failed to extract source location for element from {}", + location.printToString(source_manager_)); + return; + } + } + + // ensure the path is absolute + fs::path file_path{file}; + if (!file_path.is_absolute()) { + file_path = fs::absolute(file_path); + } + + file_path = fs::weakly_canonical(file_path); + + file = file_path.string(); + + element.set_file(file); + + if (util::is_relative_to(file_path, relative_to_path_)) { + element.set_file_relative(util::path_to_url( + fs::relative(element.file(), relative_to_path_).string())); + } + else { + element.set_file_relative(""); + } + + element.set_translation_unit(tu_path().string()); + element.set_line(line); + element.set_column(column); + element.set_location_id(location.getHashValue()); + } void set_owning_module( - const clang::Decl &decl, clanguml::common::model::element &element); + const clang::Decl &decl, clanguml::common::model::element &element) + { + if (const clang::Module *module = decl.getOwningModule(); + module != nullptr) { + std::string module_name = module->Name; + bool is_private{false}; +#if LLVM_VERSION_MAJOR < 15 + is_private = module->Kind == + clang::Module::ModuleKind::PrivateModuleFragment; +#else + is_private = module->isPrivateModule(); +#endif + if (is_private) { + // Clang just maps private modules names to "" + module_name = module->getTopLevelModule()->Name; + } + element.set_module(module_name); + element.set_module_private(is_private); + } + } virtual void add_diagram_element( std::unique_ptr element) { } -protected: /** * @brief Process comment directives in comment attached to a declaration * @@ -126,7 +247,17 @@ protected: * @param element Reference to element to be updated */ void process_comment(const clang::NamedDecl &decl, - clanguml::common::model::decorated_element &e); + clanguml::common::model::decorated_element &e) + { + assert(comment_visitor_.get() != nullptr); + + comment_visitor_->visit(decl, e); + + const auto *comment = + decl.getASTContext().getRawCommentForDeclNoCache(&decl); + + process_comment(comment, decl.getASTContext().getDiagnostics(), e); + } /** * @brief Process comment directives in raw comment @@ -137,9 +268,77 @@ protected: */ void process_comment(const clang::RawComment *comment, clang::DiagnosticsEngine &de, - clanguml::common::model::decorated_element &e); + clanguml::common::model::decorated_element &e) + { + if (comment != nullptr) { + auto [it, inserted] = processed_comments_.emplace(comment); + + if (!inserted) + return; + + // Process clang-uml decorators in the comments + // TODO: Refactor to use standard block comments processable by + // clang comments + e.add_decorators(decorators::parse( + comment->getFormattedText(source_manager_, de))); + } + } + + /** + * @brief Check if the diagram should include a declaration. + * + * @param decl Clang declaration. + * @return True, if the entity should be included in the diagram. + */ + bool should_include(const clang::NamedDecl *decl) + { + if (decl == nullptr) + return false; + + if (source_manager().isInSystemHeader( + decl->getSourceRange().getBegin())) + return false; + + auto should_include_namespace = diagram().should_include( + common::model::namespace_{decl->getQualifiedNameAsString()}); + + const auto decl_file = + decl->getLocation().printToString(source_manager()); + + const auto should_include_decl_file = + diagram().should_include(common::model::source_file{decl_file}); + + return should_include_namespace && should_include_decl_file; + } + + /** + * @brief Get diagram model reference + * + * @return Reference to diagram model created by the visitor + */ + DiagramT &diagram() { return diagram_; } + + /** + * @brief Get diagram model reference + * + * @return Reference to diagram model created by the visitor + */ + const DiagramT &diagram() const { return diagram_; } + + /** + * @brief Get diagram config instance + * + * @return Reference to config instance + */ + const ConfigT &config() const { return config_; } private: + // Reference to the output diagram model + DiagramT &diagram_; + + // Reference to class diagram config + const ConfigT &config_; + clang::SourceManager &source_manager_; std::unique_ptr comment_visitor_; diff --git a/src/include_diagram/visitor/translation_unit_visitor.cc b/src/include_diagram/visitor/translation_unit_visitor.cc index 000c213b..c6158291 100644 --- a/src/include_diagram/visitor/translation_unit_visitor.cc +++ b/src/include_diagram/visitor/translation_unit_visitor.cc @@ -37,9 +37,7 @@ translation_unit_visitor::include_visitor::include_visitor( clang::SourceManager &sm, clanguml::include_diagram::model::diagram &diagram, const clanguml::config::include_diagram &config) - : common::visitor::translation_unit_visitor{sm, config} - , diagram_{diagram} - , config_{config} + : visitor_specialization_t{sm, diagram, config} { } @@ -112,18 +110,6 @@ void translation_unit_visitor::include_visitor::InclusionDirective( } } -clanguml::include_diagram::model::diagram & -translation_unit_visitor::include_visitor::diagram() -{ - return diagram_; -} - -const clanguml::config::include_diagram & -translation_unit_visitor::include_visitor::config() const -{ - return config_; -} - void translation_unit_visitor::include_visitor::process_internal_header( const std::filesystem::path &include_path, bool is_system, const common::id_t current_file_id) diff --git a/src/include_diagram/visitor/translation_unit_visitor.h b/src/include_diagram/visitor/translation_unit_visitor.h index d59f258b..872c9709 100644 --- a/src/include_diagram/visitor/translation_unit_visitor.h +++ b/src/include_diagram/visitor/translation_unit_visitor.h @@ -35,6 +35,10 @@ namespace clanguml::include_diagram::visitor { +using visitor_specialization_t = + common::visitor::translation_unit_visitor; + /** * @brief Include diagram translation unit visitor wrapper * @@ -53,7 +57,7 @@ public: * include_visitor type from translation_unit_visitor type */ class include_visitor : public clang::PPCallbacks, - public common::visitor::translation_unit_visitor { + public visitor_specialization_t { public: /** * @brief Constructor. @@ -123,27 +127,6 @@ public: */ std::optional process_source_file( const std::filesystem::path &file); - - /** - * @brief Get reference to the include diagram model - * - * @return Reference to the include diagram model - */ - clanguml::include_diagram::model::diagram &diagram(); - - /** - * @brief Get reference to the diagram configuration - * - * @return Reference to the diagram configuration - */ - const clanguml::config::include_diagram &config() const; - - private: - // Reference to the output diagram model - clanguml::include_diagram::model::diagram &diagram_; - - // Reference to class diagram config - const clanguml::config::include_diagram &config_; }; /** diff --git a/src/package_diagram/visitor/translation_unit_visitor.cc b/src/package_diagram/visitor/translation_unit_visitor.cc index 1794bee2..15ea040a 100644 --- a/src/package_diagram/visitor/translation_unit_visitor.cc +++ b/src/package_diagram/visitor/translation_unit_visitor.cc @@ -37,9 +37,7 @@ using clanguml::common::model::relationship_t; translation_unit_visitor::translation_unit_visitor(clang::SourceManager &sm, clanguml::package_diagram::model::diagram &diagram, const clanguml::config::package_diagram &config) - : common::visitor::translation_unit_visitor{sm, config} - , diagram_{diagram} - , config_{config} + : visitor_specialization_t{sm, diagram, config} { } @@ -676,17 +674,6 @@ bool translation_unit_visitor::find_relationships(const clang::QualType &type, return result; } -clanguml::package_diagram::model::diagram &translation_unit_visitor::diagram() -{ - return diagram_; -} - -const clanguml::config::package_diagram & -translation_unit_visitor::config() const -{ - return config_; -} - void translation_unit_visitor::finalize() { } std::vector translation_unit_visitor::get_parent_package_ids( diff --git a/src/package_diagram/visitor/translation_unit_visitor.h b/src/package_diagram/visitor/translation_unit_visitor.h index 11d8dc14..5aeb2657 100644 --- a/src/package_diagram/visitor/translation_unit_visitor.h +++ b/src/package_diagram/visitor/translation_unit_visitor.h @@ -36,6 +36,10 @@ namespace clanguml::package_diagram::visitor { using found_relationships_t = std::vector< std::pair>; +using visitor_specialization_t = + common::visitor::translation_unit_visitor; + /** * @brief Package diagram translation unit visitor * @@ -44,7 +48,7 @@ using found_relationships_t = std::vector< */ class translation_unit_visitor : public clang::RecursiveASTVisitor, - public common::visitor::translation_unit_visitor { + public visitor_specialization_t { public: /** * @brief Constructor. @@ -76,20 +80,6 @@ public: virtual bool VisitFunctionDecl(clang::FunctionDecl *function_declaration); /** @} */ - /** - * @brief Get diagram model reference - * - * @return Reference to diagram model created by the visitor - */ - clanguml::package_diagram::model::diagram &diagram(); - - /** - * @brief Get diagram model reference - * - * @return Reference to diagram model created by the visitor - */ - const clanguml::config::package_diagram &config() const; - /** * @brief Finalize diagram model */ @@ -214,11 +204,5 @@ private: clang::Decl *cls, found_relationships_t &relationships); std::vector get_parent_package_ids(common::id_t id); - - // Reference to the output diagram model - clanguml::package_diagram::model::diagram &diagram_; - - // Reference to class diagram config - const clanguml::config::package_diagram &config_; }; } // namespace clanguml::package_diagram::visitor diff --git a/src/sequence_diagram/visitor/translation_unit_visitor.cc b/src/sequence_diagram/visitor/translation_unit_visitor.cc index dffaa1f4..fe8f491b 100644 --- a/src/sequence_diagram/visitor/translation_unit_visitor.cc +++ b/src/sequence_diagram/visitor/translation_unit_visitor.cc @@ -27,16 +27,18 @@ namespace clanguml::sequence_diagram::visitor { translation_unit_visitor::translation_unit_visitor(clang::SourceManager &sm, clanguml::sequence_diagram::model::diagram &diagram, const clanguml::config::sequence_diagram &config) - : common::visitor::translation_unit_visitor{sm, config} - , diagram_{diagram} - , config_{config} - , template_builder_{diagram_, config_, *this, - [uns = config_.using_namespace()](const clang::NamedDecl * /*decl*/) { - return std::make_unique(uns); - }} + : visitor_specialization_t{sm, diagram, config} + , template_builder_{diagram, config, *this} { } +std::unique_ptr +translation_unit_visitor::create_element(const clang::NamedDecl *decl) const +{ + return std::make_unique( + config().using_namespace()); +} + bool translation_unit_visitor::shouldVisitTemplateInstantiations() { return true; @@ -52,23 +54,6 @@ const call_expression_context &translation_unit_visitor::context() const return call_expression_context_; } -clanguml::sequence_diagram::model::diagram &translation_unit_visitor::diagram() -{ - return diagram_; -} - -const clanguml::sequence_diagram::model::diagram & -translation_unit_visitor::diagram() const -{ - return diagram_; -} - -const clanguml::config::sequence_diagram & -translation_unit_visitor::config() const -{ - return config_; -} - bool translation_unit_visitor::VisitCXXRecordDecl( clang::CXXRecordDecl *declaration) { @@ -153,13 +138,13 @@ bool translation_unit_visitor::VisitClassTemplateDecl( if (!class_model_ptr) return true; - // Override the id with the template id, for now we don't care about the - // underlying templated class id - process_template_parameters(*declaration, *class_model_ptr); + tbuilder().build_from_template_declaration(*class_model_ptr, *declaration); const auto class_full_name = class_model_ptr->full_name(false); const auto id = common::to_id(class_full_name); + // Override the id with the template id, for now we don't care about the + // underlying templated class id class_model_ptr->set_id(id); set_unique_id(declaration->getID(), id); @@ -198,7 +183,7 @@ bool translation_unit_visitor::VisitClassTemplateSpecializationDecl( return true; auto template_specialization_ptr = - process_template_specialization(declaration); + process_class_template_specialization(declaration); if (!template_specialization_ptr) return true; @@ -1486,8 +1471,9 @@ translation_unit_visitor::create_class_model(clang::CXXRecordDecl *cls) return {}; auto parent_class = - diagram_.get_participant( - *id_opt); + diagram() + .get_participant( + *id_opt); assert(parent_class); @@ -1555,84 +1541,6 @@ translation_unit_visitor::create_class_model(clang::CXXRecordDecl *cls) return c_ptr; } -bool translation_unit_visitor::process_template_parameters( - const clang::TemplateDecl &template_declaration, - sequence_diagram::model::template_trait &c) -{ - using common::model::template_parameter; - - LOG_TRACE("Processing class {} template parameters...", - common::get_qualified_name(template_declaration)); - - if (template_declaration.getTemplateParameters() == nullptr) - return false; - - for (const auto *parameter : - *template_declaration.getTemplateParameters()) { - if (clang::dyn_cast_or_null(parameter) != - nullptr) { - const auto *template_type_parameter = - clang::dyn_cast_or_null(parameter); - std::optional default_arg; - if (template_type_parameter->hasDefaultArgument()) { - default_arg = - template_type_parameter->getDefaultArgument().getAsString(); - } - auto ct = template_parameter::make_template_type( - template_type_parameter->getNameAsString(), default_arg, - template_type_parameter->isParameterPack()); - - c.add_template(std::move(ct)); - } - else if (clang::dyn_cast_or_null( - parameter) != nullptr) { - const auto *template_nontype_parameter = - clang::dyn_cast_or_null( - parameter); - std::optional default_arg; - if (template_nontype_parameter->hasDefaultArgument()) - default_arg = common::to_string( - template_nontype_parameter->getDefaultArgument()); - auto ct = template_parameter::make_non_type_template( - template_nontype_parameter->getType().getAsString(), - template_nontype_parameter->getNameAsString(), default_arg); - - c.add_template(std::move(ct)); - } - else if (clang::dyn_cast_or_null( - parameter) != nullptr) { - const auto *template_template_parameter = - clang::dyn_cast_or_null( - parameter); - std::optional default_arg; - if (template_template_parameter->hasDefaultArgument()) { - const auto &def_arg = - template_template_parameter->getDefaultArgument() - .getArgument(); - if (def_arg.getKind() == - clang::TemplateArgument::ArgKind::Expression) { - default_arg = common::to_string(def_arg.getAsExpr()); - } - else if (def_arg.getKind() == - clang::TemplateArgument::ArgKind::Declaration) { - default_arg = common::to_string(def_arg.getAsDecl()); - } - } - - auto ct = template_parameter::make_template_template_type( - template_template_parameter->getNameAsString(), default_arg, - template_template_parameter->isParameterPack()); - - c.add_template(std::move(ct)); - } - else { - // pass - } - } - - return false; -} - void translation_unit_visitor::set_unique_id( int64_t local_id, common::id_t global_id) { @@ -1658,12 +1566,10 @@ translation_unit_visitor::build_function_template( std::make_unique( config().using_namespace()); - common::model::namespace_ ns{declaration.getQualifiedNameAsString()}; - function_template_model_ptr->set_name(ns.name()); - ns.pop_back(); - function_template_model_ptr->set_namespace(ns); + set_qualified_name(declaration, *function_template_model_ptr); - process_template_parameters(declaration, *function_template_model_ptr); + tbuilder().build_from_template_declaration( + *function_template_model_ptr, declaration); function_template_model_ptr->return_type( common::to_string(declaration.getAsFunction()->getReturnType(), @@ -1682,35 +1588,17 @@ std::unique_ptr translation_unit_visitor::build_function_template_instantiation( const clang::FunctionDecl &decl) { - // - // Here we'll hold the template base params to replace with the - // instantiated values - // - std::deque> - template_base_params{}; - auto template_instantiation_ptr = - std::make_unique(config_.using_namespace()); + std::make_unique(config().using_namespace()); auto &template_instantiation = *template_instantiation_ptr; - // - // Set function template instantiation name - // - auto template_decl_qualified_name = decl.getQualifiedNameAsString(); - common::model::namespace_ ns{template_decl_qualified_name}; - ns.pop_back(); - template_instantiation.set_name(decl.getNameAsString()); - template_instantiation.set_namespace(ns); + set_qualified_name(decl, template_instantiation); - // - // Instantiate the template arguments - // - model::template_trait *parent{nullptr}; - build_template_instantiation_process_template_arguments(parent, - template_base_params, decl.getTemplateSpecializationArgs()->asArray(), - template_instantiation, "", decl.getPrimaryTemplate()); + tbuilder().build(template_instantiation, &decl, decl.getPrimaryTemplate(), + decl.getTemplateSpecializationArgs()->asArray(), + common::to_string(&decl)); + // Handle function parameters for (const auto *param : decl.parameters()) { template_instantiation_ptr->add_parameter( common::to_string(param->getType(), decl.getASTContext())); @@ -1743,150 +1631,11 @@ std::unique_ptr translation_unit_visitor::build_function_model( return function_model_ptr; } -void translation_unit_visitor:: - build_template_instantiation_process_template_arguments( - model::template_trait *parent, - std::deque> - & /*template_base_params*/, - const clang::ArrayRef &template_args, - model::template_trait &template_instantiation, - const std::string &full_template_specialization_name, - const clang::TemplateDecl *template_decl) -{ - for (const auto &arg : template_args) { - const auto argument_kind = arg.getKind(); - std::optional argument; - if (argument_kind == clang::TemplateArgument::Template) { - argument = - build_template_instantiation_process_template_argument(arg); - } - else if (argument_kind == clang::TemplateArgument::Type) { - argument = build_template_instantiation_process_type_argument( - parent, full_template_specialization_name, template_decl, arg, - template_instantiation); - } - else if (argument_kind == clang::TemplateArgument::Integral) { - argument = - build_template_instantiation_process_integral_argument(arg); - } - else if (argument_kind == clang::TemplateArgument::Expression) { - argument = - build_template_instantiation_process_expression_argument(arg); - } - else { - LOG_INFO("Unsupported argument type {}", arg.getKind()); - continue; - } - - simplify_system_template(argument.value(), - argument.value().to_string(config().using_namespace(), false)); - - template_instantiation.add_template(std::move(argument.value())); - } -} - -template_parameter translation_unit_visitor:: - build_template_instantiation_process_template_argument( - const clang::TemplateArgument &arg) const -{ - auto arg_name = - arg.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString(); - return template_parameter::make_template_type(arg_name); -} - -template_parameter translation_unit_visitor:: - build_template_instantiation_process_integral_argument( - const clang::TemplateArgument &arg) const -{ - assert(arg.getKind() == clang::TemplateArgument::Integral); - - return template_parameter::make_argument( - std::to_string(arg.getAsIntegral().getExtValue())); -} - -template_parameter translation_unit_visitor:: - build_template_instantiation_process_expression_argument( - const clang::TemplateArgument &arg) const -{ - assert(arg.getKind() == clang::TemplateArgument::Expression); - return template_parameter::make_argument(common::get_source_text( - arg.getAsExpr()->getSourceRange(), source_manager())); -} - -void translation_unit_visitor:: - build_template_instantiation_process_tag_argument( - model::template_trait & /*template_instantiation*/, - const std::string & /*full_template_specialization_name*/, - const clang::TemplateDecl *template_decl, - const clang::TemplateArgument &arg, - common::model::template_parameter &argument) const -{ - assert(arg.getKind() == clang::TemplateArgument::Type); - - argument.is_template_parameter(false); - argument.set_type( - common::to_string(arg.getAsType(), template_decl->getASTContext())); -} - -common::model::template_parameter -translation_unit_visitor::build_template_instantiation_process_type_argument( - model::template_trait * /*parent*/, - const std::string &full_template_specialization_name, - const clang::TemplateDecl *template_decl, - const clang::TemplateArgument &arg, - model::template_trait &template_instantiation) -{ - assert(arg.getKind() == clang::TemplateArgument::Type); - - std::optional argument; - - // If this is a nested template type - add nested templates as - // template arguments - if (const auto *function_template_type = - arg.getAsType()->getAs(); - function_template_type != nullptr) { - // TODO - argument = template_parameter::make_argument( - common::to_string(function_template_type->getReturnType(), - template_decl->getASTContext())); - } - else if (const auto *nested_template_type = - arg.getAsType()->getAs(); - nested_template_type != nullptr) { - - const auto nested_template_name = - nested_template_type->getTemplateName() - .getAsTemplateDecl() - ->getQualifiedNameAsString(); - - argument = template_parameter::make_argument(nested_template_name); - - // Check if this template should be simplified (e.g. system - // template aliases such as 'std:basic_string' should - // be simply 'std::string') - simplify_system_template(*argument, - argument.value().to_string(config().using_namespace(), false)); - } - else if (arg.getAsType()->getAs() != nullptr) { - argument = template_parameter::make_template_type( - common::to_string(arg.getAsType(), template_decl->getASTContext())); - } - else { - argument = template_parameter::make_argument({}); - // This is just a regular record type - build_template_instantiation_process_tag_argument( - template_instantiation, full_template_specialization_name, - template_decl, arg, *argument); - } - - return *argument; -} - std::unique_ptr -translation_unit_visitor::process_template_specialization( +translation_unit_visitor::process_class_template_specialization( clang::ClassTemplateSpecializationDecl *cls) { - auto c_ptr{std::make_unique(config_.using_namespace())}; + auto c_ptr{std::make_unique(config().using_namespace())}; tbuilder().build_from_class_template_specialization(*c_ptr, *cls); @@ -1919,386 +1668,6 @@ translation_unit_visitor::process_template_specialization( return c_ptr; } -void translation_unit_visitor::process_template_specialization_argument( - const clang::ClassTemplateSpecializationDecl *cls, - model::class_ &template_instantiation, const clang::TemplateArgument &arg, - size_t argument_index, bool /*in_parameter_pack*/) -{ - const auto argument_kind = arg.getKind(); - - if (argument_kind == clang::TemplateArgument::Type) { - auto argument = template_parameter::make_argument({}); - argument.is_template_parameter(false); - - // If this is a nested template type - add nested templates as - // template arguments - if (const auto *nested_template_type = - arg.getAsType()->getAs(); - nested_template_type != nullptr) { - - const auto nested_template_name = - nested_template_type->getTemplateName() - .getAsTemplateDecl() - ->getQualifiedNameAsString(); - - argument.set_type(nested_template_name); - - auto nested_template_instantiation = build_template_instantiation( - *nested_template_type, &template_instantiation); - - argument.set_id(nested_template_instantiation->id()); - - for (const auto &t : - nested_template_instantiation->template_params()) - argument.add_template_param(t); - - // Check if this template should be simplified (e.g. system - // template aliases such as 'std:basic_string' should be - // simply 'std::string') - simplify_system_template(argument, - argument.to_string(config().using_namespace(), false)); - } - else if (arg.getAsType()->getAs() != - nullptr) { - auto type_name = - common::to_string(arg.getAsType(), cls->getASTContext()); - - // clang does not provide declared template parameter/argument - // names in template specializations - so we have to extract - // them from raw source code... - if (type_name.find("type-parameter-") == 0) { - auto declaration_text = common::get_source_text_raw( - cls->getSourceRange(), source_manager()); - - declaration_text = declaration_text.substr( - declaration_text.find(cls->getNameAsString()) + - cls->getNameAsString().size() + 1); - - auto template_params = common::parse_unexposed_template_params( - declaration_text, [](const auto &t) { return t; }); - - if (template_params.size() > argument_index) - type_name = template_params[argument_index].to_string( - config().using_namespace(), false); - else { - LOG_DBG("Failed to find type specialization for argument " - "{} at index {} in declaration \n===\n{}\n===\n", - type_name, argument_index, declaration_text); - } - } - - argument.set_type(type_name); - } - else if ((arg.getAsType()->getAsCXXRecordDecl() != nullptr) && - arg.getAsType()->getAsCXXRecordDecl()->isLambda()) { - const auto maybe_id = - get_unique_id(arg.getAsType()->getAsCXXRecordDecl()->getID()); - if (maybe_id.has_value()) { - argument.set_type( - get_participant(maybe_id.value()).value().full_name(false)); - } - else { - const auto type_name = - make_lambda_name(arg.getAsType()->getAsCXXRecordDecl()); - argument.set_type(type_name); - } - } - else { - auto type_name = - common::to_string(arg.getAsType(), cls->getASTContext()); - if (type_name.find('<') != std::string::npos) { - // Sometimes template instantiation is reported as - // RecordType in the AST and getAs to - // TemplateSpecializationType returns null pointer so we - // have to at least make sure it's properly formatted - // (e.g. std:integral_constant, or any template - // specialization which contains it - see t00038) - process_unexposed_template_specialization_parameters( - type_name.substr(type_name.find('<') + 1, - type_name.size() - (type_name.find('<') + 2)), - argument, template_instantiation); - - argument.set_type(type_name.substr(0, type_name.find('<'))); - } - else if (type_name.find("type-parameter-") == 0) { - auto declaration_text = common::get_source_text_raw( - cls->getSourceRange(), source_manager()); - - declaration_text = declaration_text.substr( - declaration_text.find(cls->getNameAsString()) + - cls->getNameAsString().size() + 1); - - auto template_params = common::parse_unexposed_template_params( - declaration_text, [](const auto &t) { return t; }); - - if (template_params.size() > argument_index) - type_name = template_params[argument_index].to_string( - config().using_namespace(), false); - else { - LOG_DBG("Failed to find type specialization for argument " - "{} at index {} in declaration \n===\n{}\n===\n", - type_name, argument_index, declaration_text); - } - - // Otherwise just set the name for the template argument to - // whatever clang says - if (template_params.size() > argument_index && - template_params[argument_index].type()) - argument.set_type(type_name); - else - argument.set_name(type_name); - } - else - argument.set_type(type_name); - } - - simplify_system_template( - argument, argument.to_string(config().using_namespace(), false)); - - LOG_TRACE("Adding template instantiation argument {}", - argument.to_string(config().using_namespace(), false)); - - template_instantiation.add_template(std::move(argument)); - } - else if (argument_kind == clang::TemplateArgument::Integral) { - auto argument = template_parameter::make_argument( - std::to_string(arg.getAsIntegral().getExtValue())); - template_instantiation.add_template(std::move(argument)); - } - else if (argument_kind == clang::TemplateArgument::Expression) { - auto argument = - template_parameter::make_argument(common::get_source_text( - arg.getAsExpr()->getSourceRange(), source_manager())); - template_instantiation.add_template(std::move(argument)); - } - else if (argument_kind == clang::TemplateArgument::TemplateExpansion) { - // TODO - } - else if (argument_kind == clang::TemplateArgument::Pack) { - // This will only work for now if pack is at the end - size_t argument_pack_index{argument_index}; - for (const auto &template_argument : arg.getPackAsArray()) { - process_template_specialization_argument(cls, - template_instantiation, template_argument, - argument_pack_index++, true); - } - } - else { - LOG_INFO("Unsupported template argument kind {} [{}]", arg.getKind(), - cls->getLocation().printToString(source_manager())); - } -} - -std::unique_ptr -translation_unit_visitor::build_template_instantiation( - const clang::TemplateSpecializationType &template_type_decl, - model::class_ *parent) -{ - // TODO: Make sure we only build instantiation once - - // - // Here we'll hold the template base params to replace with the - // instantiated values - // - std::deque> - template_base_params{}; - - const auto *template_type_ptr = &template_type_decl; - if (template_type_decl.isTypeAlias() && - (template_type_decl.getAliasedType() - ->getAs() != nullptr)) - template_type_ptr = template_type_decl.getAliasedType() - ->getAs(); - - const auto &template_type = *template_type_ptr; - - // - // Create class_ instance to hold the template instantiation - // - auto template_instantiation_ptr = - std::make_unique(config_.using_namespace()); - auto &template_instantiation = *template_instantiation_ptr; - std::string full_template_specialization_name = common::to_string( - template_type.desugar(), - template_type.getTemplateName().getAsTemplateDecl()->getASTContext()); - - auto *template_decl{template_type.getTemplateName().getAsTemplateDecl()}; - - auto template_decl_qualified_name = - template_decl->getQualifiedNameAsString(); - - auto *class_template_decl{ - clang::dyn_cast(template_decl)}; - - if ((class_template_decl != nullptr) && - (class_template_decl->getTemplatedDecl() != nullptr) && - (class_template_decl->getTemplatedDecl()->getParent() != nullptr) && - class_template_decl->getTemplatedDecl()->getParent()->isRecord()) { - - common::model::namespace_ ns{ - common::get_tag_namespace(*class_template_decl->getTemplatedDecl() - ->getParent() - ->getOuterLexicalRecordContext())}; - - std::string ns_str = ns.to_string(); - std::string name = template_decl->getQualifiedNameAsString(); - if (!ns_str.empty()) { - name = name.substr(ns_str.size() + 2); - } - - util::replace_all(name, "::", "##"); - template_instantiation.set_name(name); - - template_instantiation.set_namespace(ns); - } - else { - common::model::namespace_ ns{template_decl_qualified_name}; - ns.pop_back(); - template_instantiation.set_name(template_decl->getNameAsString()); - template_instantiation.set_namespace(ns); - } - - // TODO: Refactor handling of base parameters to a separate method - - // We need this to match any possible base classes coming from template - // arguments - std::vector< - std::pair> - template_parameter_names{}; - - for (const auto *parameter : *template_decl->getTemplateParameters()) { - if (parameter->isTemplateParameter() && - (parameter->isTemplateParameterPack() || - parameter->isParameterPack())) { - template_parameter_names.emplace_back( - parameter->getNameAsString(), true); - } - else - template_parameter_names.emplace_back( - parameter->getNameAsString(), false); - } - - // Check if the primary template has any base classes - int base_index = 0; - - const auto *templated_class_decl = - clang::dyn_cast_or_null( - template_decl->getTemplatedDecl()); - - if ((templated_class_decl != nullptr) && - templated_class_decl->hasDefinition()) - for (const auto &base : templated_class_decl->bases()) { - const auto base_class_name = common::to_string( - base.getType(), templated_class_decl->getASTContext(), false); - - LOG_DBG("Found template instantiation base: {}, {}", - base_class_name, base_index); - - // Check if any of the primary template arguments has a - // parameter equal to this type - auto it = std::find_if(template_parameter_names.begin(), - template_parameter_names.end(), - [&base_class_name]( - const auto &p) { return p.first == base_class_name; }); - - if (it != template_parameter_names.end()) { - const auto ¶meter_name = it->first; - const bool is_variadic = it->second; - // Found base class which is a template parameter - LOG_DBG("Found base class which is a template parameter " - "{}, {}, {}", - parameter_name, is_variadic, - std::distance(template_parameter_names.begin(), it)); - - template_base_params.emplace_back(parameter_name, - std::distance(template_parameter_names.begin(), it), - is_variadic); - } - else { - // This is a regular base class - it is handled by - // process_template - } - base_index++; - } - - build_template_instantiation_process_template_arguments(parent, - template_base_params, template_type.template_arguments(), - template_instantiation, full_template_specialization_name, - template_decl); - - // First try to find the best match for this template in partially - // specialized templates - std::string destination{}; - std::string best_match_full_name{}; - auto full_template_name = template_instantiation.full_name(false); - int best_match{}; - common::id_t best_match_id{0}; - - for (const auto &[id, c] : diagram().participants()) { - const auto *participant_as_class = - dynamic_cast(c.get()); - if (participant_as_class == nullptr) - continue; - - if ((participant_as_class != nullptr) && - (*participant_as_class == template_instantiation)) - continue; - - auto c_full_name = participant_as_class->full_name(false); - auto match = - template_instantiation.calculate_template_specialization_match( - *participant_as_class); - - if (match > best_match) { - best_match = match; - best_match_full_name = c_full_name; - best_match_id = participant_as_class->id(); - } - } - - auto templated_decl_id = - template_type.getTemplateName().getAsTemplateDecl()->getID(); - - if (best_match_id > 0) { - destination = best_match_full_name; - } - else { - LOG_DBG("Cannot determine global id for specialization template {} " - "- delaying until the translation unit is complete ", - templated_decl_id); - } - - template_instantiation.set_id( - common::to_id(template_instantiation_ptr->full_name(false))); - - return template_instantiation_ptr; -} - -void translation_unit_visitor:: - process_unexposed_template_specialization_parameters( - const std::string &type_name, common::model::template_parameter &tp, - model::class_ & /*c*/) const -{ - auto template_params = common::parse_unexposed_template_params( - type_name, [](const std::string &t) { return t; }); - - for (auto ¶m : template_params) { - tp.add_template_param(param); - } -} - -bool translation_unit_visitor::simplify_system_template( - common::model::template_parameter &ct, const std::string &full_name) const -{ - if (config().type_aliases().count(full_name) > 0) { - ct.set_type(config().type_aliases().at(full_name)); - ct.clear_params(); - return true; - } - return false; -} - std::string translation_unit_visitor::simplify_system_template( const std::string &full_name) const { diff --git a/src/sequence_diagram/visitor/translation_unit_visitor.h b/src/sequence_diagram/visitor/translation_unit_visitor.h index 109cf779..11e4aae3 100644 --- a/src/sequence_diagram/visitor/translation_unit_visitor.h +++ b/src/sequence_diagram/visitor/translation_unit_visitor.h @@ -36,6 +36,10 @@ using common::model::template_parameter; std::string to_string(const clang::FunctionTemplateDecl *decl); +using visitor_specialization_t = common::visitor::translation_unit_visitor< + clanguml::config::sequence_diagram, + clanguml::sequence_diagram::model::diagram>; + /** * @brief Sequence diagram translation unit visitor * @@ -44,8 +48,14 @@ std::string to_string(const clang::FunctionTemplateDecl *decl); */ class translation_unit_visitor : public clang::RecursiveASTVisitor, - public common::visitor::translation_unit_visitor { + public visitor_specialization_t { public: + using visitor_specialization_t::config_t; + using visitor_specialization_t::diagram_t; + + using template_builder_t = + common::visitor::template_builder; + /** * @brief Constructor. * @@ -124,27 +134,6 @@ public: bool TraverseConditionalOperator(clang::ConditionalOperator *stmt); /** @} */ - /** - * @brief Get diagram model reference - * - * @return Reference to diagram model created by the visitor - */ - clanguml::sequence_diagram::model::diagram &diagram(); - - /** - * @brief Get diagram model reference - * - * @return Reference to diagram model created by the visitor - */ - const clanguml::sequence_diagram::model::diagram &diagram() const; - - /** - * @brief Get diagram config instance - * - * @return Reference to config instance - */ - const clanguml::config::sequence_diagram &config() const; - /** * @brief Get current call expression context reference * @@ -255,6 +244,9 @@ public: */ void finalize(); + std::unique_ptr create_element( + const clang::NamedDecl *decl) const; + private: /** * @brief Check if the diagram should include a declaration. @@ -312,21 +304,12 @@ private: */ bool should_include(const clang::ClassTemplateDecl *decl) const; - /** - * @todo #227 Refactor this group of methods to @ref template_builder - * - * @{ - */ std::unique_ptr create_class_model(clang::CXXRecordDecl *cls); std::unique_ptr create_method_model(clang::CXXMethodDecl *cls); - bool process_template_parameters( - const clang::TemplateDecl &template_declaration, - common::model::template_trait &c); - std::unique_ptr build_function_template_instantiation(const clang::FunctionDecl &pDecl); @@ -336,63 +319,10 @@ private: std::unique_ptr build_function_template( const clang::FunctionTemplateDecl &declaration); - void build_template_instantiation_process_template_arguments( - model::template_trait *parent, - std::deque> &template_base_params, - const clang::ArrayRef &template_args, - model::template_trait &template_instantiation, - const std::string &full_template_specialization_name, - const clang::TemplateDecl *template_decl); - - common::model::template_parameter - build_template_instantiation_process_template_argument( - const clang::TemplateArgument &arg) const; - - common::model::template_parameter - build_template_instantiation_process_integral_argument( - const clang::TemplateArgument &arg) const; - - common::model::template_parameter - build_template_instantiation_process_expression_argument( - const clang::TemplateArgument &arg) const; - - void build_template_instantiation_process_tag_argument( - model::template_trait &template_instantiation, - const std::string &full_template_specialization_name, - const clang::TemplateDecl *template_decl, - const clang::TemplateArgument &arg, - common::model::template_parameter &argument) const; - - common::model::template_parameter - build_template_instantiation_process_type_argument( - model::template_trait *parent, - const std::string &full_template_specialization_name, - const clang::TemplateDecl *template_decl, - const clang::TemplateArgument &arg, - model::template_trait &template_instantiation); - - std::unique_ptr process_template_specialization( + std::unique_ptr process_class_template_specialization( clang::ClassTemplateSpecializationDecl *cls); - void process_template_specialization_argument( - const clang::ClassTemplateSpecializationDecl *cls, - model::class_ &template_instantiation, - const clang::TemplateArgument &arg, size_t argument_index, - bool in_parameter_pack = false); - - void process_unexposed_template_specialization_parameters( - const std::string &type_name, common::model::template_parameter &tp, - model::class_ &c) const; - - std::unique_ptr build_template_instantiation( - const clang::TemplateSpecializationType &template_type_decl, - model::class_ *parent); - - bool simplify_system_template(common::model::template_parameter &ct, - const std::string &full_name) const; - std::string simplify_system_template(const std::string &full_name) const; - /** }@ */ /** * @brief Assuming `cls` is a lambda, create it's full name. @@ -516,13 +446,7 @@ private: * * @return Reference to 'template_builder' instance */ - common::visitor::template_builder &tbuilder() { return template_builder_; } - - // Reference to the output diagram model - clanguml::sequence_diagram::model::diagram &diagram_; - - // Reference to class diagram config - const clanguml::config::sequence_diagram &config_; + template_builder_t &tbuilder() { return template_builder_; } call_expression_context call_expression_context_; @@ -558,6 +482,6 @@ private: mutable std::set> processed_comments_; - common::visitor::template_builder template_builder_; + template_builder_t template_builder_; }; } // namespace clanguml::sequence_diagram::visitor diff --git a/tests/t20040/.clang-uml b/tests/t20040/.clang-uml new file mode 100644 index 00000000..0e2bcb05 --- /dev/null +++ b/tests/t20040/.clang-uml @@ -0,0 +1,11 @@ +diagrams: + t20040_sequence: + type: sequence + glob: + - t20040.cc + include: + namespaces: + - clanguml::t20040 + using_namespace: clanguml::t20040 + from: + - function: "clanguml::t20040::tmain()" \ No newline at end of file diff --git a/tests/t20040/t20040.cc b/tests/t20040/t20040.cc new file mode 100644 index 00000000..58d452ae --- /dev/null +++ b/tests/t20040/t20040.cc @@ -0,0 +1,32 @@ +#include +#include + +namespace clanguml { +namespace t20040 { +void print() { } + +template void print(T head, Ts... tail) +{ + std::cout << head << std::endl; + print(tail...); +} + +template void doublePrint(Ts... args) +{ + print(args + args...); +} + +void tmain() +{ + using namespace std::literals::string_literals; + + print(1, 3.14, "test"s); + + doublePrint("test"s, 2024 / 2); + + // TODO: Add separate test case for variadic class template sequence diagram + + // TODO: Add overload pattern test case +} +} +} \ No newline at end of file diff --git a/tests/t20040/test_case.h b/tests/t20040/test_case.h new file mode 100644 index 00000000..ffb4998b --- /dev/null +++ b/tests/t20040/test_case.h @@ -0,0 +1,62 @@ +/** + * tests/t20040/test_case.h + * + * Copyright (c) 2021-2024 Bartek Kryza + * + * 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. + */ + +TEST_CASE("t20040", "[test-case][sequence]") +{ + auto [config, db] = load_config("t20040"); + + auto diagram = config.diagrams["t20040_sequence"]; + + REQUIRE(diagram->name == "t20040_sequence"); + + auto model = generate_sequence_diagram(*db, diagram); + + REQUIRE(model->name() == "t20040_sequence"); + + { + auto src = generate_sequence_puml(diagram, *model); + AliasMatcher _A(src); + + REQUIRE_THAT(src, StartsWith("@startuml")); + REQUIRE_THAT(src, EndsWith("@enduml\n")); + + REQUIRE_THAT(src, + HasCall(_A("tmain()"), + _A("print(int,double,std::string)"), + "")); + + save_puml(config.output_directory(), diagram->name + ".puml", src); + } + + { + auto j = generate_sequence_json(diagram, *model); + + using namespace json; + + save_json(config.output_directory(), diagram->name + ".json", j); + } + + { + auto src = generate_sequence_mermaid(diagram, *model); + + mermaid::AliasMatcher _A(src); + using mermaid::IsClass; + + save_mermaid(config.output_directory(), diagram->name + ".mmd", src); + } +} \ No newline at end of file diff --git a/tests/test_cases.cc b/tests/test_cases.cc index 93a02baa..d74a9283 100644 --- a/tests/test_cases.cc +++ b/tests/test_cases.cc @@ -461,6 +461,7 @@ using namespace clanguml::test::matchers; #include "t20037/test_case.h" #include "t20038/test_case.h" #include "t20039/test_case.h" +#include "t20040/test_case.h" /// /// Package diagram tests