From 69268eecfb68ae3b8f825dda324d0547eafe201d Mon Sep 17 00:00:00 2001 From: Dennis Klein Date: Fri, 5 Jul 2019 19:10:16 +0200 Subject: [PATCH] Tools: Fix various clang-tidy warnings --- fairmq/tools/Network.cxx | 52 +++++++++++++++++++--------------------- fairmq/tools/Process.cxx | 19 +++++++-------- fairmq/tools/Unique.cxx | 2 +- 3 files changed, 35 insertions(+), 38 deletions(-) diff --git a/fairmq/tools/Network.cxx b/fairmq/tools/Network.cxx index f251743d..db82733a 100644 --- a/fairmq/tools/Network.cxx +++ b/fairmq/tools/Network.cxx @@ -12,24 +12,21 @@ #define _GNU_SOURCE // To get defns of NI_MAXSERV and NI_MAXHOST #endif +#include +#include +#include // trim +#include +#include +#include #include - +#include +#include +#include +#include +#include +#include #include #include -#include -#include -#include - -#include // trim -#include - -#include -#include -#include -#include -#include -#include -#include using namespace std; @@ -44,9 +41,10 @@ namespace tools map getHostIPs() { map addressMap; - struct ifaddrs *ifaddr, *ifa; + ifaddrs* ifaddr; + ifaddrs* ifa; int s; - char host[NI_MAXHOST]; + array host{}; if (getifaddrs(&ifaddr) == -1) { perror("getifaddrs"); @@ -59,13 +57,13 @@ map getHostIPs() } if (ifa->ifa_addr->sa_family == AF_INET) { - s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, nullptr, 0, NI_NUMERICHOST); + s = getnameinfo(ifa->ifa_addr, sizeof(sockaddr_in), host.data(), NI_MAXHOST, nullptr, 0, NI_NUMERICHOST); if (s != 0) { cout << "getnameinfo() failed: " << gai_strerror(s) << endl; throw runtime_error("getnameinfo() failed"); } - addressMap.insert(pair(ifa->ifa_name, host)); + addressMap.insert({ifa->ifa_name, host.data()}); } } @@ -79,12 +77,11 @@ string getInterfaceIP(const string& interface) { try { auto IPs = getHostIPs(); - if (IPs.count(interface)) { + if (IPs.count(interface) > 0) { return IPs[interface]; - } else { - LOG(error) << "Could not find provided network interface: \"" << interface << "\"!, exiting."; - return ""; } + LOG(error) << "Could not find provided network interface: \"" << interface << "\"!, exiting."; + return ""; } catch (runtime_error& re) { cout << "could not get interface IP: " << re.what(); return ""; @@ -94,7 +91,8 @@ string getInterfaceIP(const string& interface) // get name of the default route interface string getDefaultRouteNetworkInterface() { - array buffer; + const int BUFSIZE(128); + array buffer{}; string interfaceName; #ifdef __APPLE__ // MacOS @@ -108,15 +106,15 @@ string getDefaultRouteNetworkInterface() return ""; } - while (!feof(file.get())) { - if (fgets(buffer.data(), 128, file.get()) != nullptr) { + while (feof(file.get()) == 0) { + if (fgets(buffer.data(), BUFSIZE, file.get()) != nullptr) { interfaceName += buffer.data(); } } boost::algorithm::trim(interfaceName); - if (interfaceName == "") { + if (interfaceName.empty()) { LOG(error) << "Could not detect default route network interface name"; } else { LOG(debug) << "Detected network interface name for the default route: " << interfaceName; diff --git a/fairmq/tools/Process.cxx b/fairmq/tools/Process.cxx index c67820da..219c61a3 100644 --- a/fairmq/tools/Process.cxx +++ b/fairmq/tools/Process.cxx @@ -9,16 +9,15 @@ #include #include -#include #include #include - -#include // kill, signals - +#include +#include // kill, signals #include #include -#include #include +#include +#include using namespace std; namespace bp = boost::process; @@ -28,9 +27,9 @@ namespace bs = boost::system; class LinePrinter { public: - LinePrinter(stringstream& out, const string& prefix) + LinePrinter(stringstream& out, string prefix) : fOut(out) - , fPrefix(prefix) + , fPrefix(move(prefix)) {} // prints line with prefix on both cout (thread-safe) and output stream @@ -82,8 +81,8 @@ execute_result execute(const string& cmd, const string& prefix, const string& in bp::async_pipe errorPipe(ios); const string delimiter = "\n"; - ba::deadline_timer inputTimer(ios, boost::posix_time::milliseconds(100)); - ba::deadline_timer signalTimer(ios, boost::posix_time::milliseconds(100)); + ba::deadline_timer inputTimer(ios, boost::posix_time::milliseconds(100)); // NOLINT + ba::deadline_timer signalTimer(ios, boost::posix_time::milliseconds(100)); // NOLINT // child process bp::child c(cmd, bp::std_out > outputPipe, bp::std_err > errorPipe, bp::std_in < inputPipe); @@ -91,7 +90,7 @@ execute_result execute(const string& cmd, const string& prefix, const string& in p.Print(ToString("fair::mq::tools::execute: pid: ", pid)); // handle std_in with a delay - if (input != "") { + if (!input.empty()) { inputTimer.async_wait([&](const bs::error_code& ec1) { if (!ec1) { ba::async_write(inputPipe, inputBuffer, [&](const bs::error_code& ec2, size_t /* n */) { diff --git a/fairmq/tools/Unique.cxx b/fairmq/tools/Unique.cxx index 62b4b018..1660e6dc 100644 --- a/fairmq/tools/Unique.cxx +++ b/fairmq/tools/Unique.cxx @@ -12,10 +12,10 @@ // otherwise on some systems we'd get boost::uuids::entropy_error #define BOOST_UUID_RANDOM_PROVIDER_FORCE_POSIX +#include #include #include #include -#include using namespace std;