Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix C++11 warnings and make travis to compile with/without C++11 #515

Merged
merged 6 commits into from
Jan 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ compiler:

env:
matrix:
- CONF=debug ARCH=x86_64
- CONF=release ARCH=x86_64
- CONF=debug ARCH=x86
- CONF=release ARCH=x86
- CONF=debug ARCH=x86_64 CXX11=ON
- CONF=release ARCH=x86_64 CXX11=ON
- CONF=debug ARCH=x86 CXX11=ON
- CONF=release ARCH=x86 CXX11=ON
- CONF=debug ARCH=x86_64 CXX11=OFF
- CONF=debug ARCH=x86 CXX11=OFF
global:
- ARCH_FLAGS_x86='-m32' # #266: don't use SSE on 32-bit
- ARCH_FLAGS_x86_64='-msse4.2' # use SSE4.2 on 64-bit
Expand All @@ -34,6 +36,7 @@ before_script:
eval "ARCH_FLAGS=\${ARCH_FLAGS_${ARCH}}" ;
(cd build && cmake
-DRAPIDJSON_HAS_STDSTRING=ON
-DRAPIDJSON_BUILD_CXX11=$CXX11
-DCMAKE_VERBOSE_MAKEFILE=ON
-DCMAKE_BUILD_TYPE=$CONF
-DCMAKE_CXX_FLAGS="$ARCH_FLAGS $GCOV_FLAGS"
Expand Down
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,27 @@ option(RAPIDJSON_BUILD_TESTS "Build rapidjson perftests and unittests." ON)
option(RAPIDJSON_BUILD_THIRDPARTY_GTEST
"Use gtest installation in `thirdparty/gtest` by default if available" OFF)

option(RAPIDJSON_BUILD_CXX11 "Build rapidjson with C++11 (gcc/clang)" ON)

option(RAPIDJSON_HAS_STDSTRING "" OFF)
if(RAPIDJSON_HAS_STDSTRING)
add_definitions(-DRAPIDJSON_HAS_STDSTRING)
endif()

if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native -Wall -Wextra -Werror")
if (RAPIDJSON_BUILD_CXX11)
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.7.0")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
endif()
endif()
elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native -Wall -Wextra -Werror -Wno-missing-field-initializers")
if (RAPIDJSON_BUILD_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
endif()
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
add_definitions(-D_CRT_SECURE_NO_WARNINGS=1)
endif()
Expand Down
16 changes: 16 additions & 0 deletions example/serialize/serialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ using namespace rapidjson;
class Person {
public:
Person(const std::string& name, unsigned age) : name_(name), age_(age) {}
Person(const Person& rhs) : name_(rhs.name_), age_(rhs.age_) {}
virtual ~Person();

Person& operator=(const Person& rhs) {
name_ = rhs.name_;
age_ = rhs.age_;
return *this;
}

protected:
template <typename Writer>
void Serialize(Writer& writer) const {
Expand All @@ -38,6 +45,7 @@ Person::~Person() {
class Education {
public:
Education(const std::string& school, double GPA) : school_(school), GPA_(GPA) {}
Education(const Education& rhs) : school_(rhs.school_), GPA_(rhs.GPA_) {}

template <typename Writer>
void Serialize(Writer& writer) const {
Expand Down Expand Up @@ -102,8 +110,16 @@ Dependent::~Dependent() {
class Employee : public Person {
public:
Employee(const std::string& name, unsigned age, bool married) : Person(name, age), dependents_(), married_(married) {}
Employee(const Employee& rhs) : Person(rhs), dependents_(rhs.dependents_), married_(rhs.married_) {}
virtual ~Employee();

Employee& operator=(const Employee& rhs) {
static_cast<Person&>(*this) = rhs;
dependents_ = rhs.dependents_;
married_ = rhs.married_;
return *this;
}

void AddDependent(const Dependent& dependent) {
dependents_.push_back(dependent);
}
Expand Down
4 changes: 2 additions & 2 deletions include/rapidjson/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant
RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(padded)
RAPIDJSON_DIAG_OFF(switch-enum)
RAPIDJSON_DIAG_OFF(c++98-compat)
#endif

#ifdef __GNUC__
Expand Down Expand Up @@ -141,6 +142,7 @@ class GenericMemberIterator
Otherwise, the copy constructor is implicitly defined.
*/
GenericMemberIterator(const NonConstIterator & it) : ptr_(it.ptr_) {}
Iterator& operator=(const NonConstIterator & it) { ptr_ = it.ptr_; return *this; }

//! @name stepping
//@{
Expand Down Expand Up @@ -314,8 +316,6 @@ struct GenericStringRef {
const SizeType length; //!< length of the string (excluding the trailing NULL terminator)

private:
//! Disallow copy-assignment
GenericStringRef operator=(const GenericStringRef&);
//! Disallow construction from non-const array
template<SizeType N>
GenericStringRef(CharType (&str)[N]) /* = delete */;
Expand Down
9 changes: 9 additions & 0 deletions include/rapidjson/internal/stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
#include "../rapidjson.h"
#include "swap.h"

#if defined(__clang__)
RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(c++98-compat)
#endif

RAPIDJSON_NAMESPACE_BEGIN
namespace internal {

Expand Down Expand Up @@ -203,4 +208,8 @@ class Stack {
} // namespace internal
RAPIDJSON_NAMESPACE_END

#if defined(__clang__)
RAPIDJSON_DIAG_POP
#endif

#endif // RAPIDJSON_STACK_H_
9 changes: 9 additions & 0 deletions include/rapidjson/internal/swap.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@

#include "../rapidjson.h"

#if defined(__clang__)
RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(c++98-compat)
#endif

RAPIDJSON_NAMESPACE_BEGIN
namespace internal {

Expand All @@ -34,4 +39,8 @@ inline void Swap(T& a, T& b) RAPIDJSON_NOEXCEPT {
} // namespace internal
RAPIDJSON_NAMESPACE_END

#if defined(__clang__)
RAPIDJSON_DIAG_POP
#endif

#endif // RAPIDJSON_INTERNAL_SWAP_H_
9 changes: 9 additions & 0 deletions include/rapidjson/stringbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@

#include "internal/stack.h"

#if defined(__clang__)
RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(c++98-compat)
#endif

RAPIDJSON_NAMESPACE_BEGIN

//! Represents an in-memory output stream.
Expand Down Expand Up @@ -103,4 +108,8 @@ inline void PutN(GenericStringBuffer<UTF8<> >& stream, char c, size_t n) {

RAPIDJSON_NAMESPACE_END

#if defined(__clang__)
RAPIDJSON_DIAG_POP
#endif

#endif // RAPIDJSON_STRINGBUFFER_H_
14 changes: 14 additions & 0 deletions test/unittest/documenttest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
#include <sstream>
#include <algorithm>

#ifdef __clang__
RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(c++98-compat)
RAPIDJSON_DIAG_OFF(missing-variable-declarations)
#endif

using namespace rapidjson;

template <typename DocumentType>
Expand Down Expand Up @@ -328,6 +334,8 @@ TEST(Document, UTF16_Document) {

#if RAPIDJSON_HAS_CXX11_RVALUE_REFS

#if 0 // Many old compiler does not support these. Turn it off temporaily.

#include <type_traits>

TEST(Document, Traits) {
Expand Down Expand Up @@ -365,6 +373,8 @@ TEST(Document, Traits) {
#endif
}

#endif

template <typename Allocator>
struct DocumentMove: public ::testing::Test {
};
Expand Down Expand Up @@ -573,3 +583,7 @@ TYPED_TEST(DocumentMove, MoveAssignmentStack) {
// Document d2;
// d1 = d2;
//}

#ifdef __clang__
RAPIDJSON_DIAG_POP
#endif
1 change: 1 addition & 0 deletions test/unittest/readertest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ RAPIDJSON_DIAG_OFF(missing-noreturn)
#ifdef __clang__
RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(variadic-macros)
RAPIDJSON_DIAG_OFF(c++98-compat-pedantic)
#endif

template<bool expect>
Expand Down
13 changes: 13 additions & 0 deletions test/unittest/stringbuffertest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"

#ifdef __clang__
RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(c++98-compat)
#endif

using namespace rapidjson;

TEST(StringBuffer, InitialSize) {
Expand Down Expand Up @@ -69,6 +74,8 @@ TEST(StringBuffer, Pop) {

#if RAPIDJSON_HAS_CXX11_RVALUE_REFS

#if 0 // Many old compiler does not support these. Turn it off temporaily.

#include <type_traits>

TEST(StringBuffer, Traits) {
Expand Down Expand Up @@ -106,6 +113,8 @@ TEST(StringBuffer, Traits) {
#endif
}

#endif

TEST(StringBuffer, MoveConstructor) {
StringBuffer x;
x.Put('A');
Expand Down Expand Up @@ -148,3 +157,7 @@ TEST(StringBuffer, MoveAssignment) {
}

#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS

#ifdef __clang__
RAPIDJSON_DIAG_POP
#endif
11 changes: 11 additions & 0 deletions test/unittest/unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,19 @@
#include "unittest.h"
#include "rapidjson/rapidjson.h"

#ifdef __clang__
#pragma GCC diagnostic push
#if __has_warning("-Wdeprecated")
#pragma GCC diagnostic ignored "-Wdeprecated"
#endif
#endif

AssertException::~AssertException() throw() {}

#ifdef __clang__
#pragma GCC diagnostic pop
#endif

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);

Expand Down
12 changes: 12 additions & 0 deletions test/unittest/unittest.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,24 @@ inline FILE* TempFile(char *filename) {
#pragma warning(disable : 4127)
#endif

#ifdef __clang__
#pragma GCC diagnostic push
#if __has_warning("-Wdeprecated")
#pragma GCC diagnostic ignored "-Wdeprecated"
#endif
#endif

class AssertException : public std::logic_error {
public:
AssertException(const char* w) : std::logic_error(w) {}
AssertException(const AssertException& rhs) : std::logic_error(rhs) {}
virtual ~AssertException() throw();
};

#ifdef __clang__
#pragma GCC diagnostic pop
#endif

#define RAPIDJSON_ASSERT(x) if (!(x)) throw AssertException(RAPIDJSON_STRINGIFY(x))

class Random {
Expand Down
51 changes: 42 additions & 9 deletions test/unittest/valuetest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
#include "rapidjson/document.h"
#include <algorithm>

#ifdef __clang__
RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(c++98-compat)
#endif

using namespace rapidjson;

TEST(Value, DefaultConstructor) {
Expand All @@ -34,6 +39,8 @@ TEST(Value, DefaultConstructor) {

#if RAPIDJSON_HAS_CXX11_RVALUE_REFS

#if 0 // Many old compiler does not support these. Turn it off temporaily.

#include <type_traits>

TEST(Value, Traits) {
Expand Down Expand Up @@ -72,6 +79,8 @@ TEST(Value, Traits) {
#endif
}

#endif

TEST(Value, MoveConstructor) {
typedef GenericValue<UTF8<>, CrtAllocator> Value;
Value::AllocatorType allocator;
Expand Down Expand Up @@ -764,15 +773,15 @@ TEST(Value, Array) {
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
// PushBack(GenericValue&&, Allocator&);
{
Value y(kArrayType);
y.PushBack(Value(true), allocator);
y.PushBack(std::move(Value(kArrayType).PushBack(Value(1), allocator).PushBack("foo", allocator)), allocator);
EXPECT_EQ(2u, y.Size());
EXPECT_TRUE(y[0].IsTrue());
EXPECT_TRUE(y[1].IsArray());
EXPECT_EQ(2u, y[1].Size());
EXPECT_TRUE(y[1][0].IsInt());
EXPECT_TRUE(y[1][1].IsString());
Value y2(kArrayType);
y2.PushBack(Value(true), allocator);
y2.PushBack(std::move(Value(kArrayType).PushBack(Value(1), allocator).PushBack("foo", allocator)), allocator);
EXPECT_EQ(2u, y2.Size());
EXPECT_TRUE(y2[0].IsTrue());
EXPECT_TRUE(y2[1].IsArray());
EXPECT_EQ(2u, y2[1].Size());
EXPECT_TRUE(y2[1][0].IsInt());
EXPECT_TRUE(y2[1][1].IsString());
}
#endif

Expand Down Expand Up @@ -1354,3 +1363,27 @@ TEST(Value, AcceptTerminationByHandler) {
TEST_TERMINATION(11, "{\"a\":[]}");
TEST_TERMINATION(12, "{\"a\":[]}");
}

struct ValueIntComparer {
bool operator()(const Value& lhs, const Value& rhs) const {
return lhs.GetInt() < rhs.GetInt();
}
};

#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
TEST(Value, Sorting) {
Value::AllocatorType allocator;
Value a(kArrayType);
a.PushBack(5, allocator);
a.PushBack(1, allocator);
a.PushBack(3, allocator);
std::sort(a.Begin(), a.End(), ValueIntComparer());
EXPECT_EQ(1, a[0].GetInt());
EXPECT_EQ(3, a[1].GetInt());
EXPECT_EQ(5, a[2].GetInt());
}
#endif

#ifdef __clang__
RAPIDJSON_DIAG_POP
#endif