From a054c0464361b2829f905a5d45b6ce739cf33c40 Mon Sep 17 00:00:00 2001 From: Henner Zeller Date: Sat, 31 Aug 2024 09:30:55 -0700 Subject: [PATCH] dwyu: Make sure includes in toplevel libraries are correctly recognized. --- bant/tool/dwyu.cc | 3 ++- bant/tool/dwyu_test.cc | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/bant/tool/dwyu.cc b/bant/tool/dwyu.cc index 2fead28..f7df515 100644 --- a/bant/tool/dwyu.cc +++ b/bant/tool/dwyu.cc @@ -70,7 +70,8 @@ bool IsHeaderInList(std::string_view header, // so we always need to prepend that prefix_path. for (const std::string_view list_item : list) { if (header.ends_with(list_item) && // cheap first test before strcat - absl::StrCat(prefix_path, "/", list_item) == header) { + (header == list_item || + absl::StrCat(prefix_path, "/", list_item) == header)) { return true; } } diff --git a/bant/tool/dwyu_test.cc b/bant/tool/dwyu_test.cc index 212b0fe..320aa28 100644 --- a/bant/tool/dwyu_test.cc +++ b/bant/tool/dwyu_test.cc @@ -234,6 +234,26 @@ cc_library( } } +TEST(DWYUTest, ToplevelIncludesWithoutPrefixSlashWork) { + ParsedProjectTestUtil pp; + pp.Add("//", R"( +cc_library( + name = "foo", + srcs = ["foo.cc"], + hdrs = ["foo.h"] +) +)"); + + { + DWYUTestFixture tester(pp.project()); + tester.AddSource("foo.h", ""); + tester.AddSource("foo.cc", R"( +#include "foo.h" +)"); + tester.RunForTarget("//:foo"); + } +} + TEST(DWYUTest, ChooseMinimalDependencySetIfMultipleLibrariesProvideHeader) { ParsedProjectTestUtil pp; pp.Add("//path", R"(