Bug 24667 - [4.0/4.1 Regression] -Wcast-qual fails to warn about offending cast for a old style cast
Summary: [4.0/4.1 Regression] -Wcast-qual fails to warn about offending cast for a old...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.0.2
: P3 minor
Target Milestone: 4.0.3
Assignee: Mark Mitchell
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2005-11-04 06:11 UTC by David Fang
Modified: 2005-11-15 17:57 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description David Fang 2005-11-04 06:11:36 UTC
g++ -Wcast-qual doesn't seem to reject the following code, it just accepts quitely without warning.  

//======= "foo.cc" ==================
int main(int, char**) {
const int foo[2] = {1,1};
((int*)foo)[0] = 0;
}
//=========================

command: g++ -Wcast-qual foo.cc

program output: (none)

expected output (from g++ 3.3):
foo.cc: In function `int main(int, char**)':
foo.cc:3: warning: cast from `const int*' to `int*' discards qualifiers from 
   pointer target type

keywords: accepts-invalid

known to work: 3.3.x
known to fail: 4.0.2

remarks: possibly closely related to PR 20475

Originator: Dmitry Markman <dmarkman at mac dot com>, reported on xcode-users@lists.apple.com.
(I wasn't able to add him to the CC list.)
Comment 1 David Fang 2005-11-04 07:47:18 UTC
also known to work: 3.4.0
Comment 2 Andrew Pinski 2005-11-04 14:33:25 UTC
Hmm, old (C) style casts are weird, IIRC they act as either a static_cast, a reinterpret_cast, or a const_cast.  In this case they are acting like a const_cast which is what is normally goes and gets rid of a qualifier.
Comment 3 David Fang 2005-11-04 17:56:28 UTC
Am I correct in expecting that const_cast<...> should pass -Wcast-qual without warning?  That's the way I use it (in the few and rare cases that demand it) and it's met my expectations thus far.  I'm kind of surprised I haven't tripped this bug myself, but that's because I stopped using C-style casts long ago, enforced by -Wold-style-cast -Werror.  Adding -Wold-style-cast does trip a warning about old casts with the posted code, but nothing further about discarding qualifiers (gcc4).  
Comment 4 Janis Johnson 2005-11-04 21:18:25 UTC
A regression test identified the following patch to change handling of
C-style casts for PR c++/14035:

http://gcc.gnu.org/viewcvs?view=rev&rev=89300

r89300 | mmitchel | 2004-10-19 23:24:20 +0000 (Tue, 19 Oct 2004) | 35 lines
Comment 5 Mark Mitchell 2005-11-15 17:50:46 UTC
Subject: Bug 24667

Author: mmitchel
Date: Tue Nov 15 17:50:43 2005
New Revision: 107031

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=107031
Log:
	PR c++/24667
	* typeck.c (check_for_casting_away_constness): Use the diag_fn.
	(build_const_cast_1): Call it, for C-style casts.
	PR c++/24667
	* g++.dg/warn/Wcast-qual1.C: New test.

Added:
    branches/gcc-4_0-branch/gcc/testsuite/g++.dg/warn/Wcast-qual1.C
Modified:
    branches/gcc-4_0-branch/gcc/cp/ChangeLog
    branches/gcc-4_0-branch/gcc/cp/typeck.c
    branches/gcc-4_0-branch/gcc/testsuite/ChangeLog

Comment 6 Mark Mitchell 2005-11-15 17:52:38 UTC
Subject: Bug 24667

Author: mmitchel
Date: Tue Nov 15 17:52:34 2005
New Revision: 107032

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=107032
Log:
	PR c++/24667
	* typeck.c (check_for_casting_away_constness): Use the diag_fn.
	(build_const_cast_1): Call it, for C-style casts.
	PR c++/24667
	* g++.dg/warn/Wcast-qual1.C: New test.

Added:
    trunk/gcc/testsuite/g++.dg/warn/Wcast-qual1.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/typeck.c
    trunk/gcc/testsuite/ChangeLog

Comment 7 Mark Mitchell 2005-11-15 17:57:44 UTC
Fixed in 4.0.3.