From: Joel Rosdahl Date: Fri, 18 Jun 2021 19:09:12 +0000 (+0200) Subject: Use some generic lambda expressions X-Git-Tag: v4.4~195 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=547cef6cb86e5aba57df398e78d6e47bf1f7ae26;p=thirdparty%2Fccache.git Use some generic lambda expressions --- diff --git a/dockerfiles/ubuntu-14.04/Dockerfile b/dockerfiles/ubuntu-14.04/Dockerfile deleted file mode 100644 index 5b3d1dbf5..000000000 --- a/dockerfiles/ubuntu-14.04/Dockerfile +++ /dev/null @@ -1,26 +0,0 @@ -FROM ubuntu:14.04 - -RUN apt-get update \ - && apt-get install -y --no-install-recommends \ - asciidoc \ - bash \ - build-essential \ - ccache \ - clang-3.4 \ - curl \ - docbook-xml \ - docbook-xsl \ - elfutils \ - g++-multilib \ - ninja-build \ - wget \ - xsltproc \ - && rm -rf /var/lib/apt/lists/* - -# Redirect all compilers to ccache. -RUN for t in gcc g++ cc c++ clang clang++; do ln -vs /usr/bin/ccache /usr/local/bin/$t; done - -# The distribution's CMake it too old (2.8.12.2). -RUN curl -sSL https://cmake.org/files/v3.5/cmake-3.5.2-Linux-x86_64.tar.gz | sudo tar -xzC /opt \ - && cp -a /opt/cmake-3.5.2-Linux-x86_64/bin /usr/local \ - && cp -a /opt/cmake-3.5.2-Linux-x86_64/share /usr/local diff --git a/src/Args.cpp b/src/Args.cpp index 5270989d7..a1913299f 100644 --- a/src/Args.cpp +++ b/src/Args.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2020 Joel Rosdahl and other contributors +// Copyright (C) 2020-2021 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -169,7 +169,7 @@ Args::erase_with_prefix(string_view prefix) { m_args.erase(std::remove_if(m_args.begin(), m_args.end(), - [&prefix](const std::string& s) { + [&prefix](const auto& s) { return Util::starts_with(s, prefix); }), m_args.end()); diff --git a/src/Config.cpp b/src/Config.cpp index 06e35cabf..da26a24b5 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -472,14 +472,12 @@ Config::set_secondary_config_path(std::string path) bool Config::update_from_file(const std::string& path) { - return parse_config_file(path, - [&](const std::string& /*line*/, - const std::string& key, - const std::string& value) { - if (!key.empty()) { - set_item(key, value, nullopt, false, path); - } - }); + return parse_config_file( + path, [&](const auto& /*line*/, const auto& key, const auto& value) { + if (!key.empty()) { + this->set_item(key, value, nullopt, false, path); + } + }); } void @@ -675,17 +673,16 @@ Config::set_value_in_file(const std::string& path, AtomicFile output(resolved_path, AtomicFile::Mode::text); bool found = false; - if (!parse_config_file(path, - [&](const std::string& c_line, - const std::string& c_key, - const std::string& /*c_value*/) { - if (c_key == key) { - output.write(FMT("{} = {}\n", key, value)); - found = true; - } else { - output.write(FMT("{}\n", c_line)); - } - })) { + if (!parse_config_file( + path, + [&](const auto& c_line, const auto& c_key, const auto& /*c_value*/) { + if (c_key == key) { + output.write(FMT("{} = {}\n", key, value)); + found = true; + } else { + output.write(FMT("{}\n", c_line)); + } + })) { throw Error("failed to open {}: {}", path, strerror(errno)); } diff --git a/src/InodeCache.cpp b/src/InodeCache.cpp index 5e473ec7e..bcb11ce64 100644 --- a/src/InodeCache.cpp +++ b/src/InodeCache.cpp @@ -369,7 +369,7 @@ InodeCache::get(const std::string& path, } bool found = false; - const bool success = with_bucket(key_digest, [&](Bucket* const bucket) { + const bool success = with_bucket(key_digest, [&](const auto bucket) { for (uint32_t i = 0; i < k_num_entries; ++i) { if (bucket->entries[i].key_digest == key_digest) { if (i > 0) { @@ -422,7 +422,7 @@ InodeCache::put(const std::string& path, return false; } - const bool success = with_bucket(key_digest, [&](Bucket* const bucket) { + const bool success = with_bucket(key_digest, [&](const auto bucket) { memmove(&bucket->entries[1], &bucket->entries[0], sizeof(Entry) * (k_num_entries - 1)); diff --git a/src/InodeCache.hpp b/src/InodeCache.hpp index f2d049abd..9ef330afa 100644 --- a/src/InodeCache.hpp +++ b/src/InodeCache.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2020 Joel Rosdahl and other contributors +// Copyright (C) 2020-2021 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // diff --git a/src/Statistics.cpp b/src/Statistics.cpp index ba98a76c1..a630857a1 100644 --- a/src/Statistics.cpp +++ b/src/Statistics.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2020 Joel Rosdahl and other contributors +// Copyright (C) 2020-2021 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -102,14 +102,13 @@ Statistics::collect_counters(const Config& config) time_t last_updated = 0; // Add up the stats in each directory. - for_each_level_1_and_2_stats_file( - config.cache_dir(), [&](const std::string& path) { - counters.set(Statistic::stats_zeroed_timestamp, 0); // Don't add - counters.increment(Statistics::read(path)); - zero_timestamp = std::max(counters.get(Statistic::stats_zeroed_timestamp), - zero_timestamp); - last_updated = std::max(last_updated, Stat::stat(path).mtime()); - }); + for_each_level_1_and_2_stats_file(config.cache_dir(), [&](const auto& path) { + counters.set(Statistic::stats_zeroed_timestamp, 0); // Don't add + counters.increment(Statistics::read(path)); + zero_timestamp = + std::max(counters.get(Statistic::stats_zeroed_timestamp), zero_timestamp); + last_updated = std::max(last_updated, Stat::stat(path).mtime()); + }); counters.set(Statistic::stats_zeroed_timestamp, zero_timestamp); return std::make_pair(counters, last_updated); diff --git a/src/Util.cpp b/src/Util.cpp index 4a996e611..ed6d78911 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -907,7 +907,7 @@ make_relative_path(const std::string& base_dir, const auto path_suffix = std::string(original_path.substr(path.length())); const auto real_path = Util::real_path(std::string(path)); - const auto add_relpath_candidates = [&](nonstd::string_view path) { + const auto add_relpath_candidates = [&](auto path) { const std::string normalized_path = Util::normalize_absolute_path(path); relpath_candidates.push_back( Util::get_relative_path(actual_cwd, normalized_path)); @@ -924,7 +924,7 @@ make_relative_path(const std::string& base_dir, // Find best (i.e. shortest existing) match: std::sort(relpath_candidates.begin(), relpath_candidates.end(), - [](const std::string& path1, const std::string& path2) { + [](const auto& path1, const auto& path2) { return path1.length() < path2.length(); }); for (const auto& relpath : relpath_candidates) { diff --git a/src/ccache.cpp b/src/ccache.cpp index 5b1db3b43..bd8f2cb42 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -2169,10 +2169,9 @@ update_stats_and_maybe_move_cache_file(const Context& ctx, const auto stats_file = FMT("{}/{}/stats", ctx.config.cache_dir(), level_string); - auto counters = - Statistics::update(stats_file, [&counter_updates](Counters& cs) { - cs.increment(counter_updates); - }); + auto counters = Statistics::update(stats_file, [&counter_updates](auto& cs) { + cs.increment(counter_updates); + }); if (!counters) { return nullopt; } @@ -2238,8 +2237,8 @@ finalize_stats_and_trigger_cleanup(Context& ctx) const auto bucket = getpid() % 256; const auto stats_file = FMT("{}/{:x}/{:x}/stats", config.cache_dir(), bucket / 16, bucket % 16); - Statistics::update( - stats_file, [&ctx](Counters& cs) { cs.increment(ctx.counter_updates); }); + Statistics::update(stats_file, + [&ctx](auto& cs) { cs.increment(ctx.counter_updates); }); return; } diff --git a/src/cleanup.cpp b/src/cleanup.cpp index 5c76ebbeb..20e6060b5 100644 --- a/src/cleanup.cpp +++ b/src/cleanup.cpp @@ -1,5 +1,5 @@ // Copyright (C) 2002-2006 Andrew Tridgell -// Copyright (C) 2009-2020 Joel Rosdahl and other contributors +// Copyright (C) 2009-2021 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -58,7 +58,7 @@ update_counters(const std::string& dir, bool cleanup_performed) { const std::string stats_file = dir + "/stats"; - Statistics::update(stats_file, [=](Counters& cs) { + Statistics::update(stats_file, [=](auto& cs) { if (cleanup_performed) { cs.increment(Statistic::cleanups_performed); } @@ -74,8 +74,7 @@ clean_old(const Context& ctx, { Util::for_each_level_1_subdir( ctx.config.cache_dir(), - [&](const std::string& subdir, - const Util::ProgressReceiver& sub_progress_receiver) { + [&](const auto& subdir, const auto& sub_progress_receiver) { clean_up_dir(subdir, 0, 0, max_age, sub_progress_receiver); }, progress_receiver); @@ -119,10 +118,9 @@ clean_up_dir(const std::string& subdir, } // Sort according to modification time, oldest first. - std::sort( - files.begin(), files.end(), [](const CacheFile& f1, const CacheFile& f2) { - return f1.lstat().mtime() < f2.lstat().mtime(); - }); + std::sort(files.begin(), files.end(), [](const auto& f1, const auto& f2) { + return f1.lstat().mtime() < f2.lstat().mtime(); + }); LOG("Before cleanup: {:.0f} KiB, {:.0f} files", static_cast(cache_size) / 1024, @@ -186,8 +184,7 @@ clean_up_all(const Config& config, { Util::for_each_level_1_subdir( config.cache_dir(), - [&](const std::string& subdir, - const Util::ProgressReceiver& sub_progress_receiver) { + [&](const auto& subdir, const auto& sub_progress_receiver) { clean_up_dir(subdir, config.max_size() / 16, config.max_files() / 16, diff --git a/src/compress.cpp b/src/compress.cpp index 1164b7950..3d18b8734 100644 --- a/src/compress.cpp +++ b/src/compress.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2019-2020 Joel Rosdahl and other contributors +// Copyright (C) 2019-2021 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -196,7 +196,7 @@ recompress_file(RecompressionStatistics& statistics, atomic_new_file.commit(); auto new_stat = Stat::stat(cache_file.path(), Stat::OnError::log); - Statistics::update(stats_file, [=](Counters& cs) { + Statistics::update(stats_file, [=](auto& cs) { cs.increment(Statistic::cache_size_kibibyte, Util::size_change_kibibyte(old_stat, new_stat)); }); @@ -219,8 +219,7 @@ compress_stats(const Config& config, Util::for_each_level_1_subdir( config.cache_dir(), - [&](const std::string& subdir, - const Util::ProgressReceiver& sub_progress_receiver) { + [&](const auto& subdir, const auto& sub_progress_receiver) { const std::vector files = Util::get_level_1_files( subdir, [&](double progress) { sub_progress_receiver(progress / 2); }); @@ -285,8 +284,7 @@ compress_recompress(Context& ctx, Util::for_each_level_1_subdir( ctx.config.cache_dir(), - [&](const std::string& subdir, - const Util::ProgressReceiver& sub_progress_receiver) { + [&](const auto& subdir, const auto& sub_progress_receiver) { std::vector files = Util::get_level_1_files(subdir, [&](double progress) { sub_progress_receiver(0.1 * progress); diff --git a/unittest/test_Config.cpp b/unittest/test_Config.cpp index b0523356a..d646fa4ac 100644 --- a/unittest/test_Config.cpp +++ b/unittest/test_Config.cpp @@ -414,11 +414,10 @@ TEST_CASE("Config::visit_items") std::vector received_items; - config.visit_items([&](const std::string& key, - const std::string& value, - const std::string& origin) { - received_items.push_back(FMT("({}) {} = {}", origin, key, value)); - }); + config.visit_items( + [&](const auto& key, const auto& value, const auto& origin) { + received_items.push_back(FMT("({}) {} = {}", origin, key, value)); + }); std::vector expected = { "(test.conf) absolute_paths_in_stderr = true", diff --git a/unittest/test_Statistics.cpp b/unittest/test_Statistics.cpp index 5d10966d7..07d752759 100644 --- a/unittest/test_Statistics.cpp +++ b/unittest/test_Statistics.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2011-2020 Joel Rosdahl and other contributors +// Copyright (C) 2011-2021 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -97,7 +97,7 @@ TEST_CASE("Update") Util::write_file("test", "0 1 2 3 27 5\n"); - auto counters = Statistics::update("test", [](Counters& cs) { + auto counters = Statistics::update("test", [](auto& cs) { cs.increment(Statistic::internal_error, 1); cs.increment(Statistic::cache_miss, 6); }); @@ -116,7 +116,7 @@ TEST_CASE("Get result") TestContext test_context; auto counters = Statistics::update( - "test", [](Counters& cs) { cs.increment(Statistic::cache_miss, 1); }); + "test", [](auto& cs) { cs.increment(Statistic::cache_miss, 1); }); REQUIRE(counters); auto result = Statistics::get_result_message(*counters); @@ -128,7 +128,7 @@ TEST_CASE("Log result") TestContext test_context; auto counters = Statistics::update( - "test", [](Counters& cs) { cs.increment(Statistic::cache_miss, 1); }); + "test", [](auto& cs) { cs.increment(Statistic::cache_miss, 1); }); REQUIRE(counters); auto result_id = Statistics::get_result_id(*counters); diff --git a/unittest/test_Util.cpp b/unittest/test_Util.cpp index cb2be415c..243748786 100644 --- a/unittest/test_Util.cpp +++ b/unittest/test_Util.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2019-2020 Joel Rosdahl and other contributors +// Copyright (C) 2019-2021 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -249,9 +249,7 @@ TEST_CASE("Util::for_each_level_1_subdir") std::vector actual; Util::for_each_level_1_subdir( "cache_dir", - [&](const std::string& subdir, const Util::ProgressReceiver&) { - actual.push_back(subdir); - }, + [&](const auto& subdir, const auto&) { actual.push_back(subdir); }, [](double) {}); std::vector expected = { @@ -397,10 +395,9 @@ TEST_CASE("Util::get_level_1_files") // Files within a level are in arbitrary order, sort them to be able to // verify them. - std::sort( - files.begin(), files.end(), [](const CacheFile& f1, const CacheFile& f2) { - return f1.path() < f2.path(); - }); + std::sort(files.begin(), files.end(), [](const auto& f1, const auto& f2) { + return f1.path() < f2.path(); + }); CHECK(files[0].path() == os_path("0/1/file_b")); CHECK(files[0].lstat().size() == 1); diff --git a/unittest/test_ccache.cpp b/unittest/test_ccache.cpp index cd59588ae..3236a4883 100644 --- a/unittest/test_ccache.cpp +++ b/unittest/test_ccache.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2020 Joel Rosdahl and other contributors +// Copyright (C) 2020-2021 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -42,11 +42,10 @@ helper(const char* args, const char* find_executable_return_string = nullptr) { const auto find_executable_stub = - [&find_executable_return_string]( - const Context&, const std::string& s, const std::string&) -> std::string { - return find_executable_return_string ? find_executable_return_string - : "resolved_" + s; - }; + [&find_executable_return_string](const auto&, const auto& s, const auto&) { + return find_executable_return_string ? find_executable_return_string + : "resolved_" + s; + }; Context ctx; ctx.config.set_compiler(config_compiler);