Skip to content

Commit

Permalink
Fix abseil-string-find-str-contains warning
Browse files Browse the repository at this point in the history
  • Loading branch information
yperbasis committed Jan 12, 2024
1 parent efa01e7 commit d3c23b5
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 97 deletions.
1 change: 0 additions & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ HeaderFilterRegex: 'silkworm/(api|core|infra|node|sentry|rpc|sync)/.+\.hpp$'
WarningsAsErrors: '*'
Checks: >
abseil-*,
-abseil-string-find-str-contains,
boost-*,
bugprone-*,
-bugprone-easily-swappable-parameters,
Expand Down
15 changes: 8 additions & 7 deletions cmd/dev/grpc_toolbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <absl/flags/flag.h>
#include <absl/flags/parse.h>
#include <absl/flags/usage.h>
#include <absl/strings/match.h>
#include <boost/asio/co_spawn.hpp>
#include <boost/asio/signal_set.hpp>
#include <grpcpp/grpcpp.h>
Expand Down Expand Up @@ -693,7 +694,7 @@ ABSL_FLAG(uint32_t, timeout, kDefaultTimeout.count(), "gRPC call timeout as inte

int ethbackend_async() {
auto target{absl::GetFlag(FLAGS_target)};
if (target.empty() || target.find(":") == std::string::npos) {
if (target.empty() || !absl::StrContains(target, ":")) {
std::cerr << "Parameter target is invalid: [" << target << "]\n";
std::cerr << "Use --target flag to specify the location of Erigon running instance\n";
return -1;
Expand All @@ -704,7 +705,7 @@ int ethbackend_async() {

int ethbackend_coroutines() {
auto target{absl::GetFlag(FLAGS_target)};
if (target.empty() || target.find(":") == std::string::npos) {
if (target.empty() || !absl::StrContains(target, ":")) {
std::cerr << "Parameter target is invalid: [" << target << "]\n";
std::cerr << "Use --target flag to specify the location of Erigon running instance\n";
return -1;
Expand All @@ -715,7 +716,7 @@ int ethbackend_coroutines() {

int ethbackend_sync() {
auto target{absl::GetFlag(FLAGS_target)};
if (target.empty() || target.find(":") == std::string::npos) {
if (target.empty() || !absl::StrContains(target, ":")) {
std::cerr << "Parameter target is invalid: [" << target << "]\n";
std::cerr << "Use --target flag to specify the location of Erigon running instance\n";
return -1;
Expand All @@ -726,7 +727,7 @@ int ethbackend_sync() {

int kv_seek_async_callback() {
auto target{absl::GetFlag(FLAGS_target)};
if (target.empty() || target.find(":") == std::string::npos) {
if (target.empty() || !absl::StrContains(target, ":")) {
std::cerr << "Parameter target is invalid: [" << target << "]\n";
std::cerr << "Use --target flag to specify the location of Erigon running instance\n";
return -1;
Expand Down Expand Up @@ -754,7 +755,7 @@ int kv_seek_async_callback() {

int kv_seek_async() {
auto target{absl::GetFlag(FLAGS_target)};
if (target.empty() || target.find(":") == std::string::npos) {
if (target.empty() || !absl::StrContains(target, ":")) {
std::cerr << "Parameter target is invalid: [" << target << "]\n";
std::cerr << "Use --target flag to specify the location of Erigon running instance\n";
return -1;
Expand Down Expand Up @@ -782,7 +783,7 @@ int kv_seek_async() {

int kv_seek_both() {
auto target{absl::GetFlag(FLAGS_target)};
if (target.empty() || target.find(":") == std::string::npos) {
if (target.empty() || !absl::StrContains(target, ":")) {
std::cerr << "Parameter target is invalid: [" << target << "]\n";
std::cerr << "Use --target flag to specify the location of Erigon running instance\n";
return -1;
Expand Down Expand Up @@ -816,7 +817,7 @@ int kv_seek_both() {

int kv_seek() {
auto target{absl::GetFlag(FLAGS_target)};
if (target.empty() || target.find(":") == std::string::npos) {
if (target.empty() || !absl::StrContains(target, ":")) {
std::cerr << "Parameter target is invalid: [" << target << "]\n";
std::cerr << "Use --target flag to specify the location of Erigon running instance\n";
return -1;
Expand Down
13 changes: 7 additions & 6 deletions examples/get_latest_block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,20 @@
#include <absl/flags/flag.h>
#include <absl/flags/parse.h>
#include <absl/flags/usage.h>
#include <absl/strings/match.h>
#include <boost/asio/co_spawn.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/use_awaitable.hpp>
#include <boost/asio/use_future.hpp>
#include <grpcpp/grpcpp.h>
#include <silkworm/core/common/util.hpp>

#include <silkworm/core/common/util.hpp>
#include <silkworm/infra/common/log.hpp>
#include <silkworm/infra/grpc/client/client_context_pool.hpp>
#include <silkworm/rpc/common/constants.hpp>
#include <silkworm/rpc/core/blocks.hpp>
#include <silkworm/rpc/ethdb/transaction_database.hpp>
#include <silkworm/rpc/ethdb/kv/remote_database.hpp>
#include <silkworm/rpc/ethdb/transaction_database.hpp>

using namespace silkworm;
using namespace silkworm::rpc;
Expand Down Expand Up @@ -79,7 +80,7 @@ int main(int argc, char* argv[]) {

try {
auto target{absl::GetFlag(FLAGS_target)};
if (target.empty() || target.find(":") == std::string::npos) {
if (target.empty() || !absl::StrContains(target, ":")) {
std::cerr << "Parameter target is invalid: [" << target << "]\n";
std::cerr << "Use --target flag to specify the location of Silkworm/Erigon running instance\n";
return -1;
Expand All @@ -101,16 +102,16 @@ int main(int argc, char* argv[]) {

const auto latest_block_number = get_latest_block(*io_context, *database);
if (latest_block_number) {
std::cout << "latest_block_number: " << latest_block_number.value() << "\n" << std::flush;
std::cout << "latest_block_number: " << latest_block_number.value() << std::endl;
}

if (context_pool_thread.joinable()) {
context_pool_thread.join();
}
} catch (const std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n" << std::flush;
std::cerr << "Exception: " << e.what() << std::endl;
} catch (...) {
std::cerr << "Unexpected exception\n" << std::flush;
std::cerr << "Unexpected exception" << std::endl;
}

return 0;
Expand Down
51 changes: 26 additions & 25 deletions silkworm/infra/common/log_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <string>
#include <thread>

#include <absl/strings/match.h>
#include <catch2/catch.hpp>

#include <silkworm/infra/common/directories.hpp>
Expand Down Expand Up @@ -50,7 +51,7 @@ template <Level level>
void check_log_not_empty() {
auto log_buffer = LogBuffer_ForTest<level>();
log_buffer << "test";
CHECK(log_buffer.content().find("test") != std::string::npos);
CHECK(absl::StrContains(log_buffer.content(), "test"));
}

//! Build the plain key-value pair
Expand Down Expand Up @@ -113,33 +114,33 @@ TEST_CASE("LogBuffer", "[silkworm][common][log]") {
thread_id_stream << std::this_thread::get_id();
auto log_buffer1 = LogBuffer_ForTest<Level::kInfo>();
log_buffer1 << "test";
CHECK(log_buffer1.content().find(thread_id_stream.str()) == std::string::npos);
CHECK(!absl::StrContains(log_buffer1.content(), thread_id_stream.str()));

// Enable thread tracing
Settings log_settings;
log_settings.log_threads = true;
init(log_settings);
auto log_buffer2 = LogBuffer_ForTest<Level::kInfo>();
log_buffer2 << "test";
CHECK(log_buffer2.content().find(thread_id_stream.str()) != std::string::npos);
CHECK(absl::StrContains(log_buffer2.content(), thread_id_stream.str()));

// Disable thread tracing
log_settings.log_threads = false;
init(log_settings);
auto log_buffer3 = LogBuffer_ForTest<Level::kInfo>();
log_buffer3 << "test";
CHECK(log_buffer3.content().find(thread_id_stream.str()) == std::string::npos);
CHECK(!absl::StrContains(log_buffer3.content(), thread_id_stream.str()));
}

SECTION("Settings disable colorized output if log file present") {
// Default output is colorized
LogBuffer_ForTest<Level::kInfo>{"test0", {"key1", "value1", "key2", "value2"}}; // temporary log object, flush on dtor
const auto cerr_output0{string_cerr.str()};
CHECK(cerr_output0.find("test0") != std::string::npos);
CHECK(cerr_output0.find(key_value("key1", "value1")) == std::string::npos);
CHECK(cerr_output0.find(key_value("key2", "value2")) == std::string::npos);
CHECK(cerr_output0.find(prettified_key_value("key1", "value1")) != std::string::npos);
CHECK(cerr_output0.find(prettified_key_value("key2", "value2")) != std::string::npos);
CHECK(absl::StrContains(cerr_output0, "test0"));
CHECK(!absl::StrContains(cerr_output0, key_value("key1", "value1")));
CHECK(!absl::StrContains(cerr_output0, key_value("key2", "value2")));
CHECK(absl::StrContains(cerr_output0, prettified_key_value("key1", "value1")));
CHECK(absl::StrContains(cerr_output0, prettified_key_value("key2", "value2")));

// Reset cerr replacement stream
string_cerr.str("");
Expand All @@ -153,11 +154,11 @@ TEST_CASE("LogBuffer", "[silkworm][common][log]") {
init(log_settings1);
LogBuffer_ForTest<Level::kInfo>{"test1", {"key1", "value1", "key2", "value2"}}; // temporary log object, flush on dtor
const auto cerr_output1{string_cerr.str()};
CHECK(cerr_output1.find("test1") != std::string::npos);
CHECK(cerr_output1.find(key_value("key1", "value1")) != std::string::npos);
CHECK(cerr_output1.find(key_value("key2", "value2")) != std::string::npos);
CHECK(cerr_output1.find(prettified_key_value("key1", "value1")) == std::string::npos);
CHECK(cerr_output1.find(prettified_key_value("key2", "value2")) == std::string::npos);
CHECK(absl::StrContains(cerr_output1, "test1"));
CHECK(absl::StrContains(cerr_output1, key_value("key1", "value1")));
CHECK(absl::StrContains(cerr_output1, key_value("key2", "value2")));
CHECK(!absl::StrContains(cerr_output1, prettified_key_value("key1", "value1")));
CHECK(!absl::StrContains(cerr_output1, prettified_key_value("key2", "value2")));

// Reset cerr replacement stream
string_cerr.str("");
Expand All @@ -171,26 +172,26 @@ TEST_CASE("LogBuffer", "[silkworm][common][log]") {
init(log_settings2);
LogBuffer_ForTest<Level::kInfo>{"test2", {"key3", "value3", "key4", "value4"}}; // temporary log object, flush on dtor
const auto cerr_output2{string_cerr.str()};
CHECK(cerr_output2.find("test2") != std::string::npos);
CHECK(cerr_output2.find(key_value("key3", "value3")) != std::string::npos);
CHECK(cerr_output2.find(key_value("key4", "value4")) != std::string::npos);
CHECK(cerr_output2.find(prettified_key_value("key3", "value3")) == std::string::npos);
CHECK(cerr_output2.find(prettified_key_value("key4", "value4")) == std::string::npos);
CHECK(absl::StrContains(cerr_output2, "test2"));
CHECK(absl::StrContains(cerr_output2, key_value("key3", "value3")));
CHECK(absl::StrContains(cerr_output2, key_value("key4", "value4")));
CHECK(!absl::StrContains(cerr_output2, prettified_key_value("key3", "value3")));
CHECK(!absl::StrContains(cerr_output2, prettified_key_value("key4", "value4")));
}

SECTION("Variable arguments: constructor") {
auto log_buffer = LogBuffer_ForTest<Level::kInfo>("test", {"key1", "value1", "key2", "value2"});
CHECK(log_buffer.content().find("test") != std::string::npos);
CHECK(log_buffer.content().find(prettified_key_value("key1", "value1")) != std::string::npos);
CHECK(log_buffer.content().find(prettified_key_value("key2", "value2")) != std::string::npos);
CHECK(absl::StrContains(log_buffer.content(), "test"));
CHECK(absl::StrContains(log_buffer.content(), prettified_key_value("key1", "value1")));
CHECK(absl::StrContains(log_buffer.content(), prettified_key_value("key2", "value2")));
}

SECTION("Variable arguments: accumulators") {
auto log_buffer = LogBuffer_ForTest<Level::kInfo>();
log_buffer << "test" << Args{"key1", "value1", "key2", "value2"};
CHECK(log_buffer.content().find("test") != std::string::npos);
CHECK(log_buffer.content().find(prettified_key_value("key1", "value1")) != std::string::npos);
CHECK(log_buffer.content().find(prettified_key_value("key2", "value2")) != std::string::npos);
CHECK(absl::StrContains(log_buffer.content(), "test"));
CHECK(absl::StrContains(log_buffer.content(), prettified_key_value("key1", "value1")));
CHECK(absl::StrContains(log_buffer.content(), prettified_key_value("key2", "value2")));
}
}

Expand Down
Loading

0 comments on commit d3c23b5

Please sign in to comment.