From cd686cb1d99cae4a6386ec6c7dbb33d683095b2e Mon Sep 17 00:00:00 2001 From: "Markus F.X.J. Oberhumer" Date: Thu, 5 Jan 2023 00:57:05 +0100 Subject: [PATCH] src: add boost-pfr --- CMakeLists.txt | 2 +- Makefile | 3 +++ compile_flags.txt | 1 + src/conf.h | 28 ++++++++++++++++++++++++++++ src/util/dt_check.cpp | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 67 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 551d496c..531d55de 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -180,7 +180,7 @@ else() endif() set(t upx) -target_include_directories(${t} PRIVATE vendor) +target_include_directories(${t} PRIVATE vendor vendor/boost-pfr/include) target_compile_definitions(${t} PRIVATE $<$:DEBUG=1>) if(GITREV_SHORT) target_compile_definitions(${t} PRIVATE UPX_VERSION_GITREV="${GITREV_SHORT}${GITREV_PLUS}") diff --git a/Makefile b/Makefile index 9727c26b..219838e9 100644 --- a/Makefile +++ b/Makefile @@ -120,6 +120,9 @@ build/extra/cross-windows-mingw64/%: UPX_CMAKE_CONFIG_FLAGS += -DUPX_CONFIG_DISA # check git submodules #*********************************************************************** +ifeq ($(wildcard ./vendor/boost-pfr/include/.),) + $(error ERROR: missing git submodule; run 'git submodule update --init') +endif ifeq ($(wildcard ./vendor/doctest/doctest/.),) $(error ERROR: missing git submodule; run 'git submodule update --init') endif diff --git a/compile_flags.txt b/compile_flags.txt index 486ef41c..3826c7f8 100644 --- a/compile_flags.txt +++ b/compile_flags.txt @@ -1,5 +1,6 @@ -std=gnu++17 -Ivendor +-Ivendor/boost-pfr/include -fno-strict-aliasing -fno-strict-overflow -funsigned-char diff --git a/src/conf.h b/src/conf.h index 00b0b9b9..e78406c0 100644 --- a/src/conf.h +++ b/src/conf.h @@ -778,6 +778,34 @@ unsigned membuffer_get_size(MemBuffer &mb); //#define DOCTEST_CONFIG_DISABLE 1 #include +#if WITH_BOOST_PFR +#include +template +__acc_noinline std::string pfr_str(const A &a) { + std::ostringstream ss; + ss << boost::pfr::io(a); + return ss.str(); +} +template +__acc_noinline std::string pfr_str(const A &a, const B &b) { + std::ostringstream ss; + ss << boost::pfr::io(a); + ss << ' '; + ss << boost::pfr::io(b); + return ss.str(); +} +template +__acc_noinline std::string pfr_str(const A &a, const B &b, const C &c) { + std::ostringstream ss; + ss << boost::pfr::io(a); + ss << ' '; + ss << boost::pfr::io(b); + ss << ' '; + ss << boost::pfr::io(c); + return ss.str(); +} +#endif // WITH_BOOST_PFR + // util/dt_check.cpp void upx_compiler_sanity_check(); int upx_doctest_check(); diff --git a/src/util/dt_check.cpp b/src/util/dt_check.cpp index b7c54a43..ac31bb92 100644 --- a/src/util/dt_check.cpp +++ b/src/util/dt_check.cpp @@ -25,6 +25,11 @@ */ +#if DEBUG || 1 +#ifndef WITH_BOOST_PFR +#define WITH_BOOST_PFR 1 +#endif +#endif #include "../conf.h" /************************************************************************* @@ -448,4 +453,33 @@ TEST_CASE("libc snprintf") { CHECK_EQ(strcmp(buf, "-7.0.0.0.0.0.0.0.7.0xffffffffffffffff"), 0); } +#if WITH_BOOST_PFR +TEST_CASE("Boost.PFR") { + int i = -1; + CHECK_EQ(strcmp(pfr_str(i).c_str(), "-1"), 0); + BE32 b32; + b32 = 1; + LE32 l32; + l32 = 2; + CHECK_EQ(strcmp(pfr_str(b32).c_str(), "1"), 0); + CHECK_EQ(strcmp(pfr_str(l32).c_str(), "2"), 0); + struct Foo { + BE16 b16; + BE32 b32; + BE64 b64; + LE16 l16; + LE32 l32; + LE64 l64; + }; + Foo foo; + foo.b16 = 1; + foo.b32 = 2; + foo.b64 = 3; + foo.l16 = 4; + foo.l32 = 5; + foo.l64 = 6; + CHECK_EQ(strcmp(pfr_str("foo", "=", foo).c_str(), "foo = {1, 2, 3, 4, 5, 6}"), 0); +} +#endif // WITH_BOOST_PFR + /* vim:set ts=4 sw=4 et: */