From dfce2fcd545437b24d2356283c3b82ae54cd3e1f Mon Sep 17 00:00:00 2001 From: Hendrik Muhs Date: Tue, 11 Feb 2025 22:37:00 +0100 Subject: [PATCH] apply clang tidy fixes --- keyvi/bin/keyvi_c/c_api.cpp | 17 ++- keyvi/bin/keyvicompiler/keyvicompiler.cpp | 115 ++++++++++++-------- keyvi/bin/keyviinspector/keyviinspector.cpp | 32 +++--- keyvi/bin/keyvimerger/keyvimerger.cpp | 35 ++++-- 4 files changed, 123 insertions(+), 76 deletions(-) diff --git a/keyvi/bin/keyvi_c/c_api.cpp b/keyvi/bin/keyvi_c/c_api.cpp index ec1355b86..87fb0ee8e 100644 --- a/keyvi/bin/keyvi_c/c_api.cpp +++ b/keyvi/bin/keyvi_c/c_api.cpp @@ -24,12 +24,19 @@ #include "keyvi/c_api/c_api.h" +#include +#include #include +#include #include +#include +#include #include "keyvi/dictionary/completion/multiword_completion.h" #include "keyvi/dictionary/completion/prefix_completion.h" #include "keyvi/dictionary/dictionary.h" +#include "keyvi/dictionary/match.h" +#include "keyvi/dictionary/match_iterator.h" using keyvi::dictionary::Dictionary; using keyvi::dictionary::dictionary_t; @@ -41,7 +48,7 @@ using keyvi::dictionary::completion::PrefixCompletion; namespace { char* std_2_c_string(const std::string& str) { const size_t c_str_length = str.size() + 1; - auto result = static_cast(malloc(c_str_length)); + auto* result = static_cast(malloc(c_str_length)); strncpy(result, str.c_str(), c_str_length); return result; } @@ -54,7 +61,7 @@ struct keyvi_dictionary { }; struct keyvi_match { - explicit keyvi_match(const match_t& obj) : obj_(obj) {} + explicit keyvi_match(match_t obj) : obj_(std::move(obj)) {} match_t obj_; }; @@ -91,7 +98,7 @@ keyvi_dictionary* keyvi_create_dictionary(const char* filename) { try { return new keyvi_dictionary(Dictionary(filename)); } catch (const std::exception& e) { - std::cerr << e.what() << std::endl; + std::cerr << e.what() << '\n'; return nullptr; } } @@ -129,7 +136,7 @@ keyvi_match_iterator* keyvi_dictionary_get_fuzzy(const keyvi_dictionary* dict, c keyvi_match_iterator* keyvi_dictionary_get_multi_word_completions(const keyvi_dictionary* dict, const char* key, const size_t key_len, const size_t cutoff) { - MultiWordCompletion multiWordCompletion(dict->obj_); + MultiWordCompletion const multiWordCompletion(dict->obj_); return new keyvi_match_iterator(multiWordCompletion.GetCompletions(std::string(key, key_len), cutoff)); } @@ -166,7 +173,7 @@ keyvi_bytes keyvi_match_get_msgpacked_value(const struct keyvi_match* match) { if (0 == data_size) { return empty_keyvi_bytes; } - auto data_ptr = malloc(data_size); + auto* data_ptr = malloc(data_size); if (nullptr == data_ptr) { return empty_keyvi_bytes; } diff --git a/keyvi/bin/keyvicompiler/keyvicompiler.cpp b/keyvi/bin/keyvicompiler/keyvicompiler.cpp index 595e37f8c..b2eabecdb 100644 --- a/keyvi/bin/keyvicompiler/keyvicompiler.cpp +++ b/keyvi/bin/keyvicompiler/keyvicompiler.cpp @@ -23,25 +23,42 @@ * Author: hendrik */ +#include +#include +#include +#include #include - -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include #include #include #include #include -#include -#include -#include +#include +#include //NOLINT +#include +#include +#include +#include +#include //NOLINT +#include -#include "keyvi/dictionary/dictionary_compiler.h" #include "keyvi/dictionary/dictionary_types.h" +#include "keyvi/dictionary/fsa/internal/constants.h" #include "keyvi/util/configuration.h" -void callback(size_t added, size_t overall, void*) { - std::cout << "Processed " << added << "/" << overall << "(" << ((100 * added) / overall) << "%)." << std::endl; +void callback(size_t added, size_t overall, void* /*unused*/) { + std::cout << "Processed " << added << "/" << overall << "(" << ((100 * added) / overall) << "%)." << '\n'; } template @@ -50,7 +67,7 @@ void compile_multiple(CompilerType* compiler, std::function& input, const std::string const keyvi::util::parameters_t& value_store_params = keyvi::util::parameters_t()) { keyvi::dictionary::CompletionDictionaryCompiler compiler(value_store_params); - std::function(std::string)> parser = [](std::string line) { - size_t tab = line.find('\t'); + std::function(std::string)> const parser = [](const std::string& line) { + size_t const tab = line.find('\t'); - if (tab == std::string::npos) return std::pair(); + if (tab == std::string::npos) { + return std::pair(); + } - std::string key = line.substr(0, tab); - std::string value_as_string = line.substr(tab + 1); - uint32_t value; + std::string const key = line.substr(0, tab); + std::string const value_as_string = line.substr(tab + 1); + uint32_t value = 0; try { value = boost::lexical_cast(value_as_string); } catch (boost::bad_lexical_cast const&) { - std::cout << "Error: value was not valid: " << line << std::endl; + std::cout << "Error: value was not valid: " << line << '\n'; return std::pair(); } return std::pair(key, value); @@ -133,19 +152,21 @@ void compile_integer(const std::vector& input, const std::string& o const keyvi::util::parameters_t& value_store_params = keyvi::util::parameters_t()) { keyvi::dictionary::IntDictionaryCompiler compiler(value_store_params); - std::function(std::string)> parser = [](std::string line) { - size_t tab = line.find('\t'); + std::function(std::string)> const parser = [](const std::string& line) { + size_t const tab = line.find('\t'); - if (tab == std::string::npos) return std::pair(); + if (tab == std::string::npos) { + return std::pair(); + } - std::string key = line.substr(0, tab); - std::string value_as_string = line.substr(tab + 1); - uint32_t value; + std::string const key = line.substr(0, tab); + std::string const value_as_string = line.substr(tab + 1); + uint32_t value = 0; try { value = boost::lexical_cast(value_as_string); } catch (boost::bad_lexical_cast const&) { - std::cout << "Error: value was not valid: " << line << std::endl; + std::cout << "Error: value was not valid: " << line << '\n'; return std::pair(); } return std::pair(key, value); @@ -158,11 +179,13 @@ void compile_integer(const std::vector& input, const std::string& o template void compile_strings_inner(Compiler* compiler, const std::vector& input, const std::string& output, const std::string& manifest = {}) { - std::function(std::string)> parser = [](std::string line) { - size_t tab = line.find('\t'); - if (tab == std::string::npos) return std::pair(); - std::string key = line.substr(0, tab); - std::string value = line.substr(tab + 1); + std::function(std::string)> const parser = [](const std::string& line) { + size_t const tab = line.find('\t'); + if (tab == std::string::npos) { + return std::pair(); + } + std::string const key = line.substr(0, tab); + std::string const value = line.substr(tab + 1); return std::pair(key, value); }; @@ -183,9 +206,9 @@ void compile_key_only(const std::vector& input, const std::string& const keyvi::util::parameters_t& value_store_params = keyvi::util::parameters_t()) { keyvi::dictionary::KeyOnlyDictionaryCompiler compiler(value_store_params); - std::function(std::string)> parser = [](std::string line) { + std::function(std::string)> const parser = [](const std::string& line) { std::string key = line; - size_t tab = line.find('\t'); + size_t const tab = line.find('\t'); if (tab != std::string::npos) { key = line.substr(0, tab); @@ -208,9 +231,9 @@ void compile_json(const std::vector& input, const std::string& outp /** Extracts the parameters. */ keyvi::util::parameters_t extract_parameters(const boost::program_options::variables_map& vm) { keyvi::util::parameters_t ret; - for (auto& v : vm["parameter"].as>()) { + for (const auto& v : vm["parameter"].as>()) { std::vector key_value; - boost::split(key_value, v, std::bind(std::equal_to(), std::placeholders::_1, '=')); + boost::split(key_value, v, [](auto&& PH1) { return std::equal_to()(std::forward(PH1), '='); }); if (key_value.size() == 2) { ret[key_value[0]] = key_value[1]; } else { @@ -260,22 +283,22 @@ int main(int argc, char** argv) { boost::program_options::command_line_parser(argc, argv).options(description).positional(p).run(), vm); boost::program_options::notify(vm); - if (vm.count("help")) { + if (vm.count("help") != 0U) { std::cout << description; return 0; } - std::string manifest = vm["manifest"].as(); - std::cout << manifest << std::endl; + std::string const manifest = vm["manifest"].as(); + std::cout << manifest << '\n'; - std::string dictionary_type = vm["dictionary-type"].as(); + std::string const dictionary_type = vm["dictionary-type"].as(); keyvi::util::parameters_t value_store_params = extract_parameters(vm); - if (vm.count("memory-limit")) { + if (vm.count("memory-limit") != 0U) { value_store_params[MEMORY_LIMIT_KEY] = vm["memory-limit"].as(); } - if (vm.count("input-file") && vm.count("output-file")) { + if ((vm.count("input-file") != 0U) && (vm.count("output-file") != 0U)) { input_files = vm["input-file"].as>(); output_file = vm["output-file"].as(); @@ -290,19 +313,19 @@ int main(int argc, char** argv) { } else if (dictionary_type == "completion") { compile_completion(input_files, output_file, manifest, value_store_params); } else { - std::cout << "ERROR: unknown dictionary type." << std::endl << std::endl; + std::cout << "ERROR: unknown dictionary type." << '\n' << '\n'; std::cout << description; return 1; } } else { - std::cout << "ERROR: arguments wrong or missing." << std::endl << std::endl; + std::cout << "ERROR: arguments wrong or missing." << '\n' << '\n'; std::cout << description; return 1; } } catch (std::exception& e) { - std::cout << "ERROR: arguments wrong or missing." << std::endl << std::endl; + std::cout << "ERROR: arguments wrong or missing." << '\n' << '\n'; - std::cout << e.what() << std::endl << std::endl; + std::cout << e.what() << '\n' << '\n'; std::cout << description; return 1; diff --git a/keyvi/bin/keyviinspector/keyviinspector.cpp b/keyvi/bin/keyviinspector/keyviinspector.cpp index 706f71de1..485a1ff19 100644 --- a/keyvi/bin/keyviinspector/keyviinspector.cpp +++ b/keyvi/bin/keyviinspector/keyviinspector.cpp @@ -22,18 +22,24 @@ * Created on: May 13, 2014 * Author: hendrik */ +#include #include +#include #include #include +#include +#include +#include +#include #include "keyvi/dictionary/fsa/automata.h" #include "keyvi/dictionary/fsa/entry_iterator.h" void dump(const std::string& input, const std::string& output, bool keys_only = false) { - keyvi::dictionary::fsa::automata_t automata(new keyvi::dictionary::fsa::Automata(input.c_str())); + keyvi::dictionary::fsa::automata_t const automata(new keyvi::dictionary::fsa::Automata(input)); keyvi::dictionary::fsa::EntryIterator it(automata); - keyvi::dictionary::fsa::EntryIterator end_it = keyvi::dictionary::fsa::EntryIterator(); + keyvi::dictionary::fsa::EntryIterator const end_it = keyvi::dictionary::fsa::EntryIterator(); std::ofstream out_stream(output); @@ -41,8 +47,8 @@ void dump(const std::string& input, const std::string& output, bool keys_only = it.WriteKey(out_stream); if (!keys_only) { - std::string value = it.GetValueAsString(); - if (value.size()) { + std::string const value = it.GetValueAsString(); + if (!value.empty()) { out_stream << "\t"; out_stream << value; } @@ -54,9 +60,9 @@ void dump(const std::string& input, const std::string& output, bool keys_only = } void dump_with_attributes(const std::string& input, const std::string& output) { - keyvi::dictionary::fsa::automata_t automata(new keyvi::dictionary::fsa::Automata(input.c_str())); + keyvi::dictionary::fsa::automata_t const automata(new keyvi::dictionary::fsa::Automata(input)); keyvi::dictionary::fsa::EntryIterator it(automata); - keyvi::dictionary::fsa::EntryIterator end_it = keyvi::dictionary::fsa::EntryIterator(); + keyvi::dictionary::fsa::EntryIterator const end_it = keyvi::dictionary::fsa::EntryIterator(); std::ofstream out_stream(output); @@ -73,8 +79,8 @@ void dump_with_attributes(const std::string& input, const std::string& output) { } void print_statistics(const std::string& input) { - keyvi::dictionary::fsa::automata_t automata(new keyvi::dictionary::fsa::Automata(input.c_str())); - std::cout << automata->GetStatistics() << std::endl; + keyvi::dictionary::fsa::automata_t const automata(new keyvi::dictionary::fsa::Automata(input)); + std::cout << automata->GetStatistics() << '\n'; } int main(int argc, char** argv) { @@ -100,17 +106,17 @@ int main(int argc, char** argv) { boost::program_options::store( boost::program_options::command_line_parser(argc, argv).options(description).positional(p).run(), vm); boost::program_options::notify(vm); - if (vm.count("help")) { + if (vm.count("help") != 0U) { std::cout << description; return 0; } bool key_only = false; - if (vm.count("keys-only")) { + if (vm.count("keys-only") != 0U) { key_only = true; } - if (vm.count("input-file") && vm.count("output-file")) { + if ((vm.count("input-file") != 0U) && (vm.count("output-file") != 0U)) { input_file = vm["input-file"].as(); output_file = vm["output-file"].as(); @@ -119,13 +125,13 @@ int main(int argc, char** argv) { return 0; } - if (vm.count("input-file") && vm.count("statistics")) { + if ((vm.count("input-file") != 0U) && (vm.count("statistics") != 0U)) { input_file = vm["input-file"].as(); print_statistics(input_file); return 0; } - std::cout << "ERROR: arguments wrong or missing." << std::endl << std::endl; + std::cout << "ERROR: arguments wrong or missing." << '\n' << '\n'; std::cout << description; return 1; } diff --git a/keyvi/bin/keyvimerger/keyvimerger.cpp b/keyvi/bin/keyvimerger/keyvimerger.cpp index 88ec8ef58..947316c19 100644 --- a/keyvi/bin/keyvimerger/keyvimerger.cpp +++ b/keyvi/bin/keyvimerger/keyvimerger.cpp @@ -23,23 +23,34 @@ * Author: hendrik */ +#include +#include +#include +#include #include -#include +#include +#include -#include -#include -#include +#include +#include +#include #include +#include +#include +#include +#include +#include #include "keyvi/dictionary/dictionary_types.h" +#include "keyvi/dictionary/fsa/internal/constants.h" #include "keyvi/util/configuration.h" /** Extracts the parameters. */ keyvi::util::parameters_t extract_parameters(const boost::program_options::variables_map& vm) { keyvi::util::parameters_t ret; - for (auto& v : vm["parameter"].as>()) { + for (const auto& v : vm["parameter"].as>()) { std::vector key_value; - boost::split(key_value, v, std::bind(std::equal_to(), std::placeholders::_1, '=')); + boost::split(key_value, v, [](auto&& PH1) { return std::equal_to()(std::forward(PH1), '='); }); if (key_value.size() == 2) { ret[key_value[0]] = key_value[1]; } else { @@ -79,18 +90,18 @@ int main(int argc, char** argv) { boost::program_options::store( boost::program_options::command_line_parser(argc, argv).options(description).positional(p).run(), vm); boost::program_options::notify(vm); - if (vm.count("help")) { + if (vm.count("help") != 0U) { std::cout << description; return 0; } - if (vm.count("input-file") && vm.count("output-file")) { + if ((vm.count("input-file") != 0U) && (vm.count("output-file") != 0U)) { input_files = vm["input-file"].as>(); output_file = vm["output-file"].as(); std::vector inputs; - for (auto f : input_files) { + for (const auto& f : input_files) { if (boost::filesystem::is_directory(f)) { for (auto& entry : boost::make_iterator_range(boost::filesystem::directory_iterator(f), {})) { if (entry.path().extension() == ".kv") { @@ -103,19 +114,19 @@ int main(int argc, char** argv) { } keyvi::util::parameters_t params = extract_parameters(vm); - if (vm.count("memory-limit")) { + if (vm.count("memory-limit") != 0U) { params[MEMORY_LIMIT_KEY] = vm["memory-limit"].as(); } keyvi::dictionary::JsonDictionaryMerger jsonDictionaryMerger(params); - for (auto f : inputs) { + for (const auto& f : inputs) { jsonDictionaryMerger.Add(f); } jsonDictionaryMerger.Merge(output_file); } else { - std::cout << "ERROR: arguments wrong or missing." << std::endl << std::endl; + std::cout << "ERROR: arguments wrong or missing." << '\n' << '\n'; std::cout << description; return 1; }