Merge pull request #148 from bkryza/add-packages-from-directory-structure
Add packages from directory structure
This commit is contained in:
@@ -23,14 +23,13 @@
|
||||
|
||||
namespace clanguml::class_diagram::visitor {
|
||||
|
||||
template_builder::template_builder(class_diagram::model::diagram &d,
|
||||
const config::class_diagram &config,
|
||||
common::visitor::ast_id_mapper &id_mapper,
|
||||
clang::SourceManager &source_manager)
|
||||
: diagram_{d}
|
||||
, config_{config}
|
||||
, id_mapper_{id_mapper}
|
||||
, source_manager_{source_manager}
|
||||
template_builder::template_builder(
|
||||
clanguml::class_diagram::visitor::translation_unit_visitor &visitor)
|
||||
: diagram_{visitor.diagram()}
|
||||
, config_{visitor.config()}
|
||||
, id_mapper_{visitor.id_mapper()}
|
||||
, source_manager_{visitor.source_manager()}
|
||||
, visitor_{visitor}
|
||||
{
|
||||
}
|
||||
|
||||
@@ -268,6 +267,8 @@ std::unique_ptr<class_> template_builder::build(const clang::NamedDecl *cls,
|
||||
template_instantiation.set_id(
|
||||
common::to_id(template_instantiation_ptr->full_name(false)));
|
||||
|
||||
visitor_.set_source_location(*template_decl, *template_instantiation_ptr);
|
||||
|
||||
return template_instantiation_ptr;
|
||||
}
|
||||
|
||||
@@ -936,9 +937,9 @@ std::optional<template_parameter> template_builder::try_as_decl_type(
|
||||
|
||||
std::optional<template_parameter> template_builder::try_as_typedef_type(
|
||||
std::optional<clanguml::class_diagram::model::class_ *> &parent,
|
||||
const clang::NamedDecl *cls, const clang::TemplateDecl *template_decl,
|
||||
clang::QualType &type, class_ &template_instantiation,
|
||||
size_t argument_index)
|
||||
const clang::NamedDecl * /*cls*/,
|
||||
const clang::TemplateDecl * /*template_decl*/, clang::QualType &type,
|
||||
class_ & /*template_instantiation*/, size_t /*argument_index*/)
|
||||
{
|
||||
const auto *typedef_type =
|
||||
common::dereference(type)->getAs<clang::TypedefType>();
|
||||
@@ -1045,7 +1046,9 @@ template_builder::try_as_template_specialization_type(
|
||||
}
|
||||
|
||||
if (diagram().should_include(nested_template_instantiation_full_name)) {
|
||||
diagram().add_class(std::move(nested_template_instantiation));
|
||||
visitor_.set_source_location(
|
||||
*template_decl, *nested_template_instantiation);
|
||||
visitor_.add_class(std::move(nested_template_instantiation));
|
||||
}
|
||||
|
||||
return argument;
|
||||
@@ -1089,7 +1092,7 @@ std::optional<template_parameter> template_builder::try_as_template_parm_type(
|
||||
|
||||
argument.is_variadic(is_variadic);
|
||||
|
||||
ensure_lambda_type_is_relative(type_parameter_name);
|
||||
visitor_.ensure_lambda_type_is_relative(type_parameter_name);
|
||||
|
||||
return argument;
|
||||
}
|
||||
@@ -1108,7 +1111,7 @@ std::optional<template_parameter> template_builder::try_as_lambda(
|
||||
auto argument = template_parameter::make_argument("");
|
||||
type = consume_context(type, argument);
|
||||
|
||||
ensure_lambda_type_is_relative(type_name);
|
||||
visitor_.ensure_lambda_type_is_relative(type_name);
|
||||
argument.set_type(type_name);
|
||||
|
||||
return argument;
|
||||
@@ -1156,8 +1159,8 @@ std::optional<template_parameter> template_builder::try_as_record_type(
|
||||
if (parent.has_value())
|
||||
parent.value()->add_relationship(
|
||||
{relationship_t::kDependency, tag_argument->id()});
|
||||
|
||||
diagram().add_class(std::move(tag_argument));
|
||||
visitor_.set_source_location(*template_decl, *tag_argument);
|
||||
visitor_.add_class(std::move(tag_argument));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1256,40 +1259,4 @@ bool template_builder::add_base_classes(class_ &tinst,
|
||||
return variadic_params;
|
||||
}
|
||||
|
||||
void template_builder::ensure_lambda_type_is_relative(
|
||||
std::string ¶meter_type) const
|
||||
{
|
||||
#ifdef _MSC_VER
|
||||
auto root_name = fmt::format(
|
||||
"{}\\", std::filesystem::current_path().root_name().string());
|
||||
if (root_name.back() == '\\') {
|
||||
root_name.pop_back();
|
||||
root_name.push_back('/');
|
||||
}
|
||||
#else
|
||||
auto root_name = std::string{"/"};
|
||||
#endif
|
||||
|
||||
std::string lambda_prefix{fmt::format("(lambda at {}", root_name)};
|
||||
|
||||
while (parameter_type.find(lambda_prefix) != std::string::npos) {
|
||||
auto lambda_begin = parameter_type.find(lambda_prefix);
|
||||
|
||||
auto absolute_lambda_path_end =
|
||||
parameter_type.find(':', lambda_begin + lambda_prefix.size());
|
||||
auto absolute_lambda_path =
|
||||
parameter_type.substr(lambda_begin + lambda_prefix.size() - 1,
|
||||
absolute_lambda_path_end -
|
||||
(lambda_begin + lambda_prefix.size() - 1));
|
||||
|
||||
auto relative_lambda_path = util::path_to_url(std::filesystem::relative(
|
||||
absolute_lambda_path, config().relative_to())
|
||||
.string());
|
||||
|
||||
parameter_type = fmt::format("{}(lambda at {}{}",
|
||||
parameter_type.substr(0, lambda_begin), relative_lambda_path,
|
||||
parameter_type.substr(absolute_lambda_path_end));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace clanguml::class_diagram::visitor
|
||||
|
||||
@@ -34,12 +34,12 @@ using found_relationships_t =
|
||||
std::vector<std::pair<clanguml::common::model::diagram_element::id_t,
|
||||
common::model::relationship_t>>;
|
||||
|
||||
class translation_unit_visitor;
|
||||
|
||||
class template_builder {
|
||||
public:
|
||||
template_builder(class_diagram::model::diagram &d,
|
||||
const config::class_diagram &config,
|
||||
common::visitor::ast_id_mapper &id_mapper,
|
||||
clang::SourceManager &source_manager);
|
||||
template_builder(
|
||||
clanguml::class_diagram::visitor::translation_unit_visitor &visitor);
|
||||
|
||||
class_diagram::model::diagram &diagram();
|
||||
|
||||
@@ -181,8 +181,6 @@ public:
|
||||
clang::SourceManager &source_manager() const;
|
||||
|
||||
private:
|
||||
void ensure_lambda_type_is_relative(std::string ¶meter_type) const;
|
||||
|
||||
// Reference to the output diagram model
|
||||
clanguml::class_diagram::model::diagram &diagram_;
|
||||
|
||||
@@ -192,6 +190,8 @@ private:
|
||||
common::visitor::ast_id_mapper &id_mapper_;
|
||||
|
||||
clang::SourceManager &source_manager_;
|
||||
|
||||
clanguml::class_diagram::visitor::translation_unit_visitor &visitor_;
|
||||
};
|
||||
|
||||
} // namespace clanguml::class_diagram::visitor
|
||||
@@ -32,7 +32,7 @@ translation_unit_visitor::translation_unit_visitor(clang::SourceManager &sm,
|
||||
: common::visitor::translation_unit_visitor{sm, config}
|
||||
, diagram_{diagram}
|
||||
, config_{config}
|
||||
, template_builder_{diagram_, config_, id_mapper_, sm}
|
||||
, template_builder_{*this}
|
||||
{
|
||||
}
|
||||
|
||||
@@ -40,6 +40,9 @@ bool translation_unit_visitor::VisitNamespaceDecl(clang::NamespaceDecl *ns)
|
||||
{
|
||||
assert(ns != nullptr);
|
||||
|
||||
if (config().package_type() == config::package_type_t::kDirectory)
|
||||
return true;
|
||||
|
||||
if (ns->isAnonymousNamespace() || ns->isInline())
|
||||
return true;
|
||||
|
||||
@@ -81,7 +84,7 @@ bool translation_unit_visitor::VisitNamespaceDecl(clang::NamespaceDecl *ns)
|
||||
}
|
||||
|
||||
if (!p->skip()) {
|
||||
diagram().add_package(std::move(p));
|
||||
diagram().add(package_path, std::move(p));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,8 +140,8 @@ bool translation_unit_visitor::VisitEnumDecl(clang::EnumDecl *enm)
|
||||
}
|
||||
}
|
||||
|
||||
if (id_opt && diagram_.get_class(*id_opt)) {
|
||||
auto parent_class = diagram_.get_class(*id_opt);
|
||||
if (id_opt && diagram().find<class_>(*id_opt)) {
|
||||
auto parent_class = diagram().find<class_>(*id_opt);
|
||||
|
||||
e.set_namespace(ns);
|
||||
e.set_name(parent_class.value().name() + "##" + enm->getNameAsString());
|
||||
@@ -167,7 +170,7 @@ bool translation_unit_visitor::VisitEnumDecl(clang::EnumDecl *enm)
|
||||
}
|
||||
|
||||
if (diagram().should_include(qualified_name))
|
||||
diagram().add_enum(std::move(e_ptr));
|
||||
add_enum(std::move(e_ptr));
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -222,14 +225,14 @@ bool translation_unit_visitor::VisitClassTemplateSpecializationDecl(
|
||||
{relationship_t::kInstantiation, maybe_id.value()});
|
||||
}
|
||||
|
||||
if (diagram_.should_include(template_specialization)) {
|
||||
if (diagram().should_include(template_specialization)) {
|
||||
const auto full_name = template_specialization.full_name(false);
|
||||
const auto id = template_specialization.id();
|
||||
|
||||
LOG_DBG("Adding class template specialization {} with id {}", full_name,
|
||||
id);
|
||||
|
||||
diagram_.add_class(std::move(template_specialization_ptr));
|
||||
add_class(std::move(template_specialization_ptr));
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -262,13 +265,13 @@ bool translation_unit_visitor::VisitTypeAliasTemplateDecl(
|
||||
if (!template_specialization_ptr)
|
||||
return true;
|
||||
|
||||
if (diagram_.should_include(*template_specialization_ptr)) {
|
||||
if (diagram().should_include(*template_specialization_ptr)) {
|
||||
const auto name = template_specialization_ptr->full_name();
|
||||
const auto id = template_specialization_ptr->id();
|
||||
|
||||
LOG_DBG("Adding class {} with id {}", name, id);
|
||||
|
||||
diagram_.add_class(std::move(template_specialization_ptr));
|
||||
add_class(std::move(template_specialization_ptr));
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -324,11 +327,11 @@ bool translation_unit_visitor::VisitClassTemplateDecl(
|
||||
process_class_declaration(*cls->getTemplatedDecl(), *c_ptr);
|
||||
forward_declarations_.erase(id);
|
||||
|
||||
if (diagram_.should_include(*c_ptr)) {
|
||||
if (diagram().should_include(*c_ptr)) {
|
||||
const auto name = c_ptr->full_name();
|
||||
LOG_DBG("Adding class template {} with id {}", name, id);
|
||||
|
||||
diagram_.add_class(std::move(c_ptr));
|
||||
add_class(std::move(c_ptr));
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -361,8 +364,8 @@ bool translation_unit_visitor::VisitRecordDecl(clang::RecordDecl *rec)
|
||||
|
||||
id_mapper().add(rec->getID(), rec_id);
|
||||
|
||||
auto &record_model = diagram().get_class(rec_id).has_value()
|
||||
? *diagram().get_class(rec_id).get()
|
||||
auto &record_model = diagram().find<class_>(rec_id).has_value()
|
||||
? *diagram().find<class_>(rec_id).get()
|
||||
: *record_ptr;
|
||||
|
||||
if (rec->isCompleteDefinition() && !record_model.complete()) {
|
||||
@@ -377,11 +380,11 @@ bool translation_unit_visitor::VisitRecordDecl(clang::RecordDecl *rec)
|
||||
}
|
||||
forward_declarations_.erase(id);
|
||||
|
||||
if (diagram_.should_include(record_model)) {
|
||||
if (diagram().should_include(record_model)) {
|
||||
LOG_DBG("Adding struct/union {} with id {}",
|
||||
record_model.full_name(false), record_model.id());
|
||||
|
||||
diagram_.add_class(std::move(record_ptr));
|
||||
add_class(std::move(record_ptr));
|
||||
}
|
||||
else {
|
||||
LOG_DBG("Skipping struct/union {} with id {}", record_model.full_name(),
|
||||
@@ -433,11 +436,11 @@ bool translation_unit_visitor::TraverseConceptDecl(clang::ConceptDecl *cpt)
|
||||
*concept_model, cpt->getConstraintExpr());
|
||||
}
|
||||
|
||||
if (diagram_.should_include(*concept_model)) {
|
||||
if (diagram().should_include(*concept_model)) {
|
||||
LOG_DBG("Adding concept {} with id {}", concept_model->full_name(false),
|
||||
concept_model->id());
|
||||
|
||||
diagram_.add_concept(std::move(concept_model));
|
||||
add_concept(std::move(concept_model));
|
||||
}
|
||||
else {
|
||||
LOG_DBG("Skipping concept {} with id {}", concept_model->full_name(),
|
||||
@@ -727,8 +730,8 @@ bool translation_unit_visitor::VisitCXXRecordDecl(clang::CXXRecordDecl *cls)
|
||||
|
||||
id_mapper().add(cls->getID(), cls_id);
|
||||
|
||||
auto &class_model = diagram().get_class(cls_id).has_value()
|
||||
? *diagram().get_class(cls_id).get()
|
||||
auto &class_model = diagram().find<class_>(cls_id).has_value()
|
||||
? *diagram().find<class_>(cls_id).get()
|
||||
: *c_ptr;
|
||||
|
||||
if (cls->isCompleteDefinition() && !class_model.complete())
|
||||
@@ -741,11 +744,11 @@ bool translation_unit_visitor::VisitCXXRecordDecl(clang::CXXRecordDecl *cls)
|
||||
}
|
||||
forward_declarations_.erase(id);
|
||||
|
||||
if (diagram_.should_include(class_model)) {
|
||||
if (diagram().should_include(class_model)) {
|
||||
LOG_DBG("Adding class {} with id {}", class_model.full_name(false),
|
||||
class_model.id());
|
||||
|
||||
diagram_.add_class(std::move(c_ptr));
|
||||
add_class(std::move(c_ptr));
|
||||
}
|
||||
else {
|
||||
LOG_DBG("Skipping class {} with id {}", class_model.full_name(),
|
||||
@@ -896,11 +899,11 @@ void translation_unit_visitor::process_record_parent(
|
||||
}
|
||||
}
|
||||
|
||||
if (id_opt && diagram_.get_class(*id_opt)) {
|
||||
if (id_opt && diagram_.find<class_>(*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_.get_class(*id_opt);
|
||||
auto parent_class = diagram_.find<class_>(*id_opt);
|
||||
|
||||
c.set_namespace(parent_ns);
|
||||
const auto cls_name = cls->getNameAsString();
|
||||
@@ -1305,7 +1308,7 @@ void translation_unit_visitor::process_method(
|
||||
.getUnqualifiedType()
|
||||
->getAs<clang::TemplateSpecializationType>();
|
||||
templ != nullptr) {
|
||||
auto *unaliased_type = templ;
|
||||
const auto *unaliased_type = templ;
|
||||
if (unaliased_type->isTypeAlias())
|
||||
unaliased_type = unaliased_type->getAliasedType()
|
||||
->getAs<clang::TemplateSpecializationType>();
|
||||
@@ -1320,7 +1323,7 @@ void translation_unit_visitor::process_method(
|
||||
relationships.emplace_back(template_specialization_ptr->id(),
|
||||
relationship_t::kDependency);
|
||||
|
||||
diagram().add_class(std::move(template_specialization_ptr));
|
||||
add_class(std::move(template_specialization_ptr));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1657,7 +1660,7 @@ void translation_unit_visitor::process_function_parameter(
|
||||
relationships.emplace_back(template_specialization_ptr->id(),
|
||||
relationship_t::kDependency);
|
||||
|
||||
diagram().add_class(std::move(template_specialization_ptr));
|
||||
add_class(std::move(template_specialization_ptr));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1686,12 +1689,8 @@ void translation_unit_visitor::ensure_lambda_type_is_relative(
|
||||
std::string ¶meter_type) const
|
||||
{
|
||||
#ifdef _MSC_VER
|
||||
auto root_name = fmt::format(
|
||||
"{}\\", std::filesystem::current_path().root_name().string());
|
||||
if (root_name.back() == '\\') {
|
||||
root_name.pop_back();
|
||||
root_name.push_back('/');
|
||||
}
|
||||
auto root_name =
|
||||
fmt::format("{}", std::filesystem::current_path().root_name().string());
|
||||
#else
|
||||
auto root_name = std::string{"/"};
|
||||
#endif
|
||||
@@ -1700,17 +1699,19 @@ void translation_unit_visitor::ensure_lambda_type_is_relative(
|
||||
|
||||
while (parameter_type.find(lambda_prefix) != std::string::npos) {
|
||||
auto lambda_begin = parameter_type.find(lambda_prefix);
|
||||
|
||||
auto lambda_prefix_size = lambda_prefix.size();
|
||||
#ifdef _MSC_VER
|
||||
// Skip the `\` or `/` after drive letter and semicolon
|
||||
lambda_prefix_size++;
|
||||
#endif
|
||||
auto absolute_lambda_path_end =
|
||||
parameter_type.find(':', lambda_begin + lambda_prefix.size());
|
||||
auto absolute_lambda_path =
|
||||
parameter_type.substr(lambda_begin + lambda_prefix.size() - 1,
|
||||
absolute_lambda_path_end -
|
||||
(lambda_begin + lambda_prefix.size() - 1));
|
||||
parameter_type.find(':', lambda_begin + lambda_prefix_size);
|
||||
auto absolute_lambda_path = parameter_type.substr(
|
||||
lambda_begin + lambda_prefix_size - 1,
|
||||
absolute_lambda_path_end - (lambda_begin + lambda_prefix_size - 1));
|
||||
|
||||
auto relative_lambda_path = util::path_to_url(std::filesystem::relative(
|
||||
absolute_lambda_path, config().relative_to())
|
||||
.string());
|
||||
auto relative_lambda_path = util::path_to_url(
|
||||
config().make_path_relative(absolute_lambda_path).string());
|
||||
|
||||
parameter_type = fmt::format("{}(lambda at {}{}",
|
||||
parameter_type.substr(0, lambda_begin), relative_lambda_path,
|
||||
@@ -1972,7 +1973,7 @@ void translation_unit_visitor::process_field(
|
||||
// Add the template instantiation object to the diagram if it
|
||||
// matches the include pattern
|
||||
if (add_template_instantiation_to_diagram)
|
||||
diagram().add_class(std::move(template_specialization_ptr));
|
||||
add_class(std::move(template_specialization_ptr));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2004,7 +2005,7 @@ void translation_unit_visitor::add_incomplete_forward_declarations()
|
||||
{
|
||||
for (auto &[id, c] : forward_declarations_) {
|
||||
if (diagram().should_include(c->full_name(false))) {
|
||||
diagram().add_class(std::move(c));
|
||||
add_class(std::move(c));
|
||||
}
|
||||
}
|
||||
forward_declarations_.clear();
|
||||
@@ -2092,4 +2093,61 @@ bool translation_unit_visitor::has_processed_template_class(
|
||||
return util::contains(processed_template_qualified_names_, qualified_name);
|
||||
}
|
||||
|
||||
void translation_unit_visitor::add_class(std::unique_ptr<class_> &&c)
|
||||
{
|
||||
if ((config().generate_packages() &&
|
||||
config().package_type() == config::package_type_t::kDirectory)) {
|
||||
assert(!c->file().empty());
|
||||
|
||||
const auto file = config().make_path_relative(c->file());
|
||||
|
||||
common::model::path p{
|
||||
file.string(), common::model::path_type::kFilesystem};
|
||||
p.pop_back();
|
||||
|
||||
diagram().add(p, std::move(c));
|
||||
}
|
||||
else {
|
||||
diagram().add(c->path(), std::move(c));
|
||||
}
|
||||
}
|
||||
|
||||
void translation_unit_visitor::add_enum(std::unique_ptr<enum_> &&e)
|
||||
{
|
||||
if ((config().generate_packages() &&
|
||||
config().package_type() == config::package_type_t::kDirectory)) {
|
||||
assert(!e->file().empty());
|
||||
|
||||
const auto file = config().make_path_relative(e->file());
|
||||
|
||||
common::model::path p{
|
||||
file.string(), common::model::path_type::kFilesystem};
|
||||
p.pop_back();
|
||||
|
||||
diagram().add(p, std::move(e));
|
||||
}
|
||||
else {
|
||||
diagram().add(e->path(), std::move(e));
|
||||
}
|
||||
}
|
||||
|
||||
void translation_unit_visitor::add_concept(std::unique_ptr<concept_> &&c)
|
||||
{
|
||||
if ((config().generate_packages() &&
|
||||
config().package_type() == config::package_type_t::kDirectory)) {
|
||||
assert(!c->file().empty());
|
||||
|
||||
const auto file = config().make_path_relative(c->file());
|
||||
|
||||
common::model::path p{
|
||||
file.string(), common::model::path_type::kFilesystem};
|
||||
p.pop_back();
|
||||
|
||||
diagram().add(p, std::move(c));
|
||||
}
|
||||
else {
|
||||
diagram().add(c->path(), std::move(c));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace clanguml::class_diagram::visitor
|
||||
|
||||
@@ -43,6 +43,7 @@ using clanguml::class_diagram::model::class_;
|
||||
using clanguml::class_diagram::model::class_member;
|
||||
using clanguml::class_diagram::model::class_method;
|
||||
using clanguml::class_diagram::model::class_parent;
|
||||
using clanguml::class_diagram::model::concept_;
|
||||
using clanguml::class_diagram::model::diagram;
|
||||
using clanguml::class_diagram::model::enum_;
|
||||
using clanguml::class_diagram::model::method_parameter;
|
||||
@@ -118,6 +119,14 @@ public:
|
||||
*/
|
||||
void finalize();
|
||||
|
||||
common::visitor::ast_id_mapper &id_mapper() const { return id_mapper_; }
|
||||
|
||||
void add_class(std::unique_ptr<class_> &&c);
|
||||
void add_enum(std::unique_ptr<enum_> &&e);
|
||||
void add_concept(std::unique_ptr<concept_> &&c);
|
||||
|
||||
void ensure_lambda_type_is_relative(std::string ¶meter_type) const;
|
||||
|
||||
private:
|
||||
bool should_include(const clang::NamedDecl *decl);
|
||||
|
||||
@@ -182,8 +191,6 @@ private:
|
||||
const found_relationships_t &relationships,
|
||||
bool break_on_first_aggregation = false);
|
||||
|
||||
void ensure_lambda_type_is_relative(std::string ¶meter_type) const;
|
||||
|
||||
void process_record_parent(
|
||||
clang::RecordDecl *cls, class_ &c, const namespace_ &ns);
|
||||
|
||||
@@ -218,8 +225,6 @@ private:
|
||||
|
||||
bool has_processed_template_class(const std::string &qualified_name) const;
|
||||
|
||||
common::visitor::ast_id_mapper &id_mapper() const { return id_mapper_; }
|
||||
|
||||
template_builder &tbuilder() { return template_builder_; }
|
||||
|
||||
// Reference to the output diagram model
|
||||
|
||||
Reference in New Issue
Block a user