]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
C++-ify is_absolute_path
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 23 Feb 2020 08:43:41 +0000 (09:43 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 23 Feb 2020 08:43:41 +0000 (09:43 +0100)
src/Config.cpp
src/Util.cpp
src/Util.hpp
src/ccache.cpp
src/execute.cpp
src/legacy_util.cpp
src/legacy_util.hpp
unittest/test_Util.cpp

index 682efe9b2cc6e8c6c5d66ee253b41c11243e7f2b..831dd9b1d60335c1f11d3922d30a654fe19bfba6 100644 (file)
@@ -363,7 +363,7 @@ parse_unsigned(const std::string& value)
 void
 verify_absolute_path(const std::string& value)
 {
-  if (!is_absolute_path(value.c_str())) {
+  if (!Util::is_absolute_path(value)) {
     throw Error(fmt::format("not an absolute path: \"{}\"", value));
   }
 }
index 058a1cc7264e879bcff9bf3d0aa72740f19dd0be..35d8635ae9cd0d27b86cf498bae104249c61db78 100644 (file)
@@ -288,6 +288,17 @@ get_truncated_base_name(string_view path, size_t max_length)
   return input_base.substr(0, truncate_pos);
 }
 
+bool
+is_absolute_path(string_view path)
+{
+#ifdef _WIN32
+  return path.length() >= 2 && path[1] == ':'
+         && (path[2] == '/' || path[2] == '\\');
+#else
+  return !path.empty() && path[0] == '/';
+#endif
+}
+
 int
 parse_int(const std::string& value)
 {
index c438171cd0aab7c6dbe27347d2d7324c7545d08b..bc05d42c0226e83e7f73702f9faf81cee35705d7 100644 (file)
@@ -189,6 +189,9 @@ 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);
+
 // Parse a string into an integer.
 //
 // Throws Error on error.
index 56a6ad2a939e2a6de64f4e59fd9b1c87d00cffea..f238a5150a9b617b0f19dd9f3d6e08c7b9d18337 100644 (file)
@@ -812,7 +812,7 @@ process_preprocessed_file(Context& ctx,
       // p and q span the include file path.
       char* inc_path = x_strndup(p, q - p);
       if (!ctx.has_absolute_include_headers) {
-        ctx.has_absolute_include_headers = is_absolute_path(inc_path);
+        ctx.has_absolute_include_headers = Util::is_absolute_path(inc_path);
       }
       char* saved_inc_path = inc_path;
       inc_path = x_strdup(make_relative_path(ctx, inc_path).c_str());
@@ -921,7 +921,7 @@ use_relative_paths_in_depfile(const Context& ctx)
     char* token = strtok_r(buf, " \t", &saveptr);
     while (token) {
       char* relpath = nullptr;
-      if (is_absolute_path(token)
+      if (Util::is_absolute_path(token)
           && str_startswith(token, ctx.config.base_dir().c_str())) {
         relpath = x_strdup(make_relative_path(ctx, token).c_str());
         result = true;
@@ -1000,7 +1000,7 @@ result_name_from_depfile(Context& ctx, struct hash* hash)
         continue;
       }
       if (!ctx.has_absolute_include_headers) {
-        ctx.has_absolute_include_headers = is_absolute_path(token);
+        ctx.has_absolute_include_headers = Util::is_absolute_path(token);
       }
       std::string path = make_relative_path(ctx, token);
       remember_include_file(ctx, path, hash, false, hash);
index 8b40688705f5970c0d57ed9bc1c34271a6c43981..b47741950fe5682cb93d41db2f8c0a1cced5ab56 100644 (file)
@@ -304,7 +304,7 @@ execute(char** argv, int fd_out, int fd_err, pid_t* pid)
 char*
 find_executable(const Context& ctx, const char* name, const char* exclude_name)
 {
-  if (is_absolute_path(name)) {
+  if (Util::is_absolute_path(name)) {
     return x_strdup(name);
   }
 
index 40ada66bc0f5abc227645fb1e8de06e74a43ed4b..f7c43b3242e4fc267616f55bf3c77967587cfe82 100644 (file)
@@ -740,10 +740,10 @@ get_relative_path(const char* from, const char* to)
   size_t common_prefix_len;
   char* result;
 
-  assert(from && is_absolute_path(from));
+  assert(from && Util::is_absolute_path(from));
   assert(to);
 
-  if (!*to || !is_absolute_path(to)) {
+  if (!*to || !Util::is_absolute_path(to)) {
     return x_strdup(to);
   }
 
@@ -784,17 +784,6 @@ get_relative_path(const char* from, const char* to)
   return result;
 }
 
-// Return whether path is absolute.
-bool
-is_absolute_path(const char* path)
-{
-#ifdef _WIN32
-  return path[0] && path[1] == ':';
-#else
-  return path[0] == '/';
-#endif
-}
-
 // Return whether the argument is a full path.
 bool
 is_full_path(const char* path)
index 4c549a277f75bfdef6e2ad3bd7548b295ae02850..9acd09781e71e88c8a0d189bb5e7c14e9d950a51 100644 (file)
@@ -56,7 +56,6 @@ const char* get_home_directory();
 bool same_executable_name(const char* s1, const char* s2);
 size_t common_dir_prefix_length(const char* s1, const char* s2);
 char* get_relative_path(const char* from, const char* to);
-bool is_absolute_path(const char* path);
 bool is_full_path(const char* path);
 void update_mtime(const char* path);
 void x_exit(int status) ATTR_NORETURN;
index 7432e0cd51b812918bc2e504f7af32e14c1eb49f..7f6bb79b083c21093c93ef0971f541cd7d80ca7e 100644 (file)
@@ -301,6 +301,22 @@ 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(""));
+  CHECK(!Util::is_absolute_path("foo\\fie/fum"));
+  CHECK(!Util::is_absolute_path("C:foo/fie"));
+#else
+  CHECK(Util::is_absolute_path("/"));
+  CHECK(Util::is_absolute_path("/foo/fie"));
+  CHECK(!Util::is_absolute_path(""));
+  CHECK(!Util::is_absolute_path("foo/fie"));
+#endif
+}
+
 TEST_CASE("Util::parse_int")
 {
   CHECK(Util::parse_int("0") == 0);