]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Convert usage of Util::change_extension to std::filesystem
authorJoel Rosdahl <joel@rosdahl.net>
Tue, 2 Jan 2024 14:12:08 +0000 (15:12 +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/core/Result.cpp
src/core/ResultRetriever.cpp
unittest/test_Util.cpp

index 438c6068c99ead7d3e5be557cedc822bc49581d4..5f2e791c3e4c7195469bcbde25ef5b3a2347e039 100644 (file)
@@ -45,13 +45,6 @@ using util::DirEntry;
 
 namespace Util {
 
-std::string
-change_extension(std::string_view path, std::string_view new_ext)
-{
-  std::string_view without_ext = Util::remove_extension(path);
-  return std::string(without_ext).append(new_ext.data(), new_ext.length());
-}
-
 size_t
 common_dir_prefix_length(std::string_view dir, std::string_view path)
 {
index 772899b9f332557bcc3482f1215cad0ac1cc4d1b..f8a6c3e0574294ba34d065aafa2a657bb3d42be0 100644 (file)
@@ -35,10 +35,6 @@ class Context;
 
 namespace Util {
 
-// 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);
-
 // Compute the length of the longest directory path that is common to paths
 // `dir` (a directory) and `path` (any path).
 size_t common_dir_prefix_length(std::string_view dir, std::string_view path);
index 1e8ca3622dee75dd682f1d2f66d452463bf486a4..c5545b55f6c317f4764f928d172b66840ae10877 100644 (file)
@@ -150,10 +150,10 @@ detect_pch(const std::string& option,
       pch_file = included_pch_file;
       included_pch_file.clear(); // reset pch file set from /Fp
     } else {
-      std::string file = Util::change_extension(arg, ".pch");
-      if (DirEntry(file).is_regular_file()) {
+      fs::path file = fs::path(arg).replace_extension(".pch");
+      if (fs::is_regular_file(file)) {
         LOG("Detected use of precompiled header: {}", file);
-        pch_file = file;
+        pch_file = file.string();
       }
     }
   } else if (option == "-Fp") {
@@ -1272,8 +1272,10 @@ process_args(Context& ctx)
     } else {
       extension = get_default_object_file_extension(ctx.config);
     }
-    args_info.output_obj += Util::change_extension(
-      fs::path(args_info.input_file).filename().string(), extension);
+    args_info.output_obj += fs::path(args_info.input_file)
+                              .filename()
+                              .replace_extension(extension)
+                              .string();
   }
 
   args_info.orig_output_obj = args_info.output_obj;
@@ -1402,7 +1404,7 @@ process_args(Context& ctx)
       args_info.seen_split_dwarf = false;
     } else {
       args_info.output_dwo =
-        Util::change_extension(args_info.output_obj, ".dwo");
+        fs::path(args_info.output_obj).replace_extension(".dwo").string();
     }
   }
 
@@ -1463,7 +1465,8 @@ process_args(Context& ctx)
 
   if (args_info.generating_dependencies) {
     if (state.output_dep_origin == OutputDepOrigin::none) {
-      args_info.output_dep = Util::change_extension(args_info.output_obj, ".d");
+      args_info.output_dep =
+        fs::path(args_info.output_obj).replace_extension(".d").string();
       if (!config.run_second_cpp()) {
         // If we're compiling preprocessed code we're sending dep_args to the
         // preprocessor so we need to use -MF to write to the correct .d file
@@ -1496,9 +1499,11 @@ process_args(Context& ctx)
         } else if (config.compiler_type() == CompilerType::gcc) {
           // GCC strangely uses the base name of the source file but with a .o
           // extension.
-          dep_target = Util::change_extension(
-            fs::path(args_info.orig_input_file).filename().string(),
-            get_default_object_file_extension(ctx.config));
+          dep_target =
+            fs::path(args_info.orig_input_file)
+              .filename()
+              .replace_extension(get_default_object_file_extension(ctx.config))
+              .string();
         } else {
           // How other compilers behave is currently unknown, so bail out.
           LOG_RAW(
@@ -1513,8 +1518,8 @@ process_args(Context& ctx)
   }
 
   if (args_info.generating_stackusage) {
-    auto default_sufile_name =
-      Util::change_extension(args_info.output_obj, ".su");
+    std::string default_sufile_name =
+      fs::path(args_info.output_obj).replace_extension(".su").string();
     args_info.output_su = Util::make_relative_path(ctx, default_sufile_name);
   }
 
index b14b0232eff5198f03de71ef81d72ead3c9f564d..5189c30a1e8e887b55170073d55db8eefa09d52a 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2019-2023 Joel Rosdahl and other contributors
+// Copyright (C) 2019-2024 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -20,7 +20,6 @@
 
 #include "Config.hpp"
 #include "Context.hpp"
-#include "Util.hpp"
 
 #include <ccache.hpp>
 #include <core/CacheEntryDataReader.hpp>
@@ -164,13 +163,13 @@ gcno_file_in_mangled_form(const Context& ctx)
       : FMT("{}/{}", ctx.apparent_cwd, output_obj);
   std::string hashified_obj = abs_output_obj;
   std::replace(hashified_obj.begin(), hashified_obj.end(), '/', '#');
-  return Util::change_extension(hashified_obj, ".gcno");
+  return fs::path(hashified_obj).replace_extension(".gcno").string();
 }
 
 std::string
 gcno_file_in_unmangled_form(const Context& ctx)
 {
-  return Util::change_extension(ctx.args_info.output_obj, ".gcno");
+  return fs::path(ctx.args_info.output_obj).replace_extension(".gcno").string();
 }
 
 Deserializer::Deserializer(nonstd::span<const uint8_t> data) : m_data(data)
index 768ec6e1279a30eb50a750d8ce1d4fd2b8f6083b..9b8bc2ebf64def3bce8db44b4bae8c4c4f3d8184 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.
 //
@@ -22,7 +22,6 @@
 #include "Depfile.hpp"
 
 #include <Context.hpp>
-#include <Util.hpp>
 #include <core/MsvcShowIncludesOutput.hpp>
 #include <core/common.hpp>
 #include <core/exceptions.hpp>
@@ -30,6 +29,7 @@
 #include <util/Fd.hpp>
 #include <util/expected.hpp>
 #include <util/file.hpp>
+#include <util/filesystem.hpp>
 #include <util/fmtmacros.hpp>
 #include <util/logging.hpp>
 #include <util/path.hpp>
@@ -44,6 +44,8 @@
 #  include <unistd.h>
 #endif
 
+namespace fs = util::filesystem;
+
 using util::DirEntry;
 
 namespace core {
@@ -164,7 +166,9 @@ ResultRetriever::get_dest_path(FileType file_type) const
 
   case FileType::coverage_unmangled:
     if (m_ctx.args_info.generating_coverage) {
-      return Util::change_extension(m_ctx.args_info.output_obj, ".gcno");
+      return fs::path(m_ctx.args_info.output_obj)
+        .replace_extension(".gcno")
+        .string();
     }
     break;
 
index cb0f15272ee263cb74c85f2e0b308f7acf7cbe67..dde50d97bdda6dbac4b237b557de49a561619e16 100644 (file)
@@ -48,21 +48,6 @@ namespace fs = util::filesystem;
 
 TEST_SUITE_BEGIN("Util");
 
-TEST_CASE("Util::change_extension")
-{
-  CHECK(Util::change_extension("", "") == "");
-  CHECK(Util::change_extension("x", "") == "x");
-  CHECK(Util::change_extension("", "x") == "x");
-  CHECK(Util::change_extension("", ".") == ".");
-  CHECK(Util::change_extension(".", "") == ".");
-  CHECK(Util::change_extension("...", "x") == "..x");
-  CHECK(Util::change_extension("abc", "def") == "abcdef");
-  CHECK(Util::change_extension("dot.", ".dot") == "dot.dot");
-  CHECK(Util::change_extension("foo.ext", "e2") == "fooe2");
-  CHECK(Util::change_extension("bar.txt", ".o") == "bar.o");
-  CHECK(Util::change_extension("foo.bar.txt", ".o") == "foo.bar.o");
-}
-
 TEST_CASE("Util::common_dir_prefix_length")
 {
   CHECK(Util::common_dir_prefix_length("", "") == 0);