src: rename ptr_reinterpret_cast to ptr_static_cast; misc cleanups

This commit is contained in:
Markus F.X.J. Oberhumer
2023-12-20 11:44:01 +01:00
parent 3326c86e91
commit 51a6a5cca5
9 changed files with 54 additions and 50 deletions
+7 -5
View File
@@ -44,10 +44,10 @@ test:: $(top_srcdir)/build/release PHONY; cd $< && $(CTEST)
#
# "make run-testsuite"
# see https://github.com/upx/upx-testsuite.git
# git clone https://github.com/upx/upx-testsuite.git
#
# search for the UPX testsuite -- git clone https://github.com/upx/upx-testsuite.git
# search for the UPX testsuite
# you also can override upx_testsuite_SRCDIR
ifndef upx_testsuite_SRCDIR
# search standard locations below $(top_srcdir)
@@ -62,15 +62,16 @@ endif
# run the UPX testsuite
# The expected (old) checksums are in $(top_srcdir)/misc/testsuite/upx_testsuite_1-expected_sha256sums.sh
# The actual (new) checksums are in ./tmp-upx-testsuite/testsuite_1/.sha256sums.recreate
# The actual (new) checksums are in ./tmp-upx-testsuite-*/testsuite_1/.sha256sums.recreate
ifneq ($(wildcard $(upx_testsuite_SRCDIR)/files/packed/.),)
ifneq ($(wildcard $(top_srcdir)/misc/testsuite/upx_testsuite_1.sh),)
run-testsuite: run-testsuite-release
run-testsuite-%: export upx_testsuite_SRCDIR := $(upx_testsuite_SRCDIR)
run-testsuite-%: export upx_testsuite_BUILDDIR := ./tmp-upx-testsuite
run-testsuite-debug: export upx_testsuite_BUILDDIR := ./tmp-upx-testsuite-debug
run-testsuite-debug: export upx_exe := $(top_srcdir)/build/debug/upx
run-testsuite-debug: $(top_srcdir)/build/debug PHONY
time -p bash $(top_srcdir)/misc/testsuite/upx_testsuite_1.sh
run-testsuite-release: export upx_testsuite_BUILDDIR := ./tmp-upx-testsuite-release
run-testsuite-release: export upx_exe := $(top_srcdir)/build/release/upx
run-testsuite-release: $(top_srcdir)/build/release PHONY
time -p bash $(top_srcdir)/misc/testsuite/upx_testsuite_1.sh
@@ -81,7 +82,7 @@ endif
# "make check-whitespace"
#
ifneq ($(wildcard /usr/bin/env),) # needs Unix utils like bash, perl, sed, xargs, etc.
ifneq ($(wildcard /usr/bin/env),) # need Unix utils like bash, perl, sed, xargs, etc.
CHECK_WHITESPACE = bash $(top_srcdir)/misc/scripts/check_whitespace.sh $(top_srcdir)
ifneq ($(wildcard $(top_srcdir)/.git/.),)
CHECK_WHITESPACE = bash $(top_srcdir)/misc/scripts/check_whitespace_git.sh $(top_srcdir)
@@ -100,6 +101,7 @@ CLANG_FORMAT_EXCLUDE_FILES += miniacc.h stub/%.h
CLANG_FORMAT_EXCLUDE_FILES += p_elf.h p_elf_enum.h p_lx_% p_mach% p_unix% p_vmlin%
CLANG_FORMAT_FILES := $(sort $(wildcard *.[ch]* ../maint/src/*.[ch]* */*.[ch]*))
CLANG_FORMAT_FILES += $(sort $(wildcard stub/tools/*/*.[ch]*))
CLANG_FORMAT_FILES += $(sort $(wildcard ../misc/cmake/try_compile/*.[ch]*))
CLANG_FORMAT_FILES := $(filter-out $(CLANG_FORMAT_EXCLUDE_FILES),$(CLANG_FORMAT_FILES))
clang-format: $(CLANG_FORMAT_FILES) PHONY
@echo "running upx-clang-format"
+26 -26
View File
@@ -101,44 +101,44 @@ ACC_COMPILE_TIME_ASSERT_HEADER(compile_time::string_le("abc", "abz"))
// util
**************************************************************************/
TEST_CASE("ptr_reinterpret_cast") {
TEST_CASE("ptr_static_cast") {
// check that we don't trigger any -Wcast-align warnings
using upx::ptr_reinterpret_cast;
using upx::ptr_static_cast;
void *vp = nullptr;
byte *bp = nullptr;
int *ip = nullptr;
double *dp = nullptr;
assert((vp == ptr_reinterpret_cast<void *>(vp)));
assert((vp == ptr_reinterpret_cast<void *>(bp)));
assert((vp == ptr_reinterpret_cast<void *>(ip)));
assert((vp == ptr_reinterpret_cast<void *>(dp)));
assert((vp == ptr_static_cast<void *>(vp)));
assert((vp == ptr_static_cast<void *>(bp)));
assert((vp == ptr_static_cast<void *>(ip)));
assert((vp == ptr_static_cast<void *>(dp)));
assert((bp == ptr_reinterpret_cast<byte *>(vp)));
assert((bp == ptr_reinterpret_cast<byte *>(bp)));
assert((bp == ptr_reinterpret_cast<byte *>(ip)));
assert((bp == ptr_reinterpret_cast<byte *>(dp)));
assert((bp == ptr_static_cast<byte *>(vp)));
assert((bp == ptr_static_cast<byte *>(bp)));
assert((bp == ptr_static_cast<byte *>(ip)));
assert((bp == ptr_static_cast<byte *>(dp)));
assert((ip == ptr_reinterpret_cast<int *>(vp)));
assert((ip == ptr_reinterpret_cast<int *>(bp)));
assert((ip == ptr_reinterpret_cast<int *>(ip)));
assert((ip == ptr_reinterpret_cast<int *>(dp)));
assert((ip == ptr_static_cast<int *>(vp)));
assert((ip == ptr_static_cast<int *>(bp)));
assert((ip == ptr_static_cast<int *>(ip)));
assert((ip == ptr_static_cast<int *>(dp)));
assert((dp == ptr_reinterpret_cast<double *>(vp)));
assert((dp == ptr_reinterpret_cast<double *>(bp)));
assert((dp == ptr_reinterpret_cast<double *>(ip)));
assert((dp == ptr_reinterpret_cast<double *>(dp)));
assert((dp == ptr_static_cast<double *>(vp)));
assert((dp == ptr_static_cast<double *>(bp)));
assert((dp == ptr_static_cast<double *>(ip)));
assert((dp == ptr_static_cast<double *>(dp)));
const byte *bc = nullptr;
const int *ic = nullptr;
assert((bc == ptr_reinterpret_cast<byte *>(bp)));
assert((bc == ptr_reinterpret_cast<const byte *>(bc)));
assert((bc == ptr_reinterpret_cast<byte *>(ip)));
assert((bc == ptr_reinterpret_cast<const byte *>(ic)));
assert((ic == ptr_reinterpret_cast<int *>(bp)));
assert((ic == ptr_reinterpret_cast<const int *>(bc)));
assert((ic == ptr_reinterpret_cast<int *>(ip)));
assert((ic == ptr_reinterpret_cast<const int *>(ic)));
assert((bc == ptr_static_cast<byte *>(bp)));
assert((bc == ptr_static_cast<const byte *>(bc)));
assert((bc == ptr_static_cast<byte *>(ip)));
assert((bc == ptr_static_cast<const byte *>(ic)));
assert((ic == ptr_static_cast<int *>(bp)));
assert((ic == ptr_static_cast<const int *>(bc)));
assert((ic == ptr_static_cast<int *>(ip)));
assert((ic == ptr_static_cast<const int *>(ic)));
}
TEST_CASE("noncopyable") {
+5 -5
View File
@@ -63,18 +63,18 @@ struct UnsignedSizeOf {
static constexpr unsigned value = unsigned(Size);
};
// a reinterpret_cast that does not trigger -Wcast-align warnings
// a static_cast that does not trigger -Wcast-align warnings
template <class Result, class From>
forceinline Result ptr_reinterpret_cast(From *ptr) noexcept {
forceinline Result ptr_static_cast(From *ptr) noexcept {
static_assert(std::is_pointer_v<Result>);
static_assert(!std::is_const_v<std::remove_pointer_t<Result> >); // enforce same constness
return reinterpret_cast<Result>(reinterpret_cast<void *>(ptr));
return static_cast<Result>(static_cast<void *>(ptr));
}
template <class Result, class From>
forceinline Result ptr_reinterpret_cast(const From *ptr) noexcept {
forceinline Result ptr_static_cast(const From *ptr) noexcept {
static_assert(std::is_pointer_v<Result>);
static_assert(std::is_const_v<std::remove_pointer_t<Result> >); // required
return reinterpret_cast<Result>(reinterpret_cast<const void *>(ptr));
return static_cast<Result>(static_cast<const void *>(ptr));
}
class noncopyable {
+1 -1
View File
@@ -159,7 +159,7 @@ inline R *xspan_make_helper__(MemBuffer &mb) noexcept {
#define XSPAN_S_VAR(type, var, first, ...) type *var = XSPAN_S_MAKE(type, (first))
// cast to a different type (creates a new value)
#define XSPAN_TYPE_CAST(type, x) (upx::ptr_reinterpret_cast<type *>(x))
#define XSPAN_TYPE_CAST(type, x) (upx::ptr_static_cast<type *>(x))
// poison a pointer: point to a non-null invalid address
#define XSPAN_INVALIDATE(x) ptr_invalidate_and_poison(x)
+2 -2
View File
@@ -284,8 +284,8 @@ public:
inline CSelf<U> type_cast() const {
typedef CSelf<U> R;
typedef typename R::pointer rpointer;
return R(R::Unchecked, upx::ptr_reinterpret_cast<rpointer>(ptr), size_in_bytes,
upx::ptr_reinterpret_cast<rpointer>(base));
return R(R::Unchecked, upx::ptr_static_cast<rpointer>(ptr), size_in_bytes,
upx::ptr_static_cast<rpointer>(base));
}
bool operator==(pointer other) const noexcept { return ptr == other; }
+1 -1
View File
@@ -127,7 +127,7 @@ public:
inline CSelf<U> type_cast() const {
typedef CSelf<U> R;
typedef typename R::pointer rpointer;
return R(upx::ptr_reinterpret_cast<rpointer>(ptr));
return R(upx::ptr_static_cast<rpointer>(ptr));
}
// comparison