]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Make Util::is_absolute_path_with_prefix return optional
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 27 Feb 2022 19:11:47 +0000 (20:11 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 27 Feb 2022 19:12:12 +0000 (20:12 +0100)
src/Util.cpp
src/Util.hpp
src/argprocessing.cpp
unittest/test_Util.cpp

index 007006cb2485cab4241a5df7c3cf6dca2ac7860c..146ae7a9aee26765426eb69512d7cbbd89015b5b 100644 (file)
@@ -233,31 +233,6 @@ base_name(string_view path)
   return n == std::string::npos ? path : path.substr(n + 1);
 }
 
-bool
-is_absolute_path_with_prefix(nonstd::string_view path, size_t& split_pos)
-{
-#ifdef _WIN32
-  const char delim[] = "/\\";
-#else
-  const char delim[] = "/";
-#endif
-  split_pos = path.find_first_of(delim);
-  if (split_pos != std::string::npos) {
-#ifdef _WIN32
-    // -I/C:/foo and -I/c/foo will already be handled by delim_pos
-    // correctly resulting in -I and /C:/foo or /c/foo respectively.
-    // -IC:/foo will not as we would get -IC: and /foo
-    if (split_pos > 0 && path[split_pos - 1] == ':') {
-      split_pos = split_pos - 2;
-    }
-#endif
-    // this is not redundant on some platforms so nothing to simplify
-    // NOLINTNEXTLINE(readability-simplify-boolean-expr)
-    return true;
-  }
-  return false;
-}
-
 std::string
 change_extension(string_view path, string_view new_ext)
 {
@@ -808,6 +783,31 @@ hard_link(const std::string& oldpath, const std::string& newpath)
 #endif
 }
 
+nonstd::optional<size_t>
+is_absolute_path_with_prefix(nonstd::string_view path)
+{
+#ifdef _WIN32
+  const char delim[] = "/\\";
+#else
+  const char delim[] = "/";
+#endif
+  auto split_pos = path.find_first_of(delim);
+  if (split_pos != std::string::npos) {
+#ifdef _WIN32
+    // -I/C:/foo and -I/c/foo will already be handled by delim_pos correctly
+    // resulting in -I and /C:/foo or /c/foo respectively. -IC:/foo will not as
+    // we would get -IC: and /foo.
+    if (split_pos > 0 && path[split_pos - 1] == ':') {
+      split_pos = split_pos - 2;
+    }
+#endif
+    // This is not redundant on some platforms, so nothing to simplify.
+    // NOLINTNEXTLINE(readability-simplify-boolean-expr)
+    return split_pos;
+  }
+  return nonstd::nullopt;
+}
+
 #if defined(HAVE_LINUX_FS_H) || defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
 int
 is_nfs_fd(int fd, bool* is_nfs)
index b92a3d5ca4aa759f1edbfc69e8edc4baf490b7f5..9ad678c7b5e5e9b5c7856b660bc601a615edfa11 100644 (file)
@@ -46,10 +46,6 @@ enum class UnlinkLog { log_failure, ignore_failure };
 // Get base name of path.
 nonstd::string_view base_name(nonstd::string_view path);
 
-// Determine if `path` is an absolute path with prefix, returning the split
-// point
-bool is_absolute_path_with_prefix(nonstd::string_view path, size_t& split_pos);
-
 // Get an integer value from bytes in big endian order.
 //
 // Parameters:
@@ -229,6 +225,10 @@ int_to_big_endian(int8_t value, uint8_t* buffer)
   buffer[0] = value;
 }
 
