From: Joel Rosdahl Date: Mon, 3 Aug 2020 19:07:32 +0000 (+0200) Subject: Let Error constructor forward arguments to fmt::format X-Git-Tag: v4.0~227 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=0f8e918ca267b254e2d17dd8a20237447b3dcb65;p=thirdparty%2Fccache.git Let Error constructor forward arguments to fmt::format --- diff --git a/src/AtomicFile.cpp b/src/AtomicFile.cpp index b227af4f7..8c7d6c7b4 100644 --- a/src/AtomicFile.cpp +++ b/src/AtomicFile.cpp @@ -22,8 +22,6 @@ #include "Util.hpp" #include "exceptions.hpp" -#include "third_party/fmt/core.h" - AtomicFile::AtomicFile(const std::string& path, Mode mode) : m_path(path) { TemporaryFile tmp_file(path + ".tmp"); @@ -44,8 +42,7 @@ void AtomicFile::write(const std::string& data) { if (fwrite(data.data(), data.size(), 1, m_stream) != 1) { - throw Error( - fmt::format("failed to write data to {}: {}", m_path, strerror(errno))); + throw Error("failed to write data to {}: {}", m_path, strerror(errno)); } } @@ -53,8 +50,7 @@ void AtomicFile::write(const std::vector& data) { if (fwrite(data.data(), data.size(), 1, m_stream) != 1) { - throw Error( - fmt::format("failed to write data to {}: {}", m_path, strerror(errno))); + throw Error("failed to write data to {}: {}", m_path, strerror(errno)); } } @@ -66,8 +62,7 @@ AtomicFile::commit() m_stream = nullptr; if (result == EOF) { Util::unlink_tmp(m_tmp_path); - throw Error( - fmt::format("failed to write data to {}: {}", m_path, strerror(errno))); + throw Error("failed to write data to {}: {}", m_path, strerror(errno)); } Util::rename(m_tmp_path, m_path); } diff --git a/src/CacheEntryReader.cpp b/src/CacheEntryReader.cpp index 1772e1a88..5879b51e0 100644 --- a/src/CacheEntryReader.cpp +++ b/src/CacheEntryReader.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2019 Joel Rosdahl and other contributors +// Copyright (C) 2019-2020 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -39,15 +39,15 @@ CacheEntryReader::CacheEntryReader(FILE* stream, Util::big_endian_to_int(header_bytes + 7, m_content_size); if (memcmp(m_magic, expected_magic, sizeof(m_magic)) != 0) { - throw Error(fmt::format("Bad magic value 0x{:02x}{:02x}{:02x}{:02x}", - m_magic[0], - m_magic[1], - m_magic[2], - m_magic[3])); + throw Error("Bad magic value 0x{:02x}{:02x}{:02x}{:02x}", + m_magic[0], + m_magic[1], + m_magic[2], + m_magic[3]); } if (m_version != expected_version) { - throw Error(fmt::format( - "Unknown version (actual {}, expected {})", m_version, expected_version)); + throw Error( + "Unknown version (actual {}, expected {})", m_version, expected_version); } m_checksum.update(header_bytes, sizeof(header_bytes)); @@ -84,10 +84,9 @@ CacheEntryReader::finalize() Util::big_endian_to_int(buffer, expected_digest); if (actual_digest != expected_digest) { - throw Error( - fmt::format("Incorrect checksum (actual 0x{:016x}, expected 0x{:016x})", - actual_digest, - expected_digest)); + throw Error("Incorrect checksum (actual 0x{:016x}, expected 0x{:016x})", + actual_digest, + expected_digest); } m_decompressor->finalize(); diff --git a/src/Compression.cpp b/src/Compression.cpp index 81e3cce40..abe56ab2c 100644 --- a/src/Compression.cpp +++ b/src/Compression.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2019 Joel Rosdahl and other contributors +// Copyright (C) 2019-2020 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -47,7 +47,7 @@ type_from_int(uint8_t type) return Type::zstd; } - throw Error(fmt::format("Unknown type: {}", type)); + throw Error("Unknown type: {}", type); } std::string diff --git a/src/Config.cpp b/src/Config.cpp index 61bd6a03d..b1b7d44d8 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -176,11 +176,11 @@ parse_bool(const std::string& value, if (value == "0" || lower_value == "false" || lower_value == "disable" || lower_value == "no") { throw Error( - fmt::format("invalid boolean environment variable value \"{}\" (did" - " you mean to set \"CCACHE_{}{}=true\"?)", - value, - negate ? "" : "NO", - *env_var_key)); + "invalid boolean environment variable value \"{}\" (did you mean to" + " set \"CCACHE_{}{}=true\"?)", + value, + negate ? "" : "NO", + *env_var_key); } return !negate; } else if (value == "true") { @@ -188,7 +188,7 @@ parse_bool(const std::string& value, } else if (value == "false") { return false; } else { - throw Error(fmt::format("not a boolean value: \"{}\"", value)); + throw Error("not a boolean value: \"{}\"", value); } } @@ -209,7 +209,7 @@ parse_double(const std::string& value) throw Error(e.what()); } if (end != value.size()) { - throw Error(fmt::format("invalid floating point: \"{}\"", value)); + throw Error("invalid floating point: \"{}\"", value); } return result; } @@ -307,7 +307,7 @@ parse_umask(const std::string& value) size_t end; uint32_t result = std::stoul(value, &end, 8); if (end != value.size()) { - throw Error(fmt::format("not an octal integer: \"{}\"", value)); + throw Error("not an octal integer: \"{}\"", value); } return result; } @@ -326,7 +326,7 @@ void verify_absolute_path(const std::string& value) { if (!Util::is_absolute_path(value)) { - throw Error(fmt::format("not an absolute path: \"{}\"", value)); + throw Error("not an absolute path: \"{}\"", value); } } @@ -379,7 +379,7 @@ parse_config_file(const std::string& path, config_line_handler(line, key, value); } } catch (const Error& e) { - throw Error(fmt::format("{}:{}: {}", path, line_number, e.what())); + throw Error("{}:{}: {}", path, line_number, e.what()); } } return true; @@ -453,8 +453,7 @@ Config::update_from_environment() try { set_item(config_key, value, key, negate, "environment"); } catch (const Error& e) { - throw Error( - fmt::format("CCACHE_{}{}: {}", negate ? "NO" : "", key, e.what())); + throw Error("CCACHE_{}{}: {}", negate ? "NO" : "", key, e.what()); } } } @@ -464,7 +463,7 @@ Config::get_string_value(const std::string& key) const { auto it = k_config_key_table.find(key); if (it == k_config_key_table.end()) { - throw Error(fmt::format("unknown configuration option \"{}\"", key)); + throw Error("unknown configuration option \"{}\"", key); } switch (it->second) { @@ -587,7 +586,7 @@ Config::set_value_in_file(const std::string& path, const std::string& value) { if (k_config_key_table.find(key) == k_config_key_table.end()) { - throw Error(fmt::format("unknown configuration option \"{}\"", key)); + throw Error("unknown configuration option \"{}\"", key); } // Verify that the value is valid; set_item will throw if not. @@ -608,7 +607,7 @@ Config::set_value_in_file(const std::string& path, output.write(fmt::format("{}\n", c_line)); } })) { - throw Error(fmt::format("failed to open {}: {}", path, strerror(errno))); + throw Error("failed to open {}: {}", path, strerror(errno)); } if (!found) { @@ -815,10 +814,10 @@ Config::check_key_tables_consistency() { for (const auto& item : k_env_variable_table) { if (k_config_key_table.find(item.second) == k_config_key_table.end()) { - throw Error(fmt::format( + throw Error( "env var {} mapped to {} which is missing from k_config_key_table", item.first, - item.second)); + item.second); } } } diff --git a/src/NullCompressor.cpp b/src/NullCompressor.cpp index 9757fd984..13494fcb9 100644 --- a/src/NullCompressor.cpp +++ b/src/NullCompressor.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2019 Joel Rosdahl and other contributors +// Copyright (C) 2019-2020 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // diff --git a/src/Result.cpp b/src/Result.cpp index 161b2ae4d..3091e97b4 100644 --- a/src/Result.cpp +++ b/src/Result.cpp @@ -215,8 +215,7 @@ Reader::read_result(Consumer& consumer) } if (i != n_entries) { - throw Error( - fmt::format("Too few entries (read {}, expected {})", i, n_entries)); + throw Error("Too few entries (read {}, expected {})", i, n_entries); } cache_entry_reader.finalize(); @@ -237,7 +236,7 @@ Reader::read_entry(CacheEntryReader& cache_entry_reader, break; default: - throw Error(fmt::format("Unknown entry type: {}", marker)); + throw Error("Unknown entry type: {}", marker); } UnderlyingFileTypeInt type; @@ -264,11 +263,10 @@ Reader::read_entry(CacheEntryReader& cache_entry_reader, auto raw_path = get_raw_file_path(m_result_path, entry_number); auto st = Stat::stat(raw_path, Stat::OnError::throw_error); if (st.size() != file_len) { - throw Error( - fmt::format("Bad file size of {} (actual {} bytes, expected {} bytes)", - raw_path, - st.size(), - file_len)); + throw Error("Bad file size of {} (actual {} bytes, expected {} bytes)", + raw_path, + st.size(), + file_len); } consumer.on_entry_start(entry_number, file_type, file_len, raw_path); @@ -373,7 +371,7 @@ Result::Writer::write_embedded_file_entry(CacheEntryWriter& writer, { Fd file(open(path.c_str(), O_RDONLY | O_BINARY)); if (!file) { - throw Error(fmt::format("Failed to open {} for reading", path)); + throw Error("Failed to open {} for reading", path); } uint64_t remain = file_size; @@ -385,11 +383,10 @@ Result::Writer::write_embedded_file_entry(CacheEntryWriter& writer, if (errno == EINTR) { continue; } - throw Error( - fmt::format("Error reading from {}: {}", path, strerror(errno))); + throw Error("Error reading from {}: {}", path, strerror(errno)); } if (bytes_read == 0) { - throw Error(fmt::format("Error reading from {}: end of file", path)); + throw Error("Error reading from {}: end of file", path); } writer.write(buf, n); remain -= n; @@ -405,8 +402,8 @@ Result::Writer::write_raw_file_entry(const std::string& path, try { Util::clone_hard_link_or_copy_file(m_ctx, path, raw_file, true); } catch (Error& e) { - throw Error(fmt::format( - "Failed to store {} as raw file {}: {}", path, raw_file, e.what())); + throw Error( + "Failed to store {} as raw file {}: {}", path, raw_file, e.what()); } auto new_stat = Stat::stat(raw_file); stats_update_size(m_ctx.counter_updates, diff --git a/src/ResultExtractor.cpp b/src/ResultExtractor.cpp index 17891bd09..fe3ed2393 100644 --- a/src/ResultExtractor.cpp +++ b/src/ResultExtractor.cpp @@ -20,10 +20,6 @@ #include "Util.hpp" -#include "third_party/nonstd/string_view.hpp" - -using nonstd::string_view; - ResultExtractor::ResultExtractor(const std::string& directory) : m_directory(directory) { @@ -54,15 +50,15 @@ ResultExtractor::on_entry_start(uint32_t /*entry_number*/, m_dest_fd = Fd( open(m_dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666)); if (!m_dest_fd) { - throw Error(fmt::format( - "Failed to open {} for writing: {}", m_dest_path, strerror(errno))); + throw Error( + "Failed to open {} for writing: {}", m_dest_path, strerror(errno)); } } else { try { Util::copy_file(*raw_file, m_dest_path, false); } catch (Error& e) { - throw Error(fmt::format( - "Failed to copy {} to {}: {}", *raw_file, m_dest_path, e.what())); + throw Error( + "Failed to copy {} to {}: {}", *raw_file, m_dest_path, e.what()); } } } @@ -75,8 +71,7 @@ ResultExtractor::on_entry_data(const uint8_t* data, size_t size) try { Util::write_fd(*m_dest_fd, data, size); } catch (Error& e) { - throw Error( - fmt::format("Failed to write to {}: {}", m_dest_path, e.what())); + throw Error("Failed to write to {}: {}", m_dest_path, e.what()); } } diff --git a/src/ResultRetriever.cpp b/src/ResultRetriever.cpp index db78b4790..c9ac883f3 100644 --- a/src/ResultRetriever.cpp +++ b/src/ResultRetriever.cpp @@ -21,10 +21,7 @@ #include "Context.hpp" #include "Logging.hpp" -#include "third_party/nonstd/string_view.hpp" - using Logging::log; -using nonstd::string_view; using Result::FileType; ResultRetriever::ResultRetriever(Context& ctx, bool rewrite_dependency_target) @@ -111,8 +108,8 @@ ResultRetriever::on_entry_start(uint32_t entry_number, m_dest_fd = Fd( open(dest_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666)); if (!m_dest_fd) { - throw Error(fmt::format( - "Failed to open {} for writing: {}", dest_path, strerror(errno))); + throw Error( + "Failed to open {} for writing: {}", dest_path, strerror(errno)); } m_dest_path = dest_path; } @@ -132,8 +129,7 @@ ResultRetriever::on_entry_data(const uint8_t* data, size_t size) try { Util::write_fd(*m_dest_fd, data, size); } catch (Error& e) { - throw Error( - fmt::format("Failed to write to {}: {}", m_dest_path, e.what())); + throw Error("Failed to write to {}: {}", m_dest_path, e.what()); } } } @@ -174,7 +170,6 @@ ResultRetriever::write_dependency_file() m_dest_data.data() + start_pos, m_dest_data.length() - start_pos); } catch (Error& e) { - throw Error( - fmt::format("Failed to write to {}: {}", m_dest_path, e.what())); + throw Error("Failed to write to {}: {}", m_dest_path, e.what()); } } diff --git a/src/Stat.cpp b/src/Stat.cpp index 3648a3166..80805bb9d 100644 --- a/src/Stat.cpp +++ b/src/Stat.cpp @@ -20,8 +20,6 @@ #include "Logging.hpp" -#include "third_party/fmt/core.h" - using Logging::log; Stat::Stat(StatFunction stat_function, @@ -34,7 +32,7 @@ Stat::Stat(StatFunction stat_function, } else { m_errno = errno; if (on_error == OnError::throw_error) { - throw Error(fmt::format("failed to stat {}: {}", path, strerror(errno))); + throw Error("failed to stat {}: {}", path, strerror(errno)); } if (on_error == OnError::log) { log("Failed to stat {}: {}", path, strerror(errno)); diff --git a/src/Util.cpp b/src/Util.cpp index b28f536f5..eafab5eab 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -180,7 +180,7 @@ clone_file(const std::string& src, const std::string& dest, bool via_tmp_file) # if defined(__linux__) Fd src_fd(open(src.c_str(), O_RDONLY)); if (!src_fd) { - throw Error(fmt::format("{}: {}", src, strerror(errno))); + throw Error("{}: {}", src, strerror(errno)); } Fd dest_fd; @@ -193,7 +193,7 @@ clone_file(const std::string& src, const std::string& dest, bool via_tmp_file) dest_fd = Fd(open(dest.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666)); if (!dest_fd) { - throw Error(fmt::format("{}: {}", src, strerror(errno))); + throw Error("{}: {}", src, strerror(errno)); } } @@ -311,7 +311,7 @@ copy_file(const std::string& src, const std::string& dest, bool via_tmp_file) { Fd src_fd(open(src.c_str(), O_RDONLY)); if (!src_fd) { - throw Error(fmt::format("{}: {}", src, strerror(errno))); + throw Error("{}: {}", src, strerror(errno)); } Fd dest_fd; @@ -324,7 +324,7 @@ copy_file(const std::string& src, const std::string& dest, bool via_tmp_file) dest_fd = Fd(open(dest.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666)); if (!dest_fd) { - throw Error(fmt::format("{}: {}", dest, strerror(errno))); + throw Error("{}: {}", dest, strerror(errno)); } } @@ -400,8 +400,7 @@ expand_environment_variables(const std::string& str) ++right; } if (curly && *right != '}') { - throw Error( - fmt::format("syntax error: missing '}}' after \"{}\"", left)); + throw Error("syntax error: missing '}}' after \"{}\"", left); } if (right == left) { // Special case: don't consider a single $ the left of a variable. @@ -410,7 +409,7 @@ expand_environment_variables(const std::string& str) std::string name(left, right - left); const char* value = getenv(name.c_str()); if (!value) { - throw Error(fmt::format("environment variable \"{}\" not set", name)); + throw Error("environment variable \"{}\" not set", name); } result += value; if (!curly) { @@ -920,8 +919,8 @@ parse_duration(const std::string& duration) factor = 1; break; default: - throw Error(fmt::format( - "invalid suffix (supported: d (day) and s (second)): \"{}\"", duration)); + throw Error("invalid suffix (supported: d (day) and s (second)): \"{}\"", + duration); } return factor * parse_uint32(duration.substr(0, duration.length() - 1)); @@ -939,7 +938,7 @@ parse_int(const std::string& value) failed = true; } if (failed || end != value.size()) { - throw Error(fmt::format("invalid integer: \"{}\"", value)); + throw Error("invalid integer: \"{}\"", value); } return result; } @@ -952,7 +951,7 @@ parse_size(const std::string& value) char* p; double result = strtod(value.c_str(), &p); if (errno != 0 || result < 0 || p == value.c_str() || value.empty()) { - throw Error(fmt::format("invalid size: \"{}\"", value)); + throw Error("invalid size: \"{}\"", value); } while (isspace(*p)) { @@ -976,7 +975,7 @@ parse_size(const std::string& value) result *= multiplier; break; default: - throw Error(fmt::format("invalid size: \"{}\"", value)); + throw Error("invalid size: \"{}\"", value); } } else { // Default suffix: G. @@ -998,7 +997,7 @@ parse_uint32(const std::string& value) } if (failed || end != value.size() || result < 0 || result > std::numeric_limits::max()) { - throw Error(fmt::format("invalid 32-bit unsigned integer: \"{}\"", value)); + throw Error("invalid 32-bit unsigned integer: \"{}\"", value); } return result; } @@ -1129,8 +1128,8 @@ rename(const std::string& oldpath, const std::string& newpath) { #ifndef _WIN32 if (::rename(oldpath.c_str(), newpath.c_str()) != 0) { - throw Error(fmt::format( - "failed to rename {} to {}: {}", oldpath, newpath, strerror(errno))); + throw Error( + "failed to rename {} to {}: {}", oldpath, newpath, strerror(errno)); } #else // Windows' rename() won't overwrite an existing file, so need to use @@ -1138,10 +1137,10 @@ rename(const std::string& oldpath, const std::string& newpath) if (!MoveFileExA( oldpath.c_str(), newpath.c_str(), MOVEFILE_REPLACE_EXISTING)) { DWORD error = GetLastError(); - throw Error(fmt::format("failed to rename {} to {}: {}", - oldpath, - newpath, - Win32Util::error_message(error))); + throw Error("failed to rename {} to {}: {}", + oldpath, + newpath, + Win32Util::error_message(error)); } #endif } @@ -1177,7 +1176,7 @@ send_to_stderr(const std::string& text, bool strip_colors) try { write_fd(STDERR_FILENO, text_to_send->data(), text_to_send->length()); } catch (Error& e) { - throw Error(fmt::format("Failed to write to stderr: {}", e.what())); + throw Error("Failed to write to stderr: {}", e.what()); } } @@ -1283,9 +1282,9 @@ traverse(const std::string& path, const TraverseVisitor& visitor) if (stat.error_number() == ENOENT || stat.error_number() == ESTALE) { continue; } - throw Error(fmt::format("failed to lstat {}: {}", - entry_path, - strerror(stat.error_number()))); + throw Error("failed to lstat {}: {}", + entry_path, + strerror(stat.error_number())); } is_dir = stat.is_directory(); } @@ -1300,8 +1299,7 @@ traverse(const std::string& path, const TraverseVisitor& visitor) } else if (errno == ENOTDIR) { visitor(path, false); } else { - throw Error( - fmt::format("failed to open directory {}: {}", path, strerror(errno))); + throw Error("failed to open directory {}: {}", path, strerror(errno)); } } @@ -1387,10 +1385,10 @@ wipe_path(const std::string& path) traverse(path, [](const std::string& p, bool is_dir) { if (is_dir) { if (rmdir(p.c_str()) != 0 && errno != ENOENT && errno != ESTALE) { - throw Error(fmt::format("failed to rmdir {}: {}", p, strerror(errno))); + throw Error("failed to rmdir {}: {}", p, strerror(errno)); } } else if (unlink(p.c_str()) != 0 && errno != ENOENT && errno != ESTALE) { - throw Error(fmt::format("failed to unlink {}: {}", p, strerror(errno))); + throw Error("failed to unlink {}: {}", p, strerror(errno)); } }); } diff --git a/src/ccache.cpp b/src/ccache.cpp index 34b573f93..5d286afb2 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -2314,7 +2314,7 @@ handle_main_options(int argc, const char* const* argv) // for the -o=K=V case (key "=K" and value "V"). size_t eq_pos = arg.find('=', 1); if (eq_pos == std::string::npos) { - throw Error(fmt::format("missing equal sign in \"{}\"", arg)); + throw Error("missing equal sign in \"{}\"", arg); } std::string key = arg.substr(0, eq_pos); std::string value = arg.substr(eq_pos + 1); diff --git a/src/compress.cpp b/src/compress.cpp index d4be1c13c..8418ef093 100644 --- a/src/compress.cpp +++ b/src/compress.cpp @@ -42,8 +42,7 @@ open_file(const std::string& path, const char* mode) { File f(path, mode); if (!f) { - throw Error( - fmt::format("failed to open {} for reading: {}", path, strerror(errno))); + throw Error("failed to open {} for reading: {}", path, strerror(errno)); } return f; } @@ -52,7 +51,7 @@ static std::unique_ptr create_reader(const CacheFile& cache_file, FILE* stream) { if (cache_file.type() == CacheFile::Type::unknown) { - throw Error(fmt::format("unknown file type for {}", cache_file.path())); + throw Error("unknown file type for {}", cache_file.path()); } switch (cache_file.type()) { diff --git a/src/exceptions.hpp b/src/exceptions.hpp index d6ce7e9a2..ceabc6452 100644 --- a/src/exceptions.hpp +++ b/src/exceptions.hpp @@ -27,6 +27,7 @@ #include "third_party/nonstd/optional.hpp" #include +#include #include // Don't throw or catch ErrorBase directly, use a subclass. @@ -40,9 +41,24 @@ class ErrorBase : public std::runtime_error // treated similar to FatalError. class Error : public ErrorBase { - using ErrorBase::ErrorBase; +public: + // Special case: If given only one string, don't parse it as a format string. + Error(const std::string& message); + + // `args` are forwarded to `fmt::format`. + template inline Error(T&&... args); }; +inline Error::Error(const std::string& message) : ErrorBase(message) +{ +} + +template +inline Error::Error(T&&... args) + : ErrorBase(fmt::format(std::forward(args)...)) +{ +} + // Throw a FatalError to make ccache print the error message to stderr and exit // with a non-zero exit code. class FatalError : public ErrorBase diff --git a/unittest/TestUtil.cpp b/unittest/TestUtil.cpp index f130f5a64..04c4fe96e 100644 --- a/unittest/TestUtil.cpp +++ b/unittest/TestUtil.cpp @@ -50,8 +50,7 @@ void check_chdir(const std::string& dir) { if (chdir(dir.c_str()) != 0) { - throw Error(fmt::format( - "failed to change directory to {}: {}", dir, strerror(errno))); + throw Error("failed to change directory to {}: {}", dir, strerror(errno)); } }