From 51a6a5cca5c91a9381b2beb1282a351995278226 Mon Sep 17 00:00:00 2001 From: "Markus F.X.J. Oberhumer" Date: Wed, 20 Dec 2023 11:44:01 +0100 Subject: [PATCH] src: rename ptr_reinterpret_cast to ptr_static_cast; misc cleanups --- CMakeLists.txt | 2 +- Makefile | 4 +- misc/analyze/clang-tidy/run-clang-tidy.py | 16 +++---- src/Makefile | 12 +++--- src/check/dt_cxxlib.cpp | 52 +++++++++++------------ src/util/cxxlib.h | 10 ++--- src/util/xspan.h | 2 +- src/util/xspan_impl_common.h | 4 +- src/util/xspan_impl_ptr.h | 2 +- 9 files changed, 54 insertions(+), 50 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7bc98a2c..298e2dce 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -456,7 +456,7 @@ if(Threads_FOUND) OUTPUT_VARIABLE output) if(NOT result) # failed; under MinGW be sure to use the posix-threads and NOT the win32-threads version - #message(STATUS "${output}") # debug output from try_compile + #message(STATUS "Threads FAILED: ${output}") # debug output from try_compile set(Threads_FOUND OFF) break() endif() diff --git a/Makefile b/Makefile index 819c1a49..3b3f3b69 100644 --- a/Makefile +++ b/Makefile @@ -66,7 +66,9 @@ endif CTEST = ctest test:: $(.DEFAULT_GOAL) PHONY cd $(.DEFAULT_GOAL) && $(CTEST) -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. +ifneq ($(wildcard ./misc/scripts/.),) check-whitespace clang-format run-testsuite run-testsuite-debug run-testsuite-release: src/Makefile PHONY $(MAKE) -C src $@ endif +endif diff --git a/misc/analyze/clang-tidy/run-clang-tidy.py b/misc/analyze/clang-tidy/run-clang-tidy.py index 79056a62..ad2a1519 100755 --- a/misc/analyze/clang-tidy/run-clang-tidy.py +++ b/misc/analyze/clang-tidy/run-clang-tidy.py @@ -257,7 +257,7 @@ def main(): parser.add_argument( "-allow-enabling-alpha-checkers", action="store_true", - help="allow alpha checkers from " "clang-analyzer.", + help="allow alpha checkers from clang-analyzer.", ) parser.add_argument( "-clang-tidy-binary", metavar="PATH", help="path to clang-tidy binary" @@ -270,7 +270,7 @@ def main(): parser.add_argument( "-checks", default=None, - help="checks filter, when not specified, use clang-tidy " "default", + help="checks filter, when not specified, use clang-tidy default", ) config_group = parser.add_mutually_exclusive_group() config_group.add_argument( @@ -303,7 +303,7 @@ def main(): parser.add_argument( "-line-filter", default=None, - help="List of files with line ranges to filter the" "warnings.", + help="List of files with line ranges to filter the warnings.", ) if yaml: parser.add_argument( @@ -335,12 +335,12 @@ def main(): ) parser.add_argument("-fix", action="store_true", help="apply fix-its") parser.add_argument( - "-format", action="store_true", help="Reformat code " "after applying fixes" + "-format", action="store_true", help="Reformat code after applying fixes" ) parser.add_argument( "-style", default="file", - help="The style of reformat " "code after applying fixes", + help="The style of reformat code after applying fixes", ) parser.add_argument( "-use-color", @@ -359,14 +359,14 @@ def main(): dest="extra_arg", action="append", default=[], - help="Additional argument to append to the compiler " "command line.", + help="Additional argument to append to the compiler command line.", ) parser.add_argument( "-extra-arg-before", dest="extra_arg_before", action="append", default=[], - help="Additional argument to prepend to the compiler " "command line.", + help="Additional argument to prepend to the compiler command line.", ) parser.add_argument( "-quiet", action="store_true", help="Run clang-tidy in quiet mode" @@ -381,7 +381,7 @@ def main(): parser.add_argument( "-warnings-as-errors", default=None, - help="Upgrades warnings to errors. Same format as " "'-checks'", + help="Upgrades warnings to errors. Same format as '-checks'", ) args = parser.parse_args() diff --git a/src/Makefile b/src/Makefile index 0d713588..57d37b7d 100644 --- a/src/Makefile +++ b/src/Makefile @@ -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" diff --git a/src/check/dt_cxxlib.cpp b/src/check/dt_cxxlib.cpp index a221a0ed..faea0ab3 100644 --- a/src/check/dt_cxxlib.cpp +++ b/src/check/dt_cxxlib.cpp @@ -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(vp))); - assert((vp == ptr_reinterpret_cast(bp))); - assert((vp == ptr_reinterpret_cast(ip))); - assert((vp == ptr_reinterpret_cast(dp))); + assert((vp == ptr_static_cast(vp))); + assert((vp == ptr_static_cast(bp))); + assert((vp == ptr_static_cast(ip))); + assert((vp == ptr_static_cast(dp))); - assert((bp == ptr_reinterpret_cast(vp))); - assert((bp == ptr_reinterpret_cast(bp))); - assert((bp == ptr_reinterpret_cast(ip))); - assert((bp == ptr_reinterpret_cast(dp))); + assert((bp == ptr_static_cast(vp))); + assert((bp == ptr_static_cast(bp))); + assert((bp == ptr_static_cast(ip))); + assert((bp == ptr_static_cast(dp))); - assert((ip == ptr_reinterpret_cast(vp))); - assert((ip == ptr_reinterpret_cast(bp))); - assert((ip == ptr_reinterpret_cast(ip))); - assert((ip == ptr_reinterpret_cast(dp))); + assert((ip == ptr_static_cast(vp))); + assert((ip == ptr_static_cast(bp))); + assert((ip == ptr_static_cast(ip))); + assert((ip == ptr_static_cast(dp))); - assert((dp == ptr_reinterpret_cast(vp))); - assert((dp == ptr_reinterpret_cast(bp))); - assert((dp == ptr_reinterpret_cast(ip))); - assert((dp == ptr_reinterpret_cast(dp))); + assert((dp == ptr_static_cast(vp))); + assert((dp == ptr_static_cast(bp))); + assert((dp == ptr_static_cast(ip))); + assert((dp == ptr_static_cast(dp))); const byte *bc = nullptr; const int *ic = nullptr; - assert((bc == ptr_reinterpret_cast(bp))); - assert((bc == ptr_reinterpret_cast(bc))); - assert((bc == ptr_reinterpret_cast(ip))); - assert((bc == ptr_reinterpret_cast(ic))); - assert((ic == ptr_reinterpret_cast(bp))); - assert((ic == ptr_reinterpret_cast(bc))); - assert((ic == ptr_reinterpret_cast(ip))); - assert((ic == ptr_reinterpret_cast(ic))); + assert((bc == ptr_static_cast(bp))); + assert((bc == ptr_static_cast(bc))); + assert((bc == ptr_static_cast(ip))); + assert((bc == ptr_static_cast(ic))); + assert((ic == ptr_static_cast(bp))); + assert((ic == ptr_static_cast(bc))); + assert((ic == ptr_static_cast(ip))); + assert((ic == ptr_static_cast(ic))); } TEST_CASE("noncopyable") { diff --git a/src/util/cxxlib.h b/src/util/cxxlib.h index 5ebd26a4..416de3fd 100644 --- a/src/util/cxxlib.h +++ b/src/util/cxxlib.h @@ -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 -forceinline Result ptr_reinterpret_cast(From *ptr) noexcept { +forceinline Result ptr_static_cast(From *ptr) noexcept { static_assert(std::is_pointer_v); static_assert(!std::is_const_v >); // enforce same constness - return reinterpret_cast(reinterpret_cast(ptr)); + return static_cast(static_cast(ptr)); } template -forceinline Result ptr_reinterpret_cast(const From *ptr) noexcept { +forceinline Result ptr_static_cast(const From *ptr) noexcept { static_assert(std::is_pointer_v); static_assert(std::is_const_v >); // required - return reinterpret_cast(reinterpret_cast(ptr)); + return static_cast(static_cast(ptr)); } class noncopyable { diff --git a/src/util/xspan.h b/src/util/xspan.h index 67488e5c..46b8b1d6 100644 --- a/src/util/xspan.h +++ b/src/util/xspan.h @@ -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(x)) +#define XSPAN_TYPE_CAST(type, x) (upx::ptr_static_cast(x)) // poison a pointer: point to a non-null invalid address #define XSPAN_INVALIDATE(x) ptr_invalidate_and_poison(x) diff --git a/src/util/xspan_impl_common.h b/src/util/xspan_impl_common.h index 595c18d5..02bd0756 100644 --- a/src/util/xspan_impl_common.h +++ b/src/util/xspan_impl_common.h @@ -284,8 +284,8 @@ public: inline CSelf type_cast() const { typedef CSelf R; typedef typename R::pointer rpointer; - return R(R::Unchecked, upx::ptr_reinterpret_cast(ptr), size_in_bytes, - upx::ptr_reinterpret_cast(base)); + return R(R::Unchecked, upx::ptr_static_cast(ptr), size_in_bytes, + upx::ptr_static_cast(base)); } bool operator==(pointer other) const noexcept { return ptr == other; } diff --git a/src/util/xspan_impl_ptr.h b/src/util/xspan_impl_ptr.h index 4622803e..8dd93a47 100644 --- a/src/util/xspan_impl_ptr.h +++ b/src/util/xspan_impl_ptr.h @@ -127,7 +127,7 @@ public: inline CSelf type_cast() const { typedef CSelf R; typedef typename R::pointer rpointer; - return R(upx::ptr_reinterpret_cast(ptr)); + return R(upx::ptr_static_cast(ptr)); } // comparison