]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
chore: Fix some Clang-Tidy warnings
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 19 Apr 2025 18:30:01 +0000 (20:30 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 19 Apr 2025 20:02:01 +0000 (22:02 +0200)
15 files changed:
cmake/GenerateConfigurationFile.cmake
cmake/config.h.in
src/ccache/.clang-tidy
src/ccache/ccache.cpp
src/ccache/config.cpp
src/ccache/core/mainoptions.cpp
src/ccache/core/manifest.cpp
src/ccache/core/statistics.cpp
src/ccache/hashutil.cpp
src/ccache/storage/local/localstorage.cpp
src/ccache/storage/remote/filestorage.cpp
src/ccache/storage/remote/httpstorage.cpp
src/ccache/util/assertions.hpp
src/ccache/util/logging.cpp
src/ccache/util/string.cpp

index bf2cd5a5555b10e6108539198866db95187536b2..fcc7f025287aee782d8353068b83331845e06c3a 100644 (file)
@@ -25,7 +25,6 @@ endforeach()
 
 include(CheckFunctionExists)
 set(functions
-    asctime_r
     getopt_long
     getpwuid
     localtime_r
index 5e6387ed37d5870856e52f4961f04cba714b9d5d..f7228d539aa2f37c57f429e16e11036e4ad822e5 100644 (file)
 
 // === 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
 
index b82182ea7113ae968a83936cf1d5cb8963508579..f7dc1c059f67df914b58a963c1d977b2c7b1034d 100644 (file)
@@ -16,6 +16,7 @@ Checks: '
   -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,
@@ -59,12 +60,14 @@ Checks: '
   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,
index 65ed0c7df7660af5c2f61ef09fd9c0975b2ae40b..00712987acabd9b0aa57b0e33b3bdee4b75cfd55 100644 (file)
@@ -221,12 +221,13 @@ prepare_debug_path(const fs::path& cwd,
   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,
@@ -495,18 +496,19 @@ print_included_files(const Context& ctx, FILE* fp)
 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.
@@ -549,7 +551,7 @@ process_preprocessed_file(Context& ctx, Hash& hash, const fs::path& 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)) {
@@ -650,7 +652,7 @@ process_preprocessed_file(Context& ctx, Hash& hash, const fs::path& path)
         "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
@@ -1093,16 +1095,17 @@ rewrite_stdout_from_compiler(const Context& ctx, util::Bytes&& stdout_data)
       }
       // 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());
       }
@@ -2145,7 +2148,7 @@ get_result_key_from_manifest(Context& ctx, const Hash::Digest& manifest_key)
   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;
@@ -2284,7 +2287,7 @@ calculate_result_and_manifest_key(Context& ctx,
   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>
@@ -2567,8 +2570,9 @@ cache_compilation(int argc, const char* const* argv)
     }
 
     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;
index ef0fb2a0ff933c2ce64ab202d5c11bc88b482c55..963202ef5cccc6c758ad832db62258a2a9d0a4d9 100644 (file)
@@ -72,7 +72,7 @@ using util::make_path;
 
 namespace {
 
-enum class ConfigItem {
+enum class ConfigItem : uint8_t {
   absolute_paths_in_stderr,
   base_dir,
   cache_dir,
@@ -120,7 +120,7 @@ enum class ConfigItem {
   umask,
 };
 
-enum class ConfigKeyType { normal, alias };
+enum class ConfigKeyType : uint8_t { normal, alias };
 
 struct ConfigKeyTableEntry
 {
@@ -1217,7 +1217,7 @@ Config::set_item(const std::string& key,
     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;
index c3e753aaf236cab92e102a77acf32c45d910ccb7..b31190826de8bdce7ae3cd97f077397230fc2775 100644 (file)
@@ -433,7 +433,7 @@ get_usage_text(const std::string_view ccache_name)
   return FMT(USAGE_TEXT, ccache_name);
 }
 
-enum {
+enum : uint8_t {
   CHECKSUM_FILE,
   CONFIG_PATH,
   DUMP_MANIFEST,
index 9a0f92398e54bff0ca4fd677b0288e9ab2814ddb..cbd2dd33647b5d72feffda22f2226df6448a7d80 100644 (file)
@@ -102,6 +102,7 @@ Manifest::read(nonstd::span<const uint8_t> data)
   }
 
   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>()));
   }
index 2eb786dc1ecd52eb8e50adcf9f0f2915d5d3ad34..9d9aa3e4bfb9855ab71c7747ec22aace0f8140ae 100644 (file)
@@ -1,4 +1,4 @@
-// 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.
 //
@@ -274,7 +274,7 @@ format_timestamp(const util::TimePoint& value)
     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;
   }
index c8bed7e545f4505df2350d7e20866cedb45dc2a2..1a8d458331df4420a045f4d33b2e981d6cc688ec 100644 (file)
@@ -307,17 +307,8 @@ hash_source_code_file(const Context& ctx,
       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);
   }
 
@@ -463,8 +454,7 @@ hash_command_output(Hash& hash,
 
   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]);
index 4b13a4a3837956a767308972e0177951375776f7..29692b975f099d9a4900646f1dda3cc4be8356cc 100644 (file)
@@ -786,17 +786,17 @@ LocalStorage::evict(const ProgressReceiver& progress_receiver,
                     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.
@@ -1082,7 +1082,7 @@ LocalStorage::move_to_wanted_cache_level(const StatisticsCounters& counters,
     // 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));
     }
   }
index 643e718befffe134be238bbaf9373c7326a68584..580871cd506a1f1a346124c8f2837ef820dc86df 100644 (file)
@@ -1,4 +1,4 @@
-// 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.
 //
@@ -59,7 +59,7 @@ public:
   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;
index 605fef155cc5064d07a3e06f81da2e7457a3fa5c..fe0aa4b8a0aa198c3cab5f0cf0bb1cfa9ac6c9e4 100644 (file)
@@ -1,4 +1,4 @@
-// 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.
 //
@@ -54,7 +54,7 @@ public:
   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;
index 5b632ad7dcdb5e07b3e4d68db2f1dfc6d4da0ac7..3930435741d4c460fe39e62b276380d5d1a04dc6 100644 (file)
@@ -1,4 +1,4 @@
-// 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.
 //
@@ -31,7 +31,8 @@
 // release builds.
 #define ASSERT(condition)                                                      \
   do {                                                                         \
-    if (!(condition)) {                                                        \
+    if (condition) {                                                           \
+    } else {                                                                   \
       util::handle_failed_assertion(                                           \
         __FILE__, __LINE__, CCACHE_FUNCTION, #condition);                      \
     }                                                                          \
index 9cbaf7811fb12e621a07e05310b5671dec6eb6d9..b9118661d58936df127707772a61888aa804425f 100644 (file)
@@ -70,7 +70,7 @@ print_fatal_error_and_exit()
           "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);
@@ -83,12 +83,12 @@ do_log(std::string_view message, bool bulk)
 
   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) {
index 4f63edb43084d9f23bfa7d0b35a59cb1afaab3b3..35453a6912714e0fd5efc4537265262e59658af4 100644 (file)
@@ -193,12 +193,12 @@ format_iso8601_timestamp(const TimePoint& time, TimeZone time_zone)
   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;
 }
@@ -440,9 +440,9 @@ replace_first(const std::string_view string,
   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);
   }