]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Introduce and use CHECK_LIB_CALL macro
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 30 Jun 2024 16:54:13 +0000 (18:54 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 2 Jul 2024 18:41:03 +0000 (20:41 +0200)
src/ccache/core/exceptions.hpp
src/ccache/execute.cpp
src/ccache/hashutil.cpp

index 7017e930a714720fdf3b8ab26ac055946245df4c..b67bb153e1685e0735d8f6e819e29abb5c860c64 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2019-2022 Joel Rosdahl and other contributors
+// Copyright (C) 2019-2024 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -20,6 +20,7 @@
 
 #include <fmt/core.h>
 
+#include <cstring>
 #include <optional>
 #include <stdexcept>
 #include <string>
@@ -48,4 +49,14 @@ class Fatal : public ErrorBase
   using ErrorBase::ErrorBase;
 };
 
+// Call a libc-style function (returns 0 on success and sets errno) and throw
+// Fatal on error.
+#define CHECK_LIB_CALL(function, ...)                                          \
+  {                                                                            \
+    int _result = function(__VA_ARGS__);                                       \
+    if (_result != 0) {                                                        \
+      throw core::Fatal(FMT(#function " failed: {}", strerror(_result)));      \
+    }                                                                          \
+  }
+
 } // namespace core
index aadf4e232d10dd14c2df87369aefa42cd1b6dc9d..6359c17674eccd9f9a6fbd1613f459f62b6558af 100644 (file)
@@ -300,43 +300,31 @@ execute(Context& ctx,
 {
   LOG("Executing {}", util::format_argv_for_logging(argv));
 
-  int result;
-  posix_spawn_file_actions_t file_actions;
-  if ((result = posix_spawn_file_actions_init(&file_actions))) {
-    throw core::Fatal(
-      FMT("posix_spawn_file_actions_init failed: {}", strerror(result)));
-  }
+  posix_spawn_file_actions_t fa;
 
-  if ((result = posix_spawn_file_actions_adddup2(
-         &file_actions, *fd_out, STDOUT_FILENO))
-      || (result = posix_spawn_file_actions_addclose(&file_actions, *fd_out))
-      || (result = posix_spawn_file_actions_adddup2(
-            &file_actions, *fd_err, STDERR_FILENO))
-      || (result = posix_spawn_file_actions_addclose(&file_actions, *fd_err))) {
-    throw core::Fatal(FMT("posix_spawn_file_actions_addclose/dup2 failed: {}",
-                          strerror(result)));
-  }
+  CHECK_LIB_CALL(posix_spawn_file_actions_init, &fa);
+  CHECK_LIB_CALL(posix_spawn_file_actions_adddup2, &fa, *fd_out, STDOUT_FILENO);
+  CHECK_LIB_CALL(posix_spawn_file_actions_addclose, &fa, *fd_out);
+  CHECK_LIB_CALL(posix_spawn_file_actions_adddup2, &fa, *fd_err, STDERR_FILENO);
+  CHECK_LIB_CALL(posix_spawn_file_actions_addclose, &fa, *fd_err);
 
+  int result;
   {
     SignalHandlerBlocker signal_handler_blocker;
     pid_t pid;
     extern char** environ;
-    result = posix_spawn(&pid,
-                         argv[0],
-                         &file_actions,
-                         nullptr,
-                         const_cast<char* const*>(argv),
-                         environ);
-    if (!result) {
+    result = posix_spawn(
+      &pid, argv[0], &fa, nullptr, const_cast<char* const*>(argv), environ);
+    if (result == 0) {
       ctx.compiler_pid = pid;
     }
   }
 
-  posix_spawn_file_actions_destroy(&file_actions);
+  posix_spawn_file_actions_destroy(&fa);
   fd_out.close();
   fd_err.close();
 
-  if (result) {
+  if (result != 0) {
     return -1;
   }
 
index ead335be69e7ef2ddd544c0704fcaede9b064955..b9e85a6a89c317d52cf4a625ea9efdffb51eff9c 100644 (file)
@@ -376,9 +376,10 @@ hash_command_output(Hash& hash,
     }
   }
 
-  auto argv = args.to_argv();
+  auto arg_vector = args.to_argv();
+  auto argv = const_cast<char* const*>(arg_vector.data());
   LOG("Executing compiler check command {}",
-      util::format_argv_for_logging(argv.data()));
+      util::format_argv_for_logging(argv));
 
 #ifdef _WIN32
   PROCESS_INFORMATION pi;
@@ -410,7 +411,7 @@ hash_command_output(Hash& hash,
   if (using_cmd_exe) {
     win32args = adjusted_command; // quoted
   } else {
-    win32args = util::format_argv_as_win32_command_string(argv.data(), sh);
+    win32args = util::format_argv_as_win32_command_string(argv, sh);
   }
   BOOL ret = CreateProcess(path.c_str(),
                            const_cast<char*>(win32args.c_str()),
@@ -449,33 +450,21 @@ hash_command_output(Hash& hash,
     throw core::Fatal(FMT("pipe failed: {}", strerror(errno)));
   }
 
-  int result;
-  posix_spawn_file_actions_t file_actions;
-  if ((result = posix_spawn_file_actions_init(&file_actions))) {
-    throw core::Fatal(
-      FMT("posix_spawn_file_actions_init failed: {}", strerror(result)));
-  }
+  posix_spawn_file_actions_t fa;
 
-  if ((result = posix_spawn_file_actions_addclose(&file_actions, pipefd[0]))
-      || (result = posix_spawn_file_actions_addclose(&file_actions, 0))
-      || (result =
-            posix_spawn_file_actions_adddup2(&file_actions, pipefd[1], 1))
-      || (result =
-            posix_spawn_file_actions_adddup2(&file_actions, pipefd[1], 2))) {
-    throw core::Fatal(FMT("posix_spawn_file_actions_addclose/dup2 failed: {}",
-                          strerror(result)));
-  }
+  CHECK_LIB_CALL(posix_spawn_file_actions_init, &fa);
+  CHECK_LIB_CALL(posix_spawn_file_actions_init, &fa);
+  CHECK_LIB_CALL(posix_spawn_file_actions_addclose, &fa, pipefd[0]);
+  CHECK_LIB_CALL(posix_spawn_file_actions_addclose, &fa, 0);
+  CHECK_LIB_CALL(posix_spawn_file_actions_adddup2, &fa, pipefd[1], 1);
+  CHECK_LIB_CALL(posix_spawn_file_actions_adddup2, &fa, pipefd[1], 2);
 
   pid_t pid;
   extern char** environ;
-  result = posix_spawnp(&pid,
-                        argv[0],
-                        &file_actions,
-                        nullptr,
-                        const_cast<char* const*>(argv.data()),
-                        environ);
-
-  posix_spawn_file_actions_destroy(&file_actions);
+  int result = posix_spawnp(
+    &pid, argv[0], &fa, nullptr, const_cast<char* const*>(argv), environ);
+
+  posix_spawn_file_actions_destroy(&fa);
   close(pipefd[1]);
 
   const auto hash_result = hash.hash_fd(pipefd[0]);
@@ -484,7 +473,7 @@ hash_command_output(Hash& hash,
   }
   close(pipefd[0]);
 
-  if (result) {
+  if (result != 0) {
     LOG("posix_spawnp failed: {}", strerror(errno));
     return false;
   }