Commit 12e496da3d486b87fa9df43edea65232ed852510

Authored by Daniel Lemire
Committed by GitHub
1 parent 3ef9fddc

Making sure that the library can compile without warnings even when crazy pedant…

…ic flags are set (#238)

Makes some fixes to satisfy various strict warnings.
CMakeLists.txt
@@ -50,7 +50,7 @@ set(CMAKE_CXX_EXTENSIONS OFF) @@ -50,7 +50,7 @@ set(CMAKE_CXX_EXTENSIONS OFF)
50 if(MSVC) 50 if(MSVC)
51 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W2") 51 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W2")
52 elseif(CMAKE_CXX_COMPILER_ID MATCHES "[Cc]lang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU") 52 elseif(CMAKE_CXX_COMPILER_ID MATCHES "[Cc]lang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
53 - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -Wextra -Wshadow") 53 + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -Wextra -Wshadow -Weffc++ -Wsign-compare -Wshadow -Wwrite-strings -Wpointer-arith -Winit-self -Wconversion -Wno-sign-conversion")
54 endif() 54 endif()
55 55
56 add_library(cxxopts INTERFACE) 56 add_library(cxxopts INTERFACE)
include/cxxopts.hpp
@@ -147,9 +147,9 @@ namespace cxxopts @@ -147,9 +147,9 @@ namespace cxxopts
147 147
148 inline 148 inline
149 String& 149 String&
150 - stringAppend(String& s, int n, UChar32 c) 150 + stringAppend(String& s, size_t n, UChar32 c)
151 { 151 {
152 - for (int i = 0; i != n; ++i) 152 + for (size_t i = 0; i != n; ++i)
153 { 153 {
154 s.append(c); 154 s.append(c);
155 } 155 }
@@ -285,6 +285,13 @@ namespace cxxopts @@ -285,6 +285,13 @@ namespace cxxopts
285 #endif 285 #endif
286 } // namespace 286 } // namespace
287 287
  288 +#if defined(__GNUC__)
  289 +// GNU GCC with -Weffc++ will issue a warning regarding the upcoming class, we want to silence it:
  290 +// warning: base class 'class std::enable_shared_from_this<cxxopts::Value>' has accessible non-virtual destructor
  291 +#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
  292 +#pragma GCC diagnostic push
  293 +// This will be ignored under other compilers like LLVM clang.
  294 +#endif
288 class Value : public std::enable_shared_from_this<Value> 295 class Value : public std::enable_shared_from_this<Value>
289 { 296 {
290 public: 297 public:
@@ -328,7 +335,9 @@ namespace cxxopts @@ -328,7 +335,9 @@ namespace cxxopts
328 virtual bool 335 virtual bool
329 is_boolean() const = 0; 336 is_boolean() const = 0;
330 }; 337 };
331 - 338 +#if defined(__GNUC__)
  339 +#pragma GCC diagnostic pop
  340 +#endif
332 class OptionException : public std::exception 341 class OptionException : public std::exception
333 { 342 {
334 public: 343 public:
@@ -822,6 +831,8 @@ namespace cxxopts @@ -822,6 +831,8 @@ namespace cxxopts
822 831
823 ~abstract_value() override = default; 832 ~abstract_value() override = default;
824 833
  834 + abstract_value& operator=(const abstract_value&) = default;
  835 +
825 abstract_value(const abstract_value& rhs) 836 abstract_value(const abstract_value& rhs)
826 { 837 {
827 if (rhs.m_result) 838 if (rhs.m_result)
@@ -922,14 +933,14 @@ namespace cxxopts @@ -922,14 +933,14 @@ namespace cxxopts
922 } 933 }
923 934
924 protected: 935 protected:
925 - std::shared_ptr<T> m_result;  
926 - T* m_store; 936 + std::shared_ptr<T> m_result{};
  937 + T* m_store{};
927 938
928 bool m_default = false; 939 bool m_default = false;
929 bool m_implicit = false; 940 bool m_implicit = false;
930 941
931 - std::string m_default_value;  
932 - std::string m_implicit_value; 942 + std::string m_default_value{};
  943 + std::string m_implicit_value{};
933 }; 944 };
934 945
935 template <typename T> 946 template <typename T>
@@ -1067,13 +1078,13 @@ namespace cxxopts @@ -1067,13 +1078,13 @@ namespace cxxopts
1067 } 1078 }
1068 1079
1069 private: 1080 private:
1070 - std::string m_short;  
1071 - std::string m_long;  
1072 - String m_desc;  
1073 - std::shared_ptr<const Value> m_value; 1081 + std::string m_short{};
  1082 + std::string m_long{};
  1083 + String m_desc{};
  1084 + std::shared_ptr<const Value> m_value{};
