Merge pull request #148 from bkryza/add-packages-from-directory-structure

Add packages from directory structure
This commit is contained in:
Bartek Kryza
2023-05-28 22:58:45 +02:00
committed by GitHub
291 changed files with 7038 additions and 4416 deletions

View File

@@ -754,4 +754,30 @@ std::vector<std::string> tokenize_unexposed_template_parameter(
return result;
}
bool parse_source_location(const std::string &location_str, std::string &file,
unsigned &line, unsigned &column)
{
auto tokens = util::split(location_str, ":");
if (tokens.size() < 3)
return false;
file = tokens.at(0);
try {
line = std::stoi(tokens.at(1));
}
catch (std::invalid_argument &e) {
line = 0;
}
try {
column = std::stoi(tokens.at(2));
}
catch (std::invalid_argument &e) {
column = 0;
}
return true;
}
} // namespace clanguml::common

View File

@@ -172,6 +172,9 @@ void if_dyn_cast(P pointer, F &&func)
}
}
bool parse_source_location(const std::string &location_str, std::string &file,
unsigned &line, unsigned &column);
bool is_type_parameter(const std::string &t);
bool is_qualifier(const std::string &q);

View File

@@ -39,7 +39,7 @@ public:
virtual diagram_t type() const = 0;
virtual common::optional_ref<clanguml::common::model::diagram_element> get(
virtual opt_ref<clanguml::common::model::diagram_element> get(
const std::string &full_name) const = 0;
virtual common::optional_ref<clanguml::common::model::diagram_element> get(

View File

@@ -61,21 +61,21 @@ template <>
const clanguml::common::optional_ref<class_diagram::model::class_> get(
const class_diagram::model::diagram &d, const std::string &full_name)
{
return d.get_class(full_name);
return d.find<class_diagram::model::class_>(full_name);
}
template <>
const clanguml::common::optional_ref<common::model::package> get(
const package_diagram::model::diagram &d, const std::string &full_name)
{
return d.get_package(full_name);
return d.find<package>(full_name);
}
template <>
const clanguml::common::optional_ref<common::model::source_file> get(
const include_diagram::model::diagram &d, const std::string &full_name)
{
return d.get_file(full_name);
return d.find<source_file>(full_name);
}
template <>
@@ -259,7 +259,7 @@ tvl::value_t subclass_filter::match(const diagram &d, const element &e) const
clanguml::common::reference_set<class_diagram::model::class_> parents;
const auto &fn = e.full_name(false);
auto class_ref = cd.get_class(fn);
auto class_ref = cd.find<class_diagram::model::class_>(fn);
if (!class_ref.has_value())
return false;
@@ -308,7 +308,7 @@ tvl::value_t parents_filter::match(const diagram &d, const element &e) const
clanguml::common::reference_set<class_diagram::model::class_> parents;
for (const auto &child : children_) {
auto child_ref = cd.get_class(child);
auto child_ref = cd.find<class_diagram::model::class_>(child);
if (!child_ref.has_value())
continue;
parents.emplace(child_ref.value());
@@ -370,8 +370,8 @@ tvl::value_t context_filter::match(const diagram &d, const element &e) const
return tvl::any_of(context_.begin(), context_.end(),
[&e, &d](const auto &context_root_name) {
const auto &context_root =
static_cast<const class_diagram::model::diagram &>(d).get_class(
context_root_name);
static_cast<const class_diagram::model::diagram &>(d)
.find<class_diagram::model::class_>(context_root_name);
if (context_root.has_value()) {
// This is a direct match to the context root
@@ -430,8 +430,8 @@ paths_filter::paths_filter(filter_t type, const std::filesystem::path &root,
absolute_path = path;
try {
absolute_path =
std::filesystem::canonical(absolute_path.lexically_normal());
absolute_path = absolute(absolute_path);
absolute_path = canonical(absolute_path.lexically_normal());
}
catch (std::filesystem::filesystem_error &e) {
LOG_WARN("Cannot add non-existent path {} to paths filter",
@@ -539,7 +539,7 @@ void diagram_filter::init_filters(const config::diagram &c)
filter_t::kInclusive, c.include().access));
add_inclusive_filter(std::make_unique<paths_filter>(
filter_t::kInclusive, c.relative_to(), c.include().paths));
filter_t::kInclusive, c.root_directory(), c.include().paths));
// Include any of these matches even if one them does not match
std::vector<std::unique_ptr<filter_visitor>> element_filters;
@@ -623,7 +623,7 @@ void diagram_filter::init_filters(const config::diagram &c)
filter_t::kExclusive, c.exclude().namespaces));
add_exclusive_filter(std::make_unique<paths_filter>(
filter_t::kExclusive, c.relative_to(), c.exclude().paths));
filter_t::kExclusive, c.root_directory(), c.exclude().paths));
add_exclusive_filter(std::make_unique<element_filter>(
filter_t::kExclusive, c.exclude().elements));

View File

@@ -0,0 +1,70 @@
/**
* src/common/model/element_view.h
*
* Copyright (c) 2021-2023 Bartek Kryza <bkryza@gmail.com>
*
* 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.
*/
#pragma once
#include "common/types.h"
namespace clanguml::common::model {
/**
* Provides type based views over elements in a diagram.
*
* @tparam T Type of diagram element
*/
template <typename T> class element_view {
public:
/**
* @brief Add reference to diagram element
*
* @param element Reference to diagram element of specific type
*/
void add(std::reference_wrapper<T> element)
{
elements_.emplace_back(std::move(element));
}
/**
* @brief Get collection of reference to diagram elements
*
* @return
*/
const reference_vector<T> &view() const { return elements_; }
/**
* @brief Get typed diagram element by id
* @param id Global id of a diagram element
*
* @return
*/
common::optional_ref<T> get(
clanguml::common::model::diagram_element::id_t id) const
{
for (const auto &e : elements_) {
if (e.get().id() == id) {
return {e};
}
}
return {};
}
private:
reference_vector<T> elements_;
};
} // namespace clanguml::common::model

View File

@@ -29,7 +29,7 @@ struct ns_path_separator {
static constexpr std::string_view value = "::";
};
using namespace_ = path<ns_path_separator>;
using namespace_ = path;
}

