]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Move is_absolute_path to util
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 4 Jul 2021 04:59:07 +0000 (06:59 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 4 Jul 2021 10:12:10 +0000 (12:12 +0200)
13 files changed:
src/Config.cpp
src/Depfile.cpp
src/Result.cpp
src/Util.cpp
src/Util.hpp
src/ccache.cpp
src/execute.cpp
src/util/CMakeLists.txt
src/util/path_utils.cpp [new file with mode: 0644]
src/util/path_utils.hpp [new file with mode: 0644]
unittest/CMakeLists.txt
unittest/test_Util.cpp
unittest/test_util_path_utils.cpp [new file with mode: 0644]

index 2163c68b3355d1fcd2605d2e83dbb1b70ffc76c3..7eed171f1d1456069ac9eb4592c425022c556f73 100644 (file)
@@ -27,6 +27,7 @@
 #include "exceptions.hpp"
 #include "fmtmacros.hpp"
 
+#include <util/path_utils.hpp>
 #include <util/string_utils.hpp>
 
 #include "third_party/fmt/core.h"
@@ -349,7 +350,7 @@ format_umask(nonstd::optional<mode_t> umask)
 void
 verify_absolute_path(const std::string& value)
 {
-  if (!Util::is_absolute_path(value)) {
+  if (!util::is_absolute_path(value)) {
     throw Error("not an absolute path: \"{}\"", value);
   }
 }
index 13106ce7fb576cb7c092ba043de5c3bc5eac0b18..ec49f4418bde9008d9aa77a164b0c1f0f24d129b 100644 (file)
@@ -23,6 +23,8 @@
 #include "Logging.hpp"
 #include "assertions.hpp"
 
+#include <util/path_utils.hpp>
+
 static inline bool
 is_blank(const std::string& s)
 {
@@ -79,7 +81,7 @@ rewrite_paths(const Context& ctx, const std::string& file_content)
 
       const auto& token = tokens[i];
       bool token_rewritten = false;
-      if (Util::is_absolute_path(token)) {
+      if (util::is_absolute_path(token)) {
         const auto new_path = Util::make_relative_path(ctx, token);
         if (new_path != token) {
           adjusted_file_content.append(new_path);
index 86ba5a24a70a3a45b1909cb9bb75217b7cfeb924..3633c2cba5ac14322bdc3e584124676f07530dbb 100644 (file)
@@ -32,6 +32,8 @@
 #include "exceptions.hpp"
 #include "fmtmacros.hpp"
 
+#include <util/path_utils.hpp>
+
 #include <algorithm>
 
 // Result data format
@@ -182,7 +184,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)
+    util::is_absolute_path(output_obj)
       ? output_obj
       : FMT("{}/{}", ctx.apparent_cwd, output_obj);
   std::string hashified_obj = abs_output_obj;
index 92bf7892a4c290540eb0eec3e2dd80bf475bd469..e4ad446736bd2cb7d0a7e3d781e72f36ce39e6b7 100644 (file)
@@ -27,6 +27,7 @@
 #include "fmtmacros.hpp"
 
 #include <util/Tokenizer.hpp>
+#include <util/path_utils.hpp>
 
 extern "C" {
 #include "third_party/base32hex.h"
@@ -605,7 +606,7 @@ get_apparent_cwd(const std::string& actual_cwd)
   return actual_cwd;
 #else
   auto pwd = getenv("PWD");
-  if (!pwd || !Util::is_absolute_path(pwd)) {
+  if (!pwd || !util::is_absolute_path(pwd)) {
     return actual_cwd;
   }
 
@@ -716,8 +717,8 @@ get_hostname()
 std::string
 get_relative_path(string_view dir, string_view path)
 {
-  ASSERT(Util::is_absolute_path(dir));
-  ASSERT(Util::is_absolute_path(path));
+  ASSERT(util::is_absolute_path(dir));
+  ASSERT(util::is_absolute_path(path));
 
 #ifdef _WIN32
   // Paths can be escaped by a slash for use with e.g. -isystem.
@@ -802,18 +803,6 @@ hard_link(const std::string& oldpath, const std::string& newpath)
 #endif
 }
 
-bool
-is_absolute_path(string_view path)
-{
-#ifdef _WIN32
-  if (path.length() >= 2 && path[1] == ':'
-      && (path[2] == '/' || path[2] == '\\')) {
-    return true;
-  }
-#endif
-  return !path.empty() && path[0] == '/';
-}
-
 #if defined(HAVE_LINUX_FS_H) || defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
 int
 is_nfs_fd(int fd, bool* is_nfs)
@@ -946,7 +935,7 @@ matches_dir_prefix_or_file(string_view dir_prefix_or_file, string_view path)
 std::string
 normalize_absolute_path(string_view path)
 {
-  if (!is_absolute_path(path)) {
+  if (!util::is_absolute_path(path)) {
     return std::string(path);
   }
 
index e7ca46009e1632b0fc4dbd7cdadb76403e1442bd..6e3ed4a46d8547e283538fc5d03f16022ae0820f 100644 (file)
@@ -265,9 +265,6 @@ int_to_big_endian(int8_t value, uint8_t* buffer)
   buffer[0] = value;
 }
 
-// Return whether `path` is absolute.
-bool is_absolute_path(nonstd::string_view path);
-
 // Test if a file is on nfs.
 //
 // Sets is_nfs to the result if fstatfs is available and no error occurred.
index f41a323fcb801882ad336719f25e32e785608b14..a7cc3a3135354295b3c75ecbfca8dc63acaf12ef 100644 (file)
@@ -55,6 +55,7 @@
 #include "language.hpp"
 
 #include <core/types.hpp>
+#include <util/path_utils.hpp>
 
 #include "third_party/fmt/core.h"
 #include "third_party/nonstd/optional.hpp"
@@ -277,7 +278,7 @@ guess_compiler(string_view path)
     if (symlink_value.empty()) {
       break;
     }
-    if (Util::is_absolute_path(symlink_value)) {
+    if (util::is_absolute_path(symlink_value)) {
       compiler_path = symlink_value;
     } else {
       compiler_path =
@@ -608,7 +609,7 @@ process_preprocessed_file(Context& ctx,
       // p and q span the include file path.
       std::string inc_path(p, q - p);
       if (!ctx.has_absolute_include_headers) {
-        ctx.has_absolute_include_headers = Util::is_absolute_path(inc_path);
+        ctx.has_absolute_include_headers = util::is_absolute_path(inc_path);
       }
       inc_path = Util::make_relative_path(ctx, inc_path);
 
@@ -700,7 +701,7 @@ result_key_from_depfile(Context& ctx, Hash& hash)
       continue;
     }
     if (!ctx.has_absolute_include_headers) {
-      ctx.has_absolute_include_headers = Util::is_absolute_path(token);
+      ctx.has_absolute_include_headers = util::is_absolute_path(token);
     }
     std::string path = Util::make_relative_path(ctx, token);
     remember_include_file(ctx, path, hash, false, &hash);
@@ -1651,7 +1652,7 @@ calculate_result_and_manifest_key(Context& ctx,
     // 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)
+      util::is_absolute_path(ctx.args_info.profile_path)
         ? ctx.args_info.profile_path
         : FMT("{}/{}", ctx.apparent_cwd, ctx.args_info.profile_path);
     LOG("Adding profile directory {} to our hash", profile_path);
index ebcf2becbeab1c6372e0a98e28a9126845e7e962..86d0a8084a2393037e870178b524f96c834b7b75 100644 (file)
@@ -29,6 +29,8 @@
 #include "Util.hpp"
 #include "fmtmacros.hpp"
 
+#include <util/path_utils.hpp>
+
 #ifdef _WIN32
 #  include "Finalizer.hpp"
 #  include "Win32Util.hpp"
@@ -241,7 +243,7 @@ find_executable(const Context& ctx,
                 const std::string& name,
                 const std::string& exclude_name)
 {
-  if (Util::is_absolute_path(name)) {
+  if (util::is_absolute_path(name)) {
     return name;
   }
 
index a9e1f0438ad37be930c70bc934337e8a06dc6e64..669f92b07cc93728ddcce3458583b3f06ee1fe51 100644 (file)
@@ -1,7 +1,8 @@
 set(
   sources
-  ${CMAKE_CURRENT_SOURCE_DIR}/file_utils.cpp
   ${CMAKE_CURRENT_SOURCE_DIR}/Tokenizer.cpp
+  ${CMAKE_CURRENT_SOURCE_DIR}/file_utils.cpp
+  ${CMAKE_CURRENT_SOURCE_DIR}/path_utils.cpp
   ${CMAKE_CURRENT_SOURCE_DIR}/string_utils.cpp
 )
 
diff --git a/src/util/path_utils.cpp b/src/util/path_utils.cpp
new file mode 100644 (file)
index 0000000..ef0ffa3
--- /dev/null
@@ -0,0 +1,39 @@
+// Copyright (C) 2021 Joel Rosdahl and other contributors
+//
+// See doc/AUTHORS.adoc for a complete list of contributors.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along with
+// this program; if not, write to the Free Software Foundation, Inc., 51
+// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#include "path_utils.hpp"
+
+#include <Logging.hpp>
+#include <Util.hpp>
+#include <fmtmacros.hpp>
+
+namespace util {
+
+bool
+is_absolute_path(nonstd::string_view path)
+{
+#ifdef _WIN32
+  if (path.length() >= 2 && path[1] == ':'
+      && (path[2] == '/' || path[2] == '\\')) {
+    return true;
+  }
+#endif
+  return !path.empty() && path[0] == '/';
+}
+
+} // namespace util
diff --git a/src/util/path_utils.hpp b/src/util/path_utils.hpp
new file mode 100644 (file)
index 0000000..9423e57
--- /dev/null
@@ -0,0 +1,30 @@
+// Copyright (C) 2021 Joel Rosdahl and other contributors
+//
+// See doc/AUTHORS.adoc for a complete list of contributors.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along with
+// this program; if not, write to the Free Software Foundation, Inc., 51
+// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#pragma once
+
+#include <third_party/nonstd/string_view.hpp>
+
+#include <string>
+
+namespace util {
+
+// Return whether `path` is absolute.
+bool is_absolute_path(nonstd::string_view path);
+
+} // namespace util
index 68e462986ffe84d7a39510eda8064abbbcbdddb9..2d5b2d87049fa2224891bfaf5c9ad7685a97ee69 100644 (file)
@@ -23,6 +23,7 @@ set(
   test_hashutil.cpp
   test_util_Tokenizer.cpp
   test_util_string_utils.cpp
+  test_util_path_utils.cpp
 )
 
 if(INODE_CACHE_SUPPORTED)
index 92343b96622f425c9e075b008e4ceddb4cf80db5..07cdc21830f17af90002b18933498479c3aaf46b 100644 (file)
@@ -531,22 +531,6 @@ TEST_CASE("Util::int_to_big_endian")
   CHECK(bytes[7] == 0xca);
 }
 
-TEST_CASE("Util::is_absolute_path")
-{
-#ifdef _WIN32
-  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("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_dir_separator")
 {
   CHECK(!Util::is_dir_separator('x'));
diff --git a/unittest/test_util_path_utils.cpp b/unittest/test_util_path_utils.cpp
new file mode 100644 (file)
index 0000000..8f9e3f0
--- /dev/null
@@ -0,0 +1,37 @@
+// Copyright (C) 2021 Joel Rosdahl and other contributors
+//
+// See doc/AUTHORS.adoc for a complete list of contributors.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along with
+// this program; if not, write to the Free Software Foundation, Inc., 51
+// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#include <util/path_utils.hpp>
+
+#include <third_party/doctest.h>
+
+TEST_CASE("util::is_absolute_path")
+{
+#ifdef _WIN32
+  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("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"));
+}