1074 int m_count; 1085 int m_count;
1075 1086
1076 - size_t m_hash; 1087 + size_t m_hash{};
1077 }; 1088 };
1078 1089
1079 struct HelpOptionDetails 1090 struct HelpOptionDetails
@@ -1092,9 +1103,9 @@ namespace cxxopts @@ -1092,9 +1103,9 @@ namespace cxxopts
1092 1103
1093 struct HelpGroupDetails 1104 struct HelpGroupDetails
1094 { 1105 {
1095 - std::string name;  
1096 - std::string description;  
1097 - std::vector<HelpOptionDetails> options; 1106 + std::string name{};
  1107 + std::string description{};
  1108 + std::vector<HelpOptionDetails> options{};
1098 }; 1109 };
1099 1110
1100 class OptionValue 1111 class OptionValue
@@ -1163,10 +1174,11 @@ namespace cxxopts @@ -1163,10 +1174,11 @@ namespace cxxopts
1163 } 1174 }
1164 } 1175 }
1165 1176
  1177 +
1166 const std::string* m_long_name = nullptr; 1178 const std::string* m_long_name = nullptr;
1167 // Holding this pointer is safe, since OptionValue's only exist in key-value pairs, 1179 // Holding this pointer is safe, since OptionValue's only exist in key-value pairs,
1168 // where the key has the string we point to. 1180 // where the key has the string we point to.
1169 - std::shared_ptr<Value> m_value; 1181 + std::shared_ptr<Value> m_value{};
1170 size_t m_count = 0; 1182 size_t m_count = 0;
1171 bool m_default = false; 1183 bool m_default = false;
1172 }; 1184 };
@@ -1282,10 +1294,10 @@ namespace cxxopts @@ -1282,10 +1294,10 @@ namespace cxxopts
1282 } 1294 }
1283 1295
1284 private: 1296 private:
1285 - NameHashMap m_keys;  
1286 - ParsedHashMap m_values;  
1287 - std::vector<KeyValue> m_sequential;  
1288 - std::vector<std::string> m_unmatched; 1297 + NameHashMap m_keys{};
  1298 + ParsedHashMap m_values{};
  1299 + std::vector<KeyValue> m_sequential{};
  1300 + std::vector<std::string> m_unmatched{};
1289 }; 1301 };
1290 1302
1291 struct Option 1303 struct Option
@@ -1361,11 +1373,11 @@ namespace cxxopts @@ -1361,11 +1373,11 @@ namespace cxxopts
1361 const OptionMap& m_options; 1373 const OptionMap& m_options;
1362 const PositionalList& m_positional; 1374 const PositionalList& m_positional;
1363 1375
1364 - std::vector<KeyValue> m_sequential; 1376 + std::vector<KeyValue> m_sequential{};
1365 bool m_allow_unrecognised; 1377 bool m_allow_unrecognised;
1366 1378
1367 - ParsedHashMap m_parsed;  
1368 - NameHashMap m_keys; 1379 + ParsedHashMap m_parsed{};
  1380 + NameHashMap m_keys{};
1369 }; 1381 };
1370 1382
1371 class Options 1383 class Options
@@ -1489,22 +1501,22 @@ namespace cxxopts @@ -1489,22 +1501,22 @@ namespace cxxopts
1489 void 1501 void
1490 generate_all_groups_help(String& result) const; 1502 generate_all_groups_help(String& result) const;
1491 1503
1492 - std::string m_program;  
1493 - String m_help_string;  
1494 - std::string m_custom_help;  
1495 - std::string m_positional_help; 1504 + std::string m_program{};
  1505 + String m_help_string{};
  1506 + std::string m_custom_help{};
  1507 + std::string m_positional_help{};
1496 bool m_show_positional; 1508 bool m_show_positional;
1497 bool m_allow_unrecognised; 1509 bool m_allow_unrecognised;
1498 1510
1499 std::shared_ptr<OptionMap> m_options; 1511 std::shared_ptr<OptionMap> m_options;
1500 - std::vector<std::string> m_positional;  
1501 - std::unordered_set<std::string> m_positional_set; 1512 + std::vector<std::string> m_positional{};
  1513 + std::unordered_set<std::string> m_positional_set{};
1502 1514
1503 //mapping from groups to help options 1515 //mapping from groups to help options
1504 - std::map<std::string, HelpGroupDetails> m_help; 1516 + std::map<std::string, HelpGroupDetails> m_help{};
1505 1517
1506 - std::list<OptionDetails> m_option_list;  
1507 - std::unordered_map<std::string, decltype(m_option_list)::iterator> m_option_map; 1518 + std::list<OptionDetails> m_option_list{};
  1519 + std::unordered_map<std::string, decltype(m_option_list)::iterator> m_option_map{};
1508 }; 1520 };
1509 1521
1510 class OptionAdder 1522 class OptionAdder
test/options.cpp
@@ -36,7 +36,7 @@ class Argv { @@ -36,7 +36,7 @@ class Argv {
36 36
37 private: 37 private:
38 38
39 - std::vector<std::unique_ptr<char[]>> m_args; 39 + std::vector<std::unique_ptr<char[]>> m_args{};
40 std::unique_ptr<const char*[]> m_argv; 40 std::unique_ptr<const char*[]> m_argv;
41 int m_argc; 41 int m_argc;
42 }; 42 };