]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Replace util::is_absolute_path with fs::path::is_absolute
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 9 Dec 2023 12:44:17 +0000 (13:44 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 10 Dec 2023 20:02:12 +0000 (21:02 +0100)
src/Config.cpp
src/Depfile.cpp
src/Util.cpp
src/ccache.cpp
src/core/Result.cpp
src/execute.cpp
src/util/path.cpp
src/util/path.hpp
unittest/test_util_path.cpp

index 71bdc354cb49b9f638cee31fce7087d7076e329a..0e9e641fe2a8c5c353bfcc539abfe32210afdfa4 100644 (file)
@@ -385,7 +385,7 @@ format_umask(std::optional<mode_t> 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));
   }
 }
index b8d1cdd2a419833d07ec6db297b8a86db7132528..4da675871f0e8d58321ab1a59f11dd77c9adf718 100644 (file)
 #include <util/Tokenizer.hpp>
 #include <util/assertions.hpp>
 #include <util/file.hpp>
+#include <util/filesystem.hpp>
 #include <util/logging.hpp>
 #include <util/path.hpp>
 #include <util/string.hpp>
 
 #include <algorithm>
 
+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);
index 44993dc360d94e60dbfb3aaf9d89fdc04bf9cfc1..772b67f1f9defd7c2d927f0c3a0fd003375c9aaa 100644 (file)
@@ -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);
   }
 
index 833c667d30b561bc028ff4996d890dd9546b6f3d..9647d3eda21332f72499aa0fda7bc4c7c76c5fa3 100644 (file)
@@ -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);
index 5694fdcef3d2b76631f92836222c828112ba02fe..b14b0232eff5198f03de71ef81d72ead3c9f564d 100644 (file)
@@ -32,6 +32,7 @@
 #include <util/FileStream.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>
@@ -48,6 +49,8 @@
 
 #include <algorithm>
 
+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;
index 2465102411dffade500b9510f0b6550b0564f529..776bfbdced819b41c06e14b392856992e4ef8df4 100644 (file)
@@ -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;
   }
 
index bfa955aa0a3e3cda87a875236137fe1c9e2332ef..412c82c06c0ca55c546b927cdaa23da4dbf51bf1 100644 (file)
@@ -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)
 {
index 68c120708655840d6b04c97de3d27030ed3d20c0..5a29a2d6f4a1da88a07114860b17fc8f40a7596f 100644 (file)
@@ -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);
 
index f52a50f374ac4f8c224b4ef52676c200ffaa5488..882c7e88dc7b93885300dd67f6326d236e61c624 100644 (file)
@@ -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(""));