From: Joel Rosdahl Date: Sun, 31 Dec 2023 10:10:27 +0000 (+0100) Subject: refactor: Convert usage of Util::base_name to std::filesystem X-Git-Tag: v4.10~142 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=caac9d5ae4ec495f24d6b73a62cc28ceaba066b3;p=thirdparty%2Fccache.git refactor: Convert usage of Util::base_name to std::filesystem --- diff --git a/src/Util.cpp b/src/Util.cpp index 772b67f1f..c93e760cb 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -45,18 +45,6 @@ using util::DirEntry; namespace Util { -std::string_view -base_name(std::string_view path) -{ -#ifdef _WIN32 - const char delim[] = "/\\"; -#else - const char delim[] = "/"; -#endif - size_t n = path.find_last_of(delim); - return n == std::string::npos ? path : path.substr(n + 1); -} - std::string change_extension(std::string_view path, std::string_view new_ext) { diff --git a/src/Util.hpp b/src/Util.hpp index 48e543cec..6ebe6d85d 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -35,9 +35,6 @@ class Context; namespace Util { -// Get base name of path. -std::string_view base_name(std::string_view path); - // Remove the extension via `remove_extension()`, then add `new_ext`. `new_ext` // should start with a dot, no extra dot is inserted. std::string change_extension(std::string_view path, std::string_view new_ext); diff --git a/src/argprocessing.cpp b/src/argprocessing.cpp index 93046e13a..f6b80e0dc 100644 --- a/src/argprocessing.cpp +++ b/src/argprocessing.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2020-2023 Joel Rosdahl and other contributors +// Copyright (C) 2020-2024 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -1272,8 +1272,8 @@ process_args(Context& ctx) } else { extension = get_default_object_file_extension(ctx.config); } - args_info.output_obj += - Util::change_extension(Util::base_name(args_info.input_file), extension); + args_info.output_obj += Util::change_extension( + fs::path(args_info.input_file).filename().string(), extension); } args_info.orig_output_obj = args_info.output_obj; @@ -1497,7 +1497,7 @@ process_args(Context& ctx) // GCC strangely uses the base name of the source file but with a .o // extension. dep_target = Util::change_extension( - Util::base_name(args_info.orig_input_file), + fs::path(args_info.orig_input_file).filename().string(), get_default_object_file_extension(ctx.config)); } else { // How other compilers behave is currently unknown, so bail out. diff --git a/src/ccache.cpp b/src/ccache.cpp index 9647d3eda..6cb38b98a 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2009-2023 Joel Rosdahl and other contributors +// Copyright (C) 2009-2024 Joel Rosdahl and other contributors // Copyright (C) 2002-2007 Andrew Tridgell // // See doc/AUTHORS.adoc for a complete list of contributors. @@ -1438,7 +1438,7 @@ hash_common_info(const Context& ctx, // Also hash the compiler name as some compilers use hard links and behave // differently depending on the real name. hash.hash_delimiter("cc_name"); - hash.hash(Util::base_name(args[0])); + hash.hash(fs::path(args[0]).filename().string()); // Hash variables that may affect the compilation. const char* always_hash_env_vars[] = { @@ -1547,8 +1547,8 @@ hash_common_info(const Context& ctx, } else { dir = util::real_path(Util::dir_name(ctx.args_info.output_obj)); } - std::string_view stem = - Util::remove_extension(Util::base_name(ctx.args_info.output_obj)); + std::string_view stem = Util::remove_extension( + fs::path(ctx.args_info.output_obj).filename().string()); std::string gcda_path = FMT("{}/{}.gcda", dir, stem); LOG("Hashing coverage path {}", gcda_path); hash.hash_delimiter("gcda"); @@ -2182,7 +2182,7 @@ find_compiler(Context& ctx, // In case ccache is masquerading as the compiler, use only base_name so // the real compiler can be determined. : (masquerading_as_compiler - ? std::string(Util::base_name(ctx.orig_args[0])) + ? fs::path(ctx.orig_args[0]).filename().string() : ctx.orig_args[0]); const std::string resolved_compiler = @@ -2740,7 +2740,8 @@ ccache_main(int argc, const char* const* argv) try { if (is_ccache_executable(argv[0])) { if (argc < 2) { - PRINT_RAW(stderr, core::get_usage_text(Util::base_name(argv[0]))); + PRINT_RAW(stderr, + core::get_usage_text(fs::path(argv[0]).filename().string())); exit(EXIT_FAILURE); } // If the first argument isn't an option, then assume we are being diff --git a/src/core/mainoptions.cpp b/src/core/mainoptions.cpp index 826ce5235..cdb5c9ab2 100644 --- a/src/core/mainoptions.cpp +++ b/src/core/mainoptions.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2021-2023 Joel Rosdahl and other contributors +// Copyright (C) 2021-2024 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -46,6 +46,7 @@ #include #include #include +#include #include #include #include @@ -73,6 +74,8 @@ extern "C" { } #endif +namespace fs = util::filesystem; + using util::DirEntry; namespace core { @@ -667,7 +670,7 @@ process_main_options(int argc, const char* const* argv) } case 'h': // --help - PRINT(stdout, USAGE_TEXT, Util::base_name(argv[0])); + PRINT(stdout, USAGE_TEXT, fs::path(argv[0]).filename()); return EXIT_SUCCESS; case 'k': // --get-config @@ -754,7 +757,7 @@ process_main_options(int argc, const char* const* argv) case 'V': // --version { - std::string_view name = Util::base_name(argv[0]); + std::string name = fs::path(argv[0]).filename().string(); #ifdef _WIN32 name = Util::remove_extension(name); #endif @@ -793,7 +796,7 @@ process_main_options(int argc, const char* const* argv) break; default: - PRINT(stderr, USAGE_TEXT, Util::base_name(argv[0])); + PRINT(stderr, USAGE_TEXT, fs::path(argv[0]).filename()); return EXIT_FAILURE; } } diff --git a/src/storage/local/LocalStorage.cpp b/src/storage/local/LocalStorage.cpp index 51f675676..99b0d40ae 100644 --- a/src/storage/local/LocalStorage.cpp +++ b/src/storage/local/LocalStorage.cpp @@ -1085,9 +1085,10 @@ LocalStorage::move_to_wanted_cache_level(const StatisticsCounters& counters, LOG("Moving {} to {}", cache_file_path, wanted_path); fs::rename(cache_file_path, wanted_path); for (const auto& raw_file : m_added_raw_files) { - fs::rename( - raw_file, - FMT("{}/{}", Util::dir_name(wanted_path), Util::base_name(raw_file))); + fs::rename(raw_file, + FMT("{}/{}", + Util::dir_name(wanted_path), + fs::path(raw_file).filename())); } } } diff --git a/src/util/assertions.cpp b/src/util/assertions.cpp index dd32cc527..f330156e0 100644 --- a/src/util/assertions.cpp +++ b/src/util/assertions.cpp @@ -19,22 +19,26 @@ #include "Util.hpp" #include +#include #include +namespace fs = util::filesystem; + namespace util { void -handle_failed_assertion(const char* file, +handle_failed_assertion(const fs::path& file, size_t line, const char* function, const char* condition) { PRINT(stderr, "ccache: {}:{}: {}: failed assertion: {}\n", - Util::base_name(file), + file.filename(), line, function, condition); abort(); } + } // namespace util diff --git a/src/util/assertions.hpp b/src/util/assertions.hpp index cf8988315..5b632ad7d 100644 --- a/src/util/assertions.hpp +++ b/src/util/assertions.hpp @@ -19,6 +19,7 @@ #pragma once #include +#include #ifdef _MSC_VER # define CCACHE_FUNCTION __func__ @@ -46,7 +47,7 @@ namespace util { -[[noreturn]] void handle_failed_assertion(const char* file, +[[noreturn]] void handle_failed_assertion(const std::filesystem::path& file, size_t line, const char* function, const char* condition); diff --git a/unittest/test_Util.cpp b/unittest/test_Util.cpp index ba5949536..d01f960e6 100644 --- a/unittest/test_Util.cpp +++ b/unittest/test_Util.cpp @@ -48,16 +48,6 @@ namespace fs = util::filesystem; TEST_SUITE_BEGIN("Util"); -TEST_CASE("Util::base_name") -{ - CHECK(Util::base_name("") == ""); - CHECK(Util::base_name(".") == "."); - CHECK(Util::base_name("foo") == "foo"); - CHECK(Util::base_name("/") == ""); - CHECK(Util::base_name("/foo") == "foo"); - CHECK(Util::base_name("/foo/bar/f.txt") == "f.txt"); -} - TEST_CASE("Util::change_extension") { CHECK(Util::change_extension("", "") == "");