Skip to content

Commit

Permalink
Add testing engine into test_cpp17
Browse files Browse the repository at this point in the history
  • Loading branch information
vglavnyy committed Dec 19, 2019
1 parent 4d32231 commit 472c255
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 26 deletions.
46 changes: 24 additions & 22 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ set(FlatBuffers_Tests_SRCS

set(FlatBuffers_Tests_CPP17_SRCS
${FlatBuffers_Library_SRCS}
tests/test_assert.h
tests/test_assert.cpp
tests/cpp17/test_cpp17.cpp
# file generate by running compiler on tests/monster_test.fbs
${CMAKE_CURRENT_BINARY_DIR}/tests/cpp17/generated_cpp17/monster_test_generated.h
Expand Down Expand Up @@ -268,23 +270,28 @@ if(FLATBUFFERS_CODE_COVERAGE)
endif()

function(add_fsanitize_to_target _target _sanitizer)
# FLATBUFFERS_CODE_SANITIZE: boolean {ON,OFF,YES,NO} or string with list of sanitizer.
# List of sanitizer is string starts with '=': "=address,undefined,thread,memory".
if((${CMAKE_CXX_COMPILER_ID} MATCHES "Clang") OR
((${CMAKE_CXX_COMPILER_ID} MATCHES "GNU") AND NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.9"))
)
set(_sanitizer_flags "=address,undefined")
if(_sanitizer MATCHES "=.*")
# override default by user-defined sanitizer list
set(_sanitizer_flags ${_sanitizer})
if(WIN32)
target_compile_definitions(${_target} PRIVATE FLATBUFFERS_MEMORY_LEAK_TRACKING)
message(STATUS "Sanitizer MSVC::_CrtDumpMemoryLeaks added to ${_target}")
else()
# FLATBUFFERS_CODE_SANITIZE: boolean {ON,OFF,YES,NO} or string with list of sanitizer.
# List of sanitizer is string starts with '=': "=address,undefined,thread,memory".
if((${CMAKE_CXX_COMPILER_ID} MATCHES "Clang") OR
((${CMAKE_CXX_COMPILER_ID} MATCHES "GNU") AND NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.9"))
)
set(_sanitizer_flags "=address,undefined")
if(_sanitizer MATCHES "=.*")
# override default by user-defined sanitizer list
set(_sanitizer_flags ${_sanitizer})
endif()
target_compile_options(${_target} PRIVATE
-g -fsigned-char -fno-omit-frame-pointer
"-fsanitize${_sanitizer_flags}")
target_link_libraries(${_target} PRIVATE
"-fsanitize${_sanitizer_flags}")
set_property(TARGET ${_target} PROPERTY POSITION_INDEPENDENT_CODE ON)
message(STATUS "Sanitizer ${_sanitizer_flags} added to ${_target}")
endif()
target_compile_options(${_target} PRIVATE
-g -fsigned-char -fno-omit-frame-pointer
"-fsanitize${_sanitizer_flags}")
target_link_libraries(${_target} PRIVATE
"-fsanitize${_sanitizer_flags}")
set_property(TARGET ${_target} PROPERTY POSITION_INDEPENDENT_CODE ON)
message(STATUS "Sanitizer ${_sanitizer_flags} added to ${_target}")
endif()
endfunction()

Expand Down Expand Up @@ -388,12 +395,7 @@ if(FLATBUFFERS_BUILD_TESTS)
PROPERTY COMPILE_DEFINITIONS FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
FLATBUFFERS_DEBUG_VERIFICATION_FAILURE=1)
if(FLATBUFFERS_CODE_SANITIZE)
if(WIN32)
target_compile_definitions(flattests PRIVATE FLATBUFFERS_MEMORY_LEAK_TRACKING)
message(STATUS "Sanitizer MSVC::_CrtDumpMemoryLeaks added to flattests")
else()
add_fsanitize_to_target(flattests ${FLATBUFFERS_CODE_SANITIZE})
endif()
endif()

compile_flatbuffers_schema_to_cpp(samples/monster.fbs)
Expand All @@ -410,7 +412,7 @@ if(FLATBUFFERS_BUILD_TESTS)
FLATBUFFERS_DEBUG_VERIFICATION_FAILURE=1
)

if(NOT WIN32 AND FLATBUFFERS_CODE_SANITIZE)
if(FLATBUFFERS_CODE_SANITIZE)
add_fsanitize_to_target(flattests_cpp17 ${FLATBUFFERS_CODE_SANITIZE})
endif()
endif(FLATBUFFERS_BUILD_CPP17)
Expand Down
6 changes: 3 additions & 3 deletions src/idl_gen_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2989,13 +2989,13 @@ bool GenerateCPP(const Parser &parser, const std::string &path,
std::transform(cpp_std.begin(), cpp_std.end(), cpp_std.begin(), ToUpper);
if (cpp_std.empty() || cpp_std == "C++11") {
// Use the standard C++11 code generator.
opts.g_cpp_std = cpp::CppStandard::CPP_STD_11;
opts.g_cpp_std = cpp::CPP_STD_11;
opts.g_only_fixed_enums = true;
} else if (cpp_std == "C++0X") {
opts.g_cpp_std = cpp::CppStandard::CPP_STD_X0;
opts.g_cpp_std = cpp::CPP_STD_X0;
opts.g_only_fixed_enums = false;
} else if (cpp_std == "C++17") {
opts.g_cpp_std = cpp::CppStandard::CPP_STD_17;
opts.g_cpp_std = cpp::CPP_STD_17;
// With c++17 generate strong enums only.
opts.scoped_enums = true;
// By default, prefixed_enums==true, reset it.
Expand Down
19 changes: 18 additions & 1 deletion tests/cpp17/test_cpp17.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "flatbuffers/minireflect.h"
#include "flatbuffers/registry.h"
#include "flatbuffers/util.h"
#include "test_assert.h"

// Embed generated code into an isolated namespace.
namespace cpp17 {
Expand All @@ -35,4 +36,20 @@ namespace cpp11 {
#include "../monster_test_generated.h"
} // namespace cpp11

int main(int /*argc*/, const char * /*argv*/ []) { return 0; }
int FlatBufferCpp17Tests() {
TEST_ASSERT(true);
return 0;
}

int main(int /*argc*/, const char * /*argv*/[]) {
InitTestEngine();

FlatBufferCpp17Tests();

if (!testing_fails) {
TEST_OUTPUT_LINE("C++17: ALL TESTS PASSED");
} else {
TEST_OUTPUT_LINE("C++17: %d FAILED TESTS", testing_fails);
}
return CloseTestEngine();
}

0 comments on commit 472c255

Please sign in to comment.