View File

@@ -70,7 +70,9 @@ public:
if (parent && dynamic_cast<nested_trait<T, Path> *>(&parent.value()))
return dynamic_cast<nested_trait<T, Path> &>(parent.value())
.template add_element<V>(std::move(p));
spdlog::info("No parent element found at: {}", path.to_string());
LOG_INFO("No parent element found at: {}", path.to_string());
throw std::runtime_error(
"No parent element found for " + path.to_string());
}
@@ -135,7 +137,29 @@ public:
elements_.end();
}
bool is_empty() const { return elements_.empty(); }
template <typename F> bool all_of(F &&f) const
{
return std::all_of(
elements_.cbegin(), elements_.cend(), [f](const auto &e) {
const auto *package_ptr =
dynamic_cast<nested_trait<T, Path> *>(e.get());
if (package_ptr != nullptr)
return package_ptr->all_of(f);
return f(*e);
});
}
bool is_empty() const
{
return elements_.empty() ||
std::all_of(elements_.cbegin(), elements_.cend(), [](auto &e) {
const auto *package_ptr =
dynamic_cast<nested_trait<T, Path> *>(e.get());
return package_ptr != nullptr && package_ptr->is_empty();
});
}
auto begin() { return elements_.begin(); }
auto end() { return elements_.end(); }

View File

