Skip to content

Commit

Permalink
Fixed type aliases handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bkryza committed Jan 25, 2024
1 parent 9376b85 commit 757b4d0
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 30 deletions.
7 changes: 2 additions & 5 deletions src/class_diagram/visitor/translation_unit_visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1069,8 +1069,6 @@ void translation_unit_visitor::process_class_bases(

cp.set_name(name_and_ns.to_string());

base.getType().dump();

if (const auto *tsp =
base.getType()->getAs<clang::TemplateSpecializationType>();
tsp != nullptr) {
Expand Down Expand Up @@ -2227,9 +2225,8 @@ void translation_unit_visitor::find_instantiation_relationships(
common::model::template_element &template_instantiation_base,
const std::string &full_name, common::id_t templated_decl_id)
{
class_diagram::model::class_ &template_instantiation =
dynamic_cast<class_diagram::model::class_ &>(
template_instantiation_base);
auto &template_instantiation = dynamic_cast<class_diagram::model::class_ &>(
template_instantiation_base);

// First try to find the best match for this template in partially
// specialized templates
Expand Down
6 changes: 2 additions & 4 deletions src/common/clang_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -932,14 +932,12 @@ bool is_struct(const clang::NamedDecl *decl)
if (decl == nullptr)
return false;

if (const clang::CXXRecordDecl *record =
clang::dyn_cast<clang::CXXRecordDecl>(decl);
if (const auto *record = clang::dyn_cast<clang::CXXRecordDecl>(decl);
record) {
return record->isStruct();
}

if (const clang::TagDecl *tag = clang::dyn_cast<clang::TagDecl>(decl);
tag) {
if (const auto *tag = clang::dyn_cast<clang::TagDecl>(decl); tag) {
return tag->isStruct();
}

Expand Down
4 changes: 2 additions & 2 deletions src/common/model/template_element.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class template_element : public element, public template_trait {
public:
using element::element;

virtual ~template_element() = default;
~template_element() override = default;

/**
* Whether or not the class is a template.
Expand Down Expand Up @@ -76,4 +76,4 @@ class template_element : public element, public template_trait {
bool is_template_{false};
};

}
} // namespace clanguml::common::model
3 changes: 1 addition & 2 deletions src/common/visitor/template_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

namespace clanguml::common::visitor {

// using class_diagram::model::class_;
using common::model::namespace_;
using common::model::relationship_t;
using common::model::template_parameter;
Expand Down Expand Up @@ -540,4 +539,4 @@ class template_builder {
on_argument_base_found_t on_argument_base_found_;
};

} // namespace clanguml::class_diagram::visitor
} // namespace clanguml::common::visitor
11 changes: 9 additions & 2 deletions src/config/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,15 @@ std::string inheritable_diagram_options::simplify_template_type(
type_aliases_longer_first_t aliases;
aliases.insert(type_aliases().begin(), type_aliases().end());

for (const auto &[pattern, replacement] : aliases) {
util::replace_all(full_name, pattern, replacement);
bool matched{true};
while (matched) {
auto matched_in_iteration{false};
for (const auto &[pattern, replacement] : aliases) {
matched_in_iteration =
util::replace_all(full_name, pattern, replacement) ||
matched_in_iteration;
}
matched = matched_in_iteration;
}

return full_name;
Expand Down
12 changes: 11 additions & 1 deletion src/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,18 @@ struct relationship_hint_t {
using relationship_hints_t = std::map<std::string, relationship_hint_t>;

using type_aliases_t = std::map<std::string, std::string>;

struct type_aliases_longer_first_comparator {
bool operator()(const std::string &a, const std::string &b) const
{
if (a.size() == b.size())
return a > b;

return a.size() > b.size();
}
};
using type_aliases_longer_first_t =
std::map<std::string, std::string, std::greater<>>;
std::map<std::string, std::string, type_aliases_longer_first_comparator>;

enum class location_t { marker, fileline, function };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,13 @@ void generator::generate_participant(

print_debug(class_participant, ostr);

auto participant_name =
config().using_namespace().relative(config().simplify_template_type(
class_participant.full_name(false)));
common::ensure_lambda_type_is_relative(config(), participant_name);

ostr << indent(1) << "participant " << class_participant.alias()
<< " as "
<< render_participant_name(config().using_namespace().relative(
class_participant.full_name(false)));
<< " as " << render_participant_name(participant_name);

ostr << '\n';

Expand Down Expand Up @@ -418,9 +421,12 @@ void generator::generate_participant(
else {
print_debug(participant, ostr);

auto participant_name = config().using_namespace().relative(
config().simplify_template_type(participant.full_name(false)));
common::ensure_lambda_type_is_relative(config(), participant_name);

ostr << indent(1) << "participant " << participant.alias() << " as "
<< config().using_namespace().relative(
participant.full_name(false));
<< render_participant_name(participant_name);
ostr << '\n';

generated_participants_.emplace(participant_id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,13 @@ void generator::generate_participant(

print_debug(class_participant, ostr);

ostr << "participant \""
<< render_name(config().using_namespace().relative(
class_participant.full_name(false)))
<< "\" as " << class_participant.alias();
auto participant_name =
config().using_namespace().relative(config().simplify_template_type(
class_participant.full_name(false)));
common::ensure_lambda_type_is_relative(config(), participant_name);

ostr << "participant \"" << render_name(participant_name) << "\" as "
<< class_participant.alias();

if (config().generate_links) {
common_generator<diagram_config, diagram_model>::generate_link(
Expand Down Expand Up @@ -416,10 +419,12 @@ void generator::generate_participant(
else {
print_debug(participant, ostr);

ostr << "participant \""
<< config().using_namespace().relative(
participant.full_name(false))
<< "\" as " << participant.alias();
auto participant_name = config().using_namespace().relative(
config().simplify_template_type(participant.full_name(false)));
common::ensure_lambda_type_is_relative(config(), participant_name);

ostr << "participant \"" << render_name(participant_name) << "\" as "
<< participant.alias();

if (config().generate_links) {
common_generator<diagram_config, diagram_model>::generate_link(
Expand Down
2 changes: 1 addition & 1 deletion src/util/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ template <typename T, typename S>
std::unique_ptr<T> unique_pointer_cast(std::unique_ptr<S> &&p) noexcept
{
if (T *const converted = dynamic_cast<T *>(p.get())) {
p.release();
p.release(); // NOLINT
return std::unique_ptr<T>{converted};
}

Expand Down

0 comments on commit 757b4d0

Please sign in to comment.