diff --git a/.github/workflows/ubuntu_clang.yml b/.github/workflows/ubuntu_clang.yml index d9a7fc58c..48fdd379f 100644 --- a/.github/workflows/ubuntu_clang.yml +++ b/.github/workflows/ubuntu_clang.yml @@ -169,7 +169,7 @@ jobs: - name: Install ninja-build tool uses: seanmiddleditch/gha-setup-ninja@master - - name: Install clang-6 + - name: Install clang-7 run: sudo apt-get install clang-7 clang++-7 - name: ccache @@ -182,8 +182,7 @@ jobs: CXX=clang++-7 CC=clang-7 cmake -B ${{github.workspace}}/build -G Ninja \ -DCMAKE_BUILD_TYPE=${{matrix.mode}} \ - -DUSE_CCACHE=${{env.ccache}} -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ \ - -DENABLE_CPP_20=OFF + -DUSE_CCACHE=${{env.ccache}} -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ - name: Build run: cmake --build ${{github.workspace}}/build --config ${{matrix.mode}} diff --git a/.github/workflows/ubuntu_gcc.yml b/.github/workflows/ubuntu_gcc.yml index 8bd7f014b..d022a32f1 100644 --- a/.github/workflows/ubuntu_gcc.yml +++ b/.github/workflows/ubuntu_gcc.yml @@ -151,7 +151,6 @@ jobs: cmake -B ${{github.workspace}}/build -G Ninja \ -DCMAKE_BUILD_TYPE=${{matrix.mode}} \ -DUSE_CCACHE=${{env.ccache}} \ - -DENABLE_CPP_20=OFF\ - name: Build run: cmake --build ${{github.workspace}}/build --config ${{matrix.mode}} diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index d79e6f74b..620905b95 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -9,41 +9,41 @@ on: workflow_dispatch: jobs: - # windows_msvc: - # runs-on: windows-latest + windows_msvc: + runs-on: windows-latest - # strategy: - # matrix: - # mode: [ Release ] #[ Release, Debug ] #Debug not support ccache - # #https://github.com/ccache/ccache/wiki/MS-Visual-Studio - # #https://github.com/ccache/ccache/issues/1040 - # arch: [ amd64, x86 ] #[ amd64,x86 ] - # ssl: [ OFF ] #[ ON, OFF ] + strategy: + matrix: + mode: [ Release ] #[ Release, Debug ] #Debug not support ccache + #https://github.com/ccache/ccache/wiki/MS-Visual-Studio + #https://github.com/ccache/ccache/issues/1040 + arch: [ amd64, x86 ] #[ amd64,x86 ] + ssl: [ OFF ] #[ ON, OFF ] - # steps: - # - name: Checkout - # uses: actions/checkout@v3 - # - name: Enable Developer Command Prompt - # uses: ilammy/msvc-dev-cmd@v1.12.0 - # with: - # arch: ${{ matrix.arch }} - # - name: Install ninja-build tool - # uses: seanmiddleditch/gha-setup-ninja@master - # with: - # version: 1.11.1 - # - name: latest ccache - # run: choco install ccache - # - name: ccache - # uses: hendrikmuhs/ccache-action@v1.2 - # with: - # key: ${{ github.job }}-${{ matrix.mode}}-ssl( ${{ matrix.ssl}} )-arch-${{ matrix.arch}} - # - name: Configure CMake - # run: cmake -B ${{github.workspace}}\build -G Ninja -DCMAKE_BUILD_TYPE=${{ matrix.mode }} -DYLT_ENABLE_SSL=${{matrix.ssl}} -DUSE_CCACHE=ON - # - name: Build - # run: cmake --build ${{github.workspace}}\build - # - name: Test - # working-directory: ${{github.workspace}}\build - # run: ctest -C ${{matrix.mode}} -j 1 -V + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Enable Developer Command Prompt + uses: ilammy/msvc-dev-cmd@v1.12.0 + with: + arch: ${{ matrix.arch }} + - name: Install ninja-build tool + uses: seanmiddleditch/gha-setup-ninja@master + with: + version: 1.11.1 + - name: latest ccache + run: choco install ccache + - name: ccache + uses: hendrikmuhs/ccache-action@v1.2 + with: + key: ${{ github.job }}-${{ matrix.mode}}-ssl( ${{ matrix.ssl}} )-arch-${{ matrix.arch}} + - name: Configure CMake + run: cmake -B ${{github.workspace}}\build -G Ninja -DCMAKE_BUILD_TYPE=${{ matrix.mode }} -DYLT_ENABLE_SSL=${{matrix.ssl}} -DUSE_CCACHE=ON + - name: Build + run: cmake --build ${{github.workspace}}\build + - name: Test + working-directory: ${{github.workspace}}\build + run: ctest -C ${{matrix.mode}} -j 1 -V windows_msvc_2019: runs-on: windows-2019 diff --git a/cmake/build.cmake b/cmake/build.cmake index 0a41a6c97..db3b92842 100644 --- a/cmake/build.cmake +++ b/cmake/build.cmake @@ -5,11 +5,28 @@ foreach(i ${CMAKE_CXX_COMPILE_FEATURES}) if (i STREQUAL cxx_std_20) set(has_cxx_std_20 TRUE) endif() + if (i STREQUAL cxx_std_17) + set(has_cxx_std_17 TRUE) + endif() endforeach() +if (has_cxx_std_20) + if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 13) + set(has_cxx_std_20 FALSE) + endif() + elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 10) + set(has_cxx_std_20 FALSE) + endif() + endif() +endif() + if (has_cxx_std_20) option(ENABLE_CPP_20 "Enable CPP 20" ON) -else () +elseif (has_cxx_std_17) option(ENABLE_CPP_20 "Enable CPP 20" OFF) +else() + message(FATAL_ERROR "Compiler ${CMAKE_CXX_COMPILER} has no C++17 support.") endif() if (ENABLE_CPP_20) set(CMAKE_CXX_STANDARD 20) @@ -39,14 +56,16 @@ else() message(STATUS "ENDIAN: LITTLE") endif() -# force use lld if your compiler is clang - # When using coro_rpc_client to send request, only remote function declarations are required. # In the examples, RPC function declaration and definition are divided. # However, clang + ld(gold linker) + '-g' will report 'undefined reference to xxx'. # So you can use lld when compiler is clang by this code: + if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") -add_link_options(-fuse-ld=lld) + check_lld(has_lld) + if (has_lld) + add_link_options(-fuse-ld=lld) + endif() endif() # ccache diff --git a/cmake/utils.cmake b/cmake/utils.cmake index 6f057d527..f44bca457 100644 --- a/cmake/utils.cmake +++ b/cmake/utils.cmake @@ -7,6 +7,21 @@ int main() { return 0; } +]====] + ${_RESULT} + ) + unset(CMAKE_REQUIRED_FLAGS) +endmacro() + +macro(check_lld _RESULT) + include(CheckCXXSourceRuns) + set(CMAKE_REQUIRED_FLAGS "-fuse-ld=lld") + check_cxx_source_runs( + [====[ +int main() +{ + return 0; +} ]====] ${_RESULT} ) diff --git a/include/ylt/standalone/iguana/define.h b/include/ylt/standalone/iguana/define.h index 2887122cc..0c0b4485d 100644 --- a/include/ylt/standalone/iguana/define.h +++ b/include/ylt/standalone/iguana/define.h @@ -21,7 +21,8 @@ #define IGUANA__INLINE_LAMBDA constexpr __attribute__((always_inline)) #endif -#if __has_cpp_attribute(likely) && __has_cpp_attribute(unlikely) +#if __has_cpp_attribute(likely) && __has_cpp_attribute(unlikely) && \ + __cplusplus >= 202002L #define IGUANA_LIKELY [[likely]] #define IGUANA_UNLIKELY [[unlikely]] #else diff --git a/include/ylt/struct_pack/calculate_size.hpp b/include/ylt/struct_pack/calculate_size.hpp index 96888f03c..1990685a7 100644 --- a/include/ylt/struct_pack/calculate_size.hpp +++ b/include/ylt/struct_pack/calculate_size.hpp @@ -423,15 +423,15 @@ get_serialize_runtime_info(const Args &...args) { ret.len_ += sz_info.total; } else { - if SP_LIKELY (sz_info.max_size < (int64_t{1} << 8)) { + if SP_LIKELY (sz_info.max_size < (uint64_t{1} << 8)) { ret.len_ += sz_info.total + sz_info.size_cnt; } else { - if (sz_info.max_size < (int64_t{1} << 16)) { + if (sz_info.max_size < (uint64_t{1} << 16)) { ret.len_ += sz_info.total + sz_info.size_cnt * 2; ret.metainfo_ = 0b01000; } - else if (sz_info.max_size < (int64_t{1} << 32)) { + else if (sz_info.max_size < (uint64_t{1} << 32)) { ret.len_ += sz_info.total + sz_info.size_cnt * 4; ret.metainfo_ = 0b10000; } diff --git a/include/ylt/struct_pack/marco.h b/include/ylt/struct_pack/marco.h index 86d610b68..e109d180b 100644 --- a/include/ylt/struct_pack/marco.h +++ b/include/ylt/struct_pack/marco.h @@ -31,7 +31,8 @@ #define STRUCT_PACK_MAY_INLINE inline #endif -#if __has_cpp_attribute(likely) && __has_cpp_attribute(unlikely) +#if __has_cpp_attribute(likely) && __has_cpp_attribute(unlikely) && \ + __cplusplus >= 202002L #define SP_LIKELY(expr) (expr) [[likely]] #define SP_UNLIKELY(expr) (expr) [[unlikely]] #elif __GNUC__ diff --git a/include/ylt/struct_pack/packer.hpp b/include/ylt/struct_pack/packer.hpp index 4aedf1c8b..a1238b674 100644 --- a/include/ylt/struct_pack/packer.hpp +++ b/include/ylt/struct_pack/packer.hpp @@ -361,7 +361,6 @@ class packer { if constexpr (trivially_copyable_container && is_little_endian_copyable) { - using value_type = typename type::value_type; write_bytes_array(writer_, (char *)item.data(), item.size() * sizeof(typename type::value_type)); return; @@ -441,13 +440,13 @@ class packer { item, [this](auto &&...items) CONSTEXPR_INLINE_LAMBDA { constexpr uint64_t tag = get_parent_tag(); // to pass msvc with c++17 - serialize_fast_varint(items...); + this->serialize_fast_varint(items...); }); } visit_members(item, [this](auto &&...items) CONSTEXPR_INLINE_LAMBDA { constexpr uint64_t tag = get_parent_tag(); // to pass msvc with c++17 - serialize_many(items...); + this->serialize_many(items...); }); } } diff --git a/include/ylt/struct_pack/unpacker.hpp b/include/ylt/struct_pack/unpacker.hpp index 5afa327f7..0fe195698 100644 --- a/include/ylt/struct_pack/unpacker.hpp +++ b/include/ylt/struct_pack/unpacker.hpp @@ -48,16 +48,16 @@ struct memory_reader { constexpr memory_reader(const char *beg, const char *end) noexcept : now(beg), end(end) {} bool read(char *target, size_t len) { - if SP_UNLIKELY (end - now < len) { + if SP_UNLIKELY (static_cast(end - now) < len) { return false; } memcpy(target, now, len); now += len; return true; } - bool check(size_t len) { return end - now >= len; } + bool check(size_t len) { return static_cast(end - now) >= len; } const char *read_view(size_t len) { - if SP_UNLIKELY (end - now < len) { + if SP_UNLIKELY (static_cast(end - now) < len) { return nullptr; } auto ret = now; @@ -65,7 +65,7 @@ struct memory_reader { return ret; } bool ignore(size_t len) { - if SP_UNLIKELY (end - now < len) { + if SP_UNLIKELY (static_cast(end - now) < len) { return false; } now += len; @@ -482,10 +482,7 @@ class unpacker { STRUCT_PACK_INLINE std::pair deserialize_compatible(unsigned compatible_sz_len) { - constexpr std::size_t sz[] = {0, 2, 4, 8}; - auto len_sz = sz[compatible_sz_len]; std::size_t data_len = 0; - bool result; switch (compatible_sz_len) { case 1: if SP_LIKELY (low_bytes_read_wrapper<2>(reader_, data_len)) { @@ -564,7 +561,7 @@ class unpacker { } else { if constexpr (is_MD5_reader_wrapper) { - reader_.read_head((char *)¤t_types_code); + current_types_code = reader_.read_head(); if SP_LIKELY (current_types_code % 2 == 0) // unexist metainfo { size_type_ = 0; @@ -876,7 +873,6 @@ class unpacker { } else if constexpr (container) { std::size_t size = 0; - bool result{}; if constexpr (size_type == 1) { if SP_UNLIKELY (!low_bytes_read_wrapper(reader_, size)) { return struct_pack::errc::no_buffer_space; @@ -909,7 +905,7 @@ class unpacker { } } else { - std::uint64_t sz; + std::uint64_t sz{}; if SP_UNLIKELY (!low_bytes_read_wrapper(reader_, sz)) { return struct_pack::errc::no_buffer_space; @@ -1017,7 +1013,7 @@ class unpacker { return errc::no_buffer_space; } } - std::size_t mem_sz = size * sizeof(value_type); + [[maybe_unused]] std::size_t mem_sz = size * sizeof(value_type); if constexpr (NotSkip) { if constexpr (string_view || dynamic_span) { static_assert( @@ -1037,7 +1033,7 @@ class unpacker { } resize(item, size); if constexpr (is_little_endian_copyable) { - auto ec = + [[maybe_unused]] auto ec = read_bytes_array(reader_, (char *)item.data(), mem_sz); assert(ec == true); } @@ -1218,7 +1214,7 @@ class unpacker { item, [this](auto &&...items) CONSTEXPR_INLINE_LAMBDA { constexpr uint64_t tag = get_parent_tag(); // to pass msvc with c++17 - return deserialize_fast_varint(items...); + return this->deserialize_fast_varint(items...); }); if SP_UNLIKELY (code) { return code; @@ -1228,7 +1224,7 @@ class unpacker { item, [this](auto &&...items) CONSTEXPR_INLINE_LAMBDA { constexpr uint64_t tag = get_parent_tag(); // to pass msvc with c++17 - return deserialize_many( + return this->deserialize_many( items...); }); } @@ -1433,13 +1429,7 @@ struct MD5_reader_wrapper : public Reader { is_failed = !read_wrapper(*(Reader *)this, (char *)&head_chars); } - bool read_head(char *target) { - if SP_UNLIKELY (is_failed) { - return false; - } - memcpy(target, &head_chars, sizeof(head_chars)); - return true; - } + uint32_t read_head() { return head_chars; } Reader &&release_reader() { return std::move(*(Reader *)this); } bool is_failed; uint32_t get_md5() { return head_chars & 0xFFFFFFFE; } diff --git a/include/ylt/struct_pack/user_helper.hpp b/include/ylt/struct_pack/user_helper.hpp index 25f6cd29c..35a0a1f3f 100644 --- a/include/ylt/struct_pack/user_helper.hpp +++ b/include/ylt/struct_pack/user_helper.hpp @@ -1,6 +1,5 @@ #pragma once #include -#include #include "ylt/struct_pack/calculate_size.hpp" #include "ylt/struct_pack/packer.hpp" diff --git a/include/ylt/struct_pack/util.h b/include/ylt/struct_pack/util.h index 58ecdb063..d52f7e9dd 100644 --- a/include/ylt/struct_pack/util.h +++ b/include/ylt/struct_pack/util.h @@ -60,12 +60,6 @@ inline constexpr std::string_view type_string() { #endif } -#if __cpp_concepts >= 201907L -constexpr bool is_string_reserve_shrink = requires { std::string{}.reserve(); }; -#else -constexpr bool is_string_reserve_shrink = true; -#endif - template using remove_cvref_t = std::remove_cv_t>; @@ -201,12 +195,7 @@ inline void resize(std::basic_string &raw_str, std::size_t sz) { raw_str.resize(sz); #elif defined(__GLIBCXX__) || defined(_LIBCPP_VERSION) || \ defined(_MSVC_STL_VERSION) - if constexpr (is_string_reserve_shrink) { - if (sz > raw_str.capacity()) { - raw_str.reserve(sz); - } - } - else { + if (sz > raw_str.capacity()) { raw_str.reserve(sz); } std::string &str = *reinterpret_cast(&raw_str); diff --git a/src/struct_pack/examples/CMakeLists.txt b/src/struct_pack/examples/CMakeLists.txt index 3a0061b3c..d097fc10b 100644 --- a/src/struct_pack/examples/CMakeLists.txt +++ b/src/struct_pack/examples/CMakeLists.txt @@ -18,4 +18,5 @@ else() # include_directories(include) # include_directories(include/ylt/thirdparty) endif() +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") add_executable(struct_pack_example basic_usage.cpp non_aggregated_type.cpp serialize_config.cpp user_defined_serialization.cpp derived_class.cpp main.cpp) \ No newline at end of file diff --git a/src/struct_pack/examples/basic_usage.cpp b/src/struct_pack/examples/basic_usage.cpp index 28c0571bc..73fa3c97f 100644 --- a/src/struct_pack/examples/basic_usage.cpp +++ b/src/struct_pack/examples/basic_usage.cpp @@ -84,8 +84,9 @@ void basic_usage() { { auto buffer = struct_pack::serialize_with_offset(/* offset = */ 2, p); auto buffer2 = struct_pack::serialize(p); - bool result = std::string_view{buffer.data() + 2, buffer.size() - 2} == - std::string_view{buffer2.data(), buffer2.size()}; + [[maybe_unused]] bool result = + std::string_view{buffer.data() + 2, buffer.size() - 2} == + std::string_view{buffer2.data(), buffer2.size()}; assert(result); } // api 6. serialize varadic param diff --git a/src/struct_pack/examples/derived_class.cpp b/src/struct_pack/examples/derived_class.cpp index 4e5aed52d..4d708328f 100644 --- a/src/struct_pack/examples/derived_class.cpp +++ b/src/struct_pack/examples/derived_class.cpp @@ -68,7 +68,7 @@ void virtual_base_class_example(void) { struct_pack::deserialize>>(ret); assert(result.has_value()); // check deserialize ok assert(result->size() == 3); // check vector size - for (int i = 0; i < result->size(); ++i) { + for (size_t i = 0; i < result->size(); ++i) { assert(checker[i] == result.value()[i]->hello()); // check type } } @@ -123,7 +123,7 @@ void base_class_example(void) { struct_pack::deserialize>>(ret); assert(result.has_value()); assert(result->size() == 4); // check vector size - for (int i = 0; i < result->size(); ++i) { + for (size_t i = 0; i < result->size(); ++i) { assert(checker[i] == result.value()[i]->hello()); // check type } } diff --git a/src/struct_pack/examples/serialize_config.cpp b/src/struct_pack/examples/serialize_config.cpp index 001cd6e09..aba7f710f 100644 --- a/src/struct_pack/examples/serialize_config.cpp +++ b/src/struct_pack/examples/serialize_config.cpp @@ -62,7 +62,7 @@ void serialize_config() { // only need 4 bytes assert(buffer.size() == 4); // deserialize with config - auto result = + [[maybe_unused]] auto result = struct_pack::deserialize( buffer); assert(result.value() == r); @@ -75,6 +75,6 @@ void serialize_config_by_ADL() { // only need 4 bytes assert(buffer.size() == 4); // deserialize with config - auto result = struct_pack::deserialize(buffer); + [[maybe_unused]] auto result = struct_pack::deserialize(buffer); assert(result.value() == r); }