Renamed common::id_t to eid_t to avoid conflicts with system id_t

This commit is contained in:
Bartek Kryza
2024-06-05 21:19:46 +02:00
parent 2cf9d2f22a
commit 420475ab64
57 changed files with 370 additions and 356 deletions

View File

@@ -415,52 +415,52 @@ bool is_subexpr_of(const clang::Stmt *parent_stmt, const clang::Stmt *sub_stmt)
[sub_stmt](const auto *e) { return is_subexpr_of(e, sub_stmt); });
}
template <> common::id_t to_id(const std::string &full_name)
template <> eid_t to_id(const std::string &full_name)
{
return static_cast<common::id_t>(std::hash<std::string>{}(full_name));
return static_cast<eid_t>(std::hash<std::string>{}(full_name));
}
common::id_t to_id(const clang::QualType &type, const clang::ASTContext &ctx)
eid_t to_id(const clang::QualType &type, const clang::ASTContext &ctx)
{
return to_id(common::to_string(type, ctx));
}
template <> common::id_t to_id(const clang::NamespaceDecl &declaration)
template <> eid_t to_id(const clang::NamespaceDecl &declaration)
{
return to_id(get_qualified_name(declaration));
}
template <> common::id_t to_id(const clang::RecordDecl &declaration)
template <> eid_t to_id(const clang::RecordDecl &declaration)
{
return to_id(get_qualified_name(declaration));
}
template <> common::id_t to_id(const clang::EnumDecl &declaration)
template <> eid_t to_id(const clang::EnumDecl &declaration)
{
return to_id(get_qualified_name(declaration));
}
template <> common::id_t to_id(const clang::TagDecl &declaration)
template <> eid_t to_id(const clang::TagDecl &declaration)
{
return to_id(get_qualified_name(declaration));
}
template <> common::id_t to_id(const clang::CXXRecordDecl &declaration)
template <> eid_t to_id(const clang::CXXRecordDecl &declaration)
{
return to_id(get_qualified_name(declaration));
}
template <> common::id_t to_id(const clang::EnumType &t)
template <> eid_t to_id(const clang::EnumType &t)
{
return to_id(*t.getDecl());
}
template <> common::id_t to_id(const std::filesystem::path &file)
template <> eid_t to_id(const std::filesystem::path &file)
{
return to_id(file.lexically_normal().string());
}
template <> common::id_t to_id(const clang::TemplateArgument &template_argument)
template <> eid_t to_id(const clang::TemplateArgument &template_argument)
{
if (template_argument.getKind() == clang::TemplateArgument::Type) {
if (const auto *enum_type =

View File

@@ -178,27 +178,27 @@ bool is_subexpr_of(const clang::Stmt *parent_stmt, const clang::Stmt *sub_stmt);
*
* @{
*/
template <typename T> common::id_t to_id(const T &declaration);
template <typename T> eid_t to_id(const T &declaration);
template <> common::id_t to_id(const std::string &full_name);
template <> eid_t to_id(const std::string &full_name);
common::id_t to_id(const clang::QualType &type, const clang::ASTContext &ctx);
eid_t to_id(const clang::QualType &type, const clang::ASTContext &ctx);
template <> common::id_t to_id(const clang::NamespaceDecl &declaration);
template <> eid_t to_id(const clang::NamespaceDecl &declaration);
template <> common::id_t to_id(const clang::CXXRecordDecl &declaration);
template <> eid_t to_id(const clang::CXXRecordDecl &declaration);
template <> common::id_t to_id(const clang::RecordDecl &declaration);
template <> eid_t to_id(const clang::RecordDecl &declaration);
template <> common::id_t to_id(const clang::EnumDecl &declaration);
template <> eid_t to_id(const clang::EnumDecl &declaration);
template <> common::id_t to_id(const clang::TagDecl &declaration);
template <> eid_t to_id(const clang::TagDecl &declaration);
template <> common::id_t to_id(const clang::EnumType &type);
template <> eid_t to_id(const clang::EnumType &type);
template <> common::id_t to_id(const clang::TemplateSpecializationType &type);
template <> eid_t to_id(const clang::TemplateSpecializationType &type);
template <> common::id_t to_id(const std::filesystem::path &type);
template <> eid_t to_id(const std::filesystem::path &type);
/** @} */ // end of to_id
/**

View File

@@ -70,7 +70,7 @@ public:
* @return Optional reference to a diagram element.
*/
virtual common::optional_ref<clanguml::common::model::diagram_element> get(
common::id_t id) const = 0;
eid_t id) const = 0;
/**
* Return optional reference to a diagram_element by name and namespace.
@@ -152,7 +152,7 @@ public:
// Disallow std::string overload
bool should_include(const std::string &s) const = delete;
virtual bool has_element(const common::id_t /*id*/) const { return false; }
virtual bool has_element(const eid_t /*id*/) const { return false; }
virtual bool should_include(
const namespace_ &ns, const std::string &name) const;

View File

@@ -26,16 +26,16 @@ namespace clanguml::common::model {
diagram_element::diagram_element() = default;
const common::id_t &diagram_element::id() const { return id_; }
const eid_t &diagram_element::id() const { return id_; }
void diagram_element::set_id(common::id_t id) { id_ = id; }
void diagram_element::set_id(eid_t id) { id_ = id; }
std::optional<id_t> diagram_element::parent_element_id() const
std::optional<eid_t> diagram_element::parent_element_id() const
{
return parent_element_id_;
}
void diagram_element::set_parent_element_id(common::id_t id)
void diagram_element::set_parent_element_id(eid_t id)
{
parent_element_id_ = id;
}

View File

@@ -53,28 +53,28 @@ public:
*
* @return Elements id.
*/
const common::id_t &id() const;
const eid_t &id() const;
/**
* Set elements id.
*
* @param id Elements id.
*/
void set_id(common::id_t id);
void set_id(eid_t id);
/**
* Get elements parent package id.
*
* @return Parent package id if element is nested.
*/
std::optional<id_t> parent_element_id() const;
std::optional<eid_t> parent_element_id() const;
/**
* Set elements parent package id.
*
* @param id Id of parent package.
*/
void set_parent_element_id(id_t id);
void set_parent_element_id(eid_t id);
/**
* @brief Return elements' diagram alias.
@@ -185,8 +185,8 @@ public:
void complete(bool completed);
private:
id_t id_{};
std::optional<common::id_t> parent_element_id_{};
eid_t id_{};
std::optional<eid_t> parent_element_id_{};
std::string name_;
std::vector<relationship> relationships_;
bool nested_{false};

View File

@@ -81,7 +81,7 @@ const clanguml::common::optional_ref<common::model::source_file> get(
}
template <>
clanguml::common::id_t destination_comparator<common::model::source_file>(
eid_t destination_comparator<common::model::source_file>(
const common::model::source_file &f)
{
return f.id();
@@ -690,7 +690,7 @@ void context_filter::initialize_effective_context(
// Now repeat radius times - extend the effective context with elements
// matching in direct relationship to what is in context
auto radius_counter = context.radius;
std::set<clanguml::common::id_t> current_iteration_context;
std::set<eid_t> current_iteration_context;
while (radius_counter > 0 && effective_context_extended) {
// If at any iteration the effective context was not extended - we
@@ -725,8 +725,8 @@ void context_filter::initialize_effective_context(
}
void context_filter::find_elements_inheritance_relationship(const diagram &d,
std::set<id_t> &effective_context,
std::set<clanguml::common::id_t> &current_iteration_context) const
std::set<eid_t> &effective_context,
std::set<eid_t> &current_iteration_context) const
{
const auto &cd = dynamic_cast<const class_diagram::model::diagram &>(d);

View File

@@ -37,6 +37,8 @@
namespace clanguml::common::model {
using clanguml::common::eid_t;
/**
* Diagram filters can be add in 2 modes:
* - inclusive - the elements that match are included in the diagram
@@ -55,15 +57,12 @@ template <typename ElementT, typename DiagramT>
const clanguml::common::optional_ref<ElementT> get(
const DiagramT &d, const std::string &full_name);
template <typename ElementT>
common::id_t destination_comparator(const ElementT &e)
template <typename ElementT> eid_t destination_comparator(const ElementT &e)
{
return e.id();
}
template <>
clanguml::common::id_t destination_comparator(
const common::model::source_file &f);
template <> eid_t destination_comparator(const common::model::source_file &f);
} // namespace detail
/**
@@ -493,8 +492,8 @@ private:
template <typename ElementT>
void find_elements_in_direct_relationship(const diagram &d,
std::set<id_t> &effective_context,
std::set<clanguml::common::id_t> &current_iteration_context) const
std::set<eid_t> &effective_context,
std::set<eid_t> &current_iteration_context) const
{
static_assert(std::is_same_v<ElementT, class_diagram::model::class_> ||
std::is_same_v<ElementT, class_diagram::model::enum_> ||
@@ -535,8 +534,8 @@ private:
}
void find_elements_inheritance_relationship(const diagram &d,
std::set<id_t> &effective_context,
std::set<clanguml::common::id_t> &current_iteration_context) const;
std::set<eid_t> &effective_context,
std::set<eid_t> &current_iteration_context) const;
std::vector<config::context_config> context_;
@@ -544,7 +543,7 @@ private:
* Represents all elements which should belong to the diagram based
* on this filter. It is populated by the initialize() method.
*/
mutable std::vector<std::set<clanguml::common::id_t>> effective_contexts_;
mutable std::vector<std::set<eid_t>> effective_contexts_;
/*! Flag to mark whether the filter context has been computed */
mutable bool initialized_{false};

View File

@@ -21,6 +21,8 @@
namespace clanguml::common::model {
using clanguml::common::eid_t;
/**
* Provides type based views over elements in a diagram.
*
@@ -58,7 +60,7 @@ public:
*
* @return
*/
common::optional_ref<T> get(clanguml::common::id_t id) const
common::optional_ref<T> get(eid_t id) const
{
for (const auto &e : elements_) {
if (e.get().id() == id) {

View File

@@ -89,9 +89,9 @@ struct hash<std::reference_wrapper<clanguml::common::model::package>> {
const std::reference_wrapper<clanguml::common::model::package> &key)
const
{
using clanguml::common::id_t;
using clanguml::common::eid_t;
return std::hash<id_t::type>{}(key.get().id().value());
return key.get().id().value();
}
};
} // namespace std

View File

@@ -22,7 +22,7 @@
namespace clanguml::common::model {
relationship::relationship(relationship_t type, common::id_t destination,
relationship::relationship(relationship_t type, eid_t destination,
access_t access, std::string label, std::string multiplicity_source,
std::string multiplicity_destination)
: type_{type}
@@ -38,15 +38,12 @@ void relationship::set_type(relationship_t type) noexcept { type_ = type; }
relationship_t relationship::type() const noexcept { return type_; }
void relationship::set_destination(common::id_t destination)
void relationship::set_destination(eid_t destination)
{
destination_ = destination;
}
clanguml::common::id_t relationship::destination() const
{
return destination_;
}
eid_t relationship::destination() const { return destination_; }
void relationship::set_multiplicity_source(
const std::string &multiplicity_source)

View File

@@ -25,6 +25,8 @@
namespace clanguml::common::model {
using clanguml::common::eid_t;
/**
* @brief Class representing any relationship other than inheritance
*
@@ -47,7 +49,7 @@ public:
* @param multiplicity_source Multiplicity at the source
* @param multiplicity_destination Multiplicity at the destination
*/
relationship(relationship_t type, clanguml::common::id_t destination,
relationship(relationship_t type, eid_t destination,
access_t access = access_t::kPublic, std::string label = "",
std::string multiplicity_source = "",
std::string multiplicity_destination = "");
@@ -74,14 +76,14 @@ public:
*
* @param destination Target element id.
*/
void set_destination(common::id_t destination);
void set_destination(eid_t destination);
/**
* Get the id of the target element of this relationship.
*
* @return Target element id.
*/
common::id_t destination() const;
eid_t destination() const;
/**
* Set the relationship multiplicity at the source.
@@ -144,7 +146,7 @@ public:
private:
relationship_t type_;
clanguml::common::id_t destination_;
eid_t destination_;
std::string multiplicity_source_;
std::string multiplicity_destination_;
std::string label_;

View File

@@ -210,9 +210,7 @@ struct hash<std::reference_wrapper<clanguml::common::model::source_file>> {
const std::reference_wrapper<clanguml::common::model::source_file> &key)
const
{
using clanguml::common::id_t;
return std::hash<id_t::type>{}(key.get().id().value());
return key.get().id().value();
}
};
} // namespace std

View File

@@ -505,7 +505,7 @@ std::string template_parameter::to_string(
}
bool template_parameter::find_nested_relationships(
std::vector<std::pair<common::id_t, common::model::relationship_t>>
std::vector<std::pair<eid_t, common::model::relationship_t>>
&nested_relationships,
common::model::relationship_t hint,
const std::function<bool(const std::string &full_name)> &should_include)

View File

@@ -29,6 +29,8 @@
namespace clanguml::common::model {
using clanguml::common::eid_t;
/**
* Type of template parameter or argument.
*/
@@ -166,14 +168,14 @@ public:
*
* @param id Id of parameter
*/
void set_id(const common::id_t &id) { id_ = id; }
void set_id(const eid_t &id) { id_ = id; }
/**
* Get id of the template parameter
*
* @return Id of the template parameter
*/
const std::optional<common::id_t> &id() const { return id_; }
const std::optional<eid_t> &id() const { return id_; }
/**
* Set the name of the template parameter
@@ -389,7 +391,7 @@ public:
* @return
*/
bool find_nested_relationships(
std::vector<std::pair<common::id_t, common::model::relationship_t>>
std::vector<std::pair<eid_t, common::model::relationship_t>>
&nested_relationships,
common::model::relationship_t hint,
const std::function<bool(const std::string &full_name)> &should_include)
@@ -533,7 +535,7 @@ private:
*/
std::vector<template_parameter> template_params_;
std::optional<common::id_t> id_;
std::optional<eid_t> id_;
bool is_unexposed_{false};
};

View File

@@ -20,25 +20,25 @@
namespace clanguml::common {
id_t::id_t()
eid_t::eid_t()
: value_{0ULL}
, is_global_{true}
{
}
id_t::id_t(int64_t id)
eid_t::eid_t(int64_t id)
: value_{static_cast<type>(id)}
, is_global_{false}
{
}
id_t::id_t(type id)
eid_t::eid_t(type id)
: value_{id}
, is_global_{true}
{
}
id_t &id_t::operator=(int64_t ast_id)
eid_t &eid_t::operator=(int64_t ast_id)
{
// Can't assign ast_id if the id is already a global one
assert(!is_global_);
@@ -47,16 +47,16 @@ id_t &id_t::operator=(int64_t ast_id)
return *this;
}
bool id_t::is_global() const { return is_global_; }
bool eid_t::is_global() const { return is_global_; }
bool operator==(const id_t &lhs, const id_t &rhs)
bool operator==(const eid_t &lhs, const eid_t &rhs)
{
return (lhs.is_global_ == rhs.is_global_) && (lhs.value_ == rhs.value_);
}
bool operator==(const id_t &lhs, const uint64_t &v) { return lhs.value_ == v; }
bool operator==(const eid_t &lhs, const uint64_t &v) { return lhs.value_ == v; }
bool operator!=(const id_t &lhs, const uint64_t &v)
bool operator!=(const eid_t &lhs, const uint64_t &v)
{
// This is sadly necessary to catch accidental comparisons to empty
// std::optional<id_t>:
@@ -69,9 +69,9 @@ bool operator!=(const id_t &lhs, const uint64_t &v)
return lhs.value_ != v;
}
bool operator!=(const id_t &lhs, const id_t &rhs) { return !(lhs == rhs); }
bool operator!=(const eid_t &lhs, const eid_t &rhs) { return !(lhs == rhs); }
bool operator<(const id_t &lhs, const id_t &rhs)
bool operator<(const eid_t &lhs, const eid_t &rhs)
{
if (lhs.is_global_ != rhs.is_global_) {
return lhs.value_ < rhs.value_ + 1;
@@ -80,9 +80,9 @@ bool operator<(const id_t &lhs, const id_t &rhs)
return lhs.value_ < rhs.value_; // Compare values if is_global_ are the same
}
id_t::type id_t::value() const { return value_; }
eid_t::type eid_t::value() const { return value_; }
int64_t id_t::ast_local_value() const
int64_t eid_t::ast_local_value() const
{
assert(!is_global_);

View File

@@ -29,32 +29,47 @@
namespace clanguml::common {
class id_t {
/**
* @brief Universal class for representing all kinds of Id's in the diagram
* model.
*
* This class provides a convenient way of representing id's in the diagram
* model. The main problem it solves is that it allows to store both
* Clang AST ID's (obtained using getID() method), which are local to a single
* translation unit and have a type int64_t as well as global (across
* multiple translation units) id's used by clang-uml in the intermediate
* model (which are uint64_t).
*
* The class is aware which kind of value it holds and tries to ensure that
* mistakes such as using AST local ID in place where global id is needed
* do not happen.
*/
class eid_t {
public:
using type = uint64_t;
id_t();
eid_t();
explicit id_t(int64_t id);
explicit eid_t(int64_t id);
explicit id_t(type id);
explicit eid_t(type id);
id_t(const id_t &) = default;
id_t(id_t &&) noexcept = default;
id_t &operator=(const id_t &) = default;
id_t &operator=(id_t &&) noexcept = default;
eid_t(const eid_t &) = default;
eid_t(eid_t &&) noexcept = default;
eid_t &operator=(const eid_t &) = default;
eid_t &operator=(eid_t &&) noexcept = default;
id_t &operator=(int64_t ast_id);
eid_t &operator=(int64_t ast_id);
~id_t() = default;
~eid_t() = default;
bool is_global() const;
friend bool operator==(const id_t &lhs, const id_t &rhs);
friend bool operator==(const id_t &lhs, const uint64_t &v);
friend bool operator!=(const id_t &lhs, const uint64_t &v);
friend bool operator!=(const id_t &lhs, const id_t &rhs);
friend bool operator<(const id_t &lhs, const id_t &rhs);
friend bool operator==(const eid_t &lhs, const eid_t &rhs);
friend bool operator==(const eid_t &lhs, const uint64_t &v);
friend bool operator!=(const eid_t &lhs, const uint64_t &v);
friend bool operator!=(const eid_t &lhs, const eid_t &rhs);
friend bool operator<(const eid_t &lhs, const eid_t &rhs);
type value() const;
@@ -328,11 +343,11 @@ struct path_or_regex : public or_regex<std::filesystem::path> { };
} // namespace clanguml::common
template <> class fmt::formatter<clanguml::common::id_t> {
template <> class fmt::formatter<clanguml::common::eid_t> {
public:
constexpr auto parse(format_parse_context &ctx) { return ctx.begin(); }
template <typename Context>
constexpr auto format(clanguml::common::id_t const &id, Context &ctx) const
constexpr auto format(clanguml::common::eid_t const &id, Context &ctx) const
{
return format_to(ctx.out(), "{}", id.value());
}

View File

@@ -20,12 +20,12 @@
namespace clanguml::common::visitor {
void ast_id_mapper::add(int64_t ast_id, id_t global_id)
void ast_id_mapper::add(int64_t ast_id, eid_t global_id)
{
id_map_.emplace(ast_id, global_id);
}
std::optional<id_t> ast_id_mapper::get_global_id(common::id_t ast_id)
std::optional<eid_t> ast_id_mapper::get_global_id(eid_t ast_id)
{
assert(!ast_id.is_global());

View File

@@ -24,6 +24,8 @@
namespace clanguml::common::visitor {
using clanguml::common::eid_t;
/**
* @brief Mapping between Clang AST identifier and `clang-uml` unique ids
*
@@ -40,8 +42,6 @@ namespace clanguml::common::visitor {
*/
class ast_id_mapper {
public:
using id_t = common::id_t;
ast_id_mapper() = default;
/**
@@ -50,7 +50,7 @@ public:
* @param ast_id Clang's local AST id.
* @param global_id Global element id.
*/
void add(int64_t ast_id, id_t global_id);
void add(int64_t ast_id, eid_t global_id);
/**
* Get global element id based on it's local Clang AST id, if exists.
@@ -58,11 +58,11 @@ public:
* @param ast_id Clang's local AST id.
* @return Global id, if exists.
*/
std::optional<id_t> get_global_id(common::id_t ast_id);
std::optional<eid_t> get_global_id(eid_t ast_id);
private:
std::map</* Clang AST translation unit local id */ int64_t,
/* clang-uml global id */ id_t>
/* clang-uml global id */ eid_t>
id_map_;
};

View File

@@ -30,8 +30,8 @@ using common::model::namespace_;
using common::model::relationship_t;
using common::model::template_parameter;
using found_relationships_t = std::vector<
std::pair<clanguml::common::id_t, common::model::relationship_t>>;
using found_relationships_t =
std::vector<std::pair<eid_t, common::model::relationship_t>>;
namespace detail {
@@ -524,8 +524,8 @@ public:
const template_parameter &ct, found_relationships_t &relationships);
void find_instantiation_relationships(
common::model::template_element &template_instantiation,
common::id_t id, const std::string &qualified_name) const;
common::model::template_element &template_instantiation, eid_t id,
const std::string &qualified_name) const;
/**
* @brief Get reference to Clang AST to clang-uml id mapper
@@ -661,8 +661,8 @@ void template_builder<VisitorT>::build_from_template_declaration(
templated_element.value().add_relationship(
{relationship_t::kConstraint,
id_mapper()
.get_global_id(common::id_t{
named_concept->getID()})
.get_global_id(
eid_t{named_concept->getID()})
.value(),
model::access_t::kNone,
ct.name().value()});
@@ -865,8 +865,7 @@ void template_builder<VisitorT>::build(
if constexpr (std::is_same_v<typename VisitorT::diagram_t,
class_diagram::model::diagram>) {
find_instantiation_relationships(template_instantiation,
common::id_t{template_decl->getID()},
full_template_specialization_name);
eid_t{template_decl->getID()}, full_template_specialization_name);
}
template_instantiation.set_id(
@@ -911,7 +910,7 @@ void template_builder<VisitorT>::build_from_class_template_specialization(
if constexpr (std::is_same_v<typename VisitorT::diagram_t,
class_diagram::model::diagram>) {
find_instantiation_relationships(template_instantiation,
common::id_t{template_specialization.getID()}, qualified_name);
eid_t{template_specialization.getID()}, qualified_name);
}
visitor_.set_source_location(*template_decl, template_instantiation);
@@ -919,7 +918,7 @@ void template_builder<VisitorT>::build_from_class_template_specialization(
template <typename VisitorT>
void template_builder<VisitorT>::find_instantiation_relationships(
common::model::template_element &template_instantiation, common::id_t id,
common::model::template_element &template_instantiation, eid_t id,
const std::string &qualified_name) const
{
visitor_.find_instantiation_relationships(

View File

@@ -42,8 +42,8 @@
namespace clanguml::common::visitor {
using found_relationships_t = std::vector<
std::pair<clanguml::common::id_t, common::model::relationship_t>>;
using found_relationships_t =
std::vector<std::pair<eid_t, common::model::relationship_t>>;
/**
* @brief Diagram translation unit visitor base class