Skip to content

Commit 61133bd

Browse files
authored
apply clang tidy fixes (#325)
apply clang tidy fixes using --fix-errors, fixes most of the errors in keyvi/bin
1 parent acb496b commit 61133bd

File tree

4 files changed

+123
-76
lines changed

4 files changed

+123
-76
lines changed

keyvi/bin/keyvi_c/c_api.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,19 @@
2424

2525
#include "keyvi/c_api/c_api.h"
2626

27+
#include <cstdint>
28+
#include <cstdlib>
2729
#include <cstring>
30+
#include <exception>
2831
#include <iostream>
32+
#include <string>
33+
#include <utility>
2934

3035
#include "keyvi/dictionary/completion/multiword_completion.h"
3136
#include "keyvi/dictionary/completion/prefix_completion.h"
3237
#include "keyvi/dictionary/dictionary.h"
38+
#include "keyvi/dictionary/match.h"
39+
#include "keyvi/dictionary/match_iterator.h"
3340

3441
using keyvi::dictionary::Dictionary;
3542
using keyvi::dictionary::dictionary_t;
@@ -41,7 +48,7 @@ using keyvi::dictionary::completion::PrefixCompletion;
4148
namespace {
4249
char* std_2_c_string(const std::string& str) {
4350
const size_t c_str_length = str.size() + 1;
44-
auto result = static_cast<char*>(malloc(c_str_length));
51+
auto* result = static_cast<char*>(malloc(c_str_length));
4552
strncpy(result, str.c_str(), c_str_length);
4653
return result;
4754
}
@@ -54,7 +61,7 @@ struct keyvi_dictionary {
5461
};
5562

5663
struct keyvi_match {
57-
explicit keyvi_match(const match_t& obj) : obj_(obj) {}
64+
explicit keyvi_match(match_t obj) : obj_(std::move(obj)) {}
5865

5966
match_t obj_;
6067
};
@@ -91,7 +98,7 @@ keyvi_dictionary* keyvi_create_dictionary(const char* filename) {
9198
try {
9299
return new keyvi_dictionary(Dictionary(filename));
93100
} catch (const std::exception& e) {
94-
std::cerr << e.what() << std::endl;
101+
std::cerr << e.what() << '\n';
95102
return nullptr;
96103
}
97104
}
@@ -129,7 +136,7 @@ keyvi_match_iterator* keyvi_dictionary_get_fuzzy(const keyvi_dictionary* dict, c
129136

130137
keyvi_match_iterator* keyvi_dictionary_get_multi_word_completions(const keyvi_dictionary* dict, const char* key,
131138
const size_t key_len, const size_t cutoff) {
132-
MultiWordCompletion multiWordCompletion(dict->obj_);
139+
MultiWordCompletion const multiWordCompletion(dict->obj_);
133140
return new keyvi_match_iterator(multiWordCompletion.GetCompletions(std::string(key, key_len), cutoff));
134141
}
135142

@@ -166,7 +173,7 @@ keyvi_bytes keyvi_match_get_msgpacked_value(const struct keyvi_match* match) {
166173
if (0 == data_size) {
167174
return empty_keyvi_bytes;
168175
}
169-
auto data_ptr = malloc(data_size);
176+
auto* data_ptr = malloc(data_size);
170177
if (nullptr == data_ptr) {
171178
return empty_keyvi_bytes;
172179
}

keyvi/bin/keyvicompiler/keyvicompiler.cpp

Lines changed: 69 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,42 @@
2323
* Author: hendrik
2424
*/
2525

26+
#include <cstddef>
27+
#include <cstdint>
28+
#include <exception>
29+
#include <fstream>
2630
#include <functional>
27-
28-
#include <boost/algorithm/string.hpp>
29-
#include <boost/algorithm/string/join.hpp>
30-
#include <boost/filesystem.hpp>
31+
#include <ios>
32+
#include <iostream>
33+
#include <ostream>
34+
#include <stdexcept>
35+
#include <string>
36+
#include <utility>
37+
#include <vector>
38+
39+
#include <boost/algorithm/string/split.hpp>
40+
#include <boost/filesystem/directory.hpp>
41+
#include <boost/filesystem/operations.hpp>
42+
#include <boost/filesystem/path.hpp>
3143
#include <boost/iostreams/device/file.hpp>
3244
#include <boost/iostreams/filter/gzip.hpp>
3345
#include <boost/iostreams/filtering_stream.hpp>
3446
#include <boost/lexical_cast.hpp>
35-
#include <boost/program_options.hpp>
36-
#include <boost/range/adaptor/map.hpp>
37-
#include <boost/range/iterator_range.hpp>
47+
#include <boost/lexical_cast/bad_lexical_cast.hpp>
48+
#include <boost/program_options.hpp> //NOLINT
49+
#include <boost/program_options/options_description.hpp>
50+
#include <boost/program_options/positional_options.hpp>
51+
#include <boost/program_options/value_semantic.hpp>
52+
#include <boost/program_options/variables_map.hpp>
53+
#include <boost/range/adaptor/map.hpp> //NOLINT
54+
#include <boost/range/iterator_range_core.hpp>
3855

39-
#include "keyvi/dictionary/dictionary_compiler.h"
4056
#include "keyvi/dictionary/dictionary_types.h"
57+
#include "keyvi/dictionary/fsa/internal/constants.h"
4158
#include "keyvi/util/configuration.h"
4259

43-
void callback(size_t added, size_t overall, void*) {
44-
std::cout << "Processed " << added << "/" << overall << "(" << ((100 * added) / overall) << "%)." << std::endl;
60+
void callback(size_t added, size_t overall, void* /*unused*/) {
61+
std::cout << "Processed " << added << "/" << overall << "(" << ((100 * added) / overall) << "%)." << '\n';
4562
}
4663

4764
template <typename CompilerType, typename ValueType>
@@ -50,7 +67,7 @@ void compile_multiple(CompilerType* compiler, std::function<std::pair<std::strin
5067
boost::iostreams::filtering_istream input_stream;
5168
std::string line;
5269

53-
for (auto input_as_string : inputs) {
70+
for (const auto& input_as_string : inputs) {
5471
auto input = boost::filesystem::path(input_as_string);
5572

5673
if (boost::filesystem::is_directory(input)) {
@@ -60,7 +77,7 @@ void compile_multiple(CompilerType* compiler, std::function<std::pair<std::strin
6077
input_stream.push(boost::iostreams::gzip_decompressor());
6178
}
6279

63-
boost::iostreams::file_source file(entry.path().string(), std::ios_base::in | std::ios_base::binary);
80+
boost::iostreams::file_source const file(entry.path().string(), std::ios_base::in | std::ios_base::binary);
6481
input_stream.push(file);
6582
++files_added;
6683
while (std::getline(input_stream, line)) {
@@ -79,7 +96,7 @@ void compile_multiple(CompilerType* compiler, std::function<std::pair<std::strin
7996
input_stream.push(boost::iostreams::gzip_decompressor());
8097
}
8198

82-
boost::iostreams::file_source file(input.string(), std::ios_base::in | std::ios_base::binary);
99+
boost::iostreams::file_source const file(input.string(), std::ios_base::in | std::ios_base::binary);
83100

84101
input_stream.push(file);
85102
while (std::getline(input_stream, line)) {
@@ -107,19 +124,21 @@ void compile_completion(const std::vector<std::string>& input, const std::string
107124
const keyvi::util::parameters_t& value_store_params = keyvi::util::parameters_t()) {
108125
keyvi::dictionary::CompletionDictionaryCompiler compiler(value_store_params);
109126

110-
std::function<std::pair<std::string, uint32_t>(std::string)> parser = [](std::string line) {
111-
size_t tab = line.find('\t');
127+
std::function<std::pair<std::string, uint32_t>(std::string)> const parser = [](const std::string& line) {
128+
size_t const tab = line.find('\t');
112129

113-
if (tab == std::string::npos) return std::pair<std::string, uint32_t>();
130+
if (tab == std::string::npos) {
131+
return std::pair<std::string, uint32_t>();
132+
}
114133

115-
std::string key = line.substr(0, tab);
116-
std::string value_as_string = line.substr(tab + 1);
117-
uint32_t value;
134+
std::string const key = line.substr(0, tab);
135+
std::string const value_as_string = line.substr(tab + 1);
136+
uint32_t value = 0;
118137

119138
try {
120139
value = boost::lexical_cast<uint32_t>(value_as_string);
121140
} catch (boost::bad_lexical_cast const&) {
122-
std::cout << "Error: value was not valid: " << line << std::endl;
141+
std::cout << "Error: value was not valid: " << line << '\n';
123142
return std::pair<std::string, uint32_t>();
124143
}
125144
return std::pair<std::string, uint32_t>(key, value);
@@ -133,19 +152,21 @@ void compile_integer(const std::vector<std::string>& input, const std::string& o
133152
const keyvi::util::parameters_t& value_store_params = keyvi::util::parameters_t()) {
134153
keyvi::dictionary::IntDictionaryCompiler compiler(value_store_params);
135154

136-
std::function<std::pair<std::string, uint32_t>(std::string)> parser = [](std::string line) {
137-
size_t tab = line.find('\t');
155+
std::function<std::pair<std::string, uint32_t>(std::string)> const parser = [](const std::string& line) {
156+
size_t const tab = line.find('\t');
138157

139-
if (tab == std::string::npos) return std::pair<std::string, uint32_t>();
158+
if (tab == std::string::npos) {
159+
return std::pair<std::string, uint32_t>();
160+
}
140161

141-
std::string key = line.substr(0, tab);
142-
std::string value_as_string = line.substr(tab + 1);
143-
uint32_t value;
162+
std::string const key = line.substr(0, tab);
163+
std::string const value_as_string = line.substr(tab + 1);
164+
uint32_t value = 0;
144165

145166
try {
146167
value = boost::lexical_cast<uint32_t>(value_as_string);
147168
} catch (boost::bad_lexical_cast const&) {
148-
std::cout << "Error: value was not valid: " << line << std::endl;
169+
std::cout << "Error: value was not valid: " << line << '\n';
149170
return std::pair<std::string, uint32_t>();
150171
}
151172
return std::pair<std::string, uint32_t>(key, value);
@@ -158,11 +179,13 @@ void compile_integer(const std::vector<std::string>& input, const std::string& o
158179
template <class Compiler>
159180
void compile_strings_inner(Compiler* compiler, const std::vector<std::string>& input, const std::string& output,
160181
const std::string& manifest = {}) {
161-
std::function<std::pair<std::string, std::string>(std::string)> parser = [](std::string line) {
162-
size_t tab = line.find('\t');
163-
if (tab == std::string::npos) return std::pair<std::string, std::string>();
164-
std::string key = line.substr(0, tab);
165-
std::string value = line.substr(tab + 1);
182+
std::function<std::pair<std::string, std::string>(std::string)> const parser = [](const std::string& line) {
183+
size_t const tab = line.find('\t');
184+
if (tab == std::string::npos) {
185+
return std::pair<std::string, std::string>();
186+
}
187+
std::string const key = line.substr(0, tab);
188+
std::string const value = line.substr(tab + 1);
166189

167190
return std::pair<std::string, std::string>(key, value);
168191
};
@@ -183,9 +206,9 @@ void compile_key_only(const std::vector<std::string>& input, const std::string&
183206
const keyvi::util::parameters_t& value_store_params = keyvi::util::parameters_t()) {
184207
keyvi::dictionary::KeyOnlyDictionaryCompiler compiler(value_store_params);
185208

186-
std::function<std::pair<std::string, uint32_t>(std::string)> parser = [](std::string line) {
209+
std::function<std::pair<std::string, uint32_t>(std::string)> const parser = [](const std::string& line) {
187210
std::string key = line;
188-
size_t tab = line.find('\t');
211+
size_t const tab = line.find('\t');
189212

190213
if (tab != std::string::npos) {
191214
key = line.substr(0, tab);
@@ -208,9 +231,9 @@ void compile_json(const std::vector<std::string>& input, const std::string& outp
208231
/** Extracts the parameters. */
209232
keyvi::util::parameters_t extract_parameters(const boost::program_options::variables_map& vm) {
210233
keyvi::util::parameters_t ret;
211-
for (auto& v : vm["parameter"].as<std::vector<std::string>>()) {
234+
for (const auto& v : vm["parameter"].as<std::vector<std::string>>()) {
212235
std::vector<std::string> key_value;
213-
boost::split(key_value, v, std::bind(std::equal_to<char>(), std::placeholders::_1, '='));
236+
boost::split(key_value, v, [](auto&& PH1) { return std::equal_to<char>()(std::forward<decltype(PH1)>(PH1), '='); });
214237
if (key_value.size() == 2) {
215238
ret[key_value[0]] = key_value[1];
216239
} else {
@@ -260,22 +283,22 @@ int main(int argc, char** argv) {
260283
boost::program_options::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
261284
boost::program_options::notify(vm);
262285

263-
if (vm.count("help")) {
286+
if (vm.count("help") != 0U) {
264287
std::cout << description;
265288
return 0;
266289
}
267290

268-
std::string manifest = vm["manifest"].as<std::string>();
269-
std::cout << manifest << std::endl;
291+
std::string const manifest = vm["manifest"].as<std::string>();
292+
std::cout << manifest << '\n';
270293

271-
std::string dictionary_type = vm["dictionary-type"].as<std::string>();
294+
std::string const dictionary_type = vm["dictionary-type"].as<std::string>();
272295
keyvi::util::parameters_t value_store_params = extract_parameters(vm);
273296

274-
if (vm.count("memory-limit")) {
297+
if (vm.count("memory-limit") != 0U) {
275298
value_store_params[MEMORY_LIMIT_KEY] = vm["memory-limit"].as<std::string>();
276299
}
277300

278-
if (vm.count("input-file") && vm.count("output-file")) {
301+
if ((vm.count("input-file") != 0U) && (vm.count("output-file") != 0U)) {
279302
input_files = vm["input-file"].as<std::vector<std::string>>();
280303
output_file = vm["output-file"].as<std::string>();
281304

@@ -290,19 +313,19 @@ int main(int argc, char** argv) {
290313
} else if (dictionary_type == "completion") {
291314
compile_completion(input_files, output_file, manifest, value_store_params);
292315
} else {
293-
std::cout << "ERROR: unknown dictionary type." << std::endl << std::endl;
316+
std::cout << "ERROR: unknown dictionary type." << '\n' << '\n';
294317
std::cout << description;
295318
return 1;
296319
}
297320
} else {
298-
std::cout << "ERROR: arguments wrong or missing." << std::endl << std::endl;
321+
std::cout << "ERROR: arguments wrong or missing." << '\n' << '\n';
299322
std::cout << description;
300323
return 1;
301324
}
302325
} catch (std::exception& e) {
303-
std::cout << "ERROR: arguments wrong or missing." << std::endl << std::endl;
326+
std::cout << "ERROR: arguments wrong or missing." << '\n' << '\n';
304327

305-
std::cout << e.what() << std::endl << std::endl;
328+
std::cout << e.what() << '\n' << '\n';
306329
std::cout << description;
307330

308331
return 1;

keyvi/bin/keyviinspector/keyviinspector.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,33 @@
2222
* Created on: May 13, 2014
2323
* Author: hendrik
2424
*/
25+
#include <fstream>
2526
#include <iostream>
27+
#include <string>
2628

2729
#include <boost/lexical_cast.hpp>
2830
#include <boost/program_options.hpp>
31+
#include <boost/program_options/options_description.hpp>
32+
#include <boost/program_options/positional_options.hpp>
33+
#include <boost/program_options/value_semantic.hpp>
34+
#include <boost/program_options/variables_map.hpp>
2935

3036
#include "keyvi/dictionary/fsa/automata.h"
3137
#include "keyvi/dictionary/fsa/entry_iterator.h"
3238

3339
void dump(const std::string& input, const std::string& output, bool keys_only = false) {
34-
keyvi::dictionary::fsa::automata_t automata(new keyvi::dictionary::fsa::Automata(input.c_str()));
40+
keyvi::dictionary::fsa::automata_t const automata(new keyvi::dictionary::fsa::Automata(input));
3541
keyvi::dictionary::fsa::EntryIterator it(automata);
36-
keyvi::dictionary::fsa::EntryIterator end_it = keyvi::dictionary::fsa::EntryIterator();
42+
keyvi::dictionary::fsa::EntryIterator const end_it = keyvi::dictionary::fsa::EntryIterator();
3743

3844
std::ofstream out_stream(output);
3945

4046
while (it != end_it) {
4147
it.WriteKey(out_stream);
4248

4349
if (!keys_only) {
44-
std::string value = it.GetValueAsString();
45-
if (value.size()) {
50+
std::string const value = it.GetValueAsString();
51+
if (!value.empty()) {
4652
out_stream << "\t";
4753
out_stream << value;
4854
}
@@ -54,9 +60,9 @@ void dump(const std::string& input, const std::string& output, bool keys_only =
5460
}
5561

5662
void dump_with_attributes(const std::string& input, const std::string& output) {
57-
keyvi::dictionary::fsa::automata_t automata(new keyvi::dictionary::fsa::Automata(input.c_str()));
63+
keyvi::dictionary::fsa::automata_t const automata(new keyvi::dictionary::fsa::Automata(input));
5864
keyvi::dictionary::fsa::EntryIterator it(automata);
59-
keyvi::dictionary::fsa::EntryIterator end_it = keyvi::dictionary::fsa::EntryIterator();
65+
keyvi::dictionary::fsa::EntryIterator const end_it = keyvi::dictionary::fsa::EntryIterator();
6066

6167
std::ofstream out_stream(output);
6268

@@ -73,8 +79,8 @@ void dump_with_attributes(const std::string& input, const std::string& output) {
7379
}
7480

7581
void print_statistics(const std::string& input) {
76-
keyvi::dictionary::fsa::automata_t automata(new keyvi::dictionary::fsa::Automata(input.c_str()));
77-
std::cout << automata->GetStatistics() << std::endl;
82+
keyvi::dictionary::fsa::automata_t const automata(new keyvi::dictionary::fsa::Automata(input));
83+
std::cout << automata->GetStatistics() << '\n';
7884
}
7985

8086
int main(int argc, char** argv) {
@@ -100,17 +106,17 @@ int main(int argc, char** argv) {
100106
boost::program_options::store(
101107
boost::program_options::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
102108
boost::program_options::notify(vm);
103-
if (vm.count("help")) {
109+
if (vm.count("help") != 0U) {
104110
std::cout << description;
105111
return 0;
106112
}
107113

108114
bool key_only = false;
109-
if (vm.count("keys-only")) {
115+
if (vm.count("keys-only") != 0U) {
110116
key_only = true;
111117
}
112118

113-
if (vm.count("input-file") && vm.count("output-file")) {
119+
if ((vm.count("input-file") != 0U) && (vm.count("output-file") != 0U)) {
114120
input_file = vm["input-file"].as<std::string>();
115121
output_file = vm["output-file"].as<std::string>();
116122

@@ -119,13 +125,13 @@ int main(int argc, char** argv) {
119125
return 0;
120126
}
121127

122-
if (vm.count("input-file") && vm.count("statistics")) {
128+
if ((vm.count("input-file") != 0U) && (vm.count("statistics") != 0U)) {
123129
input_file = vm["input-file"].as<std::string>();
124130
print_statistics(input_file);
125131
return 0;
126132
}
127133

128-
std::cout << "ERROR: arguments wrong or missing." << std::endl << std::endl;
134+
std::cout << "ERROR: arguments wrong or missing." << '\n' << '\n';
129135
std::cout << description;
130136
return 1;
131137
}

0 commit comments

Comments
 (0)