Skip to content

Commit

Permalink
[#486] Implement deprecated attribute test in C++ runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
Mi-La committed Sep 19, 2023
1 parent 9a75135 commit 63543ad
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 14 deletions.
19 changes: 7 additions & 12 deletions cmake/compiler_utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,11 @@ endfunction()
# Prepares warnings setup for current target
function(compiler_get_test_warnings_setup VARNAME)
compiler_get_warnings_setup(WARNINGS_SETUP)
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set(WARNINGS_SETUP_LIST
"-Wno-deprecated-declarations" # used by zserio @deprecated feature (DeprecatedAttribute.h)
)
string(REPLACE ";" " " WARNINGS_SETUP "${WARNINGS_SETUP} ${WARNINGS_SETUP_LIST}")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
set(WARNINGS_SETUP_LIST
"-Wno-float-equal"
"-Wno-unused-private-field"
"-Wno-reserved-id-macro"
"-Wno-deprecated-declarations" # used by zserio @deprecated feature (DeprecatedAttribute.h)
)
string(REPLACE ";" " " WARNINGS_SETUP "${WARNINGS_SETUP} ${WARNINGS_SETUP_LIST}")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
set(WARNINGS_SETUP_LIST
"/wd4996" # used by zserio @deprecated feature (DeprecatedAttribute.h)
)
string(REPLACE ";" " " WARNINGS_SETUP "${WARNINGS_SETUP} ${WARNINGS_SETUP_LIST}")
endif ()
Expand All @@ -61,6 +50,8 @@ function(compiler_get_warnings_as_errors_setup VARNAME)
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
if (ZSERIO_ENABLE_WERROR)
set(WARNINGS_SETUP "-Werror")
set(WARNINGS_SETUP # used by zserio @deprecated feature (DeprecatedAttribute.h)
"${WARNINGS_SETUP} -Wno-deprecated-declarations")
endif ()
if (CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL "7.5.0")
set(WARNINGS_SETUP
Expand All @@ -74,6 +65,8 @@ function(compiler_get_warnings_as_errors_setup VARNAME)
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
if (ZSERIO_ENABLE_WERROR)
set(WARNINGS_SETUP "-Werror")
set(WARNINGS_SETUP # used by zserio @deprecated feature (DeprecatedAttribute.h)
"${WARNINGS_SETUP} -Wno-deprecated-declarations")
endif ()
if (CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL "8.0.0")
set(WARNINGS_SETUP_LIST
Expand All @@ -92,6 +85,8 @@ function(compiler_get_warnings_as_errors_setup VARNAME)
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
if (ZSERIO_ENABLE_WERROR)
set(WARNINGS_SETUP "/WX")
set(WARNINGS_SETUP # used by zserio @deprecated feature (DeprecatedAttribute.h)
"${WARNINGS_SETUP} /wd4996")
endif ()
if (MSVC_VERSION STREQUAL "1900")
set(WARNINGS_SETUP "${WARNINGS_SETUP} /wd4334")
Expand Down
6 changes: 4 additions & 2 deletions cmake/coverage_utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ function(create_coverage_target)
${cov_tgt_name}
COMMAND ${CMAKE_COMMAND} -E make_directory ${cov_binary_dir}
# run tests again because ctest runs tests separately (default.profraw contains only the last test)
COMMAND ${cov_test_exectable} > /dev/null
COMMAND ${LLVM_PROFDATA_BIN} merge --sparse default.profraw -o ${cov_binary_dir}/runtime.profdata
COMMAND bash -c "cd ${ZserioCppRuntimeTest_BINARY_DIR} && ${cov_test_exectable} > /dev/null"
COMMAND ${LLVM_PROFDATA_BIN} merge
--sparse ${ZserioCppRuntimeTest_BINARY_DIR}/default.profraw
-o ${cov_binary_dir}/runtime.profdata
COMMAND ${LLVM_COV_BIN} show ${cov_test_exectable}
-show-expansions -show-line-counts -show-regions -use-color
-instr-profile=${cov_binary_dir}/runtime.profdata
Expand Down
5 changes: 5 additions & 0 deletions compiler/extensions/cpp/runtime/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ set(TEST_OBJECT_SRCS
)

set(ZSERIO_CPP_RUNTIME_TEST_SRCS
zserio/deprecated_attribute/DeprecatedAttributeTest.cpp
zserio/AllocatorHolderTest.cpp
zserio/AllocatorPropagatingCopyTest.cpp
zserio/AnyHolderTest.cpp
Expand Down Expand Up @@ -205,3 +206,7 @@ target_include_directories(${PROJECT_NAME} SYSTEM PRIVATE ${SQLITE_INCDIR})
target_link_libraries(${PROJECT_NAME} PRIVATE ZserioCppRuntime gtest_main ${SQLITE_LIBRARY})

gtest_add_tests(${PROJECT_NAME} "--gtest_output=xml" ${ZSERIO_CPP_RUNTIME_TEST_SRCS})

add_subdirectory(zserio/deprecated_attribute)
set_source_files_properties(zserio/deprecated_attribute/DeprecatedAttributeTest.cpp
PROPERTIES COMPILE_DEFINITIONS ${DEPRECATED_ATTRIBUTE_COMPILE_DEFINITIONS})
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
cmake_minimum_required(VERSION 3.6.0)

include(ExternalProject)

ExternalProject_add(DeprecatedAttribute
PREFIX "${CMAKE_CURRENT_BINARY_DIR}"
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src"
LOG_CONFIGURE 1
LOG_BUILD 1
INSTALL_COMMAND ""
CMAKE_ARGS
-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
)

set_property(DIRECTORY PROPERTY ADDITIONAL_MAKE_CLEAN_FILES
${CMAKE_CURRENT_BINARY_DIR}
)

# 0 - don't check
# 1 - check in stdout
# 2 - check in stderr
set(ENABLE_CHECK_WARNINGS 0)
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "6.0.0")
set(ENABLE_CHECK_WARNINGS 2)
endif ()
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.0.0")
set(ENABLE_CHECK_WARNINGS 2)
endif ()
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
if (NOT MSVC_VERSION LESS 1920) # at least MSVC 2019, toolset v142
set(ENABLE_CHECK_WARNINGS 1)
endif ()
endif ()
set(DEPRECATED_ATTRIBUTE_COMPILE_DEFINITIONS
"DEPRECATED_ATTRIBUTE_TEST_CHECK_WARNINGS=${ENABLE_CHECK_WARNINGS}" PARENT_SCOPE)
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "gtest/gtest.h"

#include <fstream>
#include <regex>

class DeprecatedAttributeTest : public ::testing::Test
{
protected:
bool matchInFile(const std::string& fileName, const std::regex& lineRegex)
{
std::ifstream file(fileName.c_str());

bool isPresent = false;
std::string line;
while (std::getline(file, line))
{
if (std::regex_search(line, lineRegex))
{
isPresent = true;
break;
}
}
file.close();

return isPresent;
}

static const char* ERROR_LOG_PATH;
};

const char* DeprecatedAttributeTest::ERROR_LOG_PATH =
"zserio/deprecated_attribute/src/DeprecatedAttribute-stamp/DeprecatedAttribute-build-"
#if defined(DEPRECATED_ATTRIBUTE_TEST_CHECK_WARNINGS) && DEPRECATED_ATTRIBUTE_TEST_CHECK_WARNINGS == 1
"out"
#else
"err"
#endif
".log";

TEST_F(DeprecatedAttributeTest, checkWarnings)
{
#if defined(DEPRECATED_ATTRIBUTE_TEST_CHECK_WARNINGS) && DEPRECATED_ATTRIBUTE_TEST_CHECK_WARNINGS != 0
ASSERT_TRUE(matchInFile(ERROR_LOG_PATH,
std::regex("DeprecatedAttribute\\.cpp.*15.*81.*warning.*FIVE.*deprecated"))) <<
"Warning not found in '" << ERROR_LOG_PATH << "'!";
#endif
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.6.0)

project(DeprecatedAttribute)

set(ZSERIO_PROJECT_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../../..")
set(CMAKE_MODULE_PATH "${ZSERIO_PROJECT_ROOT}/cmake")

include(cmake_utils)
include(compiler_utils)

compiler_set_warnings()

add_executable(${PROJECT_NAME} DeprecatedAttribute.cpp)
set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 11 CXX_STANDARD_REQUIRED YES CXX_EXTENSIONS NO)
target_include_directories(${PROJECT_NAME} PRIVATE ../../../../src)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <type_traits>

#include "zserio/DeprecatedAttribute.h"

enum TestEnum
{
ONE = 1,
TWO,
THREE ZSERIO_DEPRECATED,
FIVE ZSERIO_DEPRECATED = 5
};

int main(int, char*[])
{
return static_cast<typename std::underlying_type<TestEnum>::type>(TestEnum::FIVE) - 5;
}

0 comments on commit 63543ad

Please sign in to comment.