shithub: scc

Download patch

ref: 3eae8536814569284d81bae4e5bdae82b1b9b3e8
parent: 677014ec4c6f86d6a2d8502c4e8bd972919ce0ec
author: Roberto E. Vargas Caballero <[email protected]>
date: Fri Sep 4 15:41:54 EDT 2015

Add condexpr()

There are two kind of conditional expressions, with and without
parentheses, but it is a good idea to handle them with the same
base function, and have the same warning in both cases.

--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -367,8 +367,7 @@
 /* expr.c */
 extern Node *expr(void), *negate(Node *np), *constexpr(void);
 extern Node *convert(Node *np, Type *tp1, char iscast);
-extern Node *eval(Node *np), *iconstexpr(void), *condition(void);
-extern Node *exp2cond(Node *np, char neg);
+extern Node *eval(Node *np), *iconstexpr(void), *condexpr(void);
 extern bool isnodecmp(int op);
 extern int negop(int op);
 extern bool cmpnode(Node *np, TUINT val);
--- a/cc1/expr.c
+++ b/cc1/expr.c
@@ -353,7 +353,7 @@
 	return np;
 }
 
-Node *
+static Node *
 exp2cond(Node *np, char neg)
 {
 	np = decay(np);
@@ -977,15 +977,12 @@
 }
 
 Node *
-condition(void)
+condexpr(void)
 {
 	Node *np;
 
-	expect('(');
 	np = exp2cond(expr(), 0);
 	if (np->constant)
 		warn("conditional expression is constant");
-	expect(')');
-
 	return np;
 }
--- a/cc1/stmt.c
+++ b/cc1/stmt.c
@@ -49,6 +49,18 @@
 	expect(';');
 }
 
+static Node *
+condition(void)
+{
+	Node *np;
+
+	expect('(');
+	np = condexpr();
+	expect(')');
+
+	return np;
+}
+
 static void
 While(Symbol *lbreak, Symbol *lcont, Caselist *lswitch)
 {
@@ -86,7 +98,7 @@
 	expect('(');
 	einit = (yytoken != ';') ? expr() : NULL;
 	expect(';');
-	econd = (yytoken != ';') ? exp2cond(expr(), 0) : NULL;
+	econd = (yytoken != ';') ? condexpr() : NULL;
 	expect(';');
 	einc = (yytoken != ')') ? expr() : NULL;
 	expect(')');