819d24
From 90ba174fcea1618af57aa594199541d47a89b7f6 Mon Sep 17 00:00:00 2001
819d24
From: William S Fulton <wsf@fultondesigns.co.uk>
819d24
Date: Sun, 17 Sep 2017 19:02:55 +0100
819d24
Subject: [PATCH 1/2] Fix generated code for constant expressions containing
819d24
 wchar_t L literals.
819d24
819d24
Such as:
819d24
  # define __WCHAR_MAX    (0x7fffffff + L'\0')
819d24
819d24
Reported on swig-user mailing list.
819d24
---
819d24
 CHANGES.current                                         | 5 +++++
819d24
 Examples/test-suite/csharp/preproc_constants_c_runme.cs | 3 ++-
819d24
 Examples/test-suite/csharp/preproc_constants_runme.cs   | 2 ++
819d24
 Examples/test-suite/d/preproc_constants_c_runme.1.d     | 2 ++
819d24
 Examples/test-suite/d/preproc_constants_c_runme.2.d     | 2 ++
819d24
 Examples/test-suite/d/preproc_constants_runme.1.d       | 2 ++
819d24
 Examples/test-suite/d/preproc_constants_runme.2.d       | 2 ++
819d24
 Examples/test-suite/php/preproc_constants_c_runme.php   | 2 ++
819d24
 Examples/test-suite/php/preproc_constants_runme.php     | 2 ++
819d24
 Examples/test-suite/php5/preproc_constants_c_runme.php  | 2 ++
819d24
 Examples/test-suite/php5/preproc_constants_runme.php    | 2 ++
819d24
 Examples/test-suite/preproc_constants.i                 | 3 +++
819d24
 Source/CParse/parser.y                                  | 2 +-
819d24
 13 files changed, 29 insertions(+), 2 deletions(-)
819d24
819d24
#diff --git a/CHANGES.current b/CHANGES.current
819d24
#index 1e4a244..b455a9f 100644
819d24
#--- a/CHANGES.current
819d24
#+++ b/CHANGES.current
819d24
#@@ -7,6 +7,11 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
819d24
# Version 4.0.0 (in progress)
819d24
# ===========================
819d24
# 
819d24
#+2017-09-17: wsfulton
819d24
#+            Fix generated code for constant expressions containing wchar_t L literals such as:
819d24
#+              # define __WCHAR_MAX    (0x7fffffff + L'\0')
819d24
#+              # define __WCHAR_MIN    (-__WCHAR_MAX - 1)
819d24
#+
819d24
# 2017-09-10: mlamarre
819d24
#             [Python] Patch #1083. Define_DEBUG to 1 to do exactly like Visual Studio
819d24
#             /LDd, /MDd or /MTd compiler options.
819d24
diff --git a/Examples/test-suite/csharp/preproc_constants_c_runme.cs b/Examples/test-suite/csharp/preproc_constants_c_runme.cs
819d24
index 76c684d..1c28e49 100644
819d24
--- a/Examples/test-suite/csharp/preproc_constants_c_runme.cs
819d24
+++ b/Examples/test-suite/csharp/preproc_constants_c_runme.cs
819d24
@@ -61,7 +61,8 @@ public class runme {
819d24
     assert( typeof(int) == preproc_constants_c.EXPR_LAND.GetType() );
819d24
     assert( typeof(int) == preproc_constants_c.EXPR_LOR.GetType() );
819d24
     assert( typeof(double) == preproc_constants_c.EXPR_CONDITIONAL.GetType() );
819d24
-
819d24
+    assert( typeof(int) == preproc_constants_c.EXPR_WCHAR_MAX.GetType() );
819d24
+    assert( typeof(int) == preproc_constants_c.EXPR_WCHAR_MIN.GetType() );
819d24
   }
