From: Joel Rosdahl Date: Fri, 31 Jul 2020 17:25:07 +0000 (+0200) Subject: Convert FATAL macro to a function X-Git-Tag: v4.0~245 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9af1c0c5615ee72889f47b479926b387c35bcffb;p=thirdparty%2Fccache.git Convert FATAL macro to a function --- diff --git a/src/TemporaryFile.cpp b/src/TemporaryFile.cpp index f7ac3ec99..8dad5cf99 100644 --- a/src/TemporaryFile.cpp +++ b/src/TemporaryFile.cpp @@ -64,12 +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 {}: {}", dir, strerror(errno)); + fatal("Failed to create directory {}: {}", dir, strerror(errno)); } initialize(path_prefix); } if (!fd) { - FATAL("Failed to create temporary file for {}: {}", path, strerror(errno)); + fatal("Failed to create temporary file for {}: {}", path, strerror(errno)); } set_cloexec_flag(*fd); diff --git a/src/Util.cpp b/src/Util.cpp index d63565ecf..652cf6633 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -611,7 +611,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)"); } const char* diff --git a/src/ccache.cpp b/src/ccache.cpp index 3e8a2341f..e2ed2ea11 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -151,7 +151,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)); + fatal("{}: {}", word, strerror(errno)); } prefix.push_back(path); @@ -1740,10 +1740,10 @@ 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); + fatal("Could not find compiler \"{}\" in PATH", base); } if (compiler == argv[0]) { - FATAL("Recursive invocation (the name of the ccache binary must be \"{}\")", + fatal("Recursive invocation (the name of the ccache binary must be \"{}\")", MYNAME); } ctx.orig_args[0] = compiler; @@ -1801,13 +1801,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( @@ -1942,7 +1942,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(execv_argv.data())); - FATAL("execv of {} failed: {}", execv_argv[0], strerror(errno)); + fatal("execv of {} failed: {}", execv_argv[0], strerror(errno)); } } diff --git a/src/exceptions.hpp b/src/exceptions.hpp index 10a959cc2..1d70ad53d 100644 --- a/src/exceptions.hpp +++ b/src/exceptions.hpp @@ -27,13 +27,7 @@ #include "third_party/nonstd/optional.hpp" #include - -// 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) +#include // Don't throw or catch ErrorBase directly, use a subclass. class ErrorBase : public std::runtime_error @@ -89,3 +83,12 @@ 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 bb67fa9da..f17e52e9d 100644 --- a/src/execute.cpp +++ b/src/execute.cpp @@ -165,7 +165,7 @@ execute(const char* const* argv, Fd&& fd_out, Fd&& fd_err, pid_t* pid) } if (*pid == -1) { - FATAL("Failed to fork: {}", strerror(errno)); + fatal("Failed to fork: {}", strerror(errno)); } if (*pid == 0) { @@ -182,7 +182,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)); + fatal("waitpid failed: {}", strerror(errno)); } { diff --git a/src/hashutil.cpp b/src/hashutil.cpp index f76ba7b8b..83306194d 100644 --- a/src/hashutil.cpp +++ b/src/hashutil.cpp @@ -464,12 +464,12 @@ hash_command_output(Hash& hash, #else int pipefd[2]; if (pipe(pipefd) == -1) { - FATAL("pipe failed: {}", strerror(errno)); + fatal("pipe failed: {}", strerror(errno)); } pid_t pid = fork(); if (pid == -1) { - FATAL("fork failed: {}", strerror(errno)); + fatal("fork failed: {}", strerror(errno)); } if (pid == 0) { diff --git a/src/logging.cpp b/src/logging.cpp index d2b62b521..94e55a4d0 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -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(),