From a68cae9021a445550afdcd1bcce3cb3352c3c8f9 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Wed, 21 Dec 2022 13:16:12 +0100 Subject: fix: Fix matching of base directory for MSVC The base directory will now match case-insensitively with absolute paths in preprocessed output, or from /showIncludes in the depend mode case, when compiling with MSVC. --- src/Util.cpp | 22 ++++++++++++++-------- src/ccache.cpp | 5 +++-- src/util/path.cpp | 6 +++++- unittest/test_argprocessing.cpp | 4 +++- unittest/test_util_path.cpp | 2 ++ 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/Util.cpp b/src/Util.cpp index 844e5497..8f6b4102 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -910,20 +910,14 @@ matches_dir_prefix_or_file(std::string_view dir_prefix_or_file, || is_dir_separator(dir_prefix_or_file.back())); } -std::string -normalize_abstract_absolute_path(std::string_view path) +static std::string +do_normalize_abstract_absolute_path(std::string_view path) { if (!util::is_absolute_path(path)) { return std::string(path); } #ifdef _WIN32 - if (path.find("\\") != std::string_view::npos) { - std::string new_path(path); - std::replace(new_path.begin(), new_path.end(), '\\', '/'); - return normalize_abstract_absolute_path(new_path); - } - std::string drive(path.substr(0, 2)); path = path.substr(2); #endif @@ -970,6 +964,18 @@ normalize_abstract_absolute_path(std::string_view path) #endif } +std::string +normalize_abstract_absolute_path(std::string_view path) +{ +#ifdef _WIN32 + std::string new_path(path); + std::replace(new_path.begin(), new_path.end(), '\\', '/'); + return do_normalize_abstract_absolute_path(new_path); +#else + return do_normalize_abstract_absolute_path(path); +#endif +} + std::string normalize_concrete_absolute_path(const std::string& path) { diff --git a/src/ccache.cpp b/src/ccache.cpp index c8a21ac3..8f71e2a2 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -704,9 +704,10 @@ struct DoExecuteResult static std::optional result_key_from_includes(Context& ctx, Hash& hash, std::string_view stdout_data) { - for (std::string_view token : core::MsvcShowIncludesOutput::get_includes( + for (std::string_view include : core::MsvcShowIncludesOutput::get_includes( stdout_data, ctx.config.msvc_dep_prefix())) { - const std::string path = Util::make_relative_path(ctx, token); + const std::string path = Util::make_relative_path( + ctx, Util::normalize_abstract_absolute_path(include)); remember_include_file(ctx, path, hash, false, &hash); } diff --git a/src/util/path.cpp b/src/util/path.cpp index a1e797de..d1e6b7e8 100644 --- a/src/util/path.cpp +++ b/src/util/path.cpp @@ -73,10 +73,14 @@ path_starts_with(std::string_view path, std::string_view prefix) if (path[i] == '\\' && prefix[j] == '/') { continue; } -#endif + if (std::tolower(path[i]) != std::tolower(prefix[j])) { + return false; + } +#else if (path[i] != prefix[j]) { return false; } +#endif } return true; } diff --git a/unittest/test_argprocessing.cpp b/unittest/test_argprocessing.cpp index 1a59f6a2..062d6622 100644 --- a/unittest/test_argprocessing.cpp +++ b/unittest/test_argprocessing.cpp @@ -59,7 +59,9 @@ get_posix_path(const std::string& path) std::string posix; // /-escape volume. - if (path[0] >= 'A' && path[0] <= 'Z' && path[1] == ':') { + if (path[1] == ':' + && ((path[0] >= 'A' && path[0] <= 'Z') + || (path[0] >= 'a' && path[0] <= 'z'))) { posix = "/" + path; } else { posix = path; diff --git a/unittest/test_util_path.cpp b/unittest/test_util_path.cpp index 43defcad..8bb703df 100644 --- a/unittest/test_util_path.cpp +++ b/unittest/test_util_path.cpp @@ -131,6 +131,8 @@ TEST_CASE("util::path_starts_with") CHECK(util::path_starts_with("C:/foo/bar", "C:\\\\foo")); CHECK(util::path_starts_with("C:\\foo\\bar", "C:/foo")); CHECK(util::path_starts_with("C:\\\\foo\\\\bar", "C:/foo")); + CHECK(util::path_starts_with("C:/FOO/BAR", "c:\\foo")); + CHECK(util::path_starts_with("c:/foo/bar", "C:\\FOO")); CHECK(!util::path_starts_with("C:\\foo\\bar", "/foo/baz")); CHECK(!util::path_starts_with("C:\\foo\\bar", "C:/foo/baz")); CHECK(!util::path_starts_with("C:\\beh\\foo", "/foo")); -- cgit v1.2.1