🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.gcc.bug
Article #6087

patch to permit void RHS for `&&' and `||'

#6087
From: Fergus Henderson
Date: Fri, 20 Dec 1996 00:00
77 lines
3531 bytes
Hi,

It would be nice if C and C++ supported the sh and Perl style
of error handling:

	some_operation || die("it failed");

Unfortunately that doesn't work in ANSI/ISO C or C++...
but here's a patch to make it work in GNU C/C++.

--- c-typeck.c.orig	Sun Jun 30 02:27:15 1996
+++ c-typeck.c	Fri Dec 20 04:05:58 1996
@@ -1782,13 +1782,31 @@
      enum tree_code code;
      tree arg1, arg2;
 {
-  tree result = build_binary_op (code, arg1, arg2, 1);
+  tree result;
 
   char class;
   char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
   char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
   enum tree_code code1 = ERROR_MARK;
   enum tree_code code2 = ERROR_MARK;
+
+  /* As an extension to ANSI C, we allow `&&' and `||' expressions with a
+     right-hand operand of type void, for code such as `errno == 0 || abort()'.
+     We implement this by simply replacing expressions such as
+     `FOO && (void) BAR' with `FOO ? (void) BAR : 0', and
+     `FOO || (void) BAR' with `FOO ? 1 : (void) BAR'.  */
+  if ((code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
+      && TREE_CODE (TREE_TYPE(arg2)) == VOID_TYPE)
+    {
+      if (pedantic)
+	pedwarn ("ANSI C forbids right-hand operand to `%s' of type void",
+		 (code == TRUTH_ANDIF_EXPR ? "&&" : "||"));
+      result = (code == TRUTH_ANDIF_EXPR
+	     ? build_conditional_expr(arg1, arg2, integer_zero_node)
+	     : build_conditional_expr(arg1, integer_one_node, arg2));
+    }
+  else
+    result = build_binary_op (code, arg1, arg2, 1);
 
   if (class1 == 'e' || class1 == '1'
       || class1 == '2' || class1 == '<')
--- cp/typeck.c.orig	Fri Dec 20 03:50:17 1996
+++ cp/typeck.c	Fri Dec 20 04:11:48 1996
@@ -2853,6 +2853,23 @@
   if (code == MEMBER_REF)
     return build_m_component_ref (build_indirect_ref (arg1, NULL_PTR),
 				  arg2);
+
+  /* As an extension to ANSI C++, we allow `&&' and `||' expressions with a
+     right-hand operand of type void, for code such as `errno == 0 || abort()'.
+     We implement this by simply replacing expressions such as
+     `FOO && (void) BAR' with `FOO ? (void) BAR : 0', and
+     `FOO || (void) BAR' with `FOO ? 1 : (void) BAR'.  */
+  if ((code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
+      && TREE_CODE (TREE_TYPE(arg2)) == VOID_TYPE)
+    {
+      if (pedantic)
+	pedwarn ("ANSI C forbids right-hand operand to `%s' of type void",
+ 	 	 (code == TRUTH_ANDIF_EXPR ? "&&" : "||"));
+      return (code == TRUTH_ANDIF_EXPR
+ 	     ? build_conditional_expr(arg1, arg2, integer_zero_node)
+ 	     : build_conditional_expr(arg1, integer_one_node, arg2));
+    }
+ 
   return build_binary_op (code, arg1, arg2, 1);
 }
 

-- 
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
Technical Details
Message-ID: <199612191718.EAA15591@kryten.cs.mu.OZ.AU.mu.OZ.AU>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 113d02,727a5cb51958e971,start
X-Google-Attributes: gid113d02,public
From: fjh@kryten.cs.mu.OZ.AU (Fergus Henderson)
Subject: patch to permit void RHS for `&&' and `||'
Date: 1996/12/20
Message-ID: <199612191718.EAA15591@kryten.cs.mu.OZ.AU.mu.OZ.AU>
X-Deja-AN: 205008624
sender: bug-gcc-request@prep.ai.mit.edu
content-type: text/plain; charset=US-ASCII
x-gateway: relay3.UU.NET from bug-gcc to gnu.gcc.bug; Thu, 19 Dec 1996 19:15:18 EST
mime-version: 1.0
newsgroups: gnu.gcc.bug
x-mailer: ELM [version 2.4 PL23]