]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Convert fatal() function to macro that forwards to fmt::format
authorJoel Rosdahl <joel@rosdahl.net>
Tue, 28 Jul 2020 17:53:13 +0000 (19:53 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 28 Jul 2020 17:56:26 +0000 (19:56 +0200)
src/TemporaryFile.cpp
src/ccache.cpp
src/exceptions.hpp
src/execute.cpp
src/hashutil.cpp
src/legacy_util.cpp
src/legacy_util.hpp
src/logging.cpp

index f91f3b999bf787d4cfb06b349564418af197437f..f7ac3ec991e8789b3d67e6c67b03a6678b216185 100644 (file)
@@ -64,16 +64,12 @@ TemporaryFile::TemporaryFile(string_view path_prefix)
   if (!initialize(path_prefix) && errno == ENOENT) {
     auto dir = Util::dir_name(path);
     if (!Util::create_dir(dir)) {
-      fatal("Failed to create directory %s: %s",
-            std::string(dir).c_str(),
-            strerror(errno));
+      FATAL("Failed to create directory {}: {}", dir, strerror(errno));
     }
     initialize(path_prefix);
   }
   if (!fd) {
-    fatal("Failed to create temporary file for %s: %s",
-          path.c_str(),
-          strerror(errno));
+    FATAL("Failed to create temporary file for {}: {}", path, strerror(errno));
   }
 
   set_cloexec_flag(*fd);
index 139db36adcd1cd86e5a781fa45f0b495b6b3c28d..7e2246367ccb07e24e29d90fa6329c9dea2a2899 100644 (file)
@@ -153,7 +153,7 @@ add_prefix(const Context& ctx, Args& args, const std::string& prefix_command)
   for (const auto& word : Util::split_into_strings(prefix_command, " ")) {
     std::string path = find_executable(ctx, word.c_str(), MYNAME);
     if (path.empty()) {
-      fatal("%s: %s", word.c_str(), strerror(errno));
+      FATAL("{}: {}", word, strerror(errno));
     }
 
     prefix.push_back(path);
@@ -1744,10 +1744,10 @@ find_compiler(Context& ctx, const char* const* argv)
 
   std::string compiler = find_executable(ctx, base.c_str(), MYNAME);
   if (compiler.empty()) {
-    fatal("Could not find compiler \"%s\" in PATH", base.c_str());
+    FATAL("Could not find compiler \"{}\" in PATH", base);
   }
   if (compiler == argv[0]) {
-    fatal("Recursive invocation (the name of the ccache binary must be \"%s\")",
+    FATAL("Recursive invocation (the name of the ccache binary must be \"{}\")",
           MYNAME);
   }
   ctx.orig_args[0] = compiler;
@@ -1814,13 +1814,13 @@ set_up_config(Config& config)
     MTR_END("config", "conf_read_secondary");
 
     if (config.cache_dir().empty()) {
-      fatal("configuration setting \"cache_dir\" must not be the empty string");
+      FATAL("configuration setting \"cache_dir\" must not be the empty string");
     }
     if ((p = getenv("CCACHE_DIR"))) {
       config.set_cache_dir(p);
     }
     if (config.cache_dir().empty()) {
-      fatal("CCACHE_DIR must not be the empty string");
+      FATAL("CCACHE_DIR must not be the empty string");
     }
 
     config.set_primary_config_path(
@@ -1956,7 +1956,7 @@ cache_compilation(int argc, const char* const* argv)
     cc_log_argv("Executing ", execv_argv.data());
     ctx.reset(); // Dump debug logs last thing before executing.
     execv(execv_argv[0], const_cast<char* const*>(execv_argv.data()));
-    fatal("execv of %s failed: %s", execv_argv[0], strerror(errno));
+    FATAL("execv of {} failed: {}", execv_argv[0], strerror(errno));
   }
 }
 
@@ -2137,9 +2137,10 @@ do_cache_compilation(Context& ctx, const char* const* argv)
     result_name = calculate_result_name(
       ctx, args_to_hash, preprocessor_args, cpp_hash, false);
     MTR_END("hash", "cpp_hash");
-    if (!result_name) {
-      fatal("internal error: calculate_result_name returned NULL for cpp");
-    }
+
+    // calculate_result_name does not return nullopt if the last (direct_mode)
+    // argument is false.
+    assert(result_name);
     ctx.set_result_name(*result_name);
 
     if (result_name_from_manifest && result_name_from_manifest != result_name) {
index cb7a32e46ad5a301759aff1bd446edf53c8dd7f5..10a959cc2c3bbbde0bbff3500a729d650a11a90c 100644 (file)
 
 #include "system.hpp"
 
+#include "FormatNonstdStringView.hpp"
 #include "stats.hpp"
 
+#include "third_party/fmt/core.h"
 #include "third_party/nonstd/optional.hpp"
 
 #include <stdexcept>
 
+// Something went badly wrong! Print a message to stderr and exit with non-zero
+// exit code.
+#define FATAL(...)                                                             \
+  do {                                                                         \
+    throw FatalError(fmt::format(__VA_ARGS__));                                \
+  } while (false)
+
 // Don't throw or catch ErrorBase directly, use a subclass.
 class ErrorBase : public std::runtime_error
 {
index cb5c93acf6faa58db6a3c2a4753c41e4fbe78602..003b28768d3dec373c71a2d7ef735e2f3502f240 100644 (file)
@@ -264,7 +264,7 @@ execute(const char* const* argv, Fd&& fd_out, Fd&& fd_err, pid_t* pid)
   }
 
   if (*pid == -1) {
-    fatal("Failed to fork: %s", strerror(errno));
+    FATAL("Failed to fork: {}", strerror(errno));
   }
 
   if (*pid == 0) {
@@ -281,7 +281,7 @@ execute(const char* const* argv, Fd&& fd_out, Fd&& fd_err, pid_t* pid)
 
   int status;
   if (waitpid(*pid, &status, 0) != *pid) {
-    fatal("waitpid failed: %s", strerror(errno));
+    FATAL("waitpid failed: {}", strerror(errno));
   }
 
   {
index 432141abc538c9e848581b3e774e83df5f136322..f636f6886ff81a55c1b9043c91784256a03d8964 100644 (file)
@@ -483,12 +483,12 @@ hash_command_output(Hash& hash, const char* command, const char* compiler)
 #else
   int pipefd[2];
   if (pipe(pipefd) == -1) {
-    fatal("pipe failed");
+    FATAL("pipe failed: {}", strerror(errno));
   }
 
   pid_t pid = fork();
   if (pid == -1) {
-    fatal("fork failed");
+    FATAL("fork failed: {}", strerror(errno));
   }
 
   if (pid == 0) {
index 4bc6076481e6c4395c7dd3595da5176cf698de7a..07629c590bbb1015f37b806e40c101b959d33476 100644 (file)
 #  include <sys/time.h>
 #endif
 
-// Something went badly wrong!
-void
-fatal(const char* format, ...)
-{
-  va_list ap;
-  va_start(ap, format);
-  char msg[8192];
-  vsnprintf(msg, sizeof(msg), format, ap);
-  va_end(ap);
-
-  throw FatalError(msg);
-}
-
 // Return a static string with the current hostname.
 const char*
 get_hostname()
@@ -128,7 +115,7 @@ get_home_directory()
     }
   }
 #endif
-  fatal("Could not determine home directory from $HOME or getpwuid(3)");
+  FATAL("Could not determine home directory from $HOME or getpwuid(3)");
 }
 
 // Return whether the argument is a full path.
index 0720d5bf1b1f268f32366df2ad9d835695fdcd9f..e91fba8df29be0047647f39b9924cb8325c284bb 100644 (file)
@@ -22,8 +22,6 @@
 
 #include <string>
 
-void fatal(const char* format, ...) ATTR_FORMAT(printf, 1, 2) ATTR_NORETURN;
-
 const char* get_hostname();
 void x_setenv(const char* name, const char* value);
 void x_unsetenv(const char* name);
index 472ccd5a6078b4cb25150a9eaec2ab7b48b1557e..c3845386bc7f2da09a05e7d221b3a6ae3b73dee6 100644 (file)
@@ -138,7 +138,7 @@ static void warn_log_fail() ATTR_NORETURN;
 static void
 warn_log_fail()
 {
-  // Note: Can't call fatal() since that would lead to recursion.
+  // Note: Can't call FATAL() since that would lead to recursion.
   fprintf(stderr,
           "ccache: error: Failed to write to %s: %s\n",
           logfile_path.c_str(),