From: Joel Rosdahl Date: Mon, 3 Aug 2020 18:03:13 +0000 (+0200) Subject: Replace fatal function with throwing FatalError exception explicitly X-Git-Tag: v4.0~228 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=adfca03bb4f9f4d53fa36aed29a64832ed2661c8;p=thirdparty%2Fccache.git Replace fatal function with throwing FatalError exception explicitly I think that this improves code readability since it’s more clear what the code flow is. --- diff --git a/src/Logging.cpp b/src/Logging.cpp index 65072a8c0..b8ee4a395 100644 --- a/src/Logging.cpp +++ b/src/Logging.cpp @@ -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, diff --git a/src/TemporaryFile.cpp b/src/TemporaryFile.cpp index 3acc30460..803ad9e8b 100644 --- a/src/TemporaryFile.cpp +++ b/src/TemporaryFile.cpp @@ -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); diff --git a/src/Util.cpp b/src/Util.cpp index 05735e356..b28f536f5 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -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* diff --git a/src/ccache.cpp b/src/ccache.cpp index e51e8da43..34b573f93 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -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(execv_argv.data())); - fatal("execv of {} failed: {}", execv_argv[0], strerror(errno)); + throw FatalError("execv of {} failed: {}", execv_argv[0], strerror(errno)); } } diff --git a/src/exceptions.hpp b/src/exceptions.hpp index 1d70ad53d..d6ce7e9a2 100644 --- a/src/exceptions.hpp +++ b/src/exceptions.hpp @@ -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 inline FatalError(T&&... args); }; +inline FatalError::FatalError(const std::string& message) : ErrorBase(message) +{ +} + +template +inline FatalError::FatalError(T&&... args) + : ErrorBase(fmt::format(std::forward(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 -[[noreturn]] inline void -fatal(T&&... args) -{ - throw FatalError(fmt::format(std::forward(args)...)); -} diff --git a/src/execute.cpp b/src/execute.cpp index 77789ac2d..7b647b7b1 100644 --- a/src/execute.cpp +++ b/src/execute.cpp @@ -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)); } { diff --git a/src/hashutil.cpp b/src/hashutil.cpp index 0a64d024e..9cc768008 100644 --- a/src/hashutil.cpp +++ b/src/hashutil.cpp @@ -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) {