🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.gcc.bug
Article #2808

GCC incorrectly compiles ``"..." == 0 ? exp1 : exp2'' into ``exp1''.

#2808
From: Casey Leedom
Date: Mon, 08 Jan 1990 00:12
52 lines
1937 bytes
[[If you develop a fix for this, I'd appreciate hearing about it.  Thanks
for your attention.]]

VERSION:
	GCC 1.34 and GCC 1.36

CLIENT MACHINE and OPERATING SYSTEM:
	GCC 1.34; Alliant FX/8; Alliant Concentrix 5.0.0
	GCC 1.36; Vax-11/785; 4.3BSD with 4.3-tahoe networking
	GCC 1.36; Sun 3/280; Sun OS 3.5

SYNOPSIS:
	GCC incorrectly compiles ``"..." == 0 ? exp1 : exp2'' into ``exp1''.

REPEAT BY:
	main()
	{
		char *s;
		int i;
	
		/* CORRECT:
		 * the following is compiled into ``i = 0'' (false)
		 */
			i = ("all" == 0);
			printf("i = %d\n", i);
	
		/* WRONG:
		 * the following is compiled into ``s = "true"''
		 */
			s = (("all" == 0) ? "true" : "false");
			printf("s = \"%s\"\n", s);
	
		/* CORRECT:
		 * the following is compiled into ``s = "false"''
		 */
			if ("all" == 0)
				s = "true";
			else
				s = "false";
			printf("s = \"%s\"\n", s);
	}

SAMPLE FIX:
	I don't know the compiler well enough to be able to specify a fix,
	but I do notice one significant difference between the handling of
	the boolean expression in the "?:" operator and if statement: on
	line 984 of c-parse.y the if statement boolean expression is passed
	directly to truthvalue_conversion(), while on line 2631 of
	c-typeck.c in the function build_conditional_expr(), the "?:"
	operator boolean expression is passed through default_conversion()
	whose result is then passed to truthvalue_conversion().  What
	significance this may have, I have no idea.
Technical Details
Message-ID: <9001080012.AA15514@gauss.llnl.gov>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!tut.cis.ohio-state.edu!GAUSS.LLNL.GOV!casey

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!tut.cis.ohio-state.edu!GAUSS.LLNL.GOV!casey
From: casey@GAUSS.LLNL.GOV (Casey Leedom)
Newsgroups: gnu.gcc.bug
Subject: GCC incorrectly compiles ``"..." == 0 ? exp1 : exp2'' into ``exp1''.
Message-ID: <9001080012.AA15514@gauss.llnl.gov>
Date: 8 Jan 90 00:12:28 GMT
Sender: daemon@tut.cis.ohio-state.edu
Distribution: gnu
Organization: GNUs Not Usenet
Lines: 52