From: Joel Rosdahl Date: Tue, 2 Jan 2024 14:12:08 +0000 (+0100) Subject: refactor: Convert usage of Util::change_extension to std::filesystem X-Git-Tag: v4.10~138 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ea25e047c21fea4d2afde434fc65f73902a85dc5;p=thirdparty%2Fccache.git refactor: Convert usage of Util::change_extension to std::filesystem --- diff --git a/src/Util.cpp b/src/Util.cpp index 438c6068c..5f2e791c3 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -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) { diff --git a/src/Util.hpp b/src/Util.hpp index 772899b9f..f8a6c3e05 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -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); diff --git a/src/argprocessing.cpp b/src/argprocessing.cpp index 1e8ca3622..c5545b55f 100644 --- a/src/argprocessing.cpp +++ b/src/argprocessing.cpp @@ -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); } diff --git a/src/core/Result.cpp b/src/core/Result.cpp index b14b0232e..5189c30a1 100644 --- a/src/core/Result.cpp +++ b/src/core/Result.cpp @@ -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 #include @@ -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 data) : m_data(data) diff --git a/src/core/ResultRetriever.cpp b/src/core/ResultRetriever.cpp index 768ec6e12..9b8bc2ebf 100644 --- a/src/core/ResultRetriever.cpp +++ b/src/core/ResultRetriever.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. // @@ -22,7 +22,6 @@ #include "Depfile.hpp" #include -#include #include #include #include @@ -30,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -44,6 +44,8 @@ # include #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; diff --git a/unittest/test_Util.cpp b/unittest/test_Util.cpp index cb0f15272..dde50d97b 100644 --- a/unittest/test_Util.cpp +++ b/unittest/test_Util.cpp @@ -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);