Skip to content

Commit

Permalink
clang-tidy: use boost ranges
Browse files Browse the repository at this point in the history
Since boost is used but not C++20, we can use the former.

Signed-off-by: Rosen Penev <[email protected]>
  • Loading branch information
neheb committed Dec 10, 2024
1 parent f21be7e commit e1dcdad
Show file tree
Hide file tree
Showing 38 changed files with 196 additions and 158 deletions.
8 changes: 5 additions & 3 deletions modules/bindbackend/bindbackend2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <boost/range/algorithm/set_algorithm.hpp>
#include <boost/range/algorithm/sort.hpp>
#include <cerrno>
#include <string>
#include <set>
Expand Down Expand Up @@ -924,7 +926,7 @@ void Bind2Backend::loadConfig(string* status) // NOLINT(readability-function-cog
}
}

sort(domains.begin(), domains.end()); // put stuff in inode order
boost::range::sort(domains); // put stuff in inode order
for (const auto& domain : domains) {
if (!(domain.hadFileDirective)) {
g_log << Logger::Warning << d_logprefix << " Zone '" << domain.name << "' has no 'file' directive set in " << getArg("config") << endl;
Expand Down Expand Up @@ -1021,7 +1023,7 @@ void Bind2Backend::loadConfig(string* status) // NOLINT(readability-function-cog
}
vector<DNSName> diff;

set_difference(oldnames.begin(), oldnames.end(), newnames.begin(), newnames.end(), back_inserter(diff));
boost::range::set_difference(oldnames, newnames, back_inserter(diff));
unsigned int remdomains = diff.size();

for (const DNSName& name : diff) {
Expand All @@ -1030,7 +1032,7 @@ void Bind2Backend::loadConfig(string* status) // NOLINT(readability-function-cog

// count number of entirely new domains
diff.clear();
set_difference(newnames.begin(), newnames.end(), oldnames.begin(), oldnames.end(), back_inserter(diff));
boost::range::set_difference(newnames, oldnames, back_inserter(diff));
newdomains = diff.size();

ostringstream msg;
Expand Down
3 changes: 2 additions & 1 deletion modules/geoipbackend/geoipbackend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <regex.h>
#include <glob.h>
#include <boost/algorithm/string/replace.hpp>
#include <boost/range/algorithm/sort.hpp>
#include <boost/format.hpp>
#include <fstream>
#include <filesystem>
Expand Down Expand Up @@ -356,7 +357,7 @@ void GeoIPBackend::loadDomainsFromDirectory(const std::string& dir, vector<GeoIP
paths.push_back(p);
}
}
std::sort(paths.begin(), paths.end());
boost::range::sort(paths);
for (const auto& p : paths) {
try {
GeoIPDomain dom;
Expand Down
8 changes: 5 additions & 3 deletions pdns/arguments.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "config.h"
#endif
#include "arguments.hh"
#include <boost/algorithm/cxx11/any_of.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/compare.hpp>
#include <boost/algorithm/string/predicate.hpp>
Expand All @@ -33,6 +34,7 @@
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <boost/range/algorithm/sort.hpp>
#include <climits>

ArgvMap::param_t::const_iterator ArgvMap::begin()
Expand Down Expand Up @@ -111,7 +113,7 @@ bool ArgvMap::contains(const string& var, const string& val)
vector<string> parts;

stringtok(parts, param->second, ", \t");
return std::any_of(parts.begin(), parts.end(), [&](const std::string& str) { return str == val; });
return boost::algorithm::any_of_equal(parts, val);
}

string ArgvMap::helpstring(string prefix)
Expand Down Expand Up @@ -440,7 +442,7 @@ void ArgvMap::parseOne(const string& arg, const string& parseOnly, bool lax)
// unknown setting encountered. see if its on the ignore list before throwing.
vector<string> parts;
stringtok(parts, d_params["ignore-unknown-settings"], " ,\t\n\r");
if (find(parts.begin(), parts.end(), var) != parts.end()) {
if (boost::algorithm::any_of_equal(parts, var)) {
d_unknownParams[var] = std::move(val);
SLOG(g_log << Logger::Warning << "Ignoring unknown setting '" << var << "' as requested" << endl,
d_log->info(Logr::Warning, "Ignoring unknown setting as requested", "name", Logging::Loggable(var)));
Expand Down Expand Up @@ -605,6 +607,6 @@ void ArgvMap::gatherIncludes(const std::string& directory, const std::string& su
throw ArgException(msg);
}

std::sort(vec.begin(), vec.end(), CIStringComparePOSIX());
boost::range::sort(vec, CIStringComparePOSIX());
extraConfigs.insert(extraConfigs.end(), vec.begin(), vec.end());
}
18 changes: 9 additions & 9 deletions pdns/auth-secondarycommunicator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
#include "dnssecinfra.hh"
#include "dnsseckeeper.hh"
#include "base32.hh"
#include <boost/range/algorithm/count.hpp>
#include <boost/range/algorithm/sort.hpp>
#include <boost/range/algorithm_ext.hpp>
#include <cerrno>
#include "communicator.hh"
#include <set>
Expand Down Expand Up @@ -98,8 +101,8 @@ static bool catalogDiff(const DomainInfo& di, vector<CatalogInfo>& fromXFR, vect
vector<CatalogInfo> retrieve;

try {
sort(fromXFR.begin(), fromXFR.end());
sort(fromDB.begin(), fromDB.end());
boost::range::sort(fromXFR);
boost::range::sort(fromDB);

auto xfr = fromXFR.cbegin();
auto db = fromDB.cbegin();
Expand Down Expand Up @@ -321,7 +324,7 @@ static bool catalogProcess(const DomainInfo& di, vector<DNSResourceRecord>& rrs,
vector<DNSResourceRecord> ret;

const auto compare = [](const DNSResourceRecord& a, const DNSResourceRecord& b) { return a.qname == b.qname ? a.qtype < b.qtype : a.qname.canonCompare(b.qname); };
sort(rrs.begin(), rrs.end(), compare);
boost::range::sort(rrs, compare);

DNSName rel;
DNSName unique;
Expand Down Expand Up @@ -490,12 +493,9 @@ void CommunicatorClass::ixfrSuck(const DNSName& domain, const TSIGTriplet& tt, c
}
}
// O(N^2)!
rrset.erase(remove_if(rrset.begin(), rrset.end(),
[&g](const DNSRecord& dr) {
return count(g.second.first.cbegin(),
g.second.first.cend(), dr);
}),
rrset.end());
boost::range::remove_erase_if(rrset, [&g](const auto& dnsr) {
return boost::algorithm::any_of_equal(g.second.first, dnsr);
});
// the DNSRecord== operator compares on name, type, class and lowercase content representation

for (const auto& x : g.second.second) {
Expand Down
5 changes: 4 additions & 1 deletion pdns/comfun.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include <boost/range/algorithm/sort.hpp>

#include "statbag.hh"
#include "zoneparser-tng.hh"
#include "namespaces.hh"
Expand Down Expand Up @@ -501,7 +504,7 @@ try
domains.push_back(nsq);
}

sort(domains.begin(), domains.end(), [](const NSQuery& a, const NSQuery& b) { return b.count < a.count; });
boost::range::sort(domains, [](const auto& first, const auto& second) { return second.count < first.count; });
for(;;) {
try {
inflighter.run();
Expand Down
16 changes: 11 additions & 5 deletions pdns/dbdnsseckeeper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
#include "dnssecinfra.hh"
#include "ueberbackend.hh"
#include "statbag.hh"
#include <boost/algorithm/cxx11/none_of.hpp>
#include <boost/range/algorithm/set_algorithm.hpp>
#include <boost/range/algorithm/sort.hpp>
#include <iostream>

#include <sys/stat.h>
Expand Down Expand Up @@ -574,7 +577,7 @@ DNSSECKeeper::keyset_t DNSSECKeeper::getKeys(const DNSName& zone, bool useCache)
algoNoSEP.insert(dkrc.d_algorithm);
}
}
set_intersection(algoSEP.begin(), algoSEP.end(), algoNoSEP.begin(), algoNoSEP.end(), std::back_inserter(algoHasSeparateKSK));
boost::range::set_intersection(algoSEP, algoNoSEP, std::back_inserter(algoHasSeparateKSK));
retkeyset.reserve(dbkeyset.size());

for(DNSBackend::KeyData& kd : dbkeyset)
Expand All @@ -591,16 +594,19 @@ DNSSECKeeper::keyset_t DNSSECKeeper::getKeys(const DNSName& zone, bool useCache)
kmd.hasSEPBit = (kd.flags == 257);
kmd.id = kd.id;

if (find(algoHasSeparateKSK.begin(), algoHasSeparateKSK.end(), dpk.getAlgorithm()) == algoHasSeparateKSK.end())
if (boost::algorithm::none_of_equal(algoHasSeparateKSK, dpk.getAlgorithm())) {
kmd.keyType = CSK;
else if(kmd.hasSEPBit)
}
else if (kmd.hasSEPBit) {
kmd.keyType = KSK;
else
}
else {
kmd.keyType = ZSK;
}

retkeyset.emplace_back(dpk, kmd);
}
sort(retkeyset.begin(), retkeyset.end(), keyCompareByKindAndID);
boost::range::sort(retkeyset, keyCompareByKindAndID);

if (ttl > 0) {
KeyCacheEntry kce;
Expand Down
3 changes: 2 additions & 1 deletion pdns/dnsbackend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <boost/range/algorithm/count.hpp>
#include <memory>
#ifdef HAVE_CONFIG_H
#include "config.h"
Expand Down Expand Up @@ -157,7 +158,7 @@ void BackendMakerClass::launch(const string& instr)
stringtok(parts, instr, ", ");

for (const auto& part : parts) {
if (count(parts.begin(), parts.end(), part) > 1) {
if (boost::range::count(parts, part) > 1) {
throw ArgException("Refusing to launch multiple backends with the same name '" + part + "', verify all 'launch' statements in your configuration");
}
}
Expand Down
3 changes: 2 additions & 1 deletion pdns/dnsbackend.hh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#pragma once

#include <algorithm>
#include <boost/algorithm/cxx11/any_of.hpp>
#include <cstddef>
class DNSPacket;

Expand Down Expand Up @@ -120,7 +121,7 @@ struct DomainInfo

[[nodiscard]] bool isPrimary(const ComboAddress& ipAddress) const
{
return std::any_of(primaries.begin(), primaries.end(), [ipAddress](auto primary) { return ComboAddress::addressOnlyEqual()(ipAddress, primary); });
return boost::algorithm::any_of(primaries, [ipAddress](auto primary) { return ComboAddress::addressOnlyEqual()(ipAddress, primary); });
}
};

Expand Down
4 changes: 2 additions & 2 deletions pdns/dnsbulktest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#pragma GCC diagnostic ignored "-Wdeprecated-copy-with-user-provided-copy"
#endif
#include <boost/accumulators/accumulators.hpp>
#include <boost/algorithm/cxx11/none_of.hpp>
#include <boost/array.hpp>
#include <boost/accumulators/statistics.hpp>
#pragma GCC diagnostic pop
Expand Down Expand Up @@ -317,8 +318,7 @@ try
pos=split.second.find('/');
if(pos != string::npos) // alexa has whole urls in the list now.
split.second.resize(pos);
if(find_if(split.second.begin(), split.second.end(), isalpha) == split.second.end())
{
if (boost::algorithm::none_of(split.second, isalpha)) {
continue; // this was an IP address
}
domains.push_back(TypedQuery(split.second, qtype));
Expand Down
9 changes: 4 additions & 5 deletions pdns/dnsdistdist/dnsdist-lua-rules.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "dnsdist-rule-chains.hh"
#include "dns_random.hh"

#include <boost/range/algorithm/remove_if.hpp>

std::shared_ptr<DNSRule> makeRule(const luadnsrule_t& var, const std::string& calledFrom)
{
if (var.type() == typeid(std::shared_ptr<DNSRule>)) {
Expand Down Expand Up @@ -141,15 +143,12 @@ static void showRules(IdentifierT identifier, boost::optional<ruleparams_t>& var
template <typename ChainTypeT, typename RuleTypeT>
static bool removeRuleFromChain(ChainTypeT& rules, const std::function<bool(const RuleTypeT& rule)>& matchFunction)
{
auto removeIt = std::remove_if(rules.begin(),
rules.end(),
matchFunction);
auto removeIt = boost::range::remove_if(rules, matchFunction);
if (removeIt == rules.end()) {
g_outputBuffer = "Error: no rule matched\n";
return false;
}
rules.erase(removeIt,
rules.end());
rules.erase(removeIt, rules.end());
return true;
}

Expand Down
4 changes: 3 additions & 1 deletion pdns/dnsdistdist/dnsdist-lua.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include <thread>
#include <vector>

#include <boost/range/algorithm_ext.hpp>

#include "dnsdist.hh"
#include "dnsdist-backend.hh"
#include "dnsdist-cache.hh"
Expand Down Expand Up @@ -760,7 +762,7 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck)
}

dnsdist::configuration::updateRuntimeConfiguration([&server](dnsdist::configuration::RuntimeConfiguration& config) {
config.d_backends.erase(std::remove(config.d_backends.begin(), config.d_backends.end(), server), config.d_backends.end());
boost::range::remove_erase(config.d_backends, server);
});

server->stop();
Expand Down
4 changes: 3 additions & 1 deletion pdns/dnsdistdist/dnsdist-protocols.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <algorithm>
#include <stdexcept>

#include <boost/range/algorithm/find.hpp>

#include "dnsdist-protocols.hh"

namespace dnsdist
Expand All @@ -49,7 +51,7 @@ const std::array<std::string, Protocol::s_numberOfProtocols> Protocol::s_prettyN

Protocol::Protocol(const std::string& protocol)
{
const auto& namesIt = std::find(s_names.begin(), s_names.end(), protocol);
auto namesIt = boost::ranges::find(s_names, protocol);
if (namesIt == s_names.end()) {
throw std::runtime_error("Unknown protocol name: '" + protocol + "'");
}
Expand Down
6 changes: 4 additions & 2 deletions pdns/dnsdistdist/doh3.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#include "doh3.hh"

#include <boost/algorithm/cxx11/any_of.hpp>

#ifdef HAVE_DNS_OVER_HTTP3
#include <quiche.h>

Expand Down Expand Up @@ -991,11 +993,11 @@ void doh3Thread(ClientState* clientState)
mplexer->getAvailableFDs(readyFDs, 500);

try {
if (std::find(readyFDs.begin(), readyFDs.end(), sock.getHandle()) != readyFDs.end()) {
if (boost::algorithm::any_of_equal(readyFDs, sock.getHandle())) {
handleSocketReadable(*frontend, *clientState, sock, buffer);
}

if (std::find(readyFDs.begin(), readyFDs.end(), responseReceiverFD) != readyFDs.end()) {
if (boost::algorithm::any_of_equal(readyFDs, responseReceiverFD)) {
flushResponses(frontend->d_server_config->d_responseReceiver);
}

Expand Down
6 changes: 4 additions & 2 deletions pdns/dnsdistdist/doq.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#include "doq.hh"

#include <boost/algorithm/cxx11/any_of.hpp>

#ifdef HAVE_DNS_OVER_QUIC
#include <quiche.h>

Expand Down Expand Up @@ -794,11 +796,11 @@ void doqThread(ClientState* clientState)
mplexer->getAvailableFDs(readyFDs, 500);

try {
if (std::find(readyFDs.begin(), readyFDs.end(), sock.getHandle()) != readyFDs.end()) {
if (boost::algorithm::any_of_equal(readyFDs, sock.getHandle())) {
handleSocketReadable(*frontend, *clientState, sock, buffer);
}

if (std::find(readyFDs.begin(), readyFDs.end(), responseReceiverFD) != readyFDs.end()) {
if (boost::algorithm::any_of_equal(readyFDs, responseReceiverFD)) {
flushResponses(frontend->d_server_config->d_responseReceiver);
}

Expand Down
4 changes: 3 additions & 1 deletion pdns/dnsname.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
#include "dnsname.hh"
#include <boost/format.hpp>
#include <boost/range/algorithm/lexicographical_compare.hpp>
#include <boost/range/adaptor/reversed.hpp>
#include <string>
#include <cinttypes>

Expand Down Expand Up @@ -464,7 +466,7 @@ void DNSName::prependRawLabel(const std::string& label)
bool DNSName::slowCanonCompare(const DNSName& rhs) const
{
auto ours=getRawLabels(), rhsLabels = rhs.getRawLabels();
return std::lexicographical_compare(ours.rbegin(), ours.rend(), rhsLabels.rbegin(), rhsLabels.rend(), CIStringCompare());
return boost::range::lexicographical_compare(boost::adaptors::reverse(ours), boost::adaptors::reverse(rhsLabels), CIStringCompare());
}

vector<std::string> DNSName::getRawLabels() const
Expand Down
Loading

0 comments on commit e1dcdad

Please sign in to comment.