Skip to content

Commit

Permalink
Replaced unnecessary weakly_canonical calls with lexically_normal (#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkryza committed Nov 5, 2024
1 parent 2379b1c commit 51d20bb
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 27 deletions.
3 changes: 2 additions & 1 deletion src/common/compilation_database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ long compilation_database::count_matching_commands(
result += std::count_if(
files.begin(), files.end(), [&command](const auto &file) {
return (command.Filename == file) ||
(std::filesystem::weakly_canonical(command.Filename)
(std::filesystem::path{command.Filename}
.lexically_normal()
.string() == file);
});
}
Expand Down
52 changes: 29 additions & 23 deletions src/common/model/filters/diagram_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1041,16 +1041,20 @@ tvl::value_t paths_filter::match(
return {};
}

auto pp = p.fs_path(root_);
for (const auto &path : paths_) {
if (pp.root_name().string() == path.root_name().string() &&
util::is_relative_to(pp.relative_path(), path.relative_path())) {

return true;
}
}

return false;
const auto source_file_path = p.fs_path(root_);

return memoize(
true,
[this](const std::filesystem::path &sfp) {
return std::any_of(
paths_.begin(), paths_.end(), [&](const auto &path) {
return sfp.root_name().string() ==
path.root_name().string() &&
util::is_relative_to(
sfp.relative_path(), path.relative_path());
});
},
source_file_path);
}

tvl::value_t paths_filter::match(
Expand All @@ -1060,24 +1064,26 @@ tvl::value_t paths_filter::match(
return {};
}

auto sl_path = std::filesystem::path{p.file()};
const auto source_location_path = std::filesystem::path{p.file()};

// Matching source paths doesn't make sens if they are not absolute or
// Matching source paths doesn't make sense if they are not absolute or
// empty
if (p.file().empty() || sl_path.is_relative()) {
if (p.file().empty() || source_location_path.is_relative()) {
return {};
}

for (const auto &path : paths_) {
if (sl_path.root_name().string() == path.root_name().string() &&
util::is_relative_to(
sl_path.relative_path(), path.relative_path())) {

return true;
}
}

return false;
return memoize(
true,
[this](const std::filesystem::path &p) {
return std::any_of(
paths_.begin(), paths_.end(), [&](const auto &path) {
return p.root_name().string() ==
path.root_name().string() &&
util::is_relative_to(
p.relative_path(), path.relative_path());
});
},
source_location_path);
}

tvl::value_t paths_filter::match(
Expand Down
5 changes: 4 additions & 1 deletion src/common/model/filters/diagram_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "config/config.h"
#include "include_diagram/model/diagram.h"
#include "sequence_diagram/model/participant.h"
#include "util/memoized.h"

#include <filesystem>
#include <utility>
Expand Down Expand Up @@ -698,7 +699,9 @@ struct context_filter : public filter_visitor {
* Match elements based on their source location, whether it matches to
* a specified file paths.
*/
struct paths_filter : public filter_visitor {
struct paths_filter : public filter_visitor,
public util::memoized<std::filesystem::path, bool,
std::filesystem::path> {
paths_filter(filter_t type, const std::vector<std::string> &p,
const std::filesystem::path &root);

Expand Down
5 changes: 3 additions & 2 deletions src/common/visitor/translation_unit_visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,9 @@ template <typename ConfigT, typename DiagramT> class translation_unit_visitor {
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()));
element.set_file_relative(
util::path_to_url(fs::path{element.file()}.lexically_relative(
relative_to_path_)));
}
else {
element.set_file_relative("");
Expand Down

0 comments on commit 51d20bb

Please sign in to comment.