all: cosmetic updates

This commit is contained in:
Markus F.X.J. Oberhumer 2023-07-06 18:03:37 +02:00
parent 525e091472
commit c79aa6ad8a
14 changed files with 155 additions and 106 deletions

View File

@ -11,8 +11,8 @@ on: [push, workflow_dispatch]
env:
DEBIAN_FRONTEND: noninteractive
UPX_CMAKE_BUILD_FLAGS: --verbose
# 2023-06-29
ZIG_DIST_VERSION: 0.11.0-dev.3886+0c1bfe271
# 2023-07-05
ZIG_DIST_VERSION: 0.11.0-dev.3937+78eb3c561
jobs:
job-rebuild-and-verify-stubs:

View File

@ -8,8 +8,8 @@ on:
workflow_dispatch:
env:
# 2023-06-29
ZIG_DIST_VERSION: 0.11.0-dev.3886+0c1bfe271
# 2023-07-05
ZIG_DIST_VERSION: 0.11.0-dev.3937+78eb3c561
jobs:
job-linux-zigcc: # uses cmake + make

View File

@ -120,11 +120,97 @@ if(is_multi_config)
set(CMAKE_CONFIGURATION_TYPES "${c}" CACHE STRING "List of supported configuration types." FORCE)
endif()
# set MSVC_FRONTEND
set(MSVC_FRONTEND 0)
if(MSVC OR ",${CMAKE_C_COMPILER_FRONTEND_VARIANT}," STREQUAL ",MSVC,")
set(MSVC_FRONTEND 1)
endif()
#***********************************************************************
# common compilation flags
#***********************************************************************
if(UPX_CONFIG_DISABLE_WSTRICT)
# enable all basic warnings
set(warn_Wall -Wall)
set(warn_WN -W3)
else()
# enable all basic warnings, and enable lots of strict warnings
set(warn_Wall -Wall -Wextra -Wcast-align -Wcast-qual -Wmissing-declarations -Wpointer-arith -Wshadow -Wvla -Wwrite-strings)
set(warn_WN -W4)
endif()
if(UPX_CONFIG_DISABLE_WERROR)
# warnings are just warnings
set(warn_Werror "")
set(warn_WX "")
else()
# warnings are fatal errors; annoy developers to keep the source code warning-free
set(warn_Werror -Werror)
set(warn_WX -WX)
endif()
if(NOT MSVC)
# use -O2 instead of -O3 to reduce code size
string(REGEX REPLACE "(^| )-O3( |$$)" "\\1-O2\\2" a "${CMAKE_C_FLAGS_RELEASE}")
string(REGEX REPLACE "(^| )-O3( |$$)" "\\1-O2\\2" b "${CMAKE_CXX_FLAGS_RELEASE}")
set(CMAKE_C_FLAGS_RELEASE "${a}" CACHE STRING "Flags used by the C compiler during RELEASE builds." FORCE)
set(CMAKE_CXX_FLAGS_RELEASE "${b}" CACHE STRING "Flags used by the CXX compiler during RELEASE builds." FORCE)
endif()
if(MSVC_FRONTEND)
# disable silly warnings about using "deprecated" POSIX functions like fopen()
add_definitions(-D_CRT_NONSTDC_NO_DEPRECATE)
add_definitions(-D_CRT_NONSTDC_NO_WARNINGS)
add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
# use -funsigned-char; set __cplusplus according to selected C++ standard; use new preprocessor
add_definitions(-J -Zc:__cplusplus -Zc:preprocessor)
else()
# protect against security threats caused by misguided compiler "optimizations"
if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
add_definitions(-fno-delete-null-pointer-checks -fno-lifetime-dse)
endif()
add_definitions(-fno-strict-aliasing -fno-strict-overflow -funsigned-char)
# disable overambitious auto-vectorization until this actually gains something
add_definitions(-fno-tree-vectorize)
# disable annoying clang warnings which get added by the Apple Xcode cmake generator
if(CMAKE_C_COMPILER_ID MATCHES "Clang" AND CMAKE_GENERATOR STREQUAL "Xcode")
add_definitions(-Wno-shorten-64-to-32)
endif()
endif()
# compile a target with -O2 even in Debug build
function(upx_compile_target_debug_with_O2)
foreach(t ${ARGV})
if(MSVC_FRONTEND)
# MSVC uses some Debug compilation options like -RTC1 that are incompatible with -O2
else()
target_compile_options(${t} PRIVATE $<$<CONFIG:Debug>:-O2>)
endif()
endforeach()
endfunction()
# sanitize a target
function(upx_sanitize_target)
foreach(t ${ARGV})
if(UPX_CONFIG_DISABLE_SANITIZE)
# no-op
elseif(MSVC_FRONTEND)
# MSVC uses -GS (similar to -fstack-protector) by default
elseif(CMAKE_C_PLATFORM_ID MATCHES "^MinGW" OR MINGW OR CYGWIN)
# avoid link errors with current MinGW-w64 versions
# see https://www.mingw-w64.org/contribute/#sanitizers-asan-tsan-usan
else()
# default sanitizer for Debug builds
target_compile_options(${t} PRIVATE $<$<CONFIG:Debug>:-fsanitize=undefined -fsanitize-undefined-trap-on-error -fstack-protector-all>)
# default sanitizer for Release builds
target_compile_options(${t} PRIVATE $<$<CONFIG:MinSizeRel>:-fstack-protector>)
target_compile_options(${t} PRIVATE $<$<CONFIG:Release>:-fstack-protector>)
target_compile_options(${t} PRIVATE $<$<CONFIG:RelWithDebInfo>:-fstack-protector>)
endif()
endforeach()
endfunction()
#***********************************************************************
# targets
#***********************************************************************
@ -188,87 +274,9 @@ if(Threads_FOUND)
endif()
#***********************************************************************
# compilation flags
# target compilation flags
#***********************************************************************
if(UPX_CONFIG_DISABLE_WSTRICT)
# enable all basic warnings
set(warn_Wall -Wall)
set(warn_WN -W3)
else()
# enable lots of strict warnings
set(warn_Wall -Wall -Wextra -Wcast-align -Wcast-qual -Wmissing-declarations -Wpointer-arith -Wshadow -Wvla -Wwrite-strings)
set(warn_WN -W4)
endif()
if(UPX_CONFIG_DISABLE_WERROR)
set(warn_Werror "")
set(warn_WX "")
else()
set(warn_Werror -Werror)
set(warn_WX -WX)
endif()
if(NOT MSVC)
# use -O2 instead of -O3 to reduce code size
string(REGEX REPLACE "(^| )-O3( |$$)" "\\1-O2\\2" a "${CMAKE_C_FLAGS_RELEASE}")
string(REGEX REPLACE "(^| )-O3( |$$)" "\\1-O2\\2" b "${CMAKE_CXX_FLAGS_RELEASE}")
set(CMAKE_C_FLAGS_RELEASE "${a}" CACHE STRING "Flags used by the C compiler during RELEASE builds." FORCE)
set(CMAKE_CXX_FLAGS_RELEASE "${b}" CACHE STRING "Flags used by the CXX compiler during RELEASE builds." FORCE)
endif()
if(MSVC_FRONTEND)
# disable silly warnings about using "deprecated" POSIX functions like fopen()
add_definitions(-D_CRT_NONSTDC_NO_DEPRECATE)
add_definitions(-D_CRT_NONSTDC_NO_WARNINGS)
add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
# set __cplusplus according to selected C++ standard; use new preprocessor
add_definitions(-J -Zc:__cplusplus -Zc:preprocessor)
else()
# protect against security threats caused by misguided compiler "optimizations"
if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
add_definitions(-fno-delete-null-pointer-checks -fno-lifetime-dse)
endif()
add_definitions(-fno-strict-aliasing -fno-strict-overflow -funsigned-char)
# disable overambitious auto-vectorization until this actually gains something
add_definitions(-fno-tree-vectorize)
# disable annoying clang warnings which get added by the Apple Xcode cmake generator
if(CMAKE_C_COMPILER_ID MATCHES "Clang" AND CMAKE_GENERATOR STREQUAL "Xcode")
add_definitions(-Wno-shorten-64-to-32)
endif()
endif()
# compile a target with -O2 even in Debug build
function(upx_compile_target_debug_with_O2)
foreach(t ${ARGV})
if(MSVC_FRONTEND)
# MSVC uses some Debug compilation options like -RTC1 that are incompatible with -O2
else()
target_compile_options(${t} PRIVATE $<$<CONFIG:Debug>:-O2>)
endif()
endforeach()
endfunction()
function(upx_sanitize_target)
foreach(t ${ARGV})
if(UPX_CONFIG_DISABLE_SANITIZE)
# no-op
elseif(MSVC_FRONTEND)
# MSVC uses -GS (similar to -fstack-protector) by default
elseif(CMAKE_C_PLATFORM_ID MATCHES "^MinGW" OR MINGW OR CYGWIN)
# avoid link errors with current MinGW-w64 versions
# see https://www.mingw-w64.org/contribute/#sanitizers-asan-tsan-usan
else()
# default sanitizer for Debug builds
target_compile_options(${t} PRIVATE $<$<CONFIG:Debug>:-fsanitize=undefined -fsanitize-undefined-trap-on-error -fstack-protector-all>)
# default sanitizer for Release builds
target_compile_options(${t} PRIVATE $<$<CONFIG:MinSizeRel>:-fstack-protector>)
target_compile_options(${t} PRIVATE $<$<CONFIG:Release>:-fstack-protector>)
target_compile_options(${t} PRIVATE $<$<CONFIG:RelWithDebInfo>:-fstack-protector>)
endif()
endforeach()
endfunction()
if(NOT UPX_CONFIG_DISABLE_BZIP2)
set(t upx_vendor_bzip2)
upx_compile_target_debug_with_O2(${t})

