]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Use nonstd::span for data+size in more places
authorJoel Rosdahl <joel@rosdahl.net>
Fri, 7 Jul 2023 13:35:33 +0000 (15:35 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 12 Jul 2023 09:07:42 +0000 (11:07 +0200)
17 files changed:
src/Hash.cpp
src/Hash.hpp
src/InodeCache.cpp
src/Util.cpp
src/core/mainoptions.cpp
src/execute.cpp
src/util/file.cpp
src/util/string.hpp
src/util/types.hpp
unittest/test_AtomicFile.cpp
unittest/test_Depfile.cpp
unittest/test_Util.cpp
unittest/test_ccache.cpp
unittest/test_core_Statistics.cpp
unittest/test_storage_local_util.cpp
unittest/test_util_Tokenizer.cpp
unittest/test_util_file.cpp

index ca68e0eb251289f5f4efda1ea3faa243e0a2f73a..63a0f76bd88f63c7dbf65e7637a50dc7b358b759 100644 (file)
@@ -34,7 +34,7 @@
 #  include <unistd.h>
 #endif
 
-const std::string_view HASH_DELIMITER("\000cCaChE\000", 8);
+const uint8_t HASH_DELIMITER[] = {0, 'c', 'C', 'a', 'C', 'h', 'E', 0};
 
 Hash::Hash()
 {
@@ -77,19 +77,17 @@ Hash::hash_delimiter(std::string_view type)
 }
 
 Hash&
-Hash::hash(const void* data, size_t size, HashType hash_type)
+Hash::hash(nonstd::span<const uint8_t> data, HashType hash_type)
 {
-  std::string_view buffer(static_cast<const char*>(data), size);
-  hash_buffer(buffer);
+  hash_buffer(data);
 
   switch (hash_type) {
   case HashType::binary:
-    add_debug_text(
-      util::format_base16({static_cast<const uint8_t*>(data), size}));
+    add_debug_text(util::format_base16(data));
     break;
 
   case HashType::text:
-    add_debug_text(buffer);
+    add_debug_text(util::to_string_view(data));
     break;
   }
 
@@ -97,10 +95,17 @@ Hash::hash(const void* data, size_t size, HashType hash_type)
   return *this;
 }
 
+Hash&
+Hash::hash(const char* data, size_t size)
+{
+  hash(util::to_span({data, size}), HashType::text);
+  return *this;
+}
+
 Hash&
 Hash::hash(std::string_view data)
 {
-  hash(data.data(), data.length());
+  hash(util::to_span(data), HashType::text);
   return *this;
 }
 
@@ -116,7 +121,7 @@ nonstd::expected<void, std::string>
 Hash::hash_fd(int fd)
 {
   return util::read_fd(
-    fd, [this](const void* data, size_t size) { hash(data, size); });
+    fd, [this](nonstd::span<const uint8_t> data) { hash(data); });
 }
 
 nonstd::expected<void, std::string>
@@ -132,7 +137,7 @@ Hash::hash_file(const std::string& path)
 }
 
 void
-Hash::hash_buffer(std::string_view buffer)
+Hash::hash_buffer(nonstd::span<const uint8_t> buffer)
 {
   blake3_hasher_update(&m_hasher, buffer.data(), buffer.size());
   if (!buffer.empty() && m_debug_binary) {
@@ -140,6 +145,12 @@ Hash::hash_buffer(std::string_view buffer)
   }
 }
 
+void
+Hash::hash_buffer(std::string_view buffer)
+{
+  hash_buffer(util::to_span(buffer));
+}
+
 void
 Hash::add_debug_text(std::string_view text)
 {
index a27ed88a6de9c60a4b417aee492c6cf9605821fa..de614d1c179b4c88ce289cd427a4fd7815934bff 100644 (file)
@@ -20,6 +20,7 @@
 
 #include "third_party/blake3/blake3.h"
 #include <third_party/nonstd/expected.hpp>
+#include <third_party/nonstd/span.hpp>
 
 #include <array>
 #include <cstdint>
@@ -67,10 +68,16 @@ public:
   //   verbatim to the text input file.
   //
   // In both cases a newline character is added as well.
-  Hash&
-  hash(const void* data, size_t size, HashType hash_type = HashType::text);
+  Hash& hash(nonstd::span<const uint8_t> data,
+             HashType hash_type = HashType::text);
 
-  // Add a string to the hash.
+  // Add string data to the hash.
+  //
+  // If hash debugging is enabled, the string is written to the text input file
+  // followed by a newline.
+  Hash& hash(const char* data, size_t size);
+
+  // Add string data to the hash.
   //
   // If hash debugging is enabled, the string is written to the text input file
   // followed by a newline.
@@ -102,5 +109,6 @@ private:
   FILE* m_debug_binary = nullptr;
   FILE* m_debug_text = nullptr;
 
+  void hash_buffer(nonstd::span<const uint8_t> buffer);
   void hash_buffer(std::string_view buffer);
 };