@@ -19,6 +19,7 @@
#include "common/model/element.h"
#include "common/model/nested_trait.h"
#include "common/model/path.h"
#include "common/model/stylable_element.h"
#include "common/types.h"
#include "util/util.h"
@@ -33,9 +34,9 @@ namespace clanguml::common::model {
class package : public element,
public stylable_element,
public nested_trait<element, namespace_> {
public nested_trait<element, path> {
public:
package(const common::model::namespace_ &using_namespace);
package(const common::model::path &using_namespace);
package(const package &) = delete;
package(package &&) = default;

View File

@@ -25,41 +25,83 @@
namespace clanguml::common::model {
template <typename Sep> class path {
enum class path_type { kNamespace, kFilesystem };
class path {
const char *separator() const
{
switch (path_type_) {
case path_type::kNamespace:
return "::";
case path_type::kFilesystem:
#ifdef _WIN32
return "\\";
#else
return "/";
#endif
}
return "::";
}
public:
using container_type = std::vector<std::string>;
path() = default;
path(path_type pt = path_type::kNamespace)
: path_type_{pt}
{
}
explicit path(const std::string &ns)
path(const std::string &ns, path_type pt = path_type::kNamespace)
: path_type_{pt}
{
if (ns.empty())
return;
path_ = util::split(ns, Sep::value);
path_ = util::split(ns, separator());
}
virtual ~path() = default;
path(container_type::const_iterator begin,
container_type::const_iterator end)
container_type::const_iterator end,
path_type pt = path_type::kNamespace)
: path(pt)
{
if (begin == end)
return;
std::copy(begin, end, std::back_inserter(path_));
}
path(const path &right)
: path_{right.path_}
{
}
path(const path &right) = default;
path &operator=(const path &right) = default;
path &operator=(const path &right)
{
if (&right == this)
return *this;
if (path_type_ != right.path_type_)
throw std::runtime_error("");
path_type_ = right.path_type_;
path_ = right.path_;
return *this;
}
path(path &&right) noexcept = default;
path &operator=(path &&right) noexcept = default;
path(std::initializer_list<std::string> ns)
path(std::initializer_list<std::string> ns,
path_type pt = path_type::kNamespace)
: path(pt)
{
if ((ns.size() == 1) && util::contains(*ns.begin(), Sep::value)) {
path_ = util::split(*ns.begin(), Sep::value);
if ((ns.size() == 1) &&
util::contains(*ns.begin(), std::string{separator()})) {
path_ = util::split(*ns.begin(), separator());
}
else if ((ns.size() == 1) && ns.begin()->empty()) {
}
@@ -67,10 +109,13 @@ public:
path_ = ns;
}
explicit path(const std::vector<std::string> &ns)
explicit path(const std::vector<std::string> &ns,
path_type pt = path_type::kNamespace)
: path(pt)
{
if ((ns.size() == 1) && util::contains(*ns.begin(), Sep::value)) {
path_ = util::split(*ns.begin(), Sep::value);
if ((ns.size() == 1) &&
util::contains(*ns.begin(), std::string{separator()})) {
path_ = util::split(*ns.begin(), separator());
}
else if ((ns.size() == 1) && ns.begin()->empty()) {
}
@@ -78,19 +123,19 @@ public:
path_ = ns;
}
friend bool operator==(const path<Sep> &left, const path<Sep> &right)
friend bool operator==(const path &left, const path &right)
{
return left.path_ == right.path_;
}
friend bool operator<(const path<Sep> &left, const path<Sep> &right)
friend bool operator<(const path &left, const path &right)
{
return std::hash<path<Sep>>{}(left) < std::hash<path<Sep>>{}(right);
return left.to_string() < right.to_string();
}
std::string to_string() const
{
return fmt::format("{}", fmt::join(path_, Sep::value));
return fmt::format("{}", fmt::join(path_, std::string{separator()}));
}
bool is_empty() const { return path_.empty(); }
@@ -115,9 +160,9 @@ public:
void operator|=(const std::string &right) { append(right); }
std::string &operator[](const int index) { return path_[index]; }
std::string &operator[](const unsigned int index) { return path_[index]; }
const std::string &operator[](const int index) const
const std::string &operator[](const unsigned int index) const
{
return path_[index];
}
@@ -190,7 +235,7 @@ public:
return name;
auto res = name;
auto ns_prefix = to_string() + std::string{Sep::value};
auto ns_prefix = to_string() + std::string{separator()};
auto it = res.find(ns_prefix);
while (it != std::string::npos) {
@@ -220,7 +265,10 @@ public:
path::container_type::const_iterator begin() const { return path_.begin(); }
path::container_type::const_iterator end() const { return path_.end(); }
path_type type() const { return path_type_; }
private:
path_type path_type_;
container_type path_;
};

View File

@@ -46,7 +46,7 @@ struct fs_path_sep {
#endif
};
using filesystem_path = common::model::path<fs_path_sep>;
using filesystem_path = common::model::path;
class source_file
: public common::model::diagram_element,
@@ -60,7 +60,7 @@ public:
{
auto preferred = p;
preferred.make_preferred();
set_path({preferred.parent_path().string()});
set_path({preferred.parent_path().string(), path_type::kFilesystem});
set_name(preferred.filename().string());
is_absolute_ = preferred.is_absolute();
set_id(common::to_id(preferred));
@@ -126,7 +126,7 @@ public:
}
private:
filesystem_path path_;
filesystem_path path_{path_type::kFilesystem};
source_file_t type_{source_file_t::kDirectory};
bool is_absolute_{false};
};
@@ -134,21 +134,24 @@ private:
namespace std {
template <> struct hash<clanguml::common::model::filesystem_path> {
std::size_t operator()(
const clanguml::common::model::filesystem_path &key) const
{
using clanguml::common::model::path;
/*
template <> struct hash<clanguml::common::model::filesystem_path> {
std::size_t operator()(
const clanguml::common::model::filesystem_path &key) const
{
using clanguml::common::model::path;
std::size_t seed = key.size();
for (const auto &ns : key) {
seed ^=
std::hash<std::string>{}(ns) + clanguml::util::hash_seed(seed);
}
std::size_t seed = key.size();
for (const auto &ns : key) {
seed ^=
std::hash<std::string>{}(ns) +
clanguml::util::hash_seed(seed);
}
return seed;
}
};
return seed;
}
};
*/
} // namespace std

View File

@@ -129,6 +129,8 @@ private:
T *value_{nullptr};
};
template <typename T> using opt_ref = optional_ref<T>;
template <typename T>
using reference_vector = std::vector<std::reference_wrapper<T>>;

View File

@@ -85,14 +85,36 @@ void translation_unit_visitor::set_source_location(
const clang::SourceLocation &location,
clanguml::common::model::source_location &element)
{
std::string file;
unsigned line{};
[[maybe_unused]] unsigned column{};
if (location.isValid()) {
element.set_file(source_manager_.getFilename(location).str());
element.set_file_relative(util::path_to_url(
std::filesystem::relative(element.file(), relative_to_path_)
.string()));
element.set_line(source_manager_.getSpellingLineNumber(location));
element.set_location_id(location.getHashValue());
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;
}
}
element.set_file(file);
element.set_file_relative(util::path_to_url(
std::filesystem::relative(element.file(), relative_to_path_).string()));
element.set_line(line);
element.set_location_id(location.getHashValue());
}
} // namespace clanguml::common::visitor

View File

@@ -62,7 +62,6 @@ public:
*/
clang::SourceManager &source_manager() const;
protected:
/**
* @brief Set source location in diagram element
*
@@ -93,6 +92,7 @@ protected:
void set_source_location(const clang::SourceLocation &location,
clanguml::common::model::source_location &element);
protected:
/**
* @brief Set source location in diagram element
*