View File

@ -4,8 +4,9 @@
#
# INFO: this Makefile is just a convenience wrapper for calling CMake
# (using a somewhat current CMake version is highly recommended)
# NOTE: if you only have an older CMake 3.x then you can invoke cmake manually like this:
# HINT: if you only have an older CMake 3.x then you can invoke cmake manually like this:
# mkdir -p build/release
# cd build/release
# cmake ../..

View File

@ -37,7 +37,9 @@ CXX="$CXX $mandatory_flags"
# HINT: set "top_srcdir" manually if your system does not have "readlink"
if test "x$top_srcdir" = "x"; then
my_argv0abs="$(readlink -fn "$my_argv0")"
test "x$my_argv0abs" = "x" && exit 1
my_argv0dir="$(dirname "$my_argv0abs")"
test "x$my_argv0dir" = "x" && exit 1
cd "$my_argv0dir/../.." || exit 1
else
cd "$top_srcdir" || exit 1

View File

@ -175,6 +175,7 @@ ACC_COMPILE_TIME_ASSERT_HEADER((wchar_t) -1 > 0)
/*************************************************************************
// upx_compiler_sanity_check()
// assert a sane architecture and compiler
// (modern compilers will optimize away most of this code)
**************************************************************************/
namespace {
@ -287,7 +288,7 @@ struct TestBELE {
static noinline bool test(void) {
CheckIntegral<T>::check();
CheckAlignment<T>::check();
// arithmetic checks (modern compilers will optimize this away)
// arithmetic checks
T allbits = {};
assert(allbits == 0);
allbits += 1;
@ -544,7 +545,7 @@ TEST_CASE("noncopyable") {
};
Test t = {};
CHECK(t.v == 1);
#if (ACC_CC_MSC) // MSVC thinks that Test is not std::is_trivially_copyable; compiler bug?
#if (ACC_CC_MSC) // MSVC thinks that Test is not std::is_trivially_copyable; true or compiler bug?
t.v = 0;
#else
mem_clear(&t);

View File

@ -53,6 +53,7 @@
#endif
#include <doctest/doctest/parts/doctest.cpp>
#endif // DOCTEST_CONFIG_DISABLE
/* vim:set ts=4 sw=4 et: */

View File

@ -51,7 +51,7 @@ public:
inline MemBufferBase() noexcept : ptr(nullptr), size_in_bytes(0) {}
inline ~MemBufferBase() noexcept {}
// NOTE: implicit conversion to underlying pointer
// IMPORTANT NOTE: automatic conversion to underlying pointer
// HINT: for fully bound-checked pointer use XSPAN_S from xspan.h
operator pointer() const noexcept { return ptr; }
@ -61,7 +61,6 @@ public:
size_t bytes = mem_size(sizeof(T), n); // check mem_size
return raw_bytes(bytes) + n; // and check bytes
}
private:
// membuffer - n -> pointer; not allowed - use raw_bytes() if needed
template <class U>
@ -94,7 +93,7 @@ private:
// global operators
#if ALLOW_INT_PLUS_MEMBUFFER
// rewrite "n + membuffer" to "membuffer + n" so that this will get checked above
// rewrite "n + membuffer" to "membuffer + n" (member function) so that this will get checked above
template <class T, class U>
inline typename std::enable_if<std::is_integral<U>::value, typename MemBufferBase<T>::pointer>::type
operator+(U n, const MemBufferBase<T> &mbb) {
@ -121,8 +120,17 @@ inline typename MemBufferBase<T>::pointer raw_index_bytes(const MemBufferBase<T>
}
// some more global overloads using a checked raw_bytes() call
#if __cplusplus >= 201703L
#define XSPAN_REQUIRES_CONVERTIBLE_ANY_DIRECTION(A, B, RType) \
std::enable_if_t<std::is_convertible_v<A *, B *> || std::is_convertible_v<B *, A *>, RType>
#elif __cplusplus >= 201103L
#define XSPAN_REQUIRES_CONVERTIBLE_ANY_DIRECTION(A, B, RType) \
typename std::enable_if< \
std::is_convertible<A *, B *>::value || std::is_convertible<B *, A *>::value, RType>::type
#else
#define XSPAN_REQUIRES_CONVERTIBLE_ANY_DIRECTION(A, B, RType) \
typename std::enable_if<std::is_same<A, B>::value, RType>::type
#endif
#define C MemBufferBase
#define XSPAN_FWD_C_IS_MEMBUFFER 1
#if WITH_XSPAN >= 1

View File

@ -80,6 +80,7 @@ using XSPAN_NAMESPACE_NAME::raw_index_bytes; // overloaded for all classes
// fully checked
// types
#define XSPAN_0(type) PtrOrSpanOrNull<type>
#define XSPAN_P(type) PtrOrSpan<type>
#define XSPAN_S(type) Span<type>
@ -98,6 +99,7 @@ using XSPAN_NAMESPACE_NAME::raw_index_bytes; // overloaded for all classes
// unchecked - just a no-op pointer wrapper, no extra functionality
// types
#define XSPAN_0(type) Ptr<type>
#define XSPAN_P(type) Ptr<type>
#define XSPAN_S(type) Ptr<type>
@ -130,6 +132,7 @@ inline R *xspan_make_helper__(R * /*dummy*/, MemBuffer &mb) noexcept {
return (R *) membuffer_get_void_ptr(mb);
}
// types
#define XSPAN_0(type) type *
#define XSPAN_P(type) type *
#define XSPAN_S(type) type *
@ -151,12 +154,15 @@ inline R *xspan_make_helper__(R * /*dummy*/, MemBuffer &mb) noexcept {
**************************************************************************/
#if 1
// types
#define SPAN_0 XSPAN_0
#define SPAN_P XSPAN_P
#define SPAN_S XSPAN_S
// create a value
#define SPAN_0_MAKE XSPAN_0_MAKE
#define SPAN_P_MAKE XSPAN_P_MAKE
#define SPAN_S_MAKE XSPAN_S_MAKE
// define a variable
#define SPAN_0_VAR XSPAN_0_VAR
#define SPAN_P_VAR XSPAN_P_VAR
#define SPAN_S_VAR XSPAN_S_VAR

View File

@ -24,14 +24,22 @@
<markus@oberhumer.com>
*/
// manually forward a number of well-known functions using a
// checked "raw_bytes()" call
// manually forward a number of well-known functions using a checked "raw_bytes()" call
//
// uses C
// uses optional D, E and XSPAN_FWD_C_IS_MEMBUFFER
// needs XSPAN_REQUIRES_CONVERTIBLE_ANY_DIRECTION
//
// example for a default implementation:
// #define XSPAN_REQUIRES_CONVERTIBLE_ANY_DIRECTION(A, B, RType)
// std::enable_if_t<std::is_convertible_v<A *, B *> || std::is_convertible_v<B *, A *>, RType>
#define XSPAN_FWD_TU(RType) \
template <class T, class U> \
inline XSPAN_REQUIRES_CONVERTIBLE_ANY_DIRECTION(T, U, RType)
#ifndef XSPAN_FWD_C_IS_MEMBUFFER
// global operator: disallow "n + C" => force using "C + n" (member function) instead
template <class T, class U>
inline typename std::enable_if<std::is_integral<U>::value, void *>::type operator+(U, const C<T> &)
XSPAN_DELETED_FUNCTION;

View File

@ -52,11 +52,11 @@ void xspan_check_range(const void *p, const void *base, ptrdiff_t size_in_bytes)
// help constructor to distinguish between number of elements and bytes
struct XSpanCount {
explicit XSpanCount(size_t n) noexcept : count(n) {}
explicit forceinline XSpanCount(size_t n) noexcept : count(n) {}
size_t count; // public
};
struct XSpanSizeInBytes {
explicit XSpanSizeInBytes(size_t bytes) noexcept : size_in_bytes(bytes) {}
explicit forceinline XSpanSizeInBytes(size_t bytes) noexcept : size_in_bytes(bytes) {}
size_t size_in_bytes; // public
};
@ -109,12 +109,16 @@ inline void xspan_mem_size_assert_ptrdiff(ptrdiff_t n) {
(void) xspan_mem_size<T>((size_t) -n);
}
#if 0
#if __cplusplus >= 201103L && 0
// unfortunately doesnt't work with some older versions of libstdc++
// (TODO later: we now require C++17, so this now probably works on all supported platforms)
template <class From, class To>
struct XSpan_is_convertible : public std::is_convertible<From *, To *> {};
#else
// manual implementation
namespace detail {
// helper for "void"
template <class T, class U>
struct XSpan_void_to_T {
typedef U type;
@ -125,7 +129,6 @@ struct XSpan_void_to_T<T, void> {
};
template <class T>
struct XSpan_void_to_T<T, const void> {
// typedef typename std::add_const<T>::type type;
typedef T type;
};
@ -180,7 +183,6 @@ ACC_COMPILE_TIME_ASSERT_HEADER((!XSpan_is_convertible<const char, int>::value))
**************************************************************************/
// forward declarations
template <class T>
struct PtrOrSpanOrNull;
template <class T>
@ -190,13 +192,24 @@ struct Span;
template <class T>
struct Ptr;
class XSpanInternalDummyArg; // not implemented
// XSpanInternalDummyArg: some type that is hard to match by accident
class XSpanInternalDummyArgFake; // not implemented on purpose
#if 0
typedef XSpanInternalDummyArgFake *XSpanInternalDummyArg;
#define XSpanInternalDummyArgInit nullptr
#else
struct XSpanInternalDummyArg {
explicit forceinline XSpanInternalDummyArg(int, XSpanInternalDummyArgFake *) noexcept {}
};
#define XSpanInternalDummyArgInit (XSPAN_NS(XSpanInternalDummyArg)(0, nullptr))
#endif
XSPAN_NAMESPACE_END
#ifndef XSPAN_DELETED_FUNCTION
#define XSPAN_DELETED_FUNCTION = delete
#endif
// function/method constraints
#define XSPAN_REQUIRES_CONVERTIBLE_ONE_DIRECTION(From, To, RType) \
typename std::enable_if<XSPAN_NS(XSpan_is_convertible) < From, To>::value, RType > ::type
#define XSPAN_REQUIRES_CONVERTIBLE_ANY_DIRECTION(A, B, RType) \
@ -206,15 +219,16 @@ XSPAN_NAMESPACE_END
// note: these use "T" and "U"
#define XSPAN_REQUIRES_CONVERTIBLE_R(RType) XSPAN_REQUIRES_CONVERTIBLE_ONE_DIRECTION(U, T, RType)
#define XSPAN_REQUIRES_CONVERTIBLE_A \
XSPAN_REQUIRES_CONVERTIBLE_R(XSPAN_NS(XSpanInternalDummyArg) *) = nullptr
#define XSPAN_REQUIRES_CONVERTIBLE_T XSPAN_REQUIRES_CONVERTIBLE_R(XSPAN_NS(XSpanInternalDummyArg) *)
XSPAN_REQUIRES_CONVERTIBLE_R(XSPAN_NS(XSpanInternalDummyArg)) = XSpanInternalDummyArgInit
#define XSPAN_REQUIRES_CONVERTIBLE_T XSPAN_REQUIRES_CONVERTIBLE_R(XSPAN_NS(XSpanInternalDummyArg))
// note: these use "T" and "U"
#define XSPAN_REQUIRES_SIZE_1_R(RType) \
typename std::enable_if<XSPAN_NS(XSpan_is_convertible) < U, T>::value &&XSPAN_NS( \
ValueForSizeOf)<T>::value == 1 && \
XSPAN_NS(ValueForSizeOf)<U>::value == 1, \
RType > ::type
#define XSPAN_REQUIRES_SIZE_1_A XSPAN_REQUIRES_SIZE_1_R(XSPAN_NS(XSpanInternalDummyArg) *) = nullptr
#define XSPAN_REQUIRES_SIZE_1_A \
XSPAN_REQUIRES_SIZE_1_R(XSPAN_NS(XSpanInternalDummyArg)) = XSpanInternalDummyArgInit
#include "xspan_impl_ptr_or_null.h"
#include "xspan_impl_ptr_or_span.h"

View File

@ -68,7 +68,7 @@ private:
xspan_check_range(ptr, base, size_in_bytes);
}
#else
inline void assertInvariants() const noexcept {}
forceinline void assertInvariants() const noexcept {}
#endif
static inline pointer makeNotNull(pointer p) {
@ -103,7 +103,7 @@ public:
#if DEBUG
inline ~CSelf() { invalidate(); }
#else
inline ~CSelf() noexcept {}
forceinline ~CSelf() noexcept {}
#endif
noinline void invalidate() {
assertInvariants();

View File

@ -182,9 +182,9 @@ public:
#endif
private:
forceinline pointer check_deref(pointer p) const { return p; }
forceinline pointer check_deref(pointer p, ptrdiff_t n) const { return p + n; }
forceinline pointer check_add(pointer p, ptrdiff_t n) const { return p + n; }
static forceinline pointer check_deref(pointer p) noexcept { return p; }
static forceinline pointer check_deref(pointer p, ptrdiff_t n) noexcept { return p + n; }
static forceinline pointer check_add(pointer p, ptrdiff_t n) noexcept { return p + n; }
public: // raw access
pointer raw_ptr() const noexcept { return ptr; }

View File

@ -86,7 +86,7 @@ public:
}
// nullptr
CSelf(std::nullptr_t) : ptr(nullptr), base(nullptr), size_in_bytes(0) {}
forceinline CSelf(std::nullptr_t) noexcept : ptr(nullptr), base(nullptr), size_in_bytes(0) {}
#undef CSelf
};