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]

[C++ PATCH] Fix bug 5089


Hi,
this fixes 5089, a change request. -Wold-style-cast would warn about
all oldstyle casts. I & the submitter think it shouldn't warn about
casts to void.
1) Those have a completely unambiguous meaning
2) IMO, (void)foo is clearer than static_cast<void> (foo)
3) as the submitter pointed out, assert macros make liberal use of
void casts, and thus renderers the warning rather useless.

built & tested on i686-pc-linux-gnu, 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
2001-12-27  Nathan Sidwell  <nathan@codesourcery.com>

	PR c++/5089
	* doc/invoke.texi (-Wold-style-cast): Only warn about non-void casts.

2001-12-27  Nathan Sidwell  <nathan@codesourcery.com>

	PR c++/5089
	* decl2.c (reparse_absdcl_as_casts): Don't warn about casts to void.

Index: cp/decl2.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/decl2.c,v
retrieving revision 1.507
diff -c -3 -p -r1.507 decl2.c
*** decl2.c	2001/12/23 16:07:09	1.507
--- decl2.c	2001/12/27 22:31:35
*************** reparse_absdcl_as_casts (decl, expr)
*** 3615,3620 ****
--- 3615,3621 ----
       tree decl, expr;
  {
    tree type;
+   int non_void_p = 0;
    
    if (TREE_CODE (expr) == CONSTRUCTOR
        && TREE_TYPE (expr) == 0)
*************** reparse_absdcl_as_casts (decl, expr)
*** 3635,3645 ****
      {
        type = groktypename (TREE_VALUE (CALL_DECLARATOR_PARMS (decl)));
        decl = TREE_OPERAND (decl, 0);
        expr = build_c_cast (type, expr);
      }
  
    if (warn_old_style_cast && ! in_system_header
!       && current_lang_name != lang_name_c)
      warning ("use of old-style cast");
  
    return expr;
--- 3636,3648 ----
      {
        type = groktypename (TREE_VALUE (CALL_DECLARATOR_PARMS (decl)));
        decl = TREE_OPERAND (decl, 0);
+       if (!VOID_TYPE_P (type))
+ 	non_void_p = 1;
        expr = build_c_cast (type, expr);
      }
  
    if (warn_old_style_cast && ! in_system_header
!       && non_void_p && current_lang_name != lang_name_c)
      warning ("use of old-style cast");
  
    return expr;
Index: doc/invoke.texi
===================================================================
RCS file: /cvs/gcc/gcc/gcc/doc/invoke.texi,v
retrieving revision 1.93
diff -c -3 -p -r1.93 invoke.texi
*** invoke.texi	2001/12/18 15:41:30	1.93
--- invoke.texi	2001/12/27 22:33:42
*************** but disables the helpful warning.
*** 1557,1566 ****
  
  @item -Wold-style-cast @r{(C++ only)}
  @opindex Wold-style-cast
! Warn if an old-style (C-style) cast is used within a C++ program.  The
! new-style casts (@samp{static_cast}, @samp{reinterpret_cast}, and
! @samp{const_cast}) are less vulnerable to unintended effects, and much
! easier to grep for.
  
  @item -Woverloaded-virtual @r{(C++ only)}
  @opindex Woverloaded-virtual
--- 1557,1566 ----
  
  @item -Wold-style-cast @r{(C++ only)}
  @opindex Wold-style-cast
! Warn if an old-style (C-style) cast to a non-void type is used within
! a C++ program.  The new-style casts (@samp{static_cast},
! @samp{reinterpret_cast}, and @samp{const_cast}) are less vulnerable to
! unintended effects, and much easier to grep for.
  
  @item -Woverloaded-virtual @r{(C++ only)}
  @opindex Woverloaded-virtual
// { dg-do compile }
// { dg-options "-ansi -pedantic-errors -Wold-style-cast" }

// Copyright (C) 2001 Free Software Foundation, Inc.
// Contributed by Nathan Sidwell 26 Dec 2001 <nathan@nathan@codesourcery.com>

// PR 5089. old style cast to void should be permitted (think assert)

void foo ()
{
  int i;
  float f = (float)i;  // { dg-warning "use of old-style cast" "" }

  (void)i;
}


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