Commit 34aec8e87c46f9cd5a33ff2d8e80253b0a68cd06
1 parent
52f72a26
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
Showing
3 changed files
with
48 additions
and
35 deletions
CMakeLists.txt
| @@ -48,6 +48,8 @@ if(CXXOPTS_USE_UNICODE_HELP) | @@ -48,6 +48,8 @@ if(CXXOPTS_USE_UNICODE_HELP) | ||
| 48 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ICU_CFLAGS} -DCXXOPTS_USE_UNICODE") | 48 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ICU_CFLAGS} -DCXXOPTS_USE_UNICODE") |
| 49 | endif() | 49 | endif() |
| 50 | 50 | ||
| 51 | +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -Wextra -Wshadow") | ||
| 52 | + | ||
| 51 | add_library(cxxopts INTERFACE) | 53 | add_library(cxxopts INTERFACE) |
| 52 | target_sources( | 54 | target_sources( |
| 53 | cxxopts INTERFACE | 55 | cxxopts INTERFACE |
include/cxxopts.hpp
| @@ -66,8 +66,8 @@ namespace cxxopts | @@ -66,8 +66,8 @@ namespace cxxopts | ||
| 66 | { | 66 | { |
| 67 | public: | 67 | public: |
| 68 | 68 | ||
| 69 | - UnicodeStringIterator(const icu::UnicodeString* s, int32_t pos) | ||
| 70 | - : s(s) | 69 | + UnicodeStringIterator(const icu::UnicodeString* string, int32_t pos) |
| 70 | + : s(string) | ||
| 71 | , i(pos) | 71 | , i(pos) |
| 72 | { | 72 | { |
| 73 | } | 73 | } |
| @@ -597,11 +597,11 @@ namespace cxxopts | @@ -597,11 +597,11 @@ namespace cxxopts | ||
| 597 | public: | 597 | public: |
| 598 | OptionDetails | 598 | OptionDetails |
| 599 | ( | 599 | ( |
| 600 | - const String& description, | ||
| 601 | - std::shared_ptr<const Value> value | 600 | + const String& desc, |
| 601 | + std::shared_ptr<const Value> val | ||
| 602 | ) | 602 | ) |
| 603 | - : m_desc(description) | ||
| 604 | - , m_value(value) | 603 | + : m_desc(desc) |
| 604 | + , m_value(val) | ||
| 605 | , m_count(0) | 605 | , m_count(0) |
| 606 | { | 606 | { |
| 607 | } | 607 | } |
| @@ -809,7 +809,11 @@ namespace cxxopts | @@ -809,7 +809,11 @@ namespace cxxopts | ||
| 809 | 809 | ||
| 810 | inline | 810 | inline |
| 811 | void | 811 | void |
| 812 | - generate_group_help(String& result, const std::vector<std::string>& groups) const; | 812 | + generate_group_help |
| 813 | + ( | ||
| 814 | + String& result, | ||
| 815 | + const std::vector<std::string>& groups | ||
| 816 | + ) const; | ||
| 813 | 817 | ||
| 814 | inline | 818 | inline |
| 815 | void | 819 | void |
| @@ -1010,32 +1014,32 @@ OptionAdder::operator() | @@ -1010,32 +1014,32 @@ OptionAdder::operator() | ||
| 1010 | throw invalid_option_format_error(opts); | 1014 | throw invalid_option_format_error(opts); |
| 1011 | } | 1015 | } |
| 1012 | 1016 | ||
| 1013 | - const auto& s = result[2]; | ||
| 1014 | - const auto& l = result[3]; | 1017 | + const auto& short_match = result[2]; |
| 1018 | + const auto& long_match = result[3]; | ||
| 1015 | 1019 | ||
| 1016 | - if (!s.length() && !l.length()) | 1020 | + if (!short_match.length() && !long_match.length()) |
| 1017 | { | 1021 | { |
| 1018 | throw invalid_option_format_error(opts); | 1022 | throw invalid_option_format_error(opts); |
| 1019 | - } else if (l.length() == 1 && s.length()) | 1023 | + } else if (long_match.length() == 1 && short_match.length()) |
| 1020 | { | 1024 | { |
| 1021 | throw invalid_option_format_error(opts); | 1025 | throw invalid_option_format_error(opts); |
| 1022 | } | 1026 | } |
| 1023 | 1027 | ||
| 1024 | auto option_names = [] | 1028 | auto option_names = [] |
| 1025 | ( | 1029 | ( |
| 1026 | - const std::sub_match<const char*>& s, | ||
| 1027 | - const std::sub_match<const char*>& l | 1030 | + const std::sub_match<const char*>& short_, |
| 1031 | + const std::sub_match<const char*>& long_ | ||
| 1028 | ) | 1032 | ) |
| 1029 | { | 1033 | { |
| 1030 | - if (l.length() == 1) | 1034 | + if (long_.length() == 1) |
| 1031 | { | 1035 | { |
| 1032 | - return std::make_tuple(l.str(), s.str()); | 1036 | + return std::make_tuple(long_.str(), short_.str()); |
| 1033 | } | 1037 | } |
| 1034 | else | 1038 | else |
| 1035 | { | 1039 | { |
| 1036 | - return std::make_tuple(s.str(), l.str()); | 1040 | + return std::make_tuple(short_.str(), long_.str()); |
| 1037 | } | 1041 | } |
| 1038 | - }(s, l); | 1042 | + }(short_match, long_match); |
| 1039 | 1043 | ||
| 1040 | m_options.add_option | 1044 | m_options.add_option |
| 1041 | ( | 1045 | ( |
| @@ -1437,14 +1441,21 @@ Options::help_one_group(const std::string& g) const | @@ -1437,14 +1441,21 @@ Options::help_one_group(const std::string& g) const | ||
| 1437 | } | 1441 | } |
| 1438 | 1442 | ||
| 1439 | void | 1443 | void |
| 1440 | -Options::generate_group_help(String& result, const std::vector<std::string>& groups) const | 1444 | +Options::generate_group_help |
| 1445 | +( | ||
| 1446 | + String& result, | ||
| 1447 | + const std::vector<std::string>& print_groups | ||
| 1448 | +) const | ||
| 1441 | { | 1449 | { |
| 1442 | - for (std::size_t i = 0; i < groups.size(); ++i) | 1450 | + for (size_t i = 0; i != print_groups.size(); ++i) |
| 1443 | { | 1451 | { |
| 1444 | - String const& group_help = help_one_group(groups[i]); | ||
| 1445 | - if (empty(group_help)) continue; | ||
| 1446 | - result += group_help; | ||
| 1447 | - if (i < groups.size() - 1) | 1452 | + const String& group_help_text = help_one_group(print_groups[i]); |
| 1453 | + if (empty(group_help_text)) | ||
| 1454 | + { | ||
| 1455 | + continue; | ||
| 1456 | + } | ||
| 1457 | + result += group_help_text; | ||
| 1458 | + if (i < print_groups.size() - 1) | ||
| 1448 | { | 1459 | { |
| 1449 | result += '\n'; | 1460 | result += '\n'; |
| 1450 | } | 1461 | } |
| @@ -1454,19 +1465,19 @@ Options::generate_group_help(String& result, const std::vector<std::string>& gro | @@ -1454,19 +1465,19 @@ Options::generate_group_help(String& result, const std::vector<std::string>& gro | ||
| 1454 | void | 1465 | void |
| 1455 | Options::generate_all_groups_help(String& result) const | 1466 | Options::generate_all_groups_help(String& result) const |
| 1456 | { | 1467 | { |
| 1457 | - std::vector<std::string> groups; | ||
| 1458 | - groups.reserve(m_help.size()); | 1468 | + std::vector<std::string> all_groups; |
| 1469 | + all_groups.reserve(m_help.size()); | ||
| 1459 | 1470 | ||
| 1460 | for (auto& group : m_help) | 1471 | for (auto& group : m_help) |
| 1461 | { | 1472 | { |
| 1462 | - groups.push_back(group.first); | 1473 | + all_groups.push_back(group.first); |
| 1463 | } | 1474 | } |
| 1464 | 1475 | ||
| 1465 | - generate_group_help(result, groups); | 1476 | + generate_group_help(result, all_groups); |
| 1466 | } | 1477 | } |
| 1467 | 1478 | ||
| 1468 | std::string | 1479 | std::string |
| 1469 | -Options::help(const std::vector<std::string>& groups) const | 1480 | +Options::help(const std::vector<std::string>& help_groups) const |
| 1470 | { | 1481 | { |
| 1471 | String result = m_help_string + "\nUsage:\n " + | 1482 | String result = m_help_string + "\nUsage:\n " + |
| 1472 | toLocalString(m_program) + " [OPTION...]"; | 1483 | toLocalString(m_program) + " [OPTION...]"; |
| @@ -1477,13 +1488,13 @@ Options::help(const std::vector<std::string>& groups) const | @@ -1477,13 +1488,13 @@ Options::help(const std::vector<std::string>& groups) const | ||
| 1477 | 1488 | ||
| 1478 | result += "\n\n"; | 1489 | result += "\n\n"; |
| 1479 | 1490 | ||
| 1480 | - if (groups.size() == 0) | 1491 | + if (help_groups.size() == 0) |
| 1481 | { | 1492 | { |
| 1482 | generate_all_groups_help(result); | 1493 | generate_all_groups_help(result); |
| 1483 | } | 1494 | } |
| 1484 | else | 1495 | else |
| 1485 | { | 1496 | { |
| 1486 | - generate_group_help(result, groups); | 1497 | + generate_group_help(result, help_groups); |
| 1487 | } | 1498 | } |
| 1488 | 1499 | ||
| 1489 | return toUTF8String(result); | 1500 | return toUTF8String(result); |
test/options.cpp
| @@ -7,13 +7,13 @@ | @@ -7,13 +7,13 @@ | ||
| 7 | class Argv { | 7 | class Argv { |
| 8 | public: | 8 | public: |
| 9 | 9 | ||
| 10 | - Argv(std::initializer_list<const char*> argv) | ||
| 11 | - : m_argv(new char*[argv.size()]) | ||
| 12 | - , m_argc(argv.size()) | 10 | + Argv(std::initializer_list<const char*> args) |
| 11 | + : m_argv(new char*[args.size()]) | ||
| 12 | + , m_argc(args.size()) | ||
| 13 | { | 13 | { |
| 14 | int i = 0; | 14 | int i = 0; |
| 15 | - auto iter = argv.begin(); | ||
| 16 | - while (iter != argv.end()) { | 15 | + auto iter = args.begin(); |
| 16 | + while (iter != args.end()) { | ||
| 17 | auto len = strlen(*iter) + 1; | 17 | auto len = strlen(*iter) + 1; |
| 18 | auto ptr = std::unique_ptr<char[]>(new char[len]); | 18 | auto ptr = std::unique_ptr<char[]>(new char[len]); |
| 19 | 19 |