+// Determine if `path` is an absolute path with prefix, returning the split
+// point.
+nonstd::optional<size_t> is_absolute_path_with_prefix(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 345cb9fa5e6fc06d562a03fc5047da645e4490b2..f8d90198bd575d8571850547b66f15d51aba2388 100644 (file)
@@ -919,12 +919,12 @@ process_arg(const Context& ctx,
 
   // Potentially rewrite concatenated absolute path argument to relative.
   if (args[i][0] == '-') {
-    size_t slash_pos = 0;
-    if (Util::is_absolute_path_with_prefix(args[i], slash_pos)) {
-      std::string option = args[i].substr(0, slash_pos);
+    const auto slash_pos = Util::is_absolute_path_with_prefix(args[i]);
+    if (slash_pos) {
+      std::string option = args[i].substr(0, *slash_pos);
       if (compopt_takes_concat_arg(option) && compopt_takes_path(option)) {
-        auto relpath =
-          Util::make_relative_path(ctx, string_view(args[i]).substr(slash_pos));
+        auto relpath = Util::make_relative_path(
+          ctx, string_view(args[i]).substr(*slash_pos));
         std::string new_option = option + relpath;
         if (compopt_affects_cpp_output(option)) {
           state.cpp_args.push_back(new_option);
index f1945d63a1c3239166272a801b56421cafb76c58..913b736246dbab3cd378b49145c8f7fd5288bc22 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2019-2021 Joel Rosdahl and other contributors
+// Copyright (C) 2019-2022 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -52,27 +52,6 @@ TEST_CASE("Util::base_name")
   CHECK(Util::base_name("/foo/bar/f.txt") == "f.txt");
 }
 
-TEST_CASE("Util::is_absolute_path_with_prefix")
-{
-  size_t delim_pos = 0;
-  CHECK(Util::is_absolute_path_with_prefix("-I/c/foo", delim_pos));
-  CHECK(delim_pos == 2);
-  CHECK(Util::is_absolute_path_with_prefix("-W,path/c/foo", delim_pos));
-  CHECK(delim_pos == 7);
-  CHECK(!Util::is_absolute_path_with_prefix("-DMACRO", delim_pos));
-#ifdef _WIN32
-  CHECK(Util::is_absolute_path_with_prefix("-I/C:/foo", delim_pos));
-  CHECK(delim_pos == 2);
-  CHECK(Util::is_absolute_path_with_prefix("-IC:/foo", delim_pos));
-  CHECK(delim_pos == 2);
-  CHECK(Util::is_absolute_path_with_prefix("-W,path/c:/foo", delim_pos));
-  CHECK(delim_pos == 7);
-  CHECK(Util::is_absolute_path_with_prefix("-W,pathc:/foo", delim_pos));
-  CHECK(delim_pos == 7);
-  CHECK(!Util::is_absolute_path_with_prefix("-opt:value", delim_pos));
-#endif
-}
-
 TEST_CASE("Util::big_endian_to_int")
 {
   uint8_t bytes[8] = {0x70, 0x9e, 0x9a, 0xbc, 0xd6, 0x54, 0x4b, 0xca};
@@ -457,6 +436,20 @@ TEST_CASE("Util::int_to_big_endian")
   CHECK(bytes[7] == 0xca);
 }
 
+TEST_CASE("Util::is_absolute_path_with_prefix")
+{
+  CHECK(*Util::is_absolute_path_with_prefix("-I/c/foo") == 2);
+  CHECK(*Util::is_absolute_path_with_prefix("-W,path/c/foo") == 7);
+  CHECK(!Util::is_absolute_path_with_prefix("-DMACRO"));
+#ifdef _WIN32
+  CHECK(*Util::is_absolute_path_with_prefix("-I/C:/foo") == 2);
+  CHECK(*Util::is_absolute_path_with_prefix("-IC:/foo") == 2);
+  CHECK(*Util::is_absolute_path_with_prefix("-W,path/c:/foo") == 7);
+  CHECK(*Util::is_absolute_path_with_prefix("-W,pathc:/foo") == 7);
+  CHECK(!Util::is_absolute_path_with_prefix("-opt:value"));
+#endif
+}
+
 TEST_CASE("Util::is_dir_separator")
 {
   CHECK(!Util::is_dir_separator('x'));