From 5a186104f4b9b5f223d4fc997c92f856539df276 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Thu, 3 Jul 2014 00:59:35 +0800 Subject: [PATCH] Fixes warnings --- build/premake4.lua | 8 ++++--- example/serialize/serialize.cpp | 13 ++++++++++-- include/rapidjson/document.h | 33 ++++++++++++++++++----------- include/rapidjson/encodedstream.h | 19 +++++++++++++++-- include/rapidjson/encodings.h | 9 ++++++++ include/rapidjson/filereadstream.h | 5 ----- include/rapidjson/filestream.h | 4 ++++ include/rapidjson/filewritestream.h | 4 ++++ include/rapidjson/internal/stack.h | 4 ++++ include/rapidjson/prettywriter.h | 14 ++++++++++++ include/rapidjson/writer.h | 5 +++-- test/perftest/perftest.h | 22 +++++++++++++++++++ test/perftest/rapidjsontest.cpp | 15 +++++++++++++ test/unittest/encodedstreamtest.cpp | 8 ++++++- test/unittest/filestreamtest.cpp | 8 +++++++ test/unittest/readertest.cpp | 25 ++++++++++++++++------ test/unittest/unittest.h | 15 +++++++++++++ 17 files changed, 178 insertions(+), 33 deletions(-) diff --git a/build/premake4.lua b/build/premake4.lua index 05650d8..444337c 100644 --- a/build/premake4.lua +++ b/build/premake4.lua @@ -64,7 +64,7 @@ solution "test" defines { "_CRT_SECURE_NO_WARNINGS" } configuration "gmake" - buildoptions "-msse4.2 -Werror=cast-qual" + buildoptions "-msse4.2 -Werror -Wall -Wextra" project "gtest" kind "StaticLib" @@ -86,6 +86,8 @@ solution "test" project "unittest" kind "ConsoleApp" + buildoptions "-Weffc++" + files { "../include/**.h", "../test/unittest/**.cpp", @@ -149,8 +151,8 @@ solution "example" configuration "vs*" defines { "_CRT_SECURE_NO_WARNINGS" } --- configuration "gmake" --- buildoptions "-Weverything" + configuration "gmake" + buildoptions "-Werror -Wall -Wextra -Weffc++" project "condense" kind "ConsoleApp" diff --git a/example/serialize/serialize.cpp b/example/serialize/serialize.cpp index efee58f..6dfe2d4 100644 --- a/example/serialize/serialize.cpp +++ b/example/serialize/serialize.cpp @@ -58,9 +58,17 @@ private: class Dependent : public Person { public: Dependent(const std::string& name, unsigned age, Education* education = 0) : Person(name, age), education_(education) {} - Dependent(const Dependent& rhs) : Person(rhs) { education_ = (rhs.education_ == 0) ? 0 : new Education(*rhs.education_); } + Dependent(const Dependent& rhs) : Person(rhs), education_(0) { education_ = (rhs.education_ == 0) ? 0 : new Education(*rhs.education_); } virtual ~Dependent(); + Dependent& operator=(const Dependent& rhs) { + if (this == &rhs) + return *this; + delete education_; + education_ = (rhs.education_ == 0) ? 0 : new Education(*rhs.education_); + return *this; + } + template void Serialize(Writer& writer) const { writer.StartObject(); @@ -77,6 +85,7 @@ public: } private: + Education *education_; }; @@ -86,7 +95,7 @@ Dependent::~Dependent() { class Employee : public Person { public: - Employee(const std::string& name, unsigned age, bool married) : Person(name, age), married_(married) {} + Employee(const std::string& name, unsigned age, bool married) : Person(name, age), dependents_(), married_(married) {} virtual ~Employee(); void AddDependent(const Dependent& dependent) { diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index a95d57f..ae13c28 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -10,6 +10,11 @@ #pragma warning(disable : 4127) // conditional expression is constant #endif +#ifdef __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Weffc++" +#endif + namespace rapidjson { // Forward declaration. @@ -59,7 +64,7 @@ public: //@{ //! Default constructor creates a null value. - GenericValue() : flags_(kNullFlag) {} + GenericValue() : data_(), flags_(kNullFlag) {} //! Copy constructor is not permitted. private: @@ -72,7 +77,7 @@ public: \param type Type of the value. \note Default content for number is zero. */ - GenericValue(Type type) : data_() { + GenericValue(Type type) : data_(), flags_() { static const unsigned defaultFlags[7] = { kNullFlag, kFalseFlag, kTrueFlag, kObjectFlag, kArrayFlag, kConstStringFlag, kNumberAnyFlag @@ -92,24 +97,24 @@ public: GenericValue(const GenericValue& rhs, Allocator & allocator); //! Constructor for boolean value. - explicit GenericValue(bool b) : flags_(b ? kTrueFlag : kFalseFlag) {} + explicit GenericValue(bool b) : data_(), flags_(b ? kTrueFlag : kFalseFlag) {} //! Constructor for int value. - explicit GenericValue(int i) : flags_(kNumberIntFlag) { + explicit GenericValue(int i) : data_(), flags_(kNumberIntFlag) { data_.n.i64 = i; if (i >= 0) flags_ |= kUintFlag | kUint64Flag; } //! Constructor for unsigned value. - explicit GenericValue(unsigned u) : flags_(kNumberUintFlag) { + explicit GenericValue(unsigned u) : data_(), flags_(kNumberUintFlag) { data_.n.u64 = u; if (!(u & 0x80000000)) flags_ |= kIntFlag | kInt64Flag; } //! Constructor for int64_t value. - explicit GenericValue(int64_t i64) : flags_(kNumberInt64Flag) { + explicit GenericValue(int64_t i64) : data_(), flags_(kNumberInt64Flag) { data_.n.i64 = i64; if (i64 >= 0) { flags_ |= kNumberUint64Flag; @@ -123,7 +128,7 @@ public: } //! Constructor for uint64_t value. - explicit GenericValue(uint64_t u64) : flags_(kNumberUint64Flag) { + explicit GenericValue(uint64_t u64) : data_(), flags_(kNumberUint64Flag) { data_.n.u64 = u64; if (!(u64 & UINT64_C(0x8000000000000000))) flags_ |= kInt64Flag; @@ -134,10 +139,10 @@ public: } //! Constructor for double value. - explicit GenericValue(double d) : flags_(kNumberDoubleFlag) { data_.n.d = d; } + explicit GenericValue(double d) : data_(), flags_(kNumberDoubleFlag) { data_.n.d = d; } //! Constructor for constant string (i.e. do not make a copy of string) - GenericValue(const Ch* s, SizeType length) { + GenericValue(const Ch* s, SizeType length) : data_(), flags_() { RAPIDJSON_ASSERT(s != NULL); flags_ = kConstStringFlag; data_.s.str = s; @@ -145,13 +150,13 @@ public: } //! Constructor for constant string (i.e. do not make a copy of string) - explicit GenericValue(const Ch* s) { SetStringRaw(s, internal::StrLen(s)); } + explicit GenericValue(const Ch* s) : data_(), flags_() { SetStringRaw(s, internal::StrLen(s)); } //! Constructor for copy-string (i.e. do make a copy of string) - GenericValue(const Ch* s, SizeType length, Allocator& allocator) { SetStringRaw(s, length, allocator); } + GenericValue(const Ch* s, SizeType length, Allocator& allocator) : data_(), flags_() { SetStringRaw(s, length, allocator); } //! Constructor for copy-string (i.e. do make a copy of string) - GenericValue(const Ch*s, Allocator& allocator) { SetStringRaw(s, internal::StrLen(s), allocator); } + GenericValue(const Ch*s, Allocator& allocator) : data_(), flags_() { SetStringRaw(s, internal::StrLen(s), allocator); } //! Destructor. /*! Need to destruct elements of array, members of object, or copy-string. @@ -962,4 +967,8 @@ GenericValue::GenericValue(const GenericValue { } // namespace rapidjson +#ifdef __GNUC__ +#pragma GCC diagnostic pop +#endif + #endif // RAPIDJSON_ENCODINGS_H_ diff --git a/include/rapidjson/filereadstream.h b/include/rapidjson/filereadstream.h index 29ab393..4d42d38 100644 --- a/include/rapidjson/filereadstream.h +++ b/include/rapidjson/filereadstream.h @@ -69,11 +69,6 @@ private: bool eof_; }; -template<> -struct StreamTraits { - typedef FileReadStream StreamCopyType; // Enable stream copy optimization. -}; - } // namespace rapidjson #endif // RAPIDJSON_FILESTREAM_H_ diff --git a/include/rapidjson/filestream.h b/include/rapidjson/filestream.h index 985265d..7e1e3b1 100644 --- a/include/rapidjson/filestream.h +++ b/include/rapidjson/filestream.h @@ -28,6 +28,10 @@ public: size_t PutEnd(char*) { return 0; } private: + // Prohibit copy constructor & assignment operator. + FileStream(const FileStream&); + FileStream& operator=(const FileStream&); + void Read() { RAPIDJSON_ASSERT(fp_ != 0); int c = fgetc(fp_); diff --git a/include/rapidjson/filewritestream.h b/include/rapidjson/filewritestream.h index 9f0ce65..4128311 100644 --- a/include/rapidjson/filewritestream.h +++ b/include/rapidjson/filewritestream.h @@ -56,6 +56,10 @@ public: size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; } private: + // Prohibit copy constructor & assignment operator. + FileWriteStream(const FileWriteStream&); + FileWriteStream& operator=(const FileWriteStream&); + FILE* fp_; char *buffer_; char *bufferEnd_; diff --git a/include/rapidjson/internal/stack.h b/include/rapidjson/internal/stack.h index c1a8bed..ff5ff82 100644 --- a/include/rapidjson/internal/stack.h +++ b/include/rapidjson/internal/stack.h @@ -69,6 +69,10 @@ public: size_t GetCapacity() const { return stack_capacity_; } private: + // Prohibit copy constructor & assignment operator. + Stack(const Stack&); + Stack& operator=(const Stack&); + Allocator* allocator_; Allocator* own_allocator_; char *stack_; diff --git a/include/rapidjson/prettywriter.h b/include/rapidjson/prettywriter.h index 5b154ad..cbbcdab 100644 --- a/include/rapidjson/prettywriter.h +++ b/include/rapidjson/prettywriter.h @@ -3,6 +3,11 @@ #include "writer.h" +#ifdef __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Weffc++" +#endif + namespace rapidjson { //! Writer with indentation and spacing. @@ -162,8 +167,17 @@ protected: Ch indentChar_; unsigned indentCharCount_; + +private: + // Prohibit copy constructor & assignment operator. + PrettyWriter(const PrettyWriter&); + PrettyWriter& operator=(const PrettyWriter&); }; } // namespace rapidjson +#ifdef __GNUC__ +#pragma GCC diagnostic pop +#endif + #endif // RAPIDJSON_RAPIDJSON_H_ diff --git a/include/rapidjson/writer.h b/include/rapidjson/writer.h index f3b088c..95c550c 100644 --- a/include/rapidjson/writer.h +++ b/include/rapidjson/writer.h @@ -284,8 +284,9 @@ protected: static const int kDefaultDoublePrecision = 6; private: - // Prohibit assignment for VC C4512 warning - Writer& operator=(const Writer& w); + // Prohibit copy constructor & assignment operator. + Writer(const Writer&); + Writer& operator=(const Writer&); }; } // namespace rapidjson diff --git a/test/perftest/perftest.h b/test/perftest/perftest.h index 57640d2..d142bee 100644 --- a/test/perftest/perftest.h +++ b/test/perftest/perftest.h @@ -24,8 +24,22 @@ #ifdef __cplusplus +// gtest indirectly included inttypes.h, without __STDC_CONSTANT_MACROS. +#ifndef __STDC_CONSTANT_MACROS +# define __STDC_CONSTANT_MACROS 1 // required by C++ standard +#endif + +#ifdef __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Weffc++" +#endif + #include "gtest/gtest.h" +#ifdef __GNUC__ +#pragma GCC diagnostic pop +#endif + #ifdef _MSC_VER #define _CRTDBG_MAP_ALLOC #include @@ -35,6 +49,8 @@ //! Base class for all performance tests class PerfTest : public ::testing::Test { public: + PerfTest() : filename_(), json_(), length_(), whitespace_(), whitespace_length_() {} + virtual void SetUp() { FILE *fp = fopen(filename_ = "data/sample.json", "rb"); if (!fp) @@ -68,8 +84,14 @@ public: virtual void TearDown() { free(json_); free(whitespace_); + json_ = 0; + whitespace_ = 0; } +private: + PerfTest(const PerfTest&); + PerfTest& operator=(const PerfTest&); + protected: const char* filename_; char *json_; diff --git a/test/perftest/rapidjsontest.cpp b/test/perftest/rapidjsontest.cpp index db54d9b..df8a0f8 100644 --- a/test/perftest/rapidjsontest.cpp +++ b/test/perftest/rapidjsontest.cpp @@ -21,6 +21,8 @@ using namespace rapidjson; class RapidJson : public PerfTest { public: + RapidJson() : temp_(), doc_() {} + virtual void SetUp() { PerfTest::SetUp(); @@ -36,6 +38,10 @@ public: free(temp_); } +private: + RapidJson(const RapidJson&); + RapidJson& operator=(const RapidJson&); + protected: char *temp_; Document doc_; @@ -165,6 +171,11 @@ TEST_F(RapidJson, DocumentTraverse) { } } +#ifdef __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Weffc++" +#endif + struct ValueCounter : public BaseReaderHandler<> { ValueCounter() : count_(1) {} // root @@ -174,6 +185,10 @@ struct ValueCounter : public BaseReaderHandler<> { SizeType count_; }; +#ifdef __GNUC__ +#pragma GCC diagnostic pop +#endif + TEST_F(RapidJson, DocumentAccept) { for (size_t i = 0; i < kTrialCount; i++) { ValueCounter counter; diff --git a/test/unittest/encodedstreamtest.cpp b/test/unittest/encodedstreamtest.cpp index 7f12206..26bfc32 100644 --- a/test/unittest/encodedstreamtest.cpp +++ b/test/unittest/encodedstreamtest.cpp @@ -8,14 +8,21 @@ using namespace rapidjson; class EncodedStreamTest : public ::testing::Test { public: + EncodedStreamTest() : json_(), length_() {} + virtual void SetUp() { json_ = ReadFile("utf8.json", true, &length_); } virtual void TearDown() { free(json_); + json_ = 0; } +private: + EncodedStreamTest(const EncodedStreamTest&); + EncodedStreamTest& operator=(const EncodedStreamTest&); + protected: static FILE* Open(const char* filename) { char buffer[1024]; @@ -131,7 +138,6 @@ protected: remove(filename); } - const char* filename_; char *json_; size_t length_; }; diff --git a/test/unittest/filestreamtest.cpp b/test/unittest/filestreamtest.cpp index 0230e4c..c653537 100644 --- a/test/unittest/filestreamtest.cpp +++ b/test/unittest/filestreamtest.cpp @@ -7,6 +7,9 @@ using namespace rapidjson; class FileStreamTest : public ::testing::Test { +public: + FileStreamTest() : filename_(), json_(), length_() {} + virtual void SetUp() { FILE *fp = fopen(filename_ = "data/sample.json", "rb"); if (!fp) @@ -24,8 +27,13 @@ class FileStreamTest : public ::testing::Test { virtual void TearDown() { free(json_); + json_ = 0; } +private: + FileStreamTest(const FileStreamTest&); + FileStreamTest& operator=(const FileStreamTest&); + protected: const char* filename_; char *json_; diff --git a/test/unittest/readertest.cpp b/test/unittest/readertest.cpp index 362cde1..2afa231 100644 --- a/test/unittest/readertest.cpp +++ b/test/unittest/readertest.cpp @@ -5,6 +5,11 @@ using namespace rapidjson; +#ifdef __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Weffc++" +#endif + template struct ParseBoolHandler : BaseReaderHandler<> { ParseBoolHandler() : step_(0) {} @@ -33,7 +38,7 @@ TEST(Reader, ParseFalse) { } struct ParseIntHandler : BaseReaderHandler<> { - ParseIntHandler() : step_(0) {} + ParseIntHandler() : step_(0), actual_() {} void Default() { FAIL(); } void Int(int i) { actual_ = i; step_++; } @@ -42,7 +47,7 @@ struct ParseIntHandler : BaseReaderHandler<> { }; struct ParseUintHandler : BaseReaderHandler<> { - ParseUintHandler() : step_(0) {} + ParseUintHandler() : step_(0), actual_() {} void Default() { FAIL(); } void Uint(unsigned i) { actual_ = i; step_++; } @@ -51,7 +56,7 @@ struct ParseUintHandler : BaseReaderHandler<> { }; struct ParseInt64Handler : BaseReaderHandler<> { - ParseInt64Handler() : step_(0) {} + ParseInt64Handler() : step_(0), actual_() {} void Default() { FAIL(); } void Int64(int64_t i) { actual_ = i; step_++; } @@ -60,7 +65,7 @@ struct ParseInt64Handler : BaseReaderHandler<> { }; struct ParseUint64Handler : BaseReaderHandler<> { - ParseUint64Handler() : step_(0) {} + ParseUint64Handler() : step_(0), actual_() {} void Default() { FAIL(); } void Uint64(uint64_t i) { actual_ = i; step_++; } @@ -69,7 +74,7 @@ struct ParseUint64Handler : BaseReaderHandler<> { }; struct ParseDoubleHandler : BaseReaderHandler<> { - ParseDoubleHandler() : step_(0) {} + ParseDoubleHandler() : step_(0), actual_() {} void Default() { FAIL(); } void Double(double d) { actual_ = d; step_++; } @@ -183,8 +188,12 @@ TEST(Reader, ParseNumber_Error) { template struct ParseStringHandler : BaseReaderHandler { - ParseStringHandler() : str_(0), length_(0) {} + ParseStringHandler() : str_(0), length_(0), copy_() {} ~ParseStringHandler() { EXPECT_TRUE(str_ != 0); if (copy_) free(const_cast(str_)); } + + ParseStringHandler(const ParseStringHandler&); + ParseStringHandler& operator=(const ParseStringHandler&); + void Default() { FAIL(); } void String(const typename Encoding::Ch* str, size_t length, bool copy) { EXPECT_EQ(0, str_); @@ -651,3 +660,7 @@ TEST(Reader, CustomStringStream) { reader.ParseObject<0>(s, h); EXPECT_EQ(20u, h.step_); } + +#ifdef __GNUC__ +#pragma GCC diagnostic pop +#endif diff --git a/test/unittest/unittest.h b/test/unittest/unittest.h index c954e55..1f44526 100644 --- a/test/unittest/unittest.h +++ b/test/unittest/unittest.h @@ -1,14 +1,29 @@ #ifndef UNITTEST_H_ #define UNITTEST_H_ + +// gtest indirectly included inttypes.h, without __STDC_CONSTANT_MACROS. +#ifndef __STDC_CONSTANT_MACROS +# define __STDC_CONSTANT_MACROS 1 // required by C++ standard +#endif + #ifdef _MSC_VER #define _CRTDBG_MAP_ALLOC #include #pragma warning(disable : 4996) // 'function': was declared deprecated #endif +#ifdef __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Weffc++" +#endif + #include "gtest/gtest.h" +#ifdef __GNUC__ +#pragma GCC diagnostic pop +#endif + template inline size_t StrLen(const Ch* s) { const Ch* p = s;