From: Joel Rosdahl Date: Tue, 28 Jul 2020 17:53:13 +0000 (+0200) Subject: Convert fatal() function to macro that forwards to fmt::format X-Git-Tag: v4.0~269 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=23aa971fbb458d0cc20dbe12815a15ee939ea6e0;p=thirdparty%2Fccache.git Convert fatal() function to macro that forwards to fmt::format --- diff --git a/src/TemporaryFile.cpp b/src/TemporaryFile.cpp index f91f3b999..f7ac3ec99 100644 --- a/src/TemporaryFile.cpp +++ b/src/TemporaryFile.cpp @@ -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); diff --git a/src/ccache.cpp b/src/ccache.cpp index 139db36ad..7e2246367 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -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(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) { diff --git a/src/exceptions.hpp b/src/exceptions.hpp index cb7a32e46..10a959cc2 100644 --- a/src/exceptions.hpp +++ b/src/exceptions.hpp @@ -20,12 +20,21 @@ #include "system.hpp" +#include "FormatNonstdStringView.hpp" #include "stats.hpp" +#include "third_party/fmt/core.h" #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) + // Don't throw or catch ErrorBase directly, use a subclass. class ErrorBase : public std::runtime_error { diff --git a/src/execute.cpp b/src/execute.cpp index cb5c93acf..003b28768 100644 --- a/src/execute.cpp +++ b/src/execute.cpp @@ -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)); } { diff --git a/src/hashutil.cpp b/src/hashutil.cpp index 432141abc..f636f6886 100644 --- a/src/hashutil.cpp +++ b/src/hashutil.cpp @@ -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) { diff --git a/src/legacy_util.cpp b/src/legacy_util.cpp index 4bc607648..07629c590 100644 --- a/src/legacy_util.cpp +++ b/src/legacy_util.cpp @@ -34,19 +34,6 @@ # include #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. diff --git a/src/legacy_util.hpp b/src/legacy_util.hpp index 0720d5bf1..e91fba8df 100644 --- a/src/legacy_util.hpp +++ b/src/legacy_util.hpp @@ -22,8 +22,6 @@ #include -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); diff --git a/src/logging.cpp b/src/logging.cpp index 472ccd5a6..c3845386b 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(),