NetBSD Problem Report #44253

From dholland@macaran.localdomain  Mon Dec 20 05:38:40 2010
Return-Path: <dholland@macaran.localdomain>
Received: from mail.netbsd.org (mail.netbsd.org [204.152.190.11])
	by www.NetBSD.org (Postfix) with ESMTP id BC0AF63B883
	for <gnats-bugs@gnats.NetBSD.org>; Mon, 20 Dec 2010 05:38:40 +0000 (UTC)
Message-Id: <20101220042001.80DD76E1B2@macaran.localdomain>
Date: Sun, 19 Dec 2010 23:20:01 -0500 (EST)
From: dholland@eecs.harvard.edu
Reply-To: dholland@eecs.harvard.edu
To: gnats-bugs@gnats.NetBSD.org
Subject: gcc's cpp mishandles ?: operator precedence
X-Send-Pr-Version: 3.95

>Number:         44253
>Category:       toolchain
>Synopsis:       gcc's cpp mishandles ?: operator precedence
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    toolchain-manager
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Mon Dec 20 05:40:00 +0000 2010
>Closed-Date:    Sat Dec 31 20:54:34 +0000 2011
>Last-Modified:  Sat Dec 31 20:54:34 +0000 2011
>Originator:     David A. Holland
>Release:        NetBSD 5.99.41 (20101130)
>Organization:
>Environment:
System: NetBSD macaran 5.99.41 NetBSD 5.99.41 (MACARAN) #3: Tue Nov 30 21:52:01 EST 2010 dholland@macaran:/usr/src/sys/arch/amd64/compile/MACARAN amd64
Architecture: x86_64
Machine: amd64
>Description:

cpp seems to mishandle nested ?: in #if expressions.

>How-To-Repeat:

  --- snip ---
#include <stdio.h>

int d =
#if 2 > 1 ? 0 : 0 ? 1 : 1
1
#else
0
#endif
;

int e =
#if (2 > 1 ? 0 : 0) ? 1 : 1
1
#else
0
#endif
;

int f =
#if 2 > 1 ? 0 : (0 ? 1 : 1)
1
#else
0
#endif
;


int
main()
{
	int a, b, c;

	a = 2 > 1 ? 0 : 0 ? 1 : 1;
	b = (2 > 1 ? 0 : 0) ? 1 : 1;
	c = 2 > 1 ? 0 : (0 ? 1 : 1);

	printf("%d %d %d\n", a, b, c);
	printf("%d %d %d\n", d, e, f);

	return 0;
}
   --- snip ---

Running this produces the output

0 1 0
1 1 0

That is, the cpp behavior is not consistent with the behavior of the C
expression parser.

According to operator(7) the C expression parser is correct and cpp is
broke.

>Fix:

apply a large hammer?

>Release-Note:

>Audit-Trail:
From: David Holland <dholland@eecs.harvard.edu>
To: gnats-bugs@netbsd.org
Cc: 
Subject: Re: toolchain/44253: gcc's cpp mishandles ?: operator precedence
Date: Tue, 21 Dec 2010 20:40:03 -0500

    ------

 From: "Joseph S. Myers" <joseph@codesourcery.com>
 To: David Holland <dholland@eecs.harvard.edu>
 cc: bug-gcc@gnu.org
 Subject: Re: gcc's cpp makes ?: left-associative
 Date: Mon, 20 Dec 2010 11:52:59 +0000 (UTC)

 On Mon, 20 Dec 2010, David Holland wrote:

 > as well as with the gcc 4.1 used by CentOS and NetBSD. The Debian one
 > is unfortunately the latest gcc I have ready access to. However, this
 > is obscure enough that I'm assuming nobody else is likely to have
 > noticed it in the meantime.

 This no longer appears and was probably fixed by:

 2008-05-06  Tom Tromey  <tromey@redhat.com>

         PR preprocessor/35313, PR preprocessor/36088:
         * expr.c (optab) <QUERY, COMMA>: Set precedence to 4.
         (reduce) <case CPP_QUERY>: Special case CPP_COMMA and CPP_COLON.

 -- 
 Joseph S. Myers
 joseph@codesourcery.com

From: David Holland <dholland-bugs@netbsd.org>
To: gnats-bugs@NetBSD.org
Cc: 
Subject: Re: toolchain/44253: gcc's cpp mishandles ?: operator precedence
Date: Sun, 26 Dec 2010 07:58:31 +0000

  >  This no longer appears and was probably fixed by:
  > [...]

 The associated upstream bug report:

 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36088

 ...however, we probably can't pull the fix without hitting gplv3 issues.

 -- 
 David A. Holland
 dholland@netbsd.org

State-Changed-From-To: open->closed
State-Changed-By: dholland@NetBSD.org
State-Changed-When: Sat, 31 Dec 2011 20:54:34 +0000
State-Changed-Why:
Fixed by updating gcc.


>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.51 2026/08/10 02:28:17 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.