]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Move Args to util namespace
authorJoel Rosdahl <joel@rosdahl.net>
Wed, 23 Jul 2025 08:44:00 +0000 (10:44 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 23 Jul 2025 08:44:00 +0000 (10:44 +0200)
19 files changed:
src/ccache/CMakeLists.txt
src/ccache/argprocessing.cpp
src/ccache/argprocessing.hpp
src/ccache/argsinfo.hpp
src/ccache/ccache.cpp
src/ccache/ccache.hpp
src/ccache/config.cpp
src/ccache/config.hpp
src/ccache/context.cpp
src/ccache/context.hpp
src/ccache/hashutil.cpp
src/ccache/util/CMakeLists.txt
src/ccache/util/args.cpp [moved from src/ccache/args.cpp with 99% similarity]
src/ccache/util/args.hpp [moved from src/ccache/args.hpp with 99% similarity]
unittest/CMakeLists.txt
unittest/test_argprocessing.cpp
unittest/test_ccache.cpp
unittest/test_config.cpp
unittest/test_util_args.cpp [moved from unittest/test_args.cpp with 99% similarity]

index 97616b68736383f5145132fdc55ce36d2f454954..03129125a1578a5ae43bfeaa94a785573dacbd14 100644 (file)
@@ -1,7 +1,6 @@
 set(
   source_files
   argprocessing.cpp
-  args.cpp
   ccache.cpp
   compopt.cpp
   config.cpp
index fcb2b8deb172d7bb0562d02972f32f97b5f7d0d4..31d58091c200e42460b6a89ab9f591d52c3edce3 100644 (file)
 
 #include "argprocessing.hpp"
 
-#include <ccache/args.hpp>
 #include <ccache/argsinfo.hpp>
 #include <ccache/compopt.hpp>
 #include <ccache/context.hpp>
 #include <ccache/core/common.hpp>
 #include <ccache/depfile.hpp>
 #include <ccache/language.hpp>
+#include <ccache/util/args.hpp>
 #include <ccache/util/assertions.hpp>
 #include <ccache/util/direntry.hpp>
 #include <ccache/util/filesystem.hpp>
@@ -151,9 +151,9 @@ public:
   }
 
 private:
-  Args m_preprocessor_args;
-  Args m_compiler_args;
-  Args m_extra_args_to_hash;
+  util::Args m_preprocessor_args;
+  util::Args m_compiler_args;
+  util::Args m_extra_args_to_hash;
 };
 
 bool
