This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Patch for java/parse.y handling of empty strings


Hi,

The optimization in build_string_concatenation is a little to eager.
We can only skip the construction of a StringBuffer in a concatenation
when the String is a constant, otherwise it could be null and in that
case the StringBuffer must create the string "null" for us.

2002-04-17  Mark Wielaard  <mark@klomp.org>

  * parse.y (build_string_concatenation): Return just op1 only when op2
  is null and op1 is a STRING_CST, otherwise always construct a
  StringBuffer.

Tested on i686-pc-linux-gnu and powerpc-unknown-linux-gnu.
This fixes the following new test case for libjava.lang.

2002-04-17  Mark Wielaard  <mark@klomp.org>

  * libjava.lang/emptystring.java: New.
  * libjava.lang/emptystring.out: New.

Since + "" is a common way to make sure a String argument is never null
and it also fixes the Mauve test discussed on the java@gcc.gnu.org
mailing list I would also like this to go onto the 3.1 branch.

Ok to commit?

Cheers,

Mark
Index: parse.y
===================================================================
RCS file: /cvs/gcc/gcc/gcc/java/parse.y,v
retrieving revision 1.353.2.17
diff -u -r1.353.2.17 parse.y
--- parse.y	15 Apr 2002 09:28:53 -0000	1.353.2.17
+++ parse.y	18 Apr 2002 06:24:55 -0000
@@ -13756,8 +13756,8 @@
     op2 = patch_string_cst (op2);
 
   /* If either one of the constant is null and the other non null
-     operand is a String object, return it. */
-  if (JSTRING_TYPE_P (TREE_TYPE (op1)) && !op2)
+     operand is a String constant, return it. */
+  if ((TREE_CODE (op1) == STRING_CST) && !op2)
     return op1;
 
   /* If OP1 isn't already a StringBuffer, create and
Index: libjava.lang/emptystring.java
===================================================================
RCS file: libjava.lang/emptystring.java
diff -N libjava.lang/emptystring.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ libjava.lang/emptystring.java	18 Apr 2002 06:27:56 -0000
@@ -0,0 +1,15 @@
+public class emptystring
+{
+        public static void main(String[] args)
+        {
+                System.out.println("null".equals(n(0) + ""));
+                System.out.println("null".equals("" + n(0)));
+                System.out.println("x".equals(n(1) + ""));
+                System.out.println("x".equals("" + n(1)));
+        }
+
+        static String n(int i)
+        {
+                if (i==0) return null; else return "x";
+        }
+}
Index: libjava.lang/emptystring.out
===================================================================
RCS file: libjava.lang/emptystring.out
diff -N libjava.lang/emptystring.out
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ libjava.lang/emptystring.out	18 Apr 2002 06:27:56 -0000
@@ -0,0 +1,4 @@
+true
+true
+true
+true

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]