Skip to content

Commit

Permalink
Canonicalize header paths, removing relative prefices.
Browse files Browse the repository at this point in the history
  • Loading branch information
hzeller committed Jun 2, 2024
1 parent d09a507 commit 6d8c244
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
13 changes: 12 additions & 1 deletion bant/explore/header-providers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@
namespace bant {
namespace {

static std::string_view LightCanonicalizePath(std::string_view path) {
while (path.starts_with("./")) {
path.remove_prefix(2);
}
while (path.starts_with("/")) {
path.remove_prefix(1);
}
return path;
}

static std::string OptionalReverse(std::string_view in, bool reverse) {
return reverse ? std::string{in.rbegin(), in.rend()} : std::string{in};
}
Expand Down Expand Up @@ -164,7 +174,8 @@ static void AppendCCLibraryHeaders(const ParsedBuildFile &build_file,
build_file, [&](const BazelTarget &cc_library, std::string_view hdr_loc,
const std::string &header_fqn) {
// Sometimes there can be multiple libraries exporting the same header.
result[OptionalReverse(header_fqn, reverse)].insert(cc_library);
const std::string_view canonicalized = LightCanonicalizePath(header_fqn);
result[OptionalReverse(canonicalized, reverse)].insert(cc_library);
});
}

Expand Down
24 changes: 24 additions & 0 deletions bant/explore/header-providers_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,30 @@ cc_library(
Ts("//prefix/dir:baz"))));
}

TEST(HeaderToLibMapping, InludePathsAreRelativePathCanonicalized) {
ParsedProjectTestUtil pp;
pp.Add("//", R"(
cc_library(
name = "foo",
srcs = ["foo.cc"],
include_prefix = ".",
hdrs = ["foo.h"]
)
cc_library(
name = "bar",
srcs = ["bar.cc"],
include_prefix = "./",
hdrs = ["bar.h"]
)
)");

std::stringstream log_absorb;
auto header_map = ExtractHeaderToLibMapping(pp.project(), log_absorb);
EXPECT_THAT(header_map, Contains(Pair("foo.h", Ts("//:foo"))));
EXPECT_THAT(header_map, Contains(Pair("bar.h", Ts("//:bar"))));
}

// Sometimes two different libraries claim to export the same header.
TEST(HeaderToLibMapping, MultipleCCLibsProvideSameHeader) {
ParsedProjectTestUtil pp;
Expand Down

0 comments on commit 6d8c244

Please sign in to comment.