diff --git a/CMakeLists.txt b/CMakeLists.txt index 3e9b862..3098445 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,7 +50,7 @@ set(CMAKE_CXX_EXTENSIONS OFF) if(MSVC) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W2") elseif(CMAKE_CXX_COMPILER_ID MATCHES "[Cc]lang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -Wextra -Wshadow") + 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") endif() add_library(cxxopts INTERFACE) diff --git a/include/cxxopts.hpp b/include/cxxopts.hpp index cc43fa7..88e8a02 100644 --- a/include/cxxopts.hpp +++ b/include/cxxopts.hpp @@ -147,9 +147,9 @@ namespace cxxopts inline String& - stringAppend(String& s, int n, UChar32 c) + stringAppend(String& s, size_t n, UChar32 c) { - for (int i = 0; i != n; ++i) + for (size_t i = 0; i != n; ++i) { s.append(c); } @@ -285,6 +285,13 @@ namespace cxxopts #endif } // namespace +#if defined(__GNUC__) +// GNU GCC with -Weffc++ will issue a warning regarding the upcoming class, we want to silence it: +// warning: base class 'class std::enable_shared_from_this' has accessible non-virtual destructor +#pragma GCC diagnostic ignored "-Wnon-virtual-dtor" +#pragma GCC diagnostic push +// This will be ignored under other compilers like LLVM clang. +#endif class Value : public std::enable_shared_from_this { public: @@ -328,7 +335,9 @@ namespace cxxopts virtual bool is_boolean() const = 0; }; - +#if defined(__GNUC__) +#pragma GCC diagnostic pop +#endif class OptionException : public std::exception { public: @@ -822,6 +831,8 @@ namespace cxxopts ~abstract_value() override = default; + abstract_value& operator=(const abstract_value&) = default; + abstract_value(const abstract_value& rhs) { if (rhs.m_result) @@ -922,14 +933,14 @@ namespace cxxopts } protected: - std::shared_ptr m_result; - T* m_store; + std::shared_ptr m_result{}; + T* m_store{}; bool m_default = false; bool m_implicit = false; - std::string m_default_value; - std::string m_implicit_value; + std::string m_default_value{}; + std::string m_implicit_value{}; }; template @@ -1067,13 +1078,13 @@ namespace cxxopts } private: - std::string m_short; - std::string m_long; - String m_desc; - std::shared_ptr m_value; + std::string m_short{}; + std::string m_long{}; + String m_desc{}; + std::shared_ptr m_value{}; int m_count; - size_t m_hash; + size_t m_hash{}; }; struct HelpOptionDetails @@ -1092,9 +1103,9 @@ namespace cxxopts struct HelpGroupDetails { - std::string name; - std::string description; - std::vector options; + std::string name{}; + std::string description{}; + std::vector options{}; }; class OptionValue @@ -1163,10 +1174,11 @@ namespace cxxopts } } + const std::string* m_long_name = nullptr; // Holding this pointer is safe, since OptionValue's only exist in key-value pairs, // where the key has the string we point to. - std::shared_ptr m_value; + std::shared_ptr m_value{}; size_t m_count = 0; bool m_default = false; }; @@ -1282,10 +1294,10 @@ namespace cxxopts } private: - NameHashMap m_keys; - ParsedHashMap m_values; - std::vector m_sequential; - std::vector m_unmatched; + NameHashMap m_keys{}; + ParsedHashMap m_values{}; + std::vector m_sequential{}; + std::vector m_unmatched{}; }; struct Option @@ -1361,11 +1373,11 @@ namespace cxxopts const OptionMap& m_options; const PositionalList& m_positional; - std::vector m_sequential; + std::vector m_sequential{}; bool m_allow_unrecognised; - ParsedHashMap m_parsed; - NameHashMap m_keys; + ParsedHashMap m_parsed{}; + NameHashMap m_keys{}; }; class Options @@ -1489,22 +1501,22 @@ namespace cxxopts void generate_all_groups_help(String& result) const; - std::string m_program; - String m_help_string; - std::string m_custom_help; - std::string m_positional_help; + std::string m_program{}; + String m_help_string{}; + std::string m_custom_help{}; + std::string m_positional_help{}; bool m_show_positional; bool m_allow_unrecognised; std::shared_ptr m_options; - std::vector m_positional; - std::unordered_set m_positional_set; + std::vector m_positional{}; + std::unordered_set m_positional_set{}; //mapping from groups to help options - std::map m_help; + std::map m_help{}; - std::list m_option_list; - std::unordered_map m_option_map; + std::list m_option_list{}; + std::unordered_map m_option_map{}; }; class OptionAdder diff --git a/test/options.cpp b/test/options.cpp index 61678a6..8bc6f49 100644 --- a/test/options.cpp +++ b/test/options.cpp @@ -36,7 +36,7 @@ class Argv { private: - std::vector> m_args; + std::vector> m_args{}; std::unique_ptr m_argv; int m_argc; };