From dfe9d8ea2f39900d128917d0e88414fb9cd34563 Mon Sep 17 00:00:00 2001 From: Olly Betts Date: Thu, 21 Nov 2024 16:39:18 +1300 Subject: [PATCH] [Java] Fix regression wrapping enum values Fix regression wrapping enum values which don't fit in a Java signed int. Fixes #3070 --- CHANGES.current | 4 ++++ Examples/test-suite/enum_thorough.i | 16 +++++++++++++++- Source/Modules/java.cxx | 20 +++++++++++++++++++- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/CHANGES.current b/CHANGES.current index c7e1a4cfe..f652ea5e8 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -7,6 +7,10 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/ Version 4.4.0 (in progress) =========================== +2024-11-21: olly + [Java] #3070 Fix regression wrapping enum values which don't fit + in a Java signed int. + 2024-11-09: wsfulton #3064 Perform repeated typedef lookups instead of a single typedef lookup on the type being applied in %apply when looking for a family diff --git a/Examples/test-suite/enum_thorough.i b/Examples/test-suite/enum_thorough.i index e71806cee..15d98af11 100644 --- a/Examples/test-suite/enum_thorough.i +++ b/Examples/test-suite/enum_thorough.i @@ -578,6 +578,14 @@ enumWithMacro enumWithMacroTest(enumWithMacro e) { return e; } } %} +#ifdef SWIGCSHARP +// It seems these never worked for SWIG/C#. +%ignore global_typeunsigned1; +%ignore global_typeunsigned2; +%ignore global_typeunsigned3; +%ignore global_typeunsigned4; +#endif + %inline %{ namespace DifferentSpace { enum DifferentTypes { @@ -600,7 +608,13 @@ enum { global_typechar = 'C', global_typedefaultint, global_typecharcompound='A'+1, - global_typecharcompound2='B' << 2 + global_typecharcompound2='B' << 2, + // Regression tests for #3070 affecting SWIG/Java, introduced in 4.3.0: + global_typeunsigned1 = 0x80000000, + global_typeunsigned2 = 0xffffffff, + // These should also work in > 4.3.0. + global_typeunsigned3 = 2147483648, + global_typeunsigned4 = 4294967295 }; int globalDifferentTypesTest(int n) { return n; } } diff --git a/Source/Modules/java.cxx b/Source/Modules/java.cxx index b28e71006..9f6edf71d 100644 --- a/Source/Modules/java.cxx +++ b/Source/Modules/java.cxx @@ -1424,7 +1424,25 @@ public: } } else { String *numval = Getattr(n, "enumnumval"); - if (numval) Setattr(n, "enumvalue", numval); + if (numval) { + const char *p = Char(numval); + if (isdigit(p[0])) { + char *e; + unsigned long long value = strtoull(p, &e, 0); + if (errno != ERANGE && *e == '\0' && value >= 0x80000000) { + // Use hex for larger unsigned integer constants in Java code since + // Java allows implicit conversion to a signed integer value. + String *hexval = NewStringf("0x%llx", value); + Setattr(n, "enumvalue", hexval); + Delete(hexval); + } else { + Setattr(n, "enumvalue", numval); + } + } else { + // Emit negative values as-is. + Setattr(n, "enumvalue", numval); + } + } } {