819d24
   static void assert(bool assertion) {
819d24
     if (!assertion)
819d24
diff --git a/Examples/test-suite/csharp/preproc_constants_runme.cs b/Examples/test-suite/csharp/preproc_constants_runme.cs
819d24
index 9fae591..6b02e30 100644
819d24
--- a/Examples/test-suite/csharp/preproc_constants_runme.cs
819d24
+++ b/Examples/test-suite/csharp/preproc_constants_runme.cs
819d24
@@ -60,6 +60,8 @@ public class runme {
819d24
     assert( typeof(bool) == preproc_constants.EXPR_LAND.GetType() );
819d24
     assert( typeof(bool) == preproc_constants.EXPR_LOR.GetType() );
819d24
     assert( typeof(double) == preproc_constants.EXPR_CONDITIONAL.GetType() );
819d24
+    assert( typeof(int) == preproc_constants.EXPR_WCHAR_MAX.GetType() );
819d24
+    assert( typeof(int) == preproc_constants.EXPR_WCHAR_MIN.GetType() );
819d24
 
819d24
   }
819d24
   static void assert(bool assertion) {
819d24
diff --git a/Examples/test-suite/d/preproc_constants_c_runme.1.d b/Examples/test-suite/d/preproc_constants_c_runme.1.d
819d24
index d846c71..2b349af 100644
819d24
--- a/Examples/test-suite/d/preproc_constants_c_runme.1.d
819d24
+++ b/Examples/test-suite/d/preproc_constants_c_runme.1.d
819d24
@@ -61,4 +61,6 @@ void main() {
819d24
   static assert(is(int == typeof(EXPR_LAND())));
819d24
   static assert(is(int == typeof(EXPR_LOR())));
819d24
   static assert(is(double == typeof(EXPR_CONDITIONAL())));
819d24
+  static assert(is(int == typeof(EXPR_WCHAR_MAX())));
819d24
+  static assert(is(int == typeof(EXPR_WCHAR_MIN())));
819d24
 }
819d24
diff --git a/Examples/test-suite/d/preproc_constants_c_runme.2.d b/Examples/test-suite/d/preproc_constants_c_runme.2.d
819d24
index 9bdbb93..1bac525 100644
819d24
--- a/Examples/test-suite/d/preproc_constants_c_runme.2.d
819d24
+++ b/Examples/test-suite/d/preproc_constants_c_runme.2.d
819d24
@@ -61,4 +61,6 @@ void main() {
819d24
   static assert(is(int == typeof(EXPR_LAND())));
819d24
   static assert(is(int == typeof(EXPR_LOR())));
819d24
   static assert(is(double == typeof(EXPR_CONDITIONAL())));
819d24
+  static assert(is(int == typeof(EXPR_WCHAR_MAX())));
819d24
+  static assert(is(int == typeof(EXPR_WCHAR_MIN())));
819d24
 }
819d24
diff --git a/Examples/test-suite/d/preproc_constants_runme.1.d b/Examples/test-suite/d/preproc_constants_runme.1.d
819d24
index 009405f..f743f48 100644
819d24
--- a/Examples/test-suite/d/preproc_constants_runme.1.d
819d24
+++ b/Examples/test-suite/d/preproc_constants_runme.1.d
819d24
@@ -60,4 +60,6 @@ void main() {
819d24
   static assert(is(bool == typeof(EXPR_LAND())));
819d24
   static assert(is(bool == typeof(EXPR_LOR())));
819d24
   static assert(is(double == typeof(EXPR_CONDITIONAL())));
819d24
+  static assert(is(int == typeof(EXPR_WCHAR_MAX())));
819d24
+  static assert(is(int == typeof(EXPR_WCHAR_MIN())));
819d24
 }
819d24
diff --git a/Examples/test-suite/d/preproc_constants_runme.2.d b/Examples/test-suite/d/preproc_constants_runme.2.d
819d24
index 2d92ef0..0d96c37 100644
819d24
--- a/Examples/test-suite/d/preproc_constants_runme.2.d
819d24
+++ b/Examples/test-suite/d/preproc_constants_runme.2.d
819d24
@@ -60,4 +60,6 @@ void main() {
819d24
   static assert(is(bool == typeof(EXPR_LAND())));
819d24
   static assert(is(bool == typeof(EXPR_LOR())));
819d24
   static assert(is(double == typeof(EXPR_CONDITIONAL())));
819d24
+  static assert(is(int == typeof(EXPR_WCHAR_MAX())));
819d24
+  static assert(is(int == typeof(EXPR_WCHAR_MIN())));
819d24
 }
819d24
diff --git a/Examples/test-suite/php/preproc_constants_c_runme.php b/Examples/test-suite/php/preproc_constants_c_runme.php
819d24
index af9b76e..e59fe18 100644
819d24
--- a/Examples/test-suite/php/preproc_constants_c_runme.php
819d24
+++ b/Examples/test-suite/php/preproc_constants_c_runme.php
819d24
@@ -62,5 +62,7 @@ check::equal(gettype(preproc_constants_c::EXPR_OR), "integer", "preproc_constant
819d24
 check::equal(gettype(preproc_constants_c::EXPR_LAND), "integer", "preproc_constants.EXPR_LAND has unexpected type");
819d24
 check::equal(gettype(preproc_constants_c::EXPR_LOR), "integer", "preproc_constants.EXPR_LOR has unexpected type");
819d24
 check::equal(gettype(preproc_constants_c::EXPR_CONDITIONAL), "double", "preproc_constants.EXPR_CONDITIONAL has unexpected type");
819d24
+check::equal(gettype(preproc_constants_c::EXPR_WCHAR_MAX), "integer", "preproc_constants.EXPR_WCHAR_MAX has unexpected type");
819d24
+check::equal(gettype(preproc_constants_c::EXPR_WCHAR_MIN), "integer", "preproc_constants.EXPR_WCHAR_MIN has unexpected type");
819d24
 
819d24
 ?>
819d24
diff --git a/Examples/test-suite/php/preproc_constants_runme.php b/Examples/test-suite/php/preproc_constants_runme.php
819d24
index 5c9119b..8e117ea 100644
819d24
--- a/Examples/test-suite/php/preproc_constants_runme.php
819d24
+++ b/Examples/test-suite/php/preproc_constants_runme.php
819d24
@@ -61,5 +61,7 @@ check::equal(gettype(preproc_constants::EXPR_OR), "integer", "preproc_constants.
819d24
 check::equal(gettype(preproc_constants::EXPR_LAND), "boolean", "preproc_constants.EXPR_LAND has unexpected type");
819d24
 check::equal(gettype(preproc_constants::EXPR_LOR), "boolean", "preproc_constants.EXPR_LOR has unexpected type");
819d24
 check::equal(gettype(preproc_constants::EXPR_CONDITIONAL), "double", "preproc_constants.EXPR_CONDITIONAL has unexpected type");
819d24
+check::equal(gettype(preproc_constants::EXPR_WCHAR_MAX), "integer", "preproc_constants.EXPR_WCHAR_MAX has unexpected type");
819d24
+check::equal(gettype(preproc_constants::EXPR_WCHAR_MIN), "integer", "preproc_constants.EXPR_WCHAR_MIN has unexpected type");
819d24
 
819d24
 ?>
819d24
diff --git a/Examples/test-suite/php5/preproc_constants_c_runme.php b/Examples/test-suite/php5/preproc_constants_c_runme.php
819d24
index 1ea0195..d978fab 100644
819d24
--- a/Examples/test-suite/php5/preproc_constants_c_runme.php
819d24
+++ b/Examples/test-suite/php5/preproc_constants_c_runme.php
819d24
@@ -62,5 +62,7 @@ check::equal(gettype(preproc_constants_c::EXPR_OR), "integer", "preproc_constant
819d24
 check::equal(gettype(preproc_constants_c::EXPR_LAND), "integer", "preproc_constants.EXPR_LAND has unexpected type");
819d24
 check::equal(gettype(preproc_constants_c::EXPR_LOR), "integer", "preproc_constants.EXPR_LOR has unexpected type");
819d24
 check::equal(gettype(preproc_constants_c::EXPR_CONDITIONAL), "double", "preproc_constants.EXPR_CONDITIONAL has unexpected type");
819d24
+check::equal(gettype(preproc_constants_c::EXPR_WCHAR_MAX), "integer", "preproc_constants.EXPR_WCHAR_MAX has unexpected type");
819d24
+check::equal(gettype(preproc_constants_c::EXPR_WCHAR_MIN), "integer", "preproc_constants.EXPR_WCHAR_MIN has unexpected type");
819d24
 
819d24
 ?>
819d24
diff --git a/Examples/test-suite/php5/preproc_constants_runme.php b/Examples/test-suite/php5/preproc_constants_runme.php
819d24
index fb9ee4f..7527026 100644
819d24
--- a/Examples/test-suite/php5/preproc_constants_runme.php
819d24
+++ b/Examples/test-suite/php5/preproc_constants_runme.php
819d24
@@ -70,5 +70,7 @@ check::equal(gettype(preproc_constants::EXPR_LAND), "integer", "preproc_constant
819d24
 check::equal(gettype(preproc_constants::EXPR_LOR), "integer", "preproc_constants.EXPR_LOR has unexpected type");
819d24
 
819d24
 check::equal(gettype(preproc_constants::EXPR_CONDITIONAL), "double", "preproc_constants.EXPR_CONDITIONAL has unexpected type");
819d24
+check::equal(gettype(preproc_constants::EXPR_WCHAR_MAX), "integer", "preproc_constants.EXPR_WCHAR_MAX has unexpected type");
819d24
+check::equal(gettype(preproc_constants::EXPR_WCHAR_MIN), "integer", "preproc_constants.EXPR_WCHAR_MIN has unexpected type");
819d24
 
819d24
 ?>
819d24
diff --git a/Examples/test-suite/preproc_constants.i b/Examples/test-suite/preproc_constants.i
819d24
index 3a999ad..16b44c9 100644
819d24
--- a/Examples/test-suite/preproc_constants.i
819d24
+++ b/Examples/test-suite/preproc_constants.i
819d24
@@ -87,6 +87,9 @@
819d24
 #define EXPR_LOR         0xFF || 1
819d24
 #define EXPR_CONDITIONAL true ? 2 : 2.2
819d24
 
819d24
+#define EXPR_WCHAR_MAX   (0x7fffffff + L'\0')
819d24
+#define EXPR_WCHAR_MIN   (-EXPR_WCHAR_MAX - 1)
819d24
+
819d24
 #define EXPR_CHAR_COMPOUND_ADD 'A' + 12
819d24
 #define EXPR_CHAR_COMPOUND_LSHIFT 'B' << 6
819d24
 #define H_SUPPRESS_SCALING_MAGIC (('s'<<24) | ('u'<<16) | ('p'<<8) | 'p')
819d24
diff --git a/Source/CParse/parser.y b/Source/CParse/parser.y
819d24
index 2e92cd0..273dadb 100644
819d24
--- a/Source/CParse/parser.y
819d24
+++ b/Source/CParse/parser.y
819d24
@@ -194,7 +194,7 @@ int SWIG_cparse_template_reduce(int treduce) {
819d24
  * ----------------------------------------------------------------------------- */
819d24
 
819d24
 static int promote_type(int t) {
819d24
-  if (t <= T_UCHAR || t == T_CHAR) return T_INT;
819d24
+  if (t <= T_UCHAR || t == T_CHAR || t == T_WCHAR) return T_INT;
819d24
   return t;
819d24
 }
819d24
 
819d24
-- 
819d24
2.9.5
819d24