Skip to content

Commit eb7b032

Browse files
committed
Refactor prefix_in_list to take vector of strings
Because this gives us compatability with modern C++ features such as ranged for loops and the algorithms in the standard template library. Because using a standard container rather than a null terminated array improves memory safety. Clang format is off for the lists of options in order to keep the existing manual formatting. This existing formatting is consistent with the other lists of options.
1 parent cc12dff commit eb7b032

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

src/goto-cc/armcc_cmdline.cpp

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ Author: Daniel Kroening
1212
#include "armcc_cmdline.h"
1313

1414
#include <util/optional.h>
15+
#include <util/prefix.h>
1516

17+
#include <algorithm>
1618
#include <cstring>
1719
#include <iostream>
20+
#include <string>
21+
#include <vector>
1822

1923
/// parses the command line options into a cmdlinet
2024
/// \par parameters: argument count, argument strings
@@ -197,7 +201,8 @@ static const char *options_no_arg[]=
197201
nullptr
198202
};
199203

200-
static const char *options_with_prefix[]=
204+
// clang-format off
205+
static const std::vector<std::string> options_with_prefix
201206
{
202207
"--project=",
203208
"--workdir=",
@@ -243,11 +248,10 @@ static const char *options_with_prefix[]=
243248
"--configure_sysroot=",
244249
"--configure_cpp_headers=",
245250
"--configure_extra_includes=",
246-
"--configure_extra_libraries=",
247-
nullptr
251+
"--configure_extra_libraries="
248252
};
249253

250-
static const char *options_with_arg[]=
254+
static const std::vector<std::string> options_with_arg
251255
{
252256
// goto-cc specific
253257
"--verbosity",
@@ -263,21 +267,20 @@ static const char *options_with_arg[]=
263267
"-Warmcc,",
264268
"-o",
265269
"--cpu",
266-
"--apcs",
267-
nullptr
270+
"--apcs"
268271
};
272+
// clang-format on
269273

270-
optionalt<std::string> prefix_in_list(const char *option, const char **list)
274+
optionalt<std::string>
275+
prefix_in_list(const std::string &option, const std::vector<std::string> &list)
271276
{
272-
for(std::size_t i = 0; list[i] != nullptr; i++)
273-
{
274-
if(strncmp(option, list[i], strlen(list[i])) == 0)
275-
{
276-
return {list[i]};
277-
}
278-
}
279-
280-
return {};
277+
const auto found =
278+
std::find_if(list.cbegin(), list.cend(), [&](const std::string &argument) {
279+
return has_prefix(argument, option);
280+
});
281+
if(found == list.cend())
282+
return {};
283+
return {*found};
281284
}
282285

283286
bool armcc_cmdlinet::parse(int argc, const char **argv)

0 commit comments

Comments
 (0)