From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 28638 invoked by alias); 6 Oct 2003 12:46:48 -0000 Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org Received: (qmail 28629 invoked from network); 6 Oct 2003 12:46:46 -0000 Received: from unknown (HELO mx3.informatik.uni-tuebingen.de) (134.2.12.26) by sources.redhat.com with SMTP; 6 Oct 2003 12:46:46 -0000 Received: from juist (semeai [134.2.15.66]) by mx3.informatik.uni-tuebingen.de (Postfix) with ESMTP id 7CF10141 for ; Mon, 6 Oct 2003 14:46:45 +0200 (DFT) Received: from falk by juist with local (Exim 3.36 #1 (Debian)) id 1A6UlA-0006It-00 for ; Mon, 06 Oct 2003 14:46:44 +0200 To: gcc@gcc.gnu.org Subject: Suggested warning: "negating an expression of unsigned type does not yield a negative value" From: Falk Hueffner Date: Mon, 06 Oct 2003 12:46:00 -0000 Message-ID: <87zngev8i4.fsf@student.uni-tuebingen.de> User-Agent: Gnus/5.0808 (Gnus v5.8.8) XEmacs/21.5 (cabbage) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-SW-Source: 2003-10/txt/msg00152.txt.bz2 Hi, I just found yet another bug of the kind: int f (int *p, unsigned x) { return p[-x]; } which only manifests on 64 bit platforms, because most (all?) platforms have wrapping address arithmetic. So I was wondering about a general warning about negating unsigned values, since I couldn't really think of a legitimate application. A quick check with the gcc source turned up: gengtype-lex.l: char *namestart; size_t namelen; [...] for (namelen = 1; !ISSPACE (namestart[-namelen]); namelen++) This looks actually invalid to me, although it will probably work everywhere. In fold_const.c, there's case RSHIFT_EXPR: int2l = -int2l; also "invalid but works" since it's later passed to a function taking int. Then there's everybody's favourite idiom "x &= -x", but it can be expressed clearer as "x &= ~x + 1". Then there's constant folding in neg_double. Hm. Damn. I can't think of any reformulation which does not obscure the code. So this warning should probably not be turned on by -W. But it seems generally useful. Any opinions? -- Falk