]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Convert usage of Util::base_name to std::filesystem
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 31 Dec 2023 10:10:27 +0000 (11:10 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 7 Jan 2024 08:57:37 +0000 (09:57 +0100)
src/Util.cpp
src/Util.hpp
src/argprocessing.cpp
src/ccache.cpp
src/core/mainoptions.cpp
src/storage/local/LocalStorage.cpp
src/util/assertions.cpp
src/util/assertions.hpp
unittest/test_Util.cpp

index 772b67f1f9defd7c2d927f0c3a0fd003375c9aaa..c93e760cbc2f77ee2ed4993c2f8bd3b1c155a4ef 100644 (file)
@@ -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)
 {
index 48e543cec97a1b7722cbad600f12df7bbde869df..6ebe6d85dcb3e8b6af08220daf3736a591832188 100644 (file)
@@ -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);
index 93046e13a9b00c8b5441ed421b1c39338f8fbcd0..f6b80e0dc0fefc620f88e3436e863e9aff64ad03 100644 (file)
@@ -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.
index 9647d3eda21332f72499aa0fda7bc4c7c76c5fa3..6cb38b98a72eb76b54d49c060d781c015ee24e82 100644 (file)
@@ -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
index 826ce5235e6f35599c9b1d857874107913b1ead8..cdb5c9ab2a7e5bbd5ee2995c2ea3a986e3259cce 100644 (file)
@@ -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 <util/environment.hpp>
 #include <util/expected.hpp>
 #include <util/file.hpp>
+#include <util/filesystem.hpp>
 #include <util/fmtmacros.hpp>
 #include <util/logging.hpp>
 #include <util/string.hpp>
@@ -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;
     }
   }
index 51f675676a0f18eac1b32b496ecc062980943a44..99b0d40aeeb3afdf8d27f0b2e9071c26e81af805 100644 (file)
@@ -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()));
     }
   }
 }
index dd32cc527e1c626c94cc1623256ed34ca3ec43b1..f330156e08d1b15f7eca3b17a025450728640ac1 100644 (file)
 #include "Util.hpp"
 
 #include <util/assertions.hpp>
+#include <util/filesystem.hpp>
 #include <util/fmtmacros.hpp>
 
+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
index cf898831592a2571b60ddde1a22f67fe6fe981c3..5b632ad7dcdb5e07b3e4d68db2f1dfc6d4da0ac7 100644 (file)
@@ -19,6 +19,7 @@
 #pragma once
 
 #include <cstddef>
+#include <filesystem>
 
 #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);
index ba5949536b8d91d718ea726cb3370dc04c8e7873..d01f960e6b9ffe8017bde45916107206af185be6 100644 (file)
@@ -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("", "") == "");