From 0a1601b2428f30df6eb7766a8716a8538cd97e04 Mon Sep 17 00:00:00 2001 From: Ivan Maidanski Date: Thu, 20 Oct 2022 08:26:46 +0300 Subject: Replace C-style casts in cpptest (refactoring) * tests/cpp.cc (A::A, D::CleanUp, main): Use static_cast or reinterpret_cast instead of C-style cast. * tests/cpp.cc (Disguise): Use GC_HIDE_POINTER macro. * tests/cpp.cc (Undisguise): Use GC_REVEAL_POINTER macro. --- tests/cpp.cc | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'tests') diff --git a/tests/cpp.cc b/tests/cpp.cc index 040df4ef..0aa9d835 100644 --- a/tests/cpp.cc +++ b/tests/cpp.cc @@ -72,7 +72,7 @@ extern "C" { class A {public: /* An uncollectible class. */ - GC_ATTR_EXPLICIT A( int iArg ): i((A_I_TYPE)iArg) {} + GC_ATTR_EXPLICIT A(int iArg): i(static_cast(iArg)) {} void Test( int iArg ) { my_assert( i == iArg );} virtual ~A() {} @@ -174,7 +174,9 @@ class D: public GC_NS_QUALIFY(gc) { public: static void CleanUp( void* obj, void* data ) { D* self = static_cast(obj); nFreed++; - my_assert( (GC_word)self->i == (GC_word)data );} + my_assert(static_cast(self->i) + == reinterpret_cast(data)); + } static void Test() { # ifndef GC_NO_FINALIZATION my_assert(nFreed >= (nAllocated / 5) * 4 || GC_get_find_leak()); @@ -233,10 +235,12 @@ int F::nAllocatedF = 0; GC_word Disguise( void* p ) { - return ~ (GC_word) p;} + return GC_HIDE_POINTER(p); +} void* Undisguise( GC_word i ) { - return (void*) ~ i;} + return GC_REVEAL_POINTER(i); +} #define GC_CHECKED_DELETE(p) \ { \ @@ -257,10 +261,11 @@ void* Undisguise( GC_word i ) { char* argv[ 3 ]; # if defined(CPPCHECK) - GC_noop1((GC_word)&WinMain); + GC_noop1(reinterpret_cast(&WinMain)); # endif if (cmd != 0) - for (argc = 1; argc < (int)(sizeof(argv) / sizeof(argv[0])); argc++) { + for (argc = 1; argc < static_cast(sizeof(argv) / sizeof(argv[0])); + argc++) { // Parse the command-line string. Non-reentrant strtok() is not used // to avoid complains of static analysis tools. (And, strtok_r() is // not available on some platforms.) The code is equivalent to: @@ -292,7 +297,7 @@ void* Undisguise( GC_word i ) { int main() { char* argv_[] = {"cpptest", "7"}; // MacOS doesn't have a command line argv = argv_; - argc = sizeof(argv_)/sizeof(argv_[0]); + argc = sizeof(argv_) / sizeof(argv_[0]); #else int main( int argc, char* argv[] ) { #endif @@ -352,7 +357,8 @@ void* Undisguise( GC_word i ) { C c1( 2 ); /* stack allocation should work too */ D* d; F* f; - d = ::new (USE_GC, D::CleanUp, (void*)(GC_word)i) D( i ); + d = ::new (USE_GC, D::CleanUp, + reinterpret_cast(static_cast(i))) D(i); GC_reachable_here(d); f = new F; F** fa = new F*[1]; -- cgit v1.2.1