index 3d18f37af638cb3719d7a1747c6e5f84f10c431d..c4ceecd059d24586c9f2419bdd3c06d0891163de 100644 (file)
@@ -43,6 +43,7 @@
 
 #include <atomic>
 #include <type_traits>
+#include <vector>
 
 // The inode cache resides on a file that is mapped into shared memory by
 // running processes. It is implemented as a two level structure, where the top
@@ -300,7 +301,8 @@ InodeCache::hash_inode(const std::string& path,
   key.st_size = stat.size();
 
   Hash hash;
-  hash.hash(&key, sizeof(Key));
+  hash.hash(nonstd::span<const uint8_t>(reinterpret_cast<const uint8_t*>(&key),
+                                        sizeof(key)));
   digest = hash.digest();
   return true;
 }
index 3f769cf5582c7a2f13ba1609881d47d469cc5688..ec01a5d7a5adfb3fe00b011e78d055ee8dd05dc6 100644 (file)
@@ -335,8 +335,8 @@ common_dir_prefix_length(std::string_view dir, std::string_view path)
 void
 copy_fd(int fd_in, int fd_out)
 {
-  util::read_fd(fd_in, [=](const void* data, size_t size) {
-    util::write_fd(fd_out, data, size);
+  util::read_fd(fd_in, [=](nonstd::span<const uint8_t> data) {
+    util::write_fd(fd_out, data.data(), data.size());
   });
 }
 
index 051b9fa1612317e8c924db0f5847b41e4fd2eb6b..b32afc5bb895cfe167e9fd4e13fbbe99cec1fe11 100644 (file)
@@ -55,6 +55,7 @@
 #include <optional>
 #include <string>
 #include <thread>
+#include <vector>
 
 #ifdef HAVE_UNISTD_H
 #  include <unistd.h>
@@ -185,8 +186,8 @@ read_from_path_or_stdin(const std::string& path)
   if (path == "-") {
     std::vector<uint8_t> output;
     const auto result =
-      util::read_fd(STDIN_FILENO, [&](const uint8_t* data, size_t size) {
-        output.insert(output.end(), data, data + size);
+      util::read_fd(STDIN_FILENO, [&](nonstd::span<const uint8_t> data) {
+        output.insert(output.end(), data.begin(), data.end());
       });
     if (!result) {
       return nonstd::make_unexpected(
@@ -578,8 +579,8 @@ process_main_options(int argc, const char* const* argv)
       util::XXH3_128 checksum;
       Fd fd(arg == "-" ? STDIN_FILENO : open(arg.c_str(), O_RDONLY));
       if (fd) {
-        util::read_fd(*fd, [&checksum](const uint8_t* data, size_t size) {
-          checksum.update({data, size});
+        util::read_fd(*fd, [&checksum](nonstd::span<const uint8_t> data) {
+          checksum.update(data);
         });
         const auto digest = checksum.digest();
         PRINT(stdout, "{}\n", util::format_base16(digest));
index ce8dca9335a5fd4e3f87b1e3c7d3c0c7a6056f18..2b3b499a0fd74daa8cdd5b063cca713523572009 100644 (file)
@@ -35,6 +35,8 @@
 #include <util/file.hpp>
 #include <util/path.hpp>
 
+#include <vector>
+
 #ifdef HAVE_UNISTD_H
 #  include <unistd.h>
 #endif
index 57a6b560f128363166cc2918db420029b83e6dd9..7b80b584b214f9ed6d90db2ac47dbb7ead41d060 100644 (file)
@@ -52,6 +52,7 @@
 #include <fstream>
 #include <locale>
 #include <type_traits>
+#include <vector>
 
 namespace util {
 
@@ -85,7 +86,7 @@ read_fd(int fd, DataReceiver data_receiver)
       break;
     }
     if (n > 0) {
-      data_receiver(buffer, n);
+      data_receiver({buffer, static_cast<size_t>(n)});
     }
   }
   if (n == -1) {
index 32e13e0b005c7fa8389cf691ca096186e8a56fd5..1ef58a139a19f9cb244a2f059de657e05119a21c 100644 (file)
@@ -150,6 +150,9 @@ bool starts_with(std::string_view string, std::string_view prefix);
 // Strip whitespace from left and right side of a string.
 [[nodiscard]] std::string strip_whitespace(std::string_view string);
 
+// Convert `data` to a `nonstd::span<const uint8_t>`.
+nonstd::span<const uint8_t> to_span(const void* data, size_t size);
+
 // Convert `value` to a `nonstd::span<const uint8_t>`.
 nonstd::span<const uint8_t> to_span(std::string_view value);
 
@@ -204,11 +207,16 @@ starts_with(const std::string_view string, const std::string_view prefix)
   return string.substr(0, prefix.size()) == prefix;
 }
 
+inline nonstd::span<const uint8_t>
+to_span(const void* data, size_t size)
+{
+  return {reinterpret_cast<const uint8_t*>(data), size};
+}
+
 inline nonstd::span<const uint8_t>
 to_span(std::string_view data)
 {
-  return nonstd::span<const uint8_t>(
-    reinterpret_cast<const uint8_t*>(data.data()), data.size());
+  return to_span(data.data(), data.size());
 }
 
 template<typename T>
index ba76839616de964d9a51c38552fdc6db38b34c72..3cdc7febb56ebd4af621153f9d42558b8849f851 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2022 Joel Rosdahl and other contributors
+// Copyright (C) 2022-2023 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
 
 #pragma once
 
-#include <cstddef>
+#include <third_party/nonstd/span.hpp>
+
 #include <cstdint>
 #include <functional>
-#include <vector>
 
 namespace util {
 
-using DataReceiver = std::function<void(const uint8_t* data, size_t size)>;
+using DataReceiver = std::function<void(nonstd::span<const uint8_t> data)>;
 
 } // namespace util
index f4005ba5418f07e76786f1c7cebac716d26390bb..bd0c9cc3387b750d15af74b1caa4a56bfe68e00d 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2011-2022 Joel Rosdahl and other contributors
+// Copyright (C) 2011-2023 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -24,6 +24,9 @@
 
 #include "third_party/doctest.h"
 
+#include <string>
+#include <vector>
+
 using TestUtil::TestContext;
 
 TEST_SUITE_BEGIN("AtomicFile");
index c7712b2dfbddaa74e52c4bcd8dc5637f364e5fd4..1915399e46541072112753f2fcab7290812391b9 100644 (file)
@@ -25,6 +25,9 @@
 
 #include "third_party/doctest.h"
 
+#include <string>
+#include <vector>
+
 using TestUtil::TestContext;
 
 TEST_SUITE_BEGIN("Depfile");
index 3907d0d528576f96d41a6e667ec09cbcd9c66cf7..a5b6b00df2d06999a676dfe8134ecb6f4fe5252b 100644 (file)
@@ -31,6 +31,8 @@
 #include <fcntl.h>
 
 #include <optional>
+#include <string>
+#include <vector>
 
 #ifdef HAVE_UNISTD_H
 #  include <unistd.h>
index 96c3dbb93d348875bf677825f282e34ac55c79e7..8e1f510e1280de3b2cf619208db5959acba7de10 100644 (file)
@@ -28,6 +28,8 @@
 #include "third_party/doctest.h"
 
 #include <optional>
+#include <string>
+#include <vector>
 
 #ifdef HAVE_UNISTD_H
 #  include <unistd.h>
index 39262596bcab145cd0f400aafce5c2fe23230922..8fe6d6cfe01d0e99fa38f74e82200b95f297d60e 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2011-2021 Joel Rosdahl and other contributors
+// Copyright (C) 2011-2023 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -24,6 +24,8 @@
 #include <third_party/doctest.h>
 
 #include <iostream> // macOS bug: https://github.com/onqtam/doctest/issues/126
+#include <string>
+#include <vector>
 
 using core::Statistic;
 using core::Statistics;
index 40b3e6f83fc50ee6af99669ea9c9574addcddae1..5c3cce2d53f2095ab56bf23916b57cdc64916df5 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2021-2022 Joel Rosdahl and other contributors
+// Copyright (C) 2021-2023 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -27,6 +27,7 @@
 
 #include <algorithm>
 #include <string>
+#include <vector>
 
 using TestUtil::TestContext;
 
index 1ea0ad4f6a0adce6cbbc6c048d46c54259891392..d6eeabed9bbe3051182579d2e6d1b318618948f5 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2021-2022 Joel Rosdahl and other contributors
+// Copyright (C) 2021-2023 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -21,6 +21,8 @@
 #include "third_party/doctest.h"
 
 #include <ostream> // https://github.com/doctest/doctest/issues/618
+#include <string>
+#include <vector>
 
 TEST_CASE("util::Tokenizer")
 {
index 77e5c0bec8f0079b1413ee6b2c77c39480c1dcaf..8b86835354f775ccc2d8bf8f11d07842e9c93571 100644 (file)
@@ -25,7 +25,9 @@
 #include <third_party/doctest.h>
 
 #include <cstring>
+#include <string>
 #include <string_view>
+#include <vector>
 
 using TestUtil::TestContext;