@@ -363,7 +363,7 @@ std::optional<Statistic>
 process_option_arg(const Context& ctx,
                    ArgsInfo& args_info,
                    Config& config,
-                   Args& args,
+                   util::Args& args,
                    size_t& args_index,
                    ArgumentProcessingState& state)
 {
@@ -420,7 +420,7 @@ process_option_arg(const Context& ctx,
       ++argpath;
     }
     auto file_args =
-      Args::from_response_file(argpath, config.response_file_format());
+      util::Args::from_response_file(argpath, config.response_file_format());
     if (!file_args) {
       LOG("Couldn't read arg file {}", argpath);
       return Statistic::bad_compiler_arguments;
@@ -443,8 +443,8 @@ process_option_arg(const Context& ctx,
     // Argument is a comma-separated list of files.
     auto paths = util::split_into_strings(args[i], ",");
     for (auto it = paths.rbegin(); it != paths.rend(); ++it) {
-      auto file_args =
-        Args::from_response_file(*it, Args::ResponseFileFormat::posix);
+      auto file_args = util::Args::from_response_file(
+        *it, util::Args::ResponseFileFormat::posix);
       if (!file_args) {
         LOG("Couldn't read CUDA options file {}", *it);
         return Statistic::bad_compiler_arguments;
@@ -1251,7 +1251,7 @@ Statistic
 process_arg(const Context& ctx,
             ArgsInfo& args_info,
             Config& config,
-            Args& args,
+            util::Args& args,
             size_t& args_index,
             ArgumentProcessingState& state)
 {
@@ -1312,7 +1312,7 @@ process_args(Context& ctx)
   // args is a copy of the original arguments given to the compiler but with
   // arguments from @file and similar constructs expanded. It's only used as a
   // temporary data structure to loop over.
-  Args args = ctx.orig_args;
+  util::Args args = ctx.orig_args;
   ArgumentProcessingState state;
 
   state.add_common_arg(args[0]); // Compiler
index 6ffc3459bce29a05107221d4912dd1e0dc68a2ee..7eabf5a1a98f1d8e77bafb0f7c78e0304aff3958 100644 (file)
@@ -18,8 +18,8 @@
 
 #pragma once
 
-#include <ccache/args.hpp>
 #include <ccache/core/statistic.hpp>
+#include <ccache/util/args.hpp>
 
 #include <tl/expected.hpp>
 
@@ -34,13 +34,13 @@ struct ProcessArgsResult
 {
   // Arguments (except "-E -o output.i") to send to the preprocessor. These are
   // part of the input hash (except those marked as AFFECTS_CPP in compopt.cpp).
-  Args preprocessor_args;
+  util::Args preprocessor_args;
 
   // Arguments to send to the real compiler. Not part of the input hash.
-  Args compiler_args;
+  util::Args compiler_args;
 
   // Arguments not sent to the preprocessor but added to the input hash anyway.
-  Args extra_args_to_hash;
+  util::Args extra_args_to_hash;
 
   // Whether to include the actual CWD in the input hash.
   bool hash_actual_cwd = false;
index e7f878ed29a7e951652e0d6674db748e7fe7e3b6..3f78b91d08c86b6bce827ece419f4cd3ce4f77a6 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2020-2024 Joel Rosdahl and other contributors
+// Copyright (C) 2020-2025 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -18,7 +18,7 @@
 
 #pragma once
 
-#include <ccache/args.hpp>
+#include <ccache/util/args.hpp>
 
 #include <filesystem>
 #include <optional>
index 4ab2eaccd6b8a97875da627b730c788f2f5fa7ae..bf8e918545cfcd4a6974dc49f495ee5b6e0e9825 100644 (file)
@@ -20,7 +20,6 @@
 #include "ccache.hpp"
 
 #include <ccache/argprocessing.hpp>
-#include <ccache/args.hpp>
 #include <ccache/argsinfo.hpp>
 #include <ccache/compopt.hpp>
 #include <ccache/context.hpp>
@@ -44,6 +43,7 @@
 #include <ccache/hashutil.hpp>
 #include <ccache/signalhandler.hpp>
 #include <ccache/storage/storage.hpp>
+#include <ccache/util/args.hpp>
 #include <ccache/util/assertions.hpp>
 #include <ccache/util/bytes.hpp>
 #include <ccache/util/clang.hpp>
@@ -174,13 +174,15 @@ should_disable_ccache_for_input_file(const fs::path& path)
 }
 
 static void
-add_prefix(const Context& ctx, Args& args, const std::string& prefix_command)
+add_prefix(const Context& ctx,
+           util::Args& args,
+           const std::string& prefix_command)
 {
   if (prefix_command.empty()) {
     return;
   }
 
-  Args prefixes;
+  util::Args prefixes;
   for (const auto& prefix : util::split_into_strings(prefix_command, " ")) {
     prefixes.push_back(prefix);
   }
@@ -807,7 +809,7 @@ result_key_from_includes(Context& ctx, Hash& hash, std::string_view stdout_data)
 // Execute the compiler/preprocessor, with logic to retry without requesting
 // colored diagnostics messages if that fails.
 static tl::expected<DoExecuteResult, Failure>
-do_execute(Context& ctx, Args& args, const bool capture_stdout = true)
+do_execute(Context& ctx, util::Args& args, const bool capture_stdout = true)
 {
   util::UmaskScope umask_scope(ctx.original_umask);
 
@@ -1174,7 +1176,7 @@ source_file_is_too_new(const Context& ctx, const fs::path& path)
 // Run the real compiler and put the result in cache. Returns the result key.
 static tl::expected<Hash::Digest, Failure>
 to_cache(Context& ctx,
-         Args& args,
+         util::Args& args,
          std::optional<Hash::Digest> result_key,
          Hash* depend_mode_hash)
 {
@@ -1353,7 +1355,7 @@ process_cuda_chunk(Context& ctx,
 }
 
 static bool
-get_clang_cu_enable_verbose_mode(const Args& args)
+get_clang_cu_enable_verbose_mode(const util::Args& args)
 {
   for (size_t i = 1; i < args.size(); i++) {
     if (args[i] == "-v") {
@@ -1367,7 +1369,7 @@ get_clang_cu_enable_verbose_mode(const Args& args)
 // Find the result key by running the compiler in preprocessor mode and
 // hashing the result.
 static tl::expected<Hash::Digest, Failure>
-get_result_key_from_cpp(Context& ctx, Args& args, Hash& hash)
+get_result_key_from_cpp(Context& ctx, util::Args& args, Hash& hash)
 {
   fs::path preprocessed_path;
   util::Bytes cpp_stderr_data;
@@ -1549,7 +1551,7 @@ hash_nvcc_host_compiler(const Context& ctx,
 
 // update a hash with information common for the direct and preprocessor modes.
 static tl::expected<void, Failure>
-hash_common_info(const Context& ctx, const Args& args, Hash& hash)
+hash_common_info(const Context& ctx, const util::Args& args, Hash& hash)
 {
   hash.hash(HASH_PREFIX);
 
@@ -1779,7 +1781,7 @@ hash_common_info(const Context& ctx, const Args& args, Hash& hash)
 
 static std::tuple<std::optional<std::string_view>,
                   std::optional<std::string_view>>
-get_option_and_value(std::string_view option, const Args& args, size_t& i)
+get_option_and_value(std::string_view option, const util::Args& args, size_t& i)
 {
   if (args[i] == option) {
     if (i + 1 < args.size()) {
@@ -1797,7 +1799,7 @@ get_option_and_value(std::string_view option, const Args& args, size_t& i)
 
 static tl::expected<void, Failure>
 hash_argument(const Context& ctx,
-              const Args& args,
+              const util::Args& args,
               size_t& i,
               Hash& hash,
               const bool is_clang,
@@ -2240,9 +2242,9 @@ static tl::expected<
   std::pair<std::optional<Hash::Digest>, std::optional<Hash::Digest>>,
   Failure>
 calculate_result_and_manifest_key(Context& ctx,
-                                  const Args& args,
+                                  const util::Args& args,
                                   Hash& hash,
-                                  Args* preprocessor_args)
+                                  util::Args* preprocessor_args)
 {
   bool direct_mode = !preprocessor_args;
   bool found_ccbin = false;
@@ -2578,7 +2580,7 @@ split_argv(int argc, const char* const* argv)
     argv_parts.config_settings.emplace_back(argv[i]);
     ++i;
   }
-  argv_parts.compiler_and_args = Args::from_argv(argc - i, argv + i);
+  argv_parts.compiler_and_args = util::Args::from_argv(argc - i, argv + i);
   return argv_parts;
 }
 
@@ -2589,7 +2591,7 @@ cache_compilation(int argc, const char* const* argv)
   tzset(); // Needed for localtime_r.
 
   bool fall_back_to_original_compiler = false;
-  Args saved_orig_args;
+  util::Args saved_orig_args;
   std::optional<uint32_t> original_umask;
   fs::path saved_temp_dir;
 
@@ -2795,7 +2797,7 @@ do_cache_compilation(Context& ctx)
   Hash direct_hash = common_hash;
   init_hash_debug(ctx, direct_hash, 'd', "DIRECT MODE", debug_text_file);
 
-  Args args_to_hash = process_args_result->preprocessor_args;
+  util::Args args_to_hash = process_args_result->preprocessor_args;
   args_to_hash.push_back(process_args_result->extra_args_to_hash);
 
   bool put_result_in_manifest = false;
index bb23027ef3311db8370e6a14cf121941818ea659..eda52dddd93c4e471ce8695f49ae5829770aca8f 100644 (file)
@@ -1,5 +1,5 @@
 // Copyright (C) 2002-2007 Andrew Tridgell
-// Copyright (C) 2009-2024 Joel Rosdahl and other contributors
+// Copyright (C) 2009-2025 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -19,8 +19,8 @@
 
 #pragma once
 
-#include <ccache/args.hpp>
 #include <ccache/config.hpp>
+#include <ccache/util/args.hpp>
 
 #include <filesystem>
 #include <functional>
@@ -43,7 +43,7 @@ struct ArgvParts
 {
   bool masquerading_as_compiler = true;
   std::vector<std::string> config_settings;
-  Args compiler_and_args;
+  util::Args compiler_and_args;
 };
 
 ArgvParts split_argv(int argc, const char* const* argv);
index 9ccf43ef4a5fa0c313b2063b6d22620c69a2b722..ef16a8dc4aeec61a1269353bd0dec28e50037619 100644 (file)
@@ -225,15 +225,15 @@ const std::unordered_map<std::string, std::string> k_env_variable_table = {
   {"UMASK",                "umask"                     },
 };
 
-Args::ResponseFileFormat
+util::Args::ResponseFileFormat
 parse_response_file_format(const std::string& value)
 {
   if (value == "posix") {
-    return Args::ResponseFileFormat::posix;
+    return util::Args::ResponseFileFormat::posix;
   } else if (value == "windows") {
-    return Args::ResponseFileFormat::windows;
+    return util::Args::ResponseFileFormat::windows;
   } else {
-    return Args::ResponseFileFormat::auto_guess;
+    return util::Args::ResponseFileFormat::auto_guess;
   }
 }
 
@@ -544,14 +544,15 @@ home_directory()
 }
 
 std::string
-response_file_format_to_string(Args::ResponseFileFormat response_file_format)
+response_file_format_to_string(
+  util::Args::ResponseFileFormat response_file_format)
 {
   switch (response_file_format) {
-  case Args::ResponseFileFormat::auto_guess:
+  case util::Args::ResponseFileFormat::auto_guess:
     return "auto";
-  case Args::ResponseFileFormat::posix:
+  case util::Args::ResponseFileFormat::posix:
     return "posix";
-  case Args::ResponseFileFormat::windows:
+  case util::Args::ResponseFileFormat::windows:
     return "windows";
   }
 
index 40d87c768a5262d08ebde72a68cd26143aaa4bb9..2bb7997544164975e1f05e32f964b56431037c3e 100644 (file)
@@ -18,8 +18,8 @@
 
 #pragma once
 
-#include <ccache/args.hpp>
 #include <ccache/core/sloppiness.hpp>
+#include <ccache/util/args.hpp>
 #include <ccache/util/noncopyable.hpp>
 #include <ccache/util/path.hpp>
 #include <ccache/util/string.hpp>
@@ -57,7 +57,7 @@ public:
   void read(const std::vector<std::string>& cmdline_config_settings = {});
 
   bool absolute_paths_in_stderr() const;
-  Args::ResponseFileFormat response_file_format() const;
+  util::Args::ResponseFileFormat response_file_format() const;
   const std::filesystem::path& base_dir() const;
   const std::filesystem::path& cache_dir() const;
   const std::string& compiler() const;
@@ -171,8 +171,8 @@ private:
   std::filesystem::path m_system_config_path;
 
   bool m_absolute_paths_in_stderr = false;
-  Args::ResponseFileFormat m_response_file_format =
-    Args::ResponseFileFormat::auto_guess;
+  util::Args::ResponseFileFormat m_response_file_format =
+    util::Args::ResponseFileFormat::auto_guess;
   std::filesystem::path m_base_dir;
   std::filesystem::path m_cache_dir;
   std::string m_compiler;
@@ -240,15 +240,15 @@ Config::absolute_paths_in_stderr() const
   return m_absolute_paths_in_stderr;
 }
 
-inline Args::ResponseFileFormat
+inline util::Args::ResponseFileFormat
 Config::response_file_format() const
 {
-  if (m_response_file_format != Args::ResponseFileFormat::auto_guess) {
+  if (m_response_file_format != util::Args::ResponseFileFormat::auto_guess) {
     return m_response_file_format;
   }
 
-  return is_compiler_group_msvc() ? Args::ResponseFileFormat::windows
-                                  : Args::ResponseFileFormat::posix;
+  return is_compiler_group_msvc() ? util::Args::ResponseFileFormat::windows
+                                  : util::Args::ResponseFileFormat::posix;
 }
 
 inline const std::filesystem::path&
index 62650f981e7fd34029cb68de2a9eb666fee8b489..681eec93857623c149208a7b9e9963b9cf2a91c6 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2020-2024 Joel Rosdahl and other contributors
+// Copyright (C) 2020-2025 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -51,7 +51,7 @@ Context::Context()
 }
 
 void
-Context::initialize(Args&& compiler_and_args,
+Context::initialize(util::Args&& compiler_and_args,
                     const std::vector<std::string>& cmdline_config_settings)
 {
   orig_args = std::move(compiler_and_args);
index 71fa71652602ef03fcc4ddf3e97258abe62ccba1..6732c68d309f2f3531c149baebf4a0c0a964ae95 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2020-2024 Joel Rosdahl and other contributors
+// Copyright (C) 2020-2025 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
 
 #pragma once
 
-#include <ccache/args.hpp>
 #include <ccache/argsinfo.hpp>
 #include <ccache/config.hpp>
 #include <ccache/core/manifest.hpp>
 #include <ccache/hash.hpp>
 #include <ccache/storage/storage.hpp>
+#include <ccache/util/args.hpp>
 #include <ccache/util/bytes.hpp>
 #include <ccache/util/filestream.hpp>
 #include <ccache/util/noncopyable.hpp>
@@ -52,7 +52,7 @@ public:
 
   // Read configuration, initialize logging, etc. Typically not called from unit
   // tests.
-  void initialize(Args&& compiler_and_args,
+  void initialize(util::Args&& compiler_and_args,
                   const std::vector<std::string>& cmdline_config_settings);
 
   ArgsInfo args_info;
@@ -65,7 +65,7 @@ public:
   std::filesystem::path apparent_cwd;
 
   // The original argument list.
-  Args orig_args;
+  util::Args orig_args;
 
   // Files included by the preprocessor and their hashes.
   std::unordered_map<std::string, Hash::Digest> included_files;
index 1a8d458331df4420a045f4d33b2e981d6cc688ec..426930a6ef3c16cef31d14557e60c7b39477a0aa 100644 (file)
 
 #include "hashutil.hpp"
 
-#include <ccache/args.hpp>
 #include <ccache/config.hpp>
 #include <ccache/context.hpp>
 #include <ccache/core/exceptions.hpp>
 #include <ccache/execute.hpp>
 #include <ccache/macroskip.hpp>
+#include <ccache/util/args.hpp>
 #include <ccache/util/cpu.hpp>
 #include <ccache/util/direntry.hpp>
 #include <ccache/util/environment.hpp>
@@ -357,7 +357,7 @@ hash_command_output(Hash& hash,
   } else {
     using_cmd_exe = false;
   }
-  Args args = Args::from_string(adjusted_command);
+  util::Args args = util::Args::from_string(adjusted_command);
   {
     auto full_path =
       find_executable_in_path(args[0], util::getenv_path_list("PATH")).string();
@@ -366,7 +366,7 @@ hash_command_output(Hash& hash,
     }
   }
 #else
-  Args args = Args::from_string(command);
+  util::Args args = util::Args::from_string(command);
 #endif
 
   for (size_t i = 0; i < args.size(); i++) {
index 87a0722fe68b8376ab69cf548449a2f8a740378b..ba2f7632b3ad191e827ead2790fbc219e0d19097 100644 (file)
@@ -1,5 +1,6 @@
 set(
   sources
+  args.cpp
   assertions.cpp
   bytes.cpp
   clang.cpp
similarity index 99%
rename from src/ccache/args.cpp
rename to src/ccache/util/args.cpp
index bfdf1b368bb6fda840681b2564dfe2001711e8a0..153b421d220a13078284cf1e3d20dd1e5bd27b02 100644 (file)
@@ -25,6 +25,8 @@
 #include <ccache/util/logging.hpp>
 #include <ccache/util/string.hpp>
 
+namespace util {
+
 Args::Args(Args&& other) noexcept
   : m_args(std::move(other.m_args))
 {
@@ -274,3 +276,5 @@ Args::replace(size_t index, const Args& args)
     insert(index, args);
   }
 }
+
+} // namespace util
similarity index 99%
rename from src/ccache/args.hpp
rename to src/ccache/util/args.hpp
index 6b46fa507ffce040a3844ab5addc69a0c149b9c1..b29280a4ce9f214c2c38c63b33b5a06600cf567b 100644 (file)
@@ -26,6 +26,8 @@
 #include <string_view>
 #include <vector>
 
+namespace util {
+
 class Args
 {
 public:
@@ -159,3 +161,5 @@ Args::push_back(const std::filesystem::path& arg)
 {
   m_args.push_back(arg.string());
 }
+
+} // namespace util
index ac14cbb762be110390ddc94bb536e756278b96ae..746c1e0569cdabf36972027cf1d20139905c62af 100644 (file)
@@ -2,7 +2,6 @@ set(
   source_files
   main.cpp
   test_argprocessing.cpp
-  test_args.cpp
   test_ccache.cpp
   test_compopt.cpp
   test_compression_types.cpp
@@ -18,6 +17,7 @@ set(
   test_hashutil.cpp
   test_storage_local_statsfile.cpp
   test_storage_local_util.cpp
+  test_util_args.cpp
   test_util_bitset.cpp
   test_util_bytes.cpp
   test_util_clang.cpp
index b8b75a09b8b68f74786684244291b81ba015eab6..5e91902a57f2aa8ecfa11966cc8ba03ca0e2f04a 100644 (file)
 #include "testutil.hpp"
 
 #include <ccache/argprocessing.hpp>
-#include <ccache/args.hpp>
 #include <ccache/config.hpp>
 #include <ccache/context.hpp>
 #include <ccache/core/statistic.hpp>
+#include <ccache/util/args.hpp>
 #include <ccache/util/file.hpp>
 #include <ccache/util/filesystem.hpp>
 #include <ccache/util/format.hpp>
@@ -38,6 +38,7 @@ namespace fs = util::filesystem;
 
 using core::Statistic;
 using TestUtil::TestContext;
+using util::Args;
 
 namespace {
 
index fcf7326e4b2ee7ed82bedf6d7d0ecb3d8c7f4cd3..9b43a1169ae1b3f32e8b59eae326de7d97fe6b9f 100644 (file)
@@ -38,6 +38,7 @@
 
 namespace fs = util::filesystem;
 using TestUtil::TestContext;
+using util::Args;
 
 TEST_SUITE_BEGIN("ccache");
 
index c8fb8cde5641723f3d6d8abf1194c80f0e726853..c0c4e46f36eee089ae6da27b2693edb72c1c4ed3 100644 (file)
@@ -307,7 +307,7 @@ TEST_CASE("Config::update_from_environment")
 
 TEST_CASE("Config::response_file_format")
 {
-  using ResponseFileFormat = Args::ResponseFileFormat;
+  using ResponseFileFormat = util::Args::ResponseFileFormat;
 
   Config config;
 
similarity index 99%
rename from unittest/test_args.cpp
rename to unittest/test_util_args.cpp
index 549a106c57ef21958e708c83a4ee782211a47f4d..e43ace0c0b86b8c21882e4bb1b7300858cafc37d 100644 (file)
@@ -18,8 +18,8 @@
 
 #include "testutil.hpp"
 
-#include <ccache/args.hpp>
 #include <ccache/config.hpp>
+#include <ccache/util/args.hpp>
 #include <ccache/util/file.hpp>
 
 #include <doctest/doctest.h>
@@ -27,6 +27,7 @@
 TEST_SUITE_BEGIN("Args");
 
 using TestUtil::TestContext;
+using util::Args;
 
 TEST_CASE("Args default constructor")
 {