Skip to content

Commit

Permalink
[struct_pack][fix] fix warning
Browse files Browse the repository at this point in the history
  • Loading branch information
poor-circle committed Aug 13, 2024
1 parent 8fec9b1 commit 5eade0c
Show file tree
Hide file tree
Showing 16 changed files with 102 additions and 89 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/ubuntu_clang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}}
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/ubuntu_gcc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
66 changes: 33 additions & 33 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]
# 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/[email protected]
# 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/[email protected]
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/[email protected]
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

Expand Down
27 changes: 23 additions & 4 deletions cmake/build.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions cmake/utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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}
)
Expand Down
3 changes: 2 additions & 1 deletion include/ylt/standalone/iguana/define.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions include/ylt/struct_pack/calculate_size.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion include/ylt/struct_pack/marco.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down
5 changes: 2 additions & 3 deletions include/ylt/struct_pack/packer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ class packer {
if constexpr (trivially_copyable_container<type> &&
is_little_endian_copyable<sizeof(
typename type::value_type)>) {
using value_type = typename type::value_type;
write_bytes_array(writer_, (char *)item.data(),
item.size() * sizeof(typename type::value_type));
return;
Expand Down Expand Up @@ -441,13 +440,13 @@ class packer {
item, [this](auto &&...items) CONSTEXPR_INLINE_LAMBDA {
constexpr uint64_t tag =
get_parent_tag<type>(); // to pass msvc with c++17
serialize_fast_varint<tag>(items...);
this->serialize_fast_varint<tag>(items...);
});
}
visit_members(item, [this](auto &&...items) CONSTEXPR_INLINE_LAMBDA {
constexpr uint64_t tag =
get_parent_tag<type>(); // to pass msvc with c++17
serialize_many<size_type, version, tag>(items...);
this->serialize_many<size_type, version, tag>(items...);
});
}
}
Expand Down
32 changes: 11 additions & 21 deletions include/ylt/struct_pack/unpacker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,24 @@ 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<size_t>(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<size_t>(end - now) >= len; }
const char *read_view(size_t len) {
if SP_UNLIKELY (end - now < len) {
if SP_UNLIKELY (static_cast<size_t>(end - now) < len) {
return nullptr;
}
auto ret = now;
now += len;
return ret;
}
bool ignore(size_t len) {
if SP_UNLIKELY (end - now < len) {
if SP_UNLIKELY (static_cast<size_t>(end - now) < len) {
return false;
}
now += len;
Expand Down Expand Up @@ -482,10 +482,7 @@ class unpacker {

STRUCT_PACK_INLINE std::pair<struct_pack::err_code, std::uint64_t>
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)) {
Expand Down Expand Up @@ -564,7 +561,7 @@ class unpacker {
}
else {
if constexpr (is_MD5_reader_wrapper<Reader>) {
reader_.read_head((char *)&current_types_code);
current_types_code = reader_.read_head();
if SP_LIKELY (current_types_code % 2 == 0) // unexist metainfo
{
size_type_ = 0;
Expand Down Expand Up @@ -876,7 +873,6 @@ class unpacker {
}
else if constexpr (container<type>) {
std::size_t size = 0;
bool result{};
if constexpr (size_type == 1) {
if SP_UNLIKELY (!low_bytes_read_wrapper<size_type>(reader_, size)) {
return struct_pack::errc::no_buffer_space;
Expand Down Expand Up @@ -909,7 +905,7 @@ class unpacker {
}
}
else {
std::uint64_t sz;
std::uint64_t sz{};
if SP_UNLIKELY (!low_bytes_read_wrapper<size_type>(reader_,
sz)) {
return struct_pack::errc::no_buffer_space;
Expand Down Expand Up @@ -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<type> || dynamic_span<type>) {
static_assert(
Expand All @@ -1037,7 +1033,7 @@ class unpacker {
}
resize(item, size);
if constexpr (is_little_endian_copyable<sizeof(value_type)>) {
auto ec =
[[maybe_unused]] auto ec =
read_bytes_array(reader_, (char *)item.data(), mem_sz);
assert(ec == true);
}
Expand Down Expand Up @@ -1218,7 +1214,7 @@ class unpacker {
item, [this](auto &&...items) CONSTEXPR_INLINE_LAMBDA {
constexpr uint64_t tag =
get_parent_tag<type>(); // to pass msvc with c++17
return deserialize_fast_varint<tag, NotSkip>(items...);
return this->deserialize_fast_varint<tag, NotSkip>(items...);
});
if SP_UNLIKELY (code) {
return code;
Expand All @@ -1228,7 +1224,7 @@ class unpacker {
item, [this](auto &&...items) CONSTEXPR_INLINE_LAMBDA {
constexpr uint64_t tag =
get_parent_tag<type>(); // to pass msvc with c++17
return deserialize_many<size_type, version, NotSkip, tag>(
return this->deserialize_many<size_type, version, NotSkip, tag>(
items...);
});
}
Expand Down Expand Up @@ -1433,13 +1429,7 @@ struct MD5_reader_wrapper : public Reader {
is_failed =
!read_wrapper<sizeof(head_chars)>(*(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; }
Expand Down
1 change: 0 additions & 1 deletion include/ylt/struct_pack/user_helper.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once
#include <cstdint>
#include <span>

#include "ylt/struct_pack/calculate_size.hpp"
#include "ylt/struct_pack/packer.hpp"
Expand Down
13 changes: 1 addition & 12 deletions include/ylt/struct_pack/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename T>
using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<T>>;

Expand Down Expand Up @@ -201,12 +195,7 @@ inline void resize(std::basic_string<ch> &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<std::string *>(&raw_str);
Expand Down
1 change: 1 addition & 0 deletions src/struct_pack/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading

0 comments on commit 5eade0c

Please sign in to comment.