Enable warnings and fix them

Fixes #50. This enables -Wall -Wextra -Wshadow and sets -Werror so that
the build fails if there are any warnings. All warnings that came up are
also fixed
This commit is contained in:
Jarryd Beck 2017-06-01 17:12:17 +10:00
parent 52f72a26e7
commit 34aec8e87c
3 changed files with 48 additions and 35 deletions

View File

@ -48,6 +48,8 @@ if(CXXOPTS_USE_UNICODE_HELP)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ICU_CFLAGS} -DCXXOPTS_USE_UNICODE") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ICU_CFLAGS} -DCXXOPTS_USE_UNICODE")
endif() endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -Wextra -Wshadow")
add_library(cxxopts INTERFACE) add_library(cxxopts INTERFACE)
target_sources( target_sources(
cxxopts INTERFACE cxxopts INTERFACE

View File

@ -66,8 +66,8 @@ namespace cxxopts
{ {
public: public:
UnicodeStringIterator(const icu::UnicodeString* s, int32_t pos) UnicodeStringIterator(const icu::UnicodeString* string, int32_t pos)
: s(s) : s(string)
, i(pos) , i(pos)
{ {
} }
@ -597,11 +597,11 @@ namespace cxxopts
public: public:
OptionDetails OptionDetails
( (
const String& description, const String& desc,
std::shared_ptr<const Value> value std::shared_ptr<const Value> val
) )
: m_desc(description) : m_desc(desc)
, m_value(value) , m_value(val)
, m_count(0) , m_count(0)
{ {
} }
@ -809,7 +809,11 @@ namespace cxxopts
inline inline
void void
generate_group_help(String& result, const std::vector<std::string>& groups) const; generate_group_help
(
String& result,
const std::vector<std::string>& groups
) const;
inline inline
void void
@ -1010,32 +1014,32 @@ OptionAdder::operator()
throw invalid_option_format_error(opts); throw invalid_option_format_error(opts);
} }
const auto& s = result[2]; const auto& short_match = result[2];
const auto& l = result[3]; const auto& long_match = result[3];
if (!s.length() && !l.length()) if (!short_match.length() && !long_match.length())
{ {
throw invalid_option_format_error(opts); throw invalid_option_format_error(opts);
} else if (l.length() == 1 && s.length()) } else if (long_match.length() == 1 && short_match.length())
{ {
throw invalid_option_format_error(opts); throw invalid_option_format_error(opts);
} }
auto option_names = [] auto option_names = []
( (
const std::sub_match<const char*>& s, const std::sub_match<const char*>& short_,
const std::sub_match<const char*>& l const std::sub_match<const char*>& long_
) )
{ {
if (l.length() == 1) if (long_.length() == 1)
{ {
return std::make_tuple(l.str(), s.str()); return std::make_tuple(long_.str(), short_.str());
} }
else else
{ {
return std::make_tuple(s.str(), l.str()); return std::make_tuple(short_.str(), long_.str());
} }
}(s, l); }(short_match, long_match);
m_options.add_option m_options.add_option
( (
@ -1437,14 +1441,21 @@ Options::help_one_group(const std::string& g) const
} }
void void
Options::generate_group_help(String& result, const std::vector<std::string>& groups) const Options::generate_group_help
(
String& result,
const std::vector<std::string>& print_groups
) const
{ {
for (std::size_t i = 0; i < groups.size(); ++i) for (size_t i = 0; i != print_groups.size(); ++i)
{ {
String const& group_help = help_one_group(groups[i]); const String& group_help_text = help_one_group(print_groups[i]);
if (empty(group_help)) continue; if (empty(group_help_text))
result += group_help; {
if (i < groups.size() - 1) continue;
}
result += group_help_text;
if (i < print_groups.size() - 1)
{ {
result += '\n'; result += '\n';
} }
@ -1454,19 +1465,19 @@ Options::generate_group_help(String& result, const std::vector<std::string>& gro
void void
Options::generate_all_groups_help(String& result) const Options::generate_all_groups_help(String& result) const
{ {
std::vector<std::string> groups; std::vector<std::string> all_groups;
groups.reserve(m_help.size()); all_groups.reserve(m_help.size());
for (auto& group : m_help) for (auto& group : m_help)
{ {
groups.push_back(group.first); all_groups.push_back(group.first);
} }
generate_group_help(result, groups); generate_group_help(result, all_groups);
} }
std::string std::string
Options::help(const std::vector<std::string>& groups) const Options::help(const std::vector<std::string>& help_groups) const
{ {
String result = m_help_string + "\nUsage:\n " + String result = m_help_string + "\nUsage:\n " +
toLocalString(m_program) + " [OPTION...]"; toLocalString(m_program) + " [OPTION...]";
@ -1477,13 +1488,13 @@ Options::help(const std::vector<std::string>& groups) const
result += "\n\n"; result += "\n\n";
if (groups.size() == 0) if (help_groups.size() == 0)
{ {
generate_all_groups_help(result); generate_all_groups_help(result);
} }
else else
{ {
generate_group_help(result, groups); generate_group_help(result, help_groups);
} }
return toUTF8String(result); return toUTF8String(result);

View File

@ -7,13 +7,13 @@
class Argv { class Argv {
public: public:
Argv(std::initializer_list<const char*> argv) Argv(std::initializer_list<const char*> args)
: m_argv(new char*[argv.size()]) : m_argv(new char*[args.size()])
, m_argc(argv.size()) , m_argc(args.size())
{ {
int i = 0; int i = 0;
auto iter = argv.begin(); auto iter = args.begin();
while (iter != argv.end()) { while (iter != args.end()) {
auto len = strlen(*iter) + 1; auto len = strlen(*iter) + 1;
auto ptr = std::unique_ptr<char[]>(new char[len]); auto ptr = std::unique_ptr<char[]>(new char[len]);