]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Let Error constructor forward arguments to fmt::format
authorJoel Rosdahl <joel@rosdahl.net>
Mon, 3 Aug 2020 19:07:32 +0000 (21:07 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Mon, 3 Aug 2020 19:31:02 +0000 (21:31 +0200)
14 files changed:
src/AtomicFile.cpp
src/CacheEntryReader.cpp
src/Compression.cpp
src/Config.cpp
src/NullCompressor.cpp
src/Result.cpp
src/ResultExtractor.cpp
src/ResultRetriever.cpp
src/Stat.cpp
src/Util.cpp
src/ccache.cpp
src/compress.cpp
src/exceptions.hpp
unittest/TestUtil.cpp

index b227af4f73d9cdb5652792f17824048b93e0245a..8c7d6c7b484c22a9f0294afe2b9828a0d2fe8b0f 100644 (file)
@@ -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<uint8_t>& 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);
 }
index 1772e1a8813fdee2e27a7703cadb2509ae8d259d..5879b51e01010560ca728e0813f3a0b8d66ee42d 100644 (file)
@@ -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();
index 81e3cce409dad3fd724cda5bb9c4fa2e91163b26..abe56ab2c2684d605b04c3c753e86c352766ad3c 100644 (file)
@@ -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
index 61bd6a03d417b938128619a7ef52a74e044e3a23..b1b7d44d88c4ac1be1dcf2d574a0c2eab63cfc79 100644 (file)
@@ -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);
     }
   }
 }
index 9757fd984eab2c12dbf0501bf9bb43c21a6c3566..13494fcb95c414980f54acff284f2f02bee6ce21 100644 (file)
@@ -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.
 //
index 161b2ae4d7ba1dc24aa03a96fa681295f1ae398a..3091e97b457da7b0cb07078b81553424314e2537 100644 (file)
@@ -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,
index 17891bd0933ef181ab31c12bcd97fd8fa6dbfc7b..fe3ed2393b239dbea4ec0eddade8016852fd0d09 100644 (file)
 
 #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());
   }
 }
 
index db78b4790d39385e7d9fba72d304e93b0460937c..c9ac883f3649c8261730b042717dfd840849cb0e 100644 (file)
 #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());
   }
 }
index 3648a316691203c066dafa41bb6dc44e2cda4e7f..80805bb9d40e131de50608dc921a5edd57f29cf0 100644 (file)
@@ -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));
index b28f536f527713122f247d3c52a9b52a4d008bf6..eafab5eabe69b7c6039998b870a20bae7347c37e 100644 (file)
@@ -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<uint32_t>::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));
     }
   });
 }
index 34b573f93d8d82d510242080f2950863c916111f..5d286afb24108e562463568cd6661188683bd78e 100644 (file)
@@ -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);
index d4be1c13cabe57751eb41481796cb08584b1c232..8418ef093334fab916b9278e1c34205b6f4813f7 100644 (file)
@@ -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<CacheEntryReader>
 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()) {
index d6ce7e9a2aedd35581b2a15b9fcbcb1c28e3b5c8..ceabc6452da54ed2cf4f96733170ac9ba0c7d64e 100644 (file)
@@ -27,6 +27,7 @@
 #include "third_party/nonstd/optional.hpp"
 
 #include <stdexcept>
+#include <string>
 #include <utility>
 
 // 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<typename... T> inline Error(T&&... args);
 };
 
+inline Error::Error(const std::string& message) : ErrorBase(message)
+{
+}
+
+template<typename... T>
+inline Error::Error(T&&... args)
+  : ErrorBase(fmt::format(std::forward<T>(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
index f130f5a645e97e48862c356c22bb083fcfa75b0f..04c4fe96ead6bcd1f571b15fa6d2d94d7dd6569b 100644 (file)
@@ -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));
   }
 }