From c6d3bc813fa5b2035ae4d3b71f9b8a6a30cc6743 Mon Sep 17 00:00:00 2001
From: florian <florian@freepascal.org>
Date: Sun, 28 Jan 2007 08:21:08 +0000
Subject: [PATCH] * fixes #8229

git-svn-id: trunk@6238 -
---
 .gitattributes         |  1 +
 compiler/nadd.pas      | 48 ++++++++++++++++++++++++++++++++++++------
 tests/webtbs/tw8229.pp | 17 +++++++++++++++
 3 files changed, 60 insertions(+), 6 deletions(-)
 create mode 100644 tests/webtbs/tw8229.pp

diff --git a/.gitattributes b/.gitattributes
index d2c9bcb506..e8d929c303 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -8014,6 +8014,7 @@ tests/webtbs/tw8199.pp svneol=native#text/plain
 tests/webtbs/tw8222.pp svneol=native#text/plain
 tests/webtbs/tw8222a.pp svneol=native#text/plain
 tests/webtbs/tw8222b.pp svneol=native#text/plain
+tests/webtbs/tw8229.pp svneol=native#text/plain
 tests/webtbs/ub1873.pp svneol=native#text/plain
 tests/webtbs/ub1883.pp svneol=native#text/plain
 tests/webtbs/uw0555.pp svneol=native#text/plain
diff --git a/compiler/nadd.pas b/compiler/nadd.pas
index 76b49b93b2..2736cd8154 100644
--- a/compiler/nadd.pas
+++ b/compiler/nadd.pas
@@ -1586,7 +1586,7 @@ implementation
       var
         p: tnode;
         newstatement : tstatementnode;
-        tempnode : ttempcreatenode;
+        tempnode,tempnode2 : ttempcreatenode;
       begin
         { when we get here, we are sure that both the left and the right }
         { node are both strings of the same stringtype (JM)              }
@@ -1645,6 +1645,8 @@ implementation
               { generate better code for comparison with empty string, we
                 only need to compare the length with 0 }
               if (nodetype in [equaln,unequaln,gtn,gten,ltn,lten]) and
+                { windows widestrings are too complicated to be handled optimized }
+                not(is_widestring(left.resultdef) and (target_info.system in system_windows)) and
                  (((left.nodetype=stringconstn) and (tstringconstnode(left).len=0)) or
                   ((right.nodetype=stringconstn) and (tstringconstnode(right).len=0))) then
                 begin
@@ -1664,11 +1666,45 @@ implementation
                       cordconstnode.create(0,s32inttype,false))
                   else
                     begin
-                      { compare the pointer with nil (for ansistrings etc), }
-                      { faster than getting the length (JM)                 }
-                      result:= caddnode.create(nodetype,
-                        ctypeconvnode.create_internal(left,voidpointertype),
-                        cpointerconstnode.create(0,voidpointertype));
+                      (*
+                      if is_widestring(left.resultdef) and
+                        (target_info.system in system_windows) then
+                        begin
+                          { windows like widestrings requires that we also check the length }
+                          result:=internalstatements(newstatement);
+                          tempnode:=ctempcreatenode.create(voidpointertype,voidpointertype.size,tt_persistent,true);
+                          tempnode2:=ctempcreatenode.create(resultdef,resultdef.size,tt_persistent,true);
+                          addstatement(newstatement,tempnode);
+                          addstatement(newstatement,tempnode2);
+                          { poor man's cse }
+                          addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode),
+                            ctypeconvnode.create_internal(left,voidpointertype))
+                          );
+                          addstatement(newstatement,cassignmentnode.create(ctemprefnode.create(tempnode2),
+                            caddnode.create(orn,
+                              caddnode.create(nodetype,
+                                ctemprefnode.create(tempnode),
+                                cpointerconstnode.create(0,voidpointertype)
+                              ),
+                              caddnode.create(nodetype,
+                                ctypeconvnode.create_internal(cderefnode.create(ctemprefnode.create(tempnode)),s32inttype),
+                                cordconstnode.create(0,s32inttype,false)
+                              )
+                            )
+                          ));
+                          addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
+                          addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode2));
+                          addstatement(newstatement,ctemprefnode.create(tempnode2));
+                        end
+                      else
+                      *)
+                        begin
+                          { compare the pointer with nil (for ansistrings etc), }
+                          { faster than getting the length (JM)                 }
+                          result:= caddnode.create(nodetype,
+                            ctypeconvnode.create_internal(left,voidpointertype),
+                            cpointerconstnode.create(0,voidpointertype));
+                        end;
                     end;
                   { left is reused }
                   left := nil;
diff --git a/tests/webtbs/tw8229.pp b/tests/webtbs/tw8229.pp
new file mode 100644
index 0000000000..e9ecea7285
--- /dev/null
+++ b/tests/webtbs/tw8229.pp
@@ -0,0 +1,17 @@
+program WideStringFault;
+
+procedure Test;
+var
+  w: widestring;
+  v: variant;
+begin
+  w := '';
+  v := w;
+  w := v;
+  if w <> '' then
+    halt(1);
+end;
+
+begin
+  Test;
+end.