From 2ad0e784ab148ec6bc93df69dc59f5db56f71ad8 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Fri, 22 Jan 2016 07:18:38 +0000 Subject: [PATCH 01/12] Minor documentation tweak --- Doc/Manual/Java.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/Manual/Java.html b/Doc/Manual/Java.html index dae6edc01..2b974da88 100644 --- a/Doc/Manual/Java.html +++ b/Doc/Manual/Java.html @@ -2288,7 +2288,7 @@ The jniclasscode pragma is quite useful for adding in a static block fo %pragma(java) jniclasscode=%{ static { try { - System.loadLibrary("example"); + System.loadLibrary("example"); } catch (UnsatisfiedLinkError e) { System.err.println("Native code library failed to load. \n" + e); System.exit(1); From 62c34fc9d94f15046b40cc3d488972eb3fdc8e89 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Tue, 26 Jan 2016 20:16:47 +0000 Subject: [PATCH 02/12] Add tests for enum values and static const member variables chars containing escape sequences --- Examples/test-suite/chartest.i | 19 ++++++++ Examples/test-suite/common.mk | 1 + Examples/test-suite/default_args.i | 4 ++ Examples/test-suite/enum_thorough.i | 44 ++++++++++++++++++- .../test-suite/python/default_args_runme.py | 18 ++++++++ 5 files changed, 85 insertions(+), 1 deletion(-) diff --git a/Examples/test-suite/chartest.i b/Examples/test-suite/chartest.i index e81cf54a4..cc30b51bc 100644 --- a/Examples/test-suite/chartest.i +++ b/Examples/test-suite/chartest.i @@ -12,4 +12,23 @@ char GetUnprintableChar() { return 0x7F; } +static const char globchar0 = '\0'; +static const char globchar1 = '\1'; +static const char globchar2 = '\n'; +static const char globcharA = 'A'; +static const char globcharB = '\102'; // B +static const char globcharC = '\x43'; // C +static const char globcharD = 0x44; // D +static const char globcharE = 69; // E + +struct CharTestClass { + static const char memberchar0 = '\0'; + static const char memberchar1 = '\1'; + static const char memberchar2 = '\n'; + static const char membercharA = 'A'; + static const char membercharB = '\102'; // B + static const char membercharC = '\x43'; // C + static const char membercharD = 0x44; // D + static const char membercharE = 69; // E +}; %} diff --git a/Examples/test-suite/common.mk b/Examples/test-suite/common.mk index 02bde2caa..206840759 100644 --- a/Examples/test-suite/common.mk +++ b/Examples/test-suite/common.mk @@ -379,6 +379,7 @@ CPP_TEST_CASES += \ static_array_member \ static_const_member \ static_const_member_2 \ + string_constants \ struct_initialization_cpp \ struct_value \ symbol_clash \ diff --git a/Examples/test-suite/default_args.i b/Examples/test-suite/default_args.i index d3014d386..02d860765 100644 --- a/Examples/test-suite/default_args.i +++ b/Examples/test-suite/default_args.i @@ -77,6 +77,10 @@ // char char chartest1(char c = 'x') { return c; } char chartest2(char c = '\0') { return c; } + char chartest3(char c = '\1') { return c; } + char chartest4(char c = '\n') { return c; } + char chartest5(char c = '\102') { return c; } // 'B' + char chartest6(char c = '\x43') { return c; } // 'C' // namespaces namespace AType { diff --git a/Examples/test-suite/enum_thorough.i b/Examples/test-suite/enum_thorough.i index 3ece5471b..70f02ac64 100644 --- a/Examples/test-suite/enum_thorough.i +++ b/Examples/test-suite/enum_thorough.i @@ -585,7 +585,50 @@ enum { }; int globalDifferentTypesTest(int n) { return n; } } +%} +#if defined(SWIGJAVA) +%javaconst(0) enumcharC; +%javaconst(0) globalenumcharC; +#elif defined(SWIGCSHARP) +%csconstvalue("1") globalenumchar1; +%csconstvalue("'B'") globalenumcharB; +%csconstvalue("1") enumchar1; +%csconstvalue("'B'") enumcharB; +#endif +%inline %{ +enum { + globalenumchar0 = '\0', + globalenumchar1 = '\1', + globalenumchar2 = '\n', + globalenumcharA = 'A', + globalenumcharB = '\102', // B + globalenumcharC = '\x43', // C + globalenumcharD = 0x44, // D + globalenumcharE = 69 // E +}; +enum EnumChar { + enumchar0 = '\0', + enumchar1 = '\1', + enumchar2 = '\n', + enumcharA = 'A', + enumcharB = '\102', // B + enumcharC = '\x43', // C + enumcharD = 0x44, // D + enumcharE = 69 // E +}; +struct EnumCharStruct { + enum EnumChar { + enumchar0 = '\0', + enumchar1 = '\1', + enumchar2 = '\n', + enumcharA = 'A', + enumcharB = '\102', // B + enumcharC = '\x43', // C + enumcharD = 0x44, // D + enumcharE = 69 // E + }; +}; %} #if defined(SWIGJAVA) @@ -614,5 +657,4 @@ enum { global_typedefaultint_noconst }; } - %} diff --git a/Examples/test-suite/python/default_args_runme.py b/Examples/test-suite/python/default_args_runme.py index 6610a4ec4..9d275e4a1 100644 --- a/Examples/test-suite/python/default_args_runme.py +++ b/Examples/test-suite/python/default_args_runme.py @@ -136,5 +136,23 @@ def run(module_name): if default_args.CDA().cdefaultargs_test2() != 1: raise RuntimeError + if default_args.chartest1() != 'x': + raise RuntimeError + + if default_args.chartest2() != '\0': + raise RuntimeError + + if default_args.chartest3() != '\1': + raise RuntimeError + + if default_args.chartest4() != '\n': + raise RuntimeError + + if default_args.chartest5() != 'B': + raise RuntimeError + + if default_args.chartest6() != 'C': + raise RuntimeError + if __name__ == "__main__": run('default_args') From 83584e504a28f0e133eca1dd39e5531f6cae5abf Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Tue, 26 Jan 2016 19:51:10 +0000 Subject: [PATCH 03/12] Java enum and static member variable escaping fix for chars For example: enum X { x = '\1' }; struct A { static const char a = '\n'; }; --- Source/Modules/java.cxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/Modules/java.cxx b/Source/Modules/java.cxx index afe8ca841..c8655a017 100644 --- a/Source/Modules/java.cxx +++ b/Source/Modules/java.cxx @@ -1375,7 +1375,7 @@ public: const char *val = Equal(Getattr(n, "enumvalue"), "true") ? "1" : "0"; Setattr(n, "enumvalue", val); } else if (swigtype == T_CHAR) { - String *val = NewStringf("'%s'", Getattr(n, "enumvalue")); + String *val = NewStringf("'%(escape)s'", Getattr(n, "enumvalue")); Setattr(n, "enumvalue", val); Delete(val); } @@ -1573,7 +1573,7 @@ public: // Alternative constant handling will use the C syntax to make a true Java constant and hope that it compiles as Java code if (Getattr(n, "wrappedasconstant")) { if (SwigType_type(t) == T_CHAR) - Printf(constants_code, "\'%s\';\n", Getattr(n, "staticmembervariableHandler:value")); + Printf(constants_code, "\'%(escape)s\';\n", Getattr(n, "staticmembervariableHandler:value")); else Printf(constants_code, "%s;\n", Getattr(n, "staticmembervariableHandler:value")); } else { From 246c90e876b339da3dad7c5bc94a0fdb1969e5fa Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Tue, 26 Jan 2016 20:19:17 +0000 Subject: [PATCH 04/12] Java char changes file update --- CHANGES.current | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/CHANGES.current b/CHANGES.current index e114acd09..4406f1225 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -5,6 +5,17 @@ See the RELEASENOTES file for a summary of changes in each release. Version 3.0.9 (in progress) =========================== +2016-01-26: wsfulton + [Java] Fix generated code parsing enum values using char escape sequences + when these values appear in the Java code (usually when using %javaconst) + such as: + + enum X { x1 = '\n', x2 = '\1' }; + + Similarly for static const member char variables such as: + + struct Y { static const char y = '\n'; } + 2016-01-12: olly [Javascript] Look for "nodejs" as well as "node", as it's packaged as the former on Debian. From 95eb6649ead4cb593e5e2c11f0899ac3b48c624d Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Wed, 27 Jan 2016 08:47:37 +0000 Subject: [PATCH 05/12] Expand char testing in enums and %constant --- Examples/test-suite/char_constant.i | 26 ++++++++++++ Examples/test-suite/enum_thorough.i | 64 +++++++++++++++++++++++++---- 2 files changed, 83 insertions(+), 7 deletions(-) diff --git a/Examples/test-suite/char_constant.i b/Examples/test-suite/char_constant.i index 5235b0581..96a0a520b 100644 --- a/Examples/test-suite/char_constant.i +++ b/Examples/test-suite/char_constant.i @@ -12,6 +12,32 @@ #define ESC_CONST '\1' #define NULL_CONST '\0' #define SPECIALCHAR 'á' +#define SPECIALCHAR2 '\n' +#define SPECIALCHARA 'A' +#define SPECIALCHARB '\102' // B +#define SPECIALCHARC '\x43' // C +#define SPECIALCHARD 0x44 // D +#define SPECIALCHARE 69 // E +#define SPECIALCHARAE1 'Æ' // AE (latin1 encoded) +#define SPECIALCHARAE2 '\306' // AE (latin1 encoded) +#define SPECIALCHARAE3 '\xC6' // AE (latin1 encoded) + +#if defined(SWIGJAVA) +%javaconst(1); +#endif + +#define X_ESC_CONST '\1' +#define X_NULL_CONST '\0' +#define X_SPECIALCHAR 'á' +#define X_SPECIALCHAR2 '\n' +#define X_SPECIALCHARA 'A' +#define X_SPECIALCHARB '\102' // B +#define X_SPECIALCHARC '\x43' // C +#define X_SPECIALCHARD 0x44 // D +#define X_SPECIALCHARE 69 // E +#define X_SPECIALCHARAE1 'Æ' // AE (latin1 encoded) +#define X_SPECIALCHARAE2 '\306' // AE (latin1 encoded) +#define X_SPECIALCHARAE3 '\xC6' // AE (latin1 encoded) %inline { diff --git a/Examples/test-suite/enum_thorough.i b/Examples/test-suite/enum_thorough.i index 70f02ac64..eb1052b69 100644 --- a/Examples/test-suite/enum_thorough.i +++ b/Examples/test-suite/enum_thorough.i @@ -587,10 +587,7 @@ int globalDifferentTypesTest(int n) { return n; } } %} -#if defined(SWIGJAVA) -%javaconst(0) enumcharC; -%javaconst(0) globalenumcharC; -#elif defined(SWIGCSHARP) +#if defined(SWIGCSHARP) %csconstvalue("1") globalenumchar1; %csconstvalue("'B'") globalenumcharB; %csconstvalue("1") enumchar1; @@ -605,7 +602,10 @@ enum { globalenumcharB = '\102', // B globalenumcharC = '\x43', // C globalenumcharD = 0x44, // D - globalenumcharE = 69 // E + globalenumcharE = 69, // E + globalenumcharAE1 = 'Æ', // AE (latin1 encoded) + globalenumcharAE2 = '\306', // AE (latin1 encoded) + globalenumcharAE3 = '\xC6' // AE (latin1 encoded) }; enum EnumChar { enumchar0 = '\0', @@ -615,7 +615,10 @@ enum EnumChar { enumcharB = '\102', // B enumcharC = '\x43', // C enumcharD = 0x44, // D - enumcharE = 69 // E + enumcharE = 69, // E + enumcharAE1 = 'Æ', // AE (latin1 encoded) + enumcharAE2 = '\306', // AE (latin1 encoded) + enumcharAE3 = '\xC6' // AE (latin1 encoded) }; struct EnumCharStruct { enum EnumChar { @@ -626,7 +629,10 @@ struct EnumCharStruct { enumcharB = '\102', // B enumcharC = '\x43', // C enumcharD = 0x44, // D - enumcharE = 69 // E + enumcharE = 69, // E + enumcharAE1 = 'Æ', // AE (latin1 encoded) + enumcharAE2 = '\306', // AE (latin1 encoded) + enumcharAE3 = '\xC6' // AE (latin1 encoded) }; }; %} @@ -637,6 +643,50 @@ struct EnumCharStruct { %csconst(0); #endif +%inline %{ +enum { + x_globalenumchar0 = '\0', + x_globalenumchar1 = '\1', + x_globalenumchar2 = '\n', + x_globalenumcharA = 'A', + x_globalenumcharB = '\102', // B + x_globalenumcharC = '\x43', // C + x_globalenumcharD = 0x44, // D + x_globalenumcharE = 69, // E + x_globalenumcharAE1 = 'Æ', // AE (latin1 encoded) + x_globalenumcharAE2 = '\306', // AE (latin1 encoded) + x_globalenumcharAE3 = '\xC6' // AE (latin1 encoded) +}; +enum X_EnumChar { + x_enumchar0 = '\0', + x_enumchar1 = '\1', + x_enumchar2 = '\n', + x_enumcharA = 'A', + x_enumcharB = '\102', // B + x_enumcharC = '\x43', // C + x_enumcharD = 0x44, // D + x_enumcharE = 69, // E + x_enumcharAE1 = 'Æ', // AE (latin1 encoded) + x_enumcharAE2 = '\306', // AE (latin1 encoded) + x_enumcharAE3 = '\xC6' // AE (latin1 encoded) +}; +struct X_EnumCharStruct { + enum X_EnumChar { + enumchar0 = '\0', + enumchar1 = '\1', + enumchar2 = '\n', + enumcharA = 'A', + enumcharB = '\102', // B + enumcharC = '\x43', // C + enumcharD = 0x44, // D + enumcharE = 69, // E + enumcharAE1 = 'Æ', // AE (latin1 encoded) + enumcharAE2 = '\306', // AE (latin1 encoded) + enumcharAE3 = '\xC6' // AE (latin1 encoded) + }; +}; +%} + %inline %{ namespace DifferentSpace { enum DifferentTypesNoConst { From 7339de974dde92dbc9bb912e4f94091aee82f0ec Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Wed, 27 Jan 2016 08:54:44 +0000 Subject: [PATCH 06/12] Fix static const char member variables wrappers with %javaconst(1). This fixes the case when an integer is used as the initializer, such as: struct W { static const char w = 100; }; The "valuetype" attribute has been added to the "cdecl" Node which enables us to distinguish the declared type from the type of the initializer. --- CHANGES.current | 8 ++++++- Examples/test-suite/chartest.i | 38 ++++++++++++++++++++++++++++++++++ Source/CParse/parser.y | 8 +++++++ Source/Modules/java.cxx | 3 ++- 4 files changed, 55 insertions(+), 2 deletions(-) diff --git a/CHANGES.current b/CHANGES.current index 4406f1225..2091d89a2 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -5,9 +5,15 @@ See the RELEASENOTES file for a summary of changes in each release. Version 3.0.9 (in progress) =========================== +2016-01-27: wsfulton + [Java] Fix static const char member variables wrappers with %javaconst(1). + This fixes the case when an integer is used as the initializer, such as: + + struct W { static const char w = 100; }; + 2016-01-26: wsfulton [Java] Fix generated code parsing enum values using char escape sequences - when these values appear in the Java code (usually when using %javaconst) + when these values appear in the Java code (usually when using %javaconst(1)) such as: enum X { x1 = '\n', x2 = '\1' }; diff --git a/Examples/test-suite/chartest.i b/Examples/test-suite/chartest.i index cc30b51bc..e9b25f782 100644 --- a/Examples/test-suite/chartest.i +++ b/Examples/test-suite/chartest.i @@ -20,6 +20,9 @@ static const char globcharB = '\102'; // B static const char globcharC = '\x43'; // C static const char globcharD = 0x44; // D static const char globcharE = 69; // E +static const char globcharAE1 = 'Æ'; // AE (latin1 encoded) +static const char globcharAE2 = '\306'; // AE (latin1 encoded) +static const char globcharAE3 = '\xC6'; // AE (latin1 encoded) struct CharTestClass { static const char memberchar0 = '\0'; @@ -30,5 +33,40 @@ struct CharTestClass { static const char membercharC = '\x43'; // C static const char membercharD = 0x44; // D static const char membercharE = 69; // E + static const char membercharAE1 = 'Æ'; // AE (latin1 encoded) + static const char membercharAE2 = '\306'; // AE (latin1 encoded) + static const char membercharAE3 = '\xC6'; // AE (latin1 encoded) +}; +%} + +#if defined(SWIGJAVA) +%javaconst(1); +#endif + +%inline %{ +static const char x_globchar0 = '\0'; +static const char x_globchar1 = '\1'; +static const char x_globchar2 = '\n'; +static const char x_globcharA = 'A'; +static const char x_globcharB = '\102'; // B +static const char x_globcharC = '\x43'; // C +static const char x_globcharD = 0x44; // D +static const char x_globcharE = 69; // E +static const char x_globcharAE1 = 'Æ'; // AE (latin1 encoded) +static const char x_globcharAE2 = '\306'; // AE (latin1 encoded) +static const char x_globcharAE3 = '\xC6'; // AE (latin1 encoded) + +struct X_CharTestClass { + static const char memberchar0 = '\0'; + static const char memberchar1 = '\1'; + static const char memberchar2 = '\n'; + static const char membercharA = 'A'; + static const char membercharB = '\102'; // B + static const char membercharC = '\x43'; // C + static const char membercharD = 0x44; // D + static const char membercharE = 69; // E + static const char membercharAE1 = 'Æ'; // AE (latin1 encoded) + static const char membercharAE2 = '\306'; // AE (latin1 encoded) + static const char membercharAE3 = '\xC6'; // AE (latin1 encoded) }; %} diff --git a/Source/CParse/parser.y b/Source/CParse/parser.y index 621d43421..a38edbc36 100644 --- a/Source/CParse/parser.y +++ b/Source/CParse/parser.y @@ -2926,6 +2926,14 @@ c_decl : storage_class type declarator initializer c_decl_tail { Setattr($$,"throws",$4.throws); Setattr($$,"throw",$4.throwf); Setattr($$,"noexcept",$4.nexcept); + if ($4.val && $4.type) { + /* store initializer type as it might be different to the declared type */ + SwigType *valuetype = NewSwigType($4.type); + if (Len(valuetype) > 0) + Setattr($$,"valuetype",valuetype); + else + Delete(valuetype); + } if (!$5) { if (Len(scanner_ccode)) { String *code = Copy(scanner_ccode); diff --git a/Source/Modules/java.cxx b/Source/Modules/java.cxx index c8655a017..01370a725 100644 --- a/Source/Modules/java.cxx +++ b/Source/Modules/java.cxx @@ -1481,6 +1481,7 @@ public: virtual int constantWrapper(Node *n) { String *symname = Getattr(n, "sym:name"); SwigType *t = Getattr(n, "type"); + SwigType *valuetype = Getattr(n, "valuetype"); ParmList *l = Getattr(n, "parms"); String *tm; String *return_type = NewString(""); @@ -1572,7 +1573,7 @@ public: } else { // Alternative constant handling will use the C syntax to make a true Java constant and hope that it compiles as Java code if (Getattr(n, "wrappedasconstant")) { - if (SwigType_type(t) == T_CHAR) + if (SwigType_type(valuetype) == T_CHAR) Printf(constants_code, "\'%(escape)s\';\n", Getattr(n, "staticmembervariableHandler:value")); else Printf(constants_code, "%s;\n", Getattr(n, "staticmembervariableHandler:value")); From 539aca58a5ae10c26332190911919cf87796ebcd Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Sat, 12 Mar 2016 00:26:49 +0000 Subject: [PATCH 07/12] Php fix for enum value of '\0' --- Source/Modules/php.cxx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Source/Modules/php.cxx b/Source/Modules/php.cxx index fbece2715..cddf61aec 100644 --- a/Source/Modules/php.cxx +++ b/Source/Modules/php.cxx @@ -1904,7 +1904,7 @@ done: enumvalue = GetChar(n, "enumvalueex"); } - if (enumvalue) { + if (enumvalue && *Char(enumvalue)) { // Check for a simple constant expression which is valid in PHP. // If we find one, initialise the const member with it; otherwise // we initialise it using the C/C++ wrapped constant. @@ -1916,7 +1916,8 @@ done: break; } } - if (!*p) set_to = enumvalue; + if (!*p) + set_to = enumvalue; } if (wrapping_member_constant) { From 6840996dba76794f3538ea51e068b09fe73be220 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Sat, 12 Mar 2016 11:26:17 +0000 Subject: [PATCH 08/12] Fix wrapping D constants using %dmanifestconst --- Examples/test-suite/chartest.i | 4 ++++ Source/Modules/d.cxx | 26 ++++++++++++++++---------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/Examples/test-suite/chartest.i b/Examples/test-suite/chartest.i index e9b25f782..b86bfdc91 100644 --- a/Examples/test-suite/chartest.i +++ b/Examples/test-suite/chartest.i @@ -41,6 +41,10 @@ struct CharTestClass { #if defined(SWIGJAVA) %javaconst(1); +//#elif SWIGCSHARP +//%csconst(1); +#elif SWIGD +%dmanifestconst; #endif %inline %{ diff --git a/Source/Modules/d.cxx b/Source/Modules/d.cxx index 5fc21ad18..cf859993a 100644 --- a/Source/Modules/d.cxx +++ b/Source/Modules/d.cxx @@ -910,7 +910,7 @@ public: const char *val = Equal(Getattr(n, "enumvalue"), "true") ? "1" : "0"; Setattr(n, "enumvalue", val); } else if (swigtype == T_CHAR) { - String *val = NewStringf("'%s'", Getattr(n, "enumvalue")); + String *val = NewStringf("'%(escape)s'", Getattr(n, "enumvalue")); Setattr(n, "enumvalue", val); Delete(val); } @@ -1423,6 +1423,7 @@ public: String *constants_code = NewString(""); SwigType *t = Getattr(n, "type"); + SwigType *valuetype = Getattr(n, "valuetype"); ParmList *l = Getattr(n, "parms"); // Attach the non-standard typemaps to the parameter list. @@ -1470,16 +1471,21 @@ public: Printf(constants_code, "%s;\n", override_value); } else { // Just take the value from the C definition and hope it compiles in D. - String* value = Getattr(n, "wrappedasconstant") ? - Getattr(n, "staticmembervariableHandler:value") : Getattr(n, "value"); - - // Add the stripped quotes back in. - if (SwigType_type(t) == T_STRING) { - Printf(constants_code, "\"%s\";\n", value); - } else if (SwigType_type(t) == T_CHAR) { - Printf(constants_code, "\'%s\';\n", value); + if (Getattr(n, "wrappedasconstant")) { + if (SwigType_type(valuetype) == T_CHAR) + Printf(constants_code, "\'%(escape)s\';\n", Getattr(n, "staticmembervariableHandler:value")); + else + Printf(constants_code, "%s;\n", Getattr(n, "staticmembervariableHandler:value")); } else { - Printf(constants_code, "%s;\n", value); + // Add the stripped quotes back in. + String* value = Getattr(n, "value"); + if (SwigType_type(t) == T_STRING) { + Printf(constants_code, "\"%s\";\n", value); + } else if (SwigType_type(t) == T_CHAR) { + Printf(constants_code, "\'%s\';\n", value); + } else { + Printf(constants_code, "%s;\n", value); + } } } From 391a3cf00a81535e75177b34dbfac0c007efbd66 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Sat, 12 Mar 2016 11:38:55 +0000 Subject: [PATCH 09/12] D testing added for %dmanifestconst and char constants --- Examples/test-suite/char_constant.i | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Examples/test-suite/char_constant.i b/Examples/test-suite/char_constant.i index 96a0a520b..30db62c4f 100644 --- a/Examples/test-suite/char_constant.i +++ b/Examples/test-suite/char_constant.i @@ -24,6 +24,10 @@ #if defined(SWIGJAVA) %javaconst(1); +//#elif SWIGCSHARP +//%csconst(1); +#elif SWIGD +%dmanifestconst; #endif #define X_ESC_CONST '\1' From 5fb6537f699cf39507ba5240b3eeccdedf8c0293 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Sat, 12 Mar 2016 17:42:37 +0000 Subject: [PATCH 10/12] C# char wrappers fixes for enum values, static const member char values and %csconst Use hex escaping for char values used as C# constants --- Examples/test-suite/char_constant.i | 4 +-- Examples/test-suite/chartest.i | 4 +-- Source/Modules/csharp.cxx | 19 +++++++++----- Source/Swig/misc.c | 39 +++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 10 deletions(-) diff --git a/Examples/test-suite/char_constant.i b/Examples/test-suite/char_constant.i index 30db62c4f..918456192 100644 --- a/Examples/test-suite/char_constant.i +++ b/Examples/test-suite/char_constant.i @@ -24,8 +24,8 @@ #if defined(SWIGJAVA) %javaconst(1); -//#elif SWIGCSHARP -//%csconst(1); +#elif SWIGCSHARP +%csconst(1); #elif SWIGD %dmanifestconst; #endif diff --git a/Examples/test-suite/chartest.i b/Examples/test-suite/chartest.i index b86bfdc91..7c187817c 100644 --- a/Examples/test-suite/chartest.i +++ b/Examples/test-suite/chartest.i @@ -41,8 +41,8 @@ struct CharTestClass { #if defined(SWIGJAVA) %javaconst(1); -//#elif SWIGCSHARP -//%csconst(1); +#elif SWIGCSHARP +%csconst(1); #elif SWIGD %dmanifestconst; #endif diff --git a/Source/Modules/csharp.cxx b/Source/Modules/csharp.cxx index eaa027f2a..b3dd7e38f 100644 --- a/Source/Modules/csharp.cxx +++ b/Source/Modules/csharp.cxx @@ -1309,7 +1309,7 @@ public: const char *val = Equal(Getattr(n, "enumvalue"), "true") ? "1" : "0"; Setattr(n, "enumvalue", val); } else if (swigtype == T_CHAR) { - String *val = NewStringf("'%s'", Getattr(n, "enumvalue")); + String *val = NewStringf("'%(hexescape)s'", Getattr(n, "enumvalue")); Setattr(n, "enumvalue", val); Delete(val); } @@ -1433,6 +1433,7 @@ public: virtual int constantWrapper(Node *n) { String *symname = Getattr(n, "sym:name"); SwigType *t = Getattr(n, "type"); + SwigType *valuetype = Getattr(n, "valuetype"); ParmList *l = Getattr(n, "parms"); String *tm; String *return_type = NewString(""); @@ -1485,13 +1486,15 @@ public: Swig_warning(WARN_CSHARP_TYPEMAP_CSWTYPE_UNDEF, input_file, line_number, "No cstype typemap defined for %s\n", SwigType_str(t, 0)); } + // Default (octal) escaping is no good - change to hex escaped value + String *hexescaped_value = Getattr(n, "rawvalue") ? NewStringf("%(hexescape)s", Getattr(n, "rawvalue")) : 0; // Add the stripped quotes back in String *new_value = NewString(""); if (SwigType_type(t) == T_STRING) { - Printf(new_value, "\"%s\"", Copy(Getattr(n, "value"))); + Printf(new_value, "\"%s\"", hexescaped_value ? hexescaped_value : Copy(Getattr(n, "value"))); Setattr(n, "value", new_value); } else if (SwigType_type(t) == T_CHAR) { - Printf(new_value, "\'%s\'", Copy(Getattr(n, "value"))); + Printf(new_value, "\'%s\'", hexescaped_value ? hexescaped_value : Copy(Getattr(n, "value"))); Setattr(n, "value", new_value); } @@ -1532,10 +1535,14 @@ public: } else { // Alternative constant handling will use the C syntax to make a true C# constant and hope that it compiles as C# code if (Getattr(n, "wrappedasconstant")) { - if (SwigType_type(t) == T_CHAR) - Printf(constants_code, "\'%s\';\n", Getattr(n, "staticmembervariableHandler:value")); - else + if (SwigType_type(t) == T_CHAR) { + if (SwigType_type(valuetype) == T_CHAR) + Printf(constants_code, "\'%(hexescape)s\';\n", Getattr(n, "staticmembervariableHandler:value")); + else + Printf(constants_code, "(char)%s;\n", Getattr(n, "staticmembervariableHandler:value")); + } else { Printf(constants_code, "%s;\n", Getattr(n, "staticmembervariableHandler:value")); + } } else { Printf(constants_code, "%s;\n", Getattr(n, "value")); } diff --git a/Source/Swig/misc.c b/Source/Swig/misc.c index c552ac2cb..402db2be3 100644 --- a/Source/Swig/misc.c +++ b/Source/Swig/misc.c @@ -309,6 +309,7 @@ int Swig_storage_isstatic(Node *n) { * Swig_string_escape() * * Takes a string object and produces a string with escape codes added to it. + * Octal escaping is used. * ----------------------------------------------------------------------------- */ String *Swig_string_escape(String *s) { @@ -342,6 +343,43 @@ String *Swig_string_escape(String *s) { return ns; } +/* ----------------------------------------------------------------------------- + * Swig_string_hexescape() + * + * Takes a string object and produces a string with escape codes added to it. + * Hex escaping is used. + * ----------------------------------------------------------------------------- */ + +String *Swig_string_hexescape(String *s) { + String *ns; + int c; + ns = NewStringEmpty(); + + while ((c = Getc(s)) != EOF) { + if (c == '\n') { + Printf(ns, "\\n"); + } else if (c == '\r') { + Printf(ns, "\\r"); + } else if (c == '\t') { + Printf(ns, "\\t"); + } else if (c == '\\') { + Printf(ns, "\\\\"); + } else if (c == '\'') { + Printf(ns, "\\'"); + } else if (c == '\"') { + Printf(ns, "\\\""); + } else if (c == ' ') { + Putc(c, ns); + } else if (!isgraph(c)) { + if (c < 0) + c += UCHAR_MAX + 1; + Printf(ns, "\\x%X", c); + } else { + Putc(c, ns); + } + } + return ns; +} /* ----------------------------------------------------------------------------- * Swig_string_upper() @@ -1392,6 +1430,7 @@ String *Swig_pcre_version(void) { void Swig_init() { /* Set some useful string encoding methods */ DohEncoding("escape", Swig_string_escape); + DohEncoding("hexescape", Swig_string_hexescape); DohEncoding("upper", Swig_string_upper); DohEncoding("lower", Swig_string_lower); DohEncoding("title", Swig_string_title); From 5e614da429bb38286bb6b4b9f456c1becee281e9 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Sat, 12 Mar 2016 19:04:41 +0000 Subject: [PATCH 11/12] changes file update for char wrappers --- CHANGES.current | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/CHANGES.current b/CHANGES.current index 2091d89a2..1a9dfc382 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -5,14 +5,14 @@ See the RELEASENOTES file for a summary of changes in each release. Version 3.0.9 (in progress) =========================== -2016-01-27: wsfulton - [Java] Fix static const char member variables wrappers with %javaconst(1). +2016-03-12: wsfulton + [Java, C#, D] Fix static const char member variables wrappers with %javaconst(1) + %csconst(1) or %dmanifestconst. This fixes the case when an integer is used as the initializer, such as: struct W { static const char w = 100; }; -2016-01-26: wsfulton - [Java] Fix generated code parsing enum values using char escape sequences + Fix generated code parsing enum values using char escape sequences when these values appear in the Java code (usually when using %javaconst(1)) such as: @@ -22,6 +22,9 @@ Version 3.0.9 (in progress) struct Y { static const char y = '\n'; } + Likewise for D and %dmanifestconstant. For C# and %csconst(1), char + values in C# are now hex escaped as C# doesn't support C octal escaping. + 2016-01-12: olly [Javascript] Look for "nodejs" as well as "node", as it's packaged as the former on Debian. From c985dd8bd1255bc6238fc471055d136840f97ca8 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Sat, 12 Mar 2016 23:26:23 +0000 Subject: [PATCH 12/12] Add missing string_constant.i testcase --- Examples/test-suite/string_constants.i | 44 ++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Examples/test-suite/string_constants.i diff --git a/Examples/test-suite/string_constants.i b/Examples/test-suite/string_constants.i new file mode 100644 index 000000000..60f8c1859 --- /dev/null +++ b/Examples/test-suite/string_constants.i @@ -0,0 +1,44 @@ +%module string_constants +// Test unusual string constants + +%warnfilter(SWIGWARN_TYPEMAP_CHARLEAK); + +#if defined(SWIGCSHARP) +%csconst(1); +%csconstvalue("\"AEIOU\\n\"") SS1; +%csconstvalue("\"AEIOU\\n\"") SS2; +#endif +#if defined(SWIGJAVA) +%javaconst(1); +#endif +%inline %{ +#define SS1 "ÆÎOU\n" +#define AA1 "A\rB\nC" +#define EE1 "\124\125\126" +#define XX1 "\x57\x58\x59" +#define ZS1 "\0" +#define ES1 "" +%} +%constant SS2="ÆÎOU\n"; +%constant AA2="A\rB\nC"; +%constant EE2="\124\125\126"; +%constant XX2="\x57\x58\x59"; +%constant ZS2="\0"; +%constant ES2=""; + +%inline %{ +static const char *SS3 = "ÆÎOU\n"; +static const char *AA3 = "A\rB\nC"; +static const char *EE3 = "\124\125\126"; +static const char *XX3 = "\x57\x58\x59"; +static const char *ZS3 = "\0"; +static const char *ES3 = ""; +struct things { + const char * defarguments1(const char *SS4 = "ÆÎOU\n") { return SS4; } + const char * defarguments2(const char *AA4 = "A\rB\nC") { return AA4; } + const char * defarguments3(const char *EE4 = "\124\125\126") { return EE4; } + const char * defarguments4(const char *XX4 = "\x57\x58\x59") { return XX4; } + const char * defarguments5(const char *ZS4 = "\0") { return ZS4; } + const char * defarguments6(const char *ES4 = "") { return ES4; } +}; +%}