From: Joel Rosdahl Date: Sat, 9 Dec 2023 12:44:17 +0000 (+0100) Subject: refactor: Replace util::is_absolute_path with fs::path::is_absolute X-Git-Tag: v4.9~12 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=887de1a04e60325c7a83de6bd375fa92655663ab;p=thirdparty%2Fccache.git refactor: Replace util::is_absolute_path with fs::path::is_absolute --- diff --git a/src/Config.cpp b/src/Config.cpp index 71bdc354c..0e9e641fe 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -385,7 +385,7 @@ format_umask(std::optional umask) void verify_absolute_path(const std::string& value) { - if (!util::is_absolute_path(value)) { + if (!fs::path(value).is_absolute()) { throw core::Error(FMT("not an absolute path: \"{}\"", value)); } } diff --git a/src/Depfile.cpp b/src/Depfile.cpp index b8d1cdd2a..4da675871 100644 --- a/src/Depfile.cpp +++ b/src/Depfile.cpp @@ -26,12 +26,15 @@ #include #include #include +#include #include #include #include #include +namespace fs = util::filesystem; + static inline bool is_blank(const std::string& s) { @@ -95,7 +98,7 @@ rewrite_source_paths(const Context& ctx, std::string_view file_content) const auto& token = tokens[i]; bool token_rewritten = false; - if (seen_target_token && util::is_absolute_path(token)) { + if (seen_target_token && fs::path(token).is_absolute()) { const auto new_path = Util::make_relative_path(ctx, token); if (new_path != token) { adjusted_file_content.append(new_path); diff --git a/src/Util.cpp b/src/Util.cpp index 44993dc36..772b67f1f 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -143,8 +143,8 @@ get_extension(std::string_view path) std::string get_relative_path(std::string_view dir, std::string_view path) { - ASSERT(util::is_absolute_path(dir)); - ASSERT(util::is_absolute_path(path)); + ASSERT(fs::path(dir).is_absolute()); + ASSERT(fs::path(path).is_absolute()); #ifdef _WIN32 // Paths can be escaped by a slash for use with e.g. -isystem. @@ -289,7 +289,7 @@ make_relative_path(const Context& ctx, std::string_view path) static std::string do_normalize_abstract_absolute_path(std::string_view path) { - if (!util::is_absolute_path(path)) { + if (!fs::path(path).is_absolute()) { return std::string(path); } diff --git a/src/ccache.cpp b/src/ccache.cpp index 833c667d3..9647d3eda 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -1509,7 +1509,7 @@ hash_common_info(const Context& ctx, // filename is included in the hash anyway. if (ctx.config.is_compiler_group_msvc() && ctx.config.hash_dir()) { const std::string output_obj_dir = - util::is_absolute_path(args_info.output_obj) + fs::path(args_info.output_obj).is_absolute() ? std::string(Util::dir_name(args_info.output_obj)) : ctx.actual_cwd; LOG("Hashing object file directory {}", output_obj_dir); @@ -1942,7 +1942,7 @@ hash_profiling_related_data(const Context& ctx, Hash& hash) // the profile filename so we need to include the same information in the // hash. const std::string profile_path = - util::is_absolute_path(ctx.args_info.profile_path) + fs::path(ctx.args_info.profile_path).is_absolute() ? ctx.args_info.profile_path : FMT("{}/{}", ctx.apparent_cwd, ctx.args_info.profile_path); LOG("Adding profile directory {} to our hash", profile_path); diff --git a/src/core/Result.cpp b/src/core/Result.cpp index 5694fdcef..b14b0232e 100644 --- a/src/core/Result.cpp +++ b/src/core/Result.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -48,6 +49,8 @@ #include +namespace fs = util::filesystem; + // Result data format // ================== // @@ -156,7 +159,7 @@ gcno_file_in_mangled_form(const Context& ctx) { const auto& output_obj = ctx.args_info.output_obj; const std::string abs_output_obj = - util::is_absolute_path(output_obj) + fs::path(output_obj).is_absolute() ? output_obj : FMT("{}/{}", ctx.apparent_cwd, output_obj); std::string hashified_obj = abs_output_obj; diff --git a/src/execute.cpp b/src/execute.cpp index 246510241..776bfbdce 100644 --- a/src/execute.cpp +++ b/src/execute.cpp @@ -351,7 +351,7 @@ find_executable(const Context& ctx, const std::string& name, const std::string& exclude_path) { - if (util::is_absolute_path(name)) { + if (fs::path(name).is_absolute()) { return name; } diff --git a/src/util/path.cpp b/src/util/path.cpp index bfa955aa0..412c82c06 100644 --- a/src/util/path.cpp +++ b/src/util/path.cpp @@ -66,7 +66,7 @@ apparent_cwd(const std::string& actual_cwd) return actual_cwd; #else auto pwd = getenv("PWD"); - if (!pwd || !is_absolute_path(pwd)) { + if (!pwd || !fs::path(pwd).is_absolute()) { return actual_cwd; } @@ -84,18 +84,6 @@ get_dev_null_path() return k_dev_null_path; } -bool -is_absolute_path(std::string_view path) -{ -#ifdef _WIN32 - if (path.length() >= 3 && path[1] == ':' - && (path[2] == '/' || path[2] == '\\')) { - return true; - } -#endif - return !path.empty() && path[0] == '/'; -} - bool path_starts_with(std::string_view path, std::string_view prefix) { diff --git a/src/util/path.hpp b/src/util/path.hpp index 68c120708..5a29a2d6f 100644 --- a/src/util/path.hpp +++ b/src/util/path.hpp @@ -44,9 +44,6 @@ std::string apparent_cwd(const std::string& actual_cwd); const char* get_dev_null_path(); -// Return whether `path` is absolute. -bool is_absolute_path(std::string_view path); - // Return whether `path` is /dev/null or (on Windows) NUL. bool is_dev_null_path(std::string_view path); diff --git a/unittest/test_util_path.cpp b/unittest/test_util_path.cpp index f52a50f37..882c7e88d 100644 --- a/unittest/test_util_path.cpp +++ b/unittest/test_util_path.cpp @@ -32,24 +32,6 @@ TEST_CASE("util::add_exe_suffix") CHECK(util::add_exe_suffix("foo.sh") == "foo.sh"); } -TEST_CASE("util::is_absolute_path") -{ -#ifdef _WIN32 - CHECK(util::is_absolute_path("C:/")); - CHECK(util::is_absolute_path("C:\\")); - CHECK(util::is_absolute_path("C:\\foo/fie")); - CHECK(util::is_absolute_path("/C:\\foo/fie")); // MSYS/Cygwin path - CHECK(!util::is_absolute_path("")); - CHECK(!util::is_absolute_path("C:")); - CHECK(!util::is_absolute_path("foo\\fie/fum")); - CHECK(!util::is_absolute_path("C:foo/fie")); -#endif - CHECK(util::is_absolute_path("/")); - CHECK(util::is_absolute_path("/foo/fie")); - CHECK(!util::is_absolute_path("")); - CHECK(!util::is_absolute_path("foo/fie")); -} - TEST_CASE("util::is_full_path") { CHECK(!util::is_full_path(""));