include(CheckFunctionExists)
set(functions
- asctime_r
getopt_long
getpwuid
localtime_r
// === Functions ===
-// Define if you have the "asctime_r" function.
-#cmakedefine HAVE_ASCTIME_R
-
// Define if you have the "getopt_long" function.
#cmakedefine HAVE_GETOPT_LONG
-bugprone-implicit-widening-of-multiplication-result,
-bugprone-narrowing-conversions,
-bugprone-signed-char-misuse,
+ -bugprone-switch-missing-default-case,-warnings-as-errors,
-bugprone-unhandled-exception-at-new,
cert-*,
-cert-dcl50-cpp,
performance-*,
-performance-unnecessary-value-param,
readability-*,
+ -readability-avoid-nested-conditional-operator,
-readability-convert-member-functions-to-static,
-readability-else-after-return,
-readability-function-cognitive-complexity,
-readability-identifier-length,
-readability-implicit-bool-conversion,
-readability-magic-numbers,
+ -readability-math-missing-parentheses,
-readability-named-parameter,
-readability-qualified-auto,
-readability-redundant-declaration,
char timestamp[100];
const auto tm = util::localtime(time_of_invocation);
if (tm) {
- strftime(timestamp, sizeof(timestamp), "%Y%m%d_%H%M%S", &*tm);
+ (void)strftime(timestamp, sizeof(timestamp), "%Y%m%d_%H%M%S", &*tm);
} else {
- snprintf(timestamp,
- sizeof(timestamp),
- "%llu",
- static_cast<long long unsigned int>(time_of_invocation.sec()));
+ (void)snprintf(
+ timestamp,
+ sizeof(timestamp),
+ "%llu",
+ static_cast<long long unsigned int>(time_of_invocation.sec()));
}
return FMT("{}.{}_{:06}.ccache-{}",
prefix,
static tl::expected<void, Failure>
process_preprocessed_file(Context& ctx, Hash& hash, const fs::path& path)
{
- auto data = util::read_file<std::string>(path);
- if (!data) {
- LOG("Failed to read {}: {}", path, data.error());
+ auto content = util::read_file<std::string>(path);
+ if (!content) {
+ LOG("Failed to read {}: {}", path, content.error());
return tl::unexpected(Statistic::internal_error);
}
std::unordered_map<std::string, std::string> relative_inc_path_cache;
// Bytes between p and q are pending to be hashed.
- char* q = &(*data)[0];
+ std::string& data = *content;
+ char* q = data.data();
const char* p = q;
- const char* end = p + data->length();
+ const char* end = p + data.length();
// There must be at least 7 characters (# 1 "x") left to potentially find an
// include file path.
// HP/AIX:
|| (q[1] == 'l' && q[2] == 'i' && q[3] == 'n' && q[4] == 'e'
&& q[5] == ' '))
- && (q == data->data() || q[-1] == '\n')) {
+ && (q == data.data() || q[-1] == '\n')) {
// Workarounds for preprocessor linemarker bugs in GCC version 6.
if (q[2] == '3') {
if (util::starts_with(q, hash_31_command_line_newline)) {
"bin directive in source code");
return tl::unexpected(Failure(Statistic::unsupported_code_directive));
} else if (strncmp(q, "___________", 10) == 0
- && (q == data->data() || q[-1] == '\n')) {
+ && (q == data.data() || q[-1] == '\n')) {
// Unfortunately the distcc-pump wrapper outputs standard output lines:
// __________Using distcc-pump from /usr/bin
// __________Using # distcc servers in pump mode
}
// The MSVC /FC option causes paths in diagnostics messages to become
// absolute. Those within basedir need to be changed into relative paths.
- else if (std::size_t path_end = 0;
- ctx.config.compiler_type() == CompilerType::msvc
- && !ctx.config.base_dir().empty()
- && (path_end = core::get_diagnostics_path_length(line)) != 0) {
- std::string_view abs_path = line.substr(0, path_end);
- fs::path rel_path = core::make_relative_path(ctx, abs_path);
- std::string line_with_rel =
- util::replace_all(line, abs_path, util::pstr(rel_path).str());
- new_stdout_data.insert(
- new_stdout_data.end(), line_with_rel.data(), line_with_rel.size());
+ else if (ctx.config.compiler_type() == CompilerType::msvc
+ && !ctx.config.base_dir().empty()) {
+ size_t path_end = core::get_diagnostics_path_length(line);
+ if (path_end != 0) {
+ std::string_view abs_path = line.substr(0, path_end);
+ fs::path rel_path = core::make_relative_path(ctx, abs_path);
+ std::string line_with_rel =
+ util::replace_all(line, abs_path, util::pstr(rel_path).str());
+ new_stdout_data.insert(
+ new_stdout_data.end(), line_with_rel.data(), line_with_rel.size());
+ }
} else {
new_stdout_data.insert(new_stdout_data.end(), line.data(), line.size());
}
std::optional<Hash::Digest> result_key;
size_t read_manifests = 0;
ctx.storage.get(
- manifest_key, core::CacheEntryType::manifest, [&](util::Bytes&& value) {
+ manifest_key, core::CacheEntryType::manifest, [&](const auto& value) {
try {
read_manifest(ctx, value);
++read_manifests;
return std::make_pair(result_key, manifest_key);
}
-enum class FromCacheCallMode { direct, cpp };
+enum class FromCacheCallMode : uint8_t { direct, cpp };
// Try to return the compile result from cache.
static tl::expected<bool, Failure>
}
if (!result) {
- if (result.error().exit_code()) {
- return *result.error().exit_code();
+ const auto& exit_code = result.error().exit_code();
+ if (exit_code) {
+ return *exit_code;
}
// Else: Fall back to running the real compiler.
fall_back_to_original_compiler = true;
namespace {
-enum class ConfigItem {
+enum class ConfigItem : uint8_t {
absolute_paths_in_stderr,
base_dir,
cache_dir,
umask,
};
-enum class ConfigKeyType { normal, alias };
+enum class ConfigKeyType : uint8_t { normal, alias };
struct ConfigKeyTableEntry
{
break;
}
- const std::string canonical_key = it->second.alias ? *it->second.alias : key;
+ const std::string& canonical_key = it->second.alias.value_or(key);
const auto& [element, inserted] = m_origins.emplace(canonical_key, origin);
if (!inserted) {
element->second = origin;
return FMT(USAGE_TEXT, ccache_name);
}
-enum {
+enum : uint8_t {
CHECKSUM_FILE,
CONFIG_PATH,
DUMP_MANIFEST,
}
const auto file_count = reader.read_int<uint32_t>();
+ files.reserve(file_count);
for (uint32_t i = 0; i < file_count; ++i) {
files.emplace_back(reader.read_str(reader.read_int<uint16_t>()));
}
-// Copyright (C) 2021-2024 Joel Rosdahl and other contributors
+// Copyright (C) 2021-2025 Joel Rosdahl and other contributors
//
// See doc/AUTHORS.adoc for a complete list of contributors.
//
const auto tm = util::localtime(value);
char buffer[100] = "?";
if (tm) {
- strftime(buffer, sizeof(buffer), "%c", &*tm);
+ (void)strftime(buffer, sizeof(buffer), "%c", &*tm);
}
return buffer;
}
return result;
}
hash.hash_delimiter("timestamp");
-#ifdef HAVE_ASCTIME_R
- char buffer[26];
- const char* timestamp = asctime_r(&*modified_time, buffer);
-#else
- // cppcheck-suppress asctimeCalled; thread-safety not needed here
- const char* timestamp = asctime(&*modified_time);
-#endif
- if (!timestamp) {
- result.insert(HashSourceCode::error);
- return result;
- }
+ char timestamp[26];
+ (void)strftime(timestamp, sizeof(timestamp), "%c", &*modified_time);
hash.hash(timestamp);
}
pid_t pid;
extern char** environ;
- int result = posix_spawnp(
- &pid, argv[0], &fa, nullptr, const_cast<char* const*>(argv), environ);
+ int result = posix_spawnp(&pid, argv[0], &fa, nullptr, argv, environ);
posix_spawn_file_actions_destroy(&fa);
close(pipefd[1]);
std::optional<uint64_t> max_age,
std::optional<std::string> namespace_)
{
- return do_clean_all(progress_receiver, 0, 0, max_age, namespace_);
+ do_clean_all(progress_receiver, 0, 0, max_age, namespace_);
}
void
LocalStorage::clean_all(const ProgressReceiver& progress_receiver)
{
- return do_clean_all(progress_receiver,
- m_config.max_size(),
- m_config.max_files(),
- std::nullopt,
- std::nullopt);
+ do_clean_all(progress_receiver,
+ m_config.max_size(),
+ m_config.max_files(),
+ std::nullopt,
+ std::nullopt);
}
// Wipe all cached files in all subdirectories.
// to rename is OK.
LOG("Moving {} to {}", cache_file_path, wanted_path);
fs::rename(cache_file_path, wanted_path);
- for (auto [file_number, dest_path] : m_added_raw_files) {
+ for (const auto& [file_number, dest_path] : m_added_raw_files) {
fs::rename(dest_path, get_raw_file_path(wanted_path, file_number));
}
}
-// Copyright (C) 2021-2024 Joel Rosdahl and other contributors
+// Copyright (C) 2021-2025 Joel Rosdahl and other contributors
//
// See doc/AUTHORS.adoc for a complete list of contributors.
//
tl::expected<bool, Failure> remove(const Hash::Digest& key) override;
private:
- enum class Layout { flat, subdirs };
+ enum class Layout : uint8_t { flat, subdirs };
std::string m_dir;
std::optional<mode_t> m_umask;
-// Copyright (C) 2021-2024 Joel Rosdahl and other contributors
+// Copyright (C) 2021-2025 Joel Rosdahl and other contributors
//
// See doc/AUTHORS.adoc for a complete list of contributors.
//
tl::expected<bool, Failure> remove(const Hash::Digest& key) override;
private:
- enum class Layout { bazel, flat, subdirs };
+ enum class Layout : uint8_t { bazel, flat, subdirs };
std::string m_url_path;
httplib::Client m_http_client;
-// Copyright (C) 2020-2023 Joel Rosdahl and other contributors
+// Copyright (C) 2020-2025 Joel Rosdahl and other contributors
//
// See doc/AUTHORS.adoc for a complete list of contributors.
//
// release builds.
#define ASSERT(condition) \
do { \
- if (!(condition)) { \
+ if (condition) { \
+ } else { \
util::handle_failed_assertion( \
__FILE__, __LINE__, CCACHE_FUNCTION, #condition); \
} \
"ccache: error: Failed to write to {}: {}\n",
logfile_path,
strerror(errno));
- } catch (std::runtime_error&) {
+ } catch (std::runtime_error&) { // NOLINT: this is deliberate
// Ignore since we can't do anything about it.
}
exit(EXIT_FAILURE);
if (!bulk || prefix[0] == '\0') {
const auto now = util::TimePoint::now();
- snprintf(prefix,
- sizeof(prefix),
- "[%s.%06u %-5d] ",
- util::format_iso8601_timestamp(now).c_str(),
- static_cast<unsigned int>(now.nsec_decimal_part() / 1000),
- static_cast<int>(getpid()));
+ (void)snprintf(prefix,
+ sizeof(prefix),
+ "[%s.%06u %-5d] ",
+ util::format_iso8601_timestamp(now).c_str(),
+ static_cast<unsigned int>(now.nsec_decimal_part() / 1000),
+ static_cast<int>(getpid()));
}
if (logfile) {
const auto tm =
(time_zone == TimeZone::local ? util::localtime : util::gmtime)(time);
if (tm) {
- strftime(timestamp, sizeof(timestamp), "%Y-%m-%dT%H:%M:%S", &*tm);
+ (void)strftime(timestamp, sizeof(timestamp), "%Y-%m-%dT%H:%M:%S", &*tm);
} else {
- snprintf(timestamp,
- sizeof(timestamp),
- "%llu",
- static_cast<long long unsigned int>(time.sec()));
+ (void)snprintf(timestamp,
+ sizeof(timestamp),
+ "%llu",
+ static_cast<long long unsigned int>(time.sec()));
}
return timestamp;
}
std::string result;
const auto pos = string.find(from);
if (pos != std::string_view::npos) {
- result.append(string.data(), pos);
- result.append(to.data(), to.length());
- result.append(string.data() + pos + from.size());
+ result.append(string.substr(0, pos));
+ result.append(to);
+ result.append(string.substr(pos + from.size()));
} else {
result = std::string(string);
}