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]

[c++ patch] merging strings


Yowee!
We were looking inside array constant decls too early and causing
the attached test case to fail *both* conditionals (we failed
to look inside for the == case, but did in the assignment).
The C frontend does not suffer this problem.

This is a case of premature optimization *by* the compiler! And
no prizes for guessing where this came from.

I attach what looks like the obvious fix. Ok?

nathan
-- 
Dr Nathan Sidwell   ::   http://www.codesourcery.com   ::   CodeSourcery LLC
         'But that's a lie.' - 'Yes it is. What's your point?'
nathan@codesourcery.com : http://www.cs.bris.ac.uk/~nathan/ : nathan@acm.org
2000-03-06  Nathan Sidwell  <nathan@codesourcery.com>

	* typeck.c (convert_for_assignment): Don't look at array
	initializer.
	* call.c (convert_like_real): Likewise.

Index: cp/typeck.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cp/typeck.c,v
retrieving revision 1.258
diff -c -3 -p -r1.258 typeck.c
*** typeck.c	2000/03/06 15:45:11	1.258
--- typeck.c	2000/03/06 21:36:30
*************** convert_for_assignment (type, rhs, errty
*** 6524,6530 ****
    /* Simplify the RHS if possible.  */
    if (TREE_CODE (rhs) == CONST_DECL)
      rhs = DECL_INITIAL (rhs);
!   else if (TREE_READONLY_DECL_P (rhs))
      rhs = decl_constant_value (rhs);
  
    /* [expr.ass]
--- 6520,6526 ----
    /* Simplify the RHS if possible.  */
    if (TREE_CODE (rhs) == CONST_DECL)
      rhs = DECL_INITIAL (rhs);
!   else if (TREE_READONLY_DECL_P (rhs) && coder != ARRAY_TYPE)
      rhs = decl_constant_value (rhs);
  
    /* [expr.ass]
Index: cp/call.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/cp/call.c,v
retrieving revision 1.200
diff -c -3 -p -r1.200 call.c
*** call.c	2000/03/05 10:22:15	1.200
--- call.c	2000/03/06 21:36:31
*************** convert_like_real (convs, expr, fn, argn
*** 3683,3692 ****
    if (expr == error_mark_node)
      return error_mark_node;
  
!   /* Convert a constant variable to its underlying value, unless we
       are about to bind it to a reference, in which case we need to
       leave it as an lvalue.  */
!   if (TREE_READONLY_DECL_P (expr) && TREE_CODE (convs) != REF_BIND)
      expr = decl_constant_value (expr);
  
    switch (TREE_CODE (convs))
--- 3683,3693 ----
    if (expr == error_mark_node)
      return error_mark_node;
  
!   /* Convert a non-array constant variable to its underlying value, unless we
       are about to bind it to a reference, in which case we need to
       leave it as an lvalue.  */
!   if (TREE_READONLY_DECL_P (expr) && TREE_CODE (convs) != REF_BIND
!       && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
      expr = decl_constant_value (expr);
  
    switch (TREE_CODE (convs))

// Copyright (C) 2000 Free Software Foundation, Inc.
// Contributed by Nathan Sidwell 6 Mar 2000 <nathan@codesourcery.com>

// A char const array should never be confused for a string literal.

int main ()
{
  static const char ary[] = "wibble";
  void const *ptr = 0;
  
  ptr = ary;
  if (ptr == "wibble")
    return 1;
  if (ptr != ary)
    return 1;
  return 0;
}

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