]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Replace fatal function with throwing FatalError exception explicitly
authorJoel Rosdahl <joel@rosdahl.net>
Mon, 3 Aug 2020 18:03:13 +0000 (20:03 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Mon, 3 Aug 2020 19:31:02 +0000 (21:31 +0200)
I think that this improves code readability since it’s more clear what
the code flow is.

src/Logging.cpp
src/TemporaryFile.cpp
src/Util.cpp
src/ccache.cpp
src/exceptions.hpp
src/execute.cpp
src/hashutil.cpp

index 65072a8c06e151ef6b49990710939bc64fa1abc5..b8ee4a39552abf421a6fb9d1256df3fe1e390bee 100644 (file)
@@ -68,7 +68,7 @@ bool debug_log_enabled = false;
 [[noreturn]] void
 print_fatal_error_and_exit()
 {
-  // Note: Can't call fatal() since that would lead to recursion.
+  // Note: Can't throw FatalError since that would lead to recursion.
   fmt::print(stderr,
              "ccache: error: Failed to write to {}: {}\n",
              logfile_path,
index 3acc304602da5d03bb0a0ced2245b820b68a451d..803ad9e8b2e3b70034ea81d67e39e31699326fc7 100644 (file)
@@ -63,12 +63,14 @@ 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 {}: {}", dir, strerror(errno));
+      throw FatalError(
+        "Failed to create directory {}: {}", dir, strerror(errno));
     }
     initialize(path_prefix);
   }
   if (!fd) {
-    fatal("Failed to create temporary file for {}: {}", path, strerror(errno));
+    throw FatalError(
+      "Failed to create temporary file for {}: {}", path, strerror(errno));
   }
 
   Util::set_cloexec_flag(*fd);
index 05735e3564ff03fb5101c6bece53ab0a000ea89b..b28f536f527713122f247d3c52a9b52a4d008bf6 100644 (file)
@@ -634,7 +634,8 @@ get_home_directory()
     }
   }
 #endif
-  fatal("Could not determine home directory from $HOME or getpwuid(3)");
+  throw FatalError(
+    "Could not determine home directory from $HOME or getpwuid(3)");
 }
 
 const char*
index e51e8da439f5d5a79cd1f526bc992a02b4eff834..34b573f93d8d82d510242080f2950863c916111f 100644 (file)
@@ -154,7 +154,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, MYNAME);
     if (path.empty()) {
-      fatal("{}: {}", word, strerror(errno));
+      throw FatalError("{}: {}", word, strerror(errno));
     }
 
     prefix.push_back(path);
@@ -1713,11 +1713,12 @@ find_compiler(Context& ctx, const char* const* argv)
 
   std::string compiler = find_executable(ctx, base, MYNAME);
   if (compiler.empty()) {
-    fatal("Could not find compiler \"{}\" in PATH", base);
+    throw FatalError("Could not find compiler \"{}\" in PATH", base);
   }
   if (compiler == argv[0]) {
-    fatal("Recursive invocation (the name of the ccache binary must be \"{}\")",
-          MYNAME);
+    throw FatalError(
+      "Recursive invocation (the name of the ccache binary must be \"{}\")",
+      MYNAME);
   }
   ctx.orig_args[0] = compiler;
 }
@@ -1775,13 +1776,14 @@ 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");
+      throw FatalError(
+        "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");
+      throw FatalError("CCACHE_DIR must not be the empty string");
     }
 
     config.set_primary_config_path(
@@ -1924,7 +1926,7 @@ cache_compilation(int argc, const char* const* argv)
     log("Executing {}", Util::format_argv_for_logging(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 {} failed: {}", execv_argv[0], strerror(errno));
+    throw FatalError("execv of {} failed: {}", execv_argv[0], strerror(errno));
   }
 }
 
index 1d70ad53db3d30151990aec71da2833fc72d4d1e..d6ce7e9a2aedd35581b2a15b9fcbcb1c28e3b5c8 100644 (file)
@@ -47,9 +47,24 @@ class Error : public ErrorBase
 // with a non-zero exit code.
 class FatalError : public ErrorBase
 {
-  using ErrorBase::ErrorBase;
+public:
+  // Special case: If given only one string, don't parse it as a format string.
+  FatalError(const std::string& message);
+
+  // `args` are forwarded to `fmt::format`.
+  template<typename... T> inline FatalError(T&&... args);
 };
 
+inline FatalError::FatalError(const std::string& message) : ErrorBase(message)
+{
+}
+
+template<typename... T>
+inline FatalError::FatalError(T&&... args)
+  : ErrorBase(fmt::format(std::forward<T>(args)...))
+{
+}
+
 // Throw a Failure if ccache did not succeed in getting or putting a result in
 // the cache. If `exit_code` is set, just exit with that code directly,
 // otherwise execute the real compiler and exit with its exit code. Also updates
@@ -83,12 +98,3 @@ Failure::stat() const
 {
   return m_stat;
 }
-
-// Something went badly wrong! Print a message to stderr and exit with non-zero
-// exit code. `args` are forwarded to `fmt::format`.
-template<typename... T>
-[[noreturn]] inline void
-fatal(T&&... args)
-{
-  throw FatalError(fmt::format(std::forward<T>(args)...));
-}
index 77789ac2d83f3b5438a45e363b10b8d7facb8b28..7b647b7b1f58e5687ea7f8c7a9a5953277aa5711 100644 (file)
@@ -166,7 +166,7 @@ execute(const char* const* argv, Fd&& fd_out, Fd&& fd_err, pid_t* pid)
   }
 
   if (*pid == -1) {
-    fatal("Failed to fork: {}", strerror(errno));
+    throw FatalError("Failed to fork: {}", strerror(errno));
   }
 
   if (*pid == 0) {
@@ -183,7 +183,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: {}", strerror(errno));
+    throw FatalError("waitpid failed: {}", strerror(errno));
   }
 
   {
index 0a64d024ed1d1cf3b74575607c38048d02b039c8..9cc76800855a5ccbddd40fed7ad5b3269dcac402 100644 (file)
@@ -466,12 +466,12 @@ hash_command_output(Hash& hash,
 #else
   int pipefd[2];
   if (pipe(pipefd) == -1) {
-    fatal("pipe failed: {}", strerror(errno));
+    throw FatalError("pipe failed: {}", strerror(errno));
   }
 
   pid_t pid = fork();
   if (pid == -1) {
-    fatal("fork failed: {}", strerror(errno));
+    throw FatalError("fork failed: {}", strerror(errno));
   }
 
   if (pid == 0) {