Skip to content

Commit

Permalink
[fix, test] fix warnings in MSVC (C4623, C5027, C4626); add build tes…
Browse files Browse the repository at this point in the history
…t for "ulbn.hpp"
  • Loading branch information
DreamPast committed Nov 22, 2024
1 parent 9e0e166 commit 1fdef76
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 75 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
.vscode/*
!.vscode/extensions.json
.idea
.vs
*.suo
*.ntvs*
*.njsproj
Expand All @@ -16,3 +17,5 @@ dev/**
# Build results
build*/**
Testing/**
out/**
CMakeSettings.json
38 changes: 26 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,26 @@ option(BUILD_TEST "Build test" ON)

if(BUILD_SHARED_LIBRARY)
add_library(ulbn SHARED ulbn.c)
target_compile_definitions(ulbn PRIVATE ULBN_CONF_EXPORT_PUBLIC=1)

add_library(ulbn-cxx SHARED ulbn_build.cpp)
target_compile_definitions(ulbn-cxx PRIVATE ULBN_CONF_EXPORT_PUBLIC=1)
set_target_properties(ulbn-cxx PROPERTIES CXX_STANDARD 98)
add_library(ulbn-cxx SHARED ./test_build/ulbn_build.cpp)
add_library(ulbn-hxx SHARED ./test_build/ulbn_build_hpp.cpp)
else()
add_library(ulbn STATIC ulbn.c)

add_library(ulbn-cxx STATIC ulbn_build.cpp)
set_target_properties(ulbn-cxx PROPERTIES CXX_STANDARD 98)
add_library(ulbn-cxx STATIC ./test_build/ulbn_build.cpp)
add_library(ulbn-hxx STATIC ./test_build/ulbn_build_hpp.cpp)
endif()
if(UNIX)
target_link_libraries(ulbn PRIVATE m)

if(BUILD_SHARED_LIBRARY)
target_compile_definitions(ulbn PRIVATE ULBN_CONF_EXPORT_PUBLIC=1)
target_compile_definitions(ulbn-cxx PRIVATE ULBN_CONF_EXPORT_PUBLIC=1)
target_compile_definitions(ulbn-hxx PRIVATE ULBN_CONF_EXPORT_PUBLIC=1)
endif()

set_target_properties(ulbn-cxx PROPERTIES CXX_STANDARD 98)
set_target_properties(ulbn-hxx PROPERTIES CXX_STANDARD 20)
target_include_directories(ulbn-cxx PRIVATE ./)
target_include_directories(ulbn-hxx PRIVATE ./)


set(GNUC_ALL_C_WARNINGS
-Wall
-Wextra
Expand Down Expand Up @@ -80,17 +85,23 @@ set(CLANG_ALL_CXX_WARNINGS
-Weverything
)
set(CLANG_CLOSE_WARNINGS
-Wno-unsafe-buffer-usage
-Wno-reserved-identifier
-Wno-language-extension-token

-Wno-padded
-Wno-weak-vtables

-Wno-long-long
-Wno-c++98-compat
-Wno-c++98-compat-pedantic
-Wno-c++11-long-long
)
if(WIN32)
set(CLANG_CLOSE_WARNINGS ${CLANG_CLOSE_WARNINGS} -Wno-unsafe-buffer-usage)
endif()

set(MSVC_CLOSE_WARNINGS
/wd4514 # 'function' : unreferenced inline function has been removed
/wd4710 # 'function' : function not inlined
/wd4711 # function 'function' selected for inline expansion
/wd4820 # 'bytes' bytes padding added after construct 'member_name'
/wd5045 # Compiler will insert Spectre mitigation for memory load if /Qspectre switch specified
Expand All @@ -101,12 +112,15 @@ set(MSVC_CLOSE_WARNINGS
if(CMAKE_COMPILER_IS_GNUCXX)
target_compile_options(ulbn PRIVATE ${GNUC_ALL_C_WARNINGS} ${GNUC_CLOSE_WARNINGS})
target_compile_options(ulbn-cxx PRIVATE ${GNUC_ALL_CXX_WARNINGS} ${GNUC_CLOSE_WARNINGS})
target_compile_options(ulbn-hxx PRIVATE ${GNUC_ALL_CXX_WARNINGS} ${GNUC_CLOSE_WARNINGS})
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
target_compile_options(ulbn PRIVATE ${CLANG_ALL_C_WARNINGS} ${CLANG_CLOSE_WARNINGS})
target_compile_options(ulbn-cxx PRIVATE ${CLANG_ALL_CXX_WARNINGS} ${CLANG_CLOSE_WARNINGS})
target_compile_options(ulbn-hxx PRIVATE ${CLANG_ALL_CXX_WARNINGS} ${CLANG_CLOSE_WARNINGS})
elseif(MSVC)
target_compile_options(ulbn PRIVATE /Wall /WX ${MSVC_CLOSE_WARNINGS})
target_compile_options(ulbn-cxx PRIVATE /Wall /WX ${MSVC_CLOSE_WARNINGS})
target_compile_options(ulbn-hxx PRIVATE /Wall /WX ${MSVC_CLOSE_WARNINGS})
endif()

if(BUILD_TEST)
Expand Down
7 changes: 0 additions & 7 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
-Wno-unsafe-buffer-usage
-Wno-reserved-identifier
-Wno-language-extension-token
-Wno-missing-prototypes
-Wno-unused-function
-Wno-c++98-compat
-Wno-c++98-compat-pedantic
Expand All @@ -32,12 +31,6 @@ elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
elseif(MSVC)
target_compile_options(ulbn-test PRIVATE
/Wall /WX ${MSVC_CLOSE_WARNINGS}

/wd4514 # 'function' : unreferenced inline function has been removed
/wd4623 # 'derived class' : default constructor was implicitly defined as deleted because a base class default constructor is inaccessible or deleted
/wd5027 # 'type': move assignment operator was implicitly defined as deleted
/wd4626 # 'derived class' : assignment operator was implicitly defined as deleted because a base class assignment operator is inaccessible or deleted
/wd4710 # 'function' : function not inlined
)
endif()

Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions test_build/ulbn_build_hpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* only test building the ulbn.hpp file */
#include "ulbn.hpp"
#include "ulbn.c"
104 changes: 48 additions & 56 deletions ulbn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <cstring>
#include <tuple>
#include <cstdio>
#include <algorithm>

#include "ulbn.h"

Expand Down Expand Up @@ -59,7 +60,7 @@ class Exception : public std::runtime_error {
int _error;
};

ulbn_rand_t* getCurrentRand() {
inline ulbn_rand_t* getCurrentRand() {
struct _RandManager {
_RandManager() {
ulbn_rand_init(&hold);
Expand Down Expand Up @@ -971,29 +972,15 @@ class BigInt {


std::string toString(int base = 10) const {
struct OstreamWrapper {
std::exception_ptr exception;
std::string ret;
} wrapper;

Wrapper<std::string> wrapper(std::string{});
int err = ulbi_print_ex(
_ctx(),
[](void* opaque, const char* str, size_t len) -> int {
OstreamWrapper* o = reinterpret_cast<OstreamWrapper*>(opaque);
try {
o->ret.append(str, len);
} catch(...) {
o->exception = std::current_exception();
return -1;
}
return 0;
return reinterpret_cast<Wrapper<std::string>*>(opaque)->call([&](std::string& s) { s.append(str, len); });
},
&wrapper, _value, base
);
if(err == ULBN_ERR_EXTERNAL)
std::rethrow_exception(wrapper.exception);
_check(err);
return wrapper.ret;
return wrapper.check(err);
}
friend std::ostream& operator<<(std::ostream& ost, const BigInt& value) {
value.print(ost);
Expand All @@ -1002,57 +989,31 @@ class BigInt {
void print(FILE* fp, int base = 10) const {
_check(ulbi_print(_ctx(), fp, _value, base));
}
void print(std::ostream& ost, int base = 10) const {
struct OstreamWrapper {
std::exception_ptr exception;
std::ostream& ost;
};
OstreamWrapper wrapper = { {}, ost };
std::ostream& print(std::ostream& ost, int base = 10) const {
Wrapper<std::ostream&> wrapper(ost);
int err = ulbi_print_ex(
_ctx(),
[](void* opaque, const char* ptr, size_t len) -> int {
OstreamWrapper* o = reinterpret_cast<OstreamWrapper*>(opaque);
try {
// todo: check std::streamsize?
o->ost.write(ptr, static_cast<std::streamsize>(len));
} catch(...) {
o->exception = std::current_exception();
return 1;
}
return 0;
return reinterpret_cast<Wrapper<std::ostream&>*>(opaque)->call([&](std::ostream& os) {
os.write(ptr, static_cast<std::streamsize>(len));
});
},
&wrapper, _value, base
);
if(err == ULBN_ERR_EXTERNAL)
std::rethrow_exception(wrapper.exception);
_check(err);
return wrapper.check(err);
}
template<class Iter>
requires std::output_iterator<Iter, char>
void print(Iter& iter, int base = 10) const {
struct IterWrapper {
std::exception_ptr exception;
Iter& iter;
};
IterWrapper wrapper = { {}, iter };
Iter print(Iter iter, int base = 10) const {
Wrapper<Iter&> wrapper(iter);
int err = ulbi_print_ex(
_ctx(), _value, base,
[](void* opaque, const char* ptr, size_t len) -> int {
IterWrapper* o = reinterpret_cast<IterWrapper*>(opaque);
try {
for(size_t i = 0; i < len; ++i)
*o->iter++ = ptr[i];
} catch(...) {
o->exception = std::current_exception();
return 1;
}
return 0;
return reinterpret_cast<Wrapper<Iter&>*>(opaque)->call([&](Iter& itr) { std::copy_n(ptr, len, itr); });
},
&wrapper
);
if(err == ULBN_ERR_EXTERNAL)
std::rethrow_exception(wrapper.exception);
_check(err);
return wrapper->check(err);
}


Expand Down Expand Up @@ -1264,13 +1225,44 @@ class BigInt {
return err;
}

template<class T>
struct Wrapper {
Wrapper() = delete;
Wrapper(const Wrapper&) = default;
Wrapper(Wrapper&&) = delete;
Wrapper& operator=(const Wrapper&) = delete;
Wrapper& operator=(Wrapper&&) = delete;

template<class TValue>
Wrapper(TValue&& construct_value) : value(std::forward<TValue>(construct_value)) { }
template<class Func>
int call(Func&& func) noexcept {
try {
func(value);
return 0;
} catch(...) {
exception = std::current_exception();
return -1;
}
}
T check(int err) {
if(err == ULBN_ERR_EXTERNAL)
std::rethrow_exception(exception);
_check(err);
return value;
}

T value;
std::exception_ptr exception;
};

ulbi_t _value[1];
};

BigInt operator""_bi(const char* str) {
inline BigInt operator""_bi(const char* str) {
return BigInt(str);
}
BigInt operator""_bi(const char* str, size_t len) {
inline BigInt operator""_bi(const char* str, size_t len) {
(void)len;
return BigInt(str);
}
Expand Down

0 comments on commit 1fdef76

Please sign in to comment.