This is the mail archive of the gcc-bugs@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/5330: gcc-3.0.3 -Wcast-qual and casting pointers to static functions



>Number:         5330
>Category:       c
>Synopsis:       -Wcast-qual and casting pointers to static functions
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    unassigned
>State:          open
>Class:          rejects-legal
>Submitter-Id:   net
>Arrival-Date:   Wed Jan 09 00:16:01 PST 2002
>Closed-Date:
>Last-Modified:
>Originator:     Mark D. Baushke
>Release:        3.0.3
>Organization:
>Environment:
System: FreeBSD mdb-bsd.juniper.net 4.2-STABLE FreeBSD 4.2-STABLE #0: Wed Jan 17 12:03:49 PST 2001 root@mdb-bsd.juniper.net:/usr/obj/usr/src/sys/GENERIC i386


host: i386-unknown-freebsd4.2
build: i386-unknown-freebsd4.2
target: i386-unknown-freebsd4.2
configured with: ../gcc-3.0.3/configure --prefix=/usr/home/mdb/gnu
>Description:

The -Wcast-qual switch does not interact well with casting a static
function as it is not possible to have the cast do anything but
discard the TYPE_QUAL_CONST qualifier. If both the in_type and
in_otype reduce to FUNCTION_TYPE nodes, it should be safe to ignore
the TYPE_QUAL_CONST qualifier.

In the test case below, the workaround is to either remove the
'static' attribute from the null_foo function, or to avoid using
the -Wcast-qual switch and use -Wno-cast-qual instead.

The patch given in the '>Fix' below seems to work around the problem.

>How-To-Repeat:

% cat <<EOF >cast-qual.c
typedef int (*baz_t) (int);
typedef struct baz_desc { int num; baz_t deflt; } baz_desc_t;
static int null_foo (int myarg) { return 0; }
baz_desc_t fooa_desc = { 0, null_foo };
baz_desc_t foob_desc = { 0, (baz_t) null_foo };
EOF
% gcc -c -O -g -Werror -Wcast-qual cast-qual.c
cc1: warnings being treated as errors
cast-qual.c:5: warning: cast discards qualifiers from pointer target type
%

>Fix:
2002-01-08  Mark D. Baushke  <mdb@gnu.org>

        * c-typeck.c (build_c_cast): Discard the TYPE_QUAL_CONST bit
        that arises if the function being cast is a static function.

Index:gcc-3.0.3/gcc/c-typeck.c
--- c-typeck.c.orig	Mon Sep 17 12:56:35 2001
+++ c-typeck.c	Tue Jan  8 23:29:54 2002
@@ -3822,6 +3822,12 @@ build_c_cast (type, expr)
 	  while (TREE_CODE (in_type) == POINTER_TYPE
 		 && TREE_CODE (in_otype) == POINTER_TYPE);
 
+	  if (TREE_CODE (in_type) == FUNCTION_TYPE
+              && TREE_CODE (in_otype) == FUNCTION_TYPE)
+	    /* Only static functions will have a TYPE_QUAL_CONST
+	       bit set here, so mask it off. */
+	    warn &= ~TYPE_QUAL_CONST;
+
 	  if (warn)
 	    /* There are qualifiers present in IN_OTYPE that are not
 	       present in IN_TYPE.  */
>Release-Note:
>Audit-Trail:
>Unformatted:


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