commit 3eae8536814569284d81bae4e5bdae82b1b9b3e8
parent 677014ec4c6f86d6a2d8502c4e8bd972919ce0ec
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Fri, 4 Sep 2015 19:41:54 +0200
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.
Diffstat:
3 files changed, 16 insertions(+), 8 deletions(-)
diff --git a/cc1/cc1.h b/cc1/cc1.h
@@ -367,8 +367,7 @@ extern Node *castcode(Node *np, Type *newtp);
/* 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);
diff --git a/cc1/expr.c b/cc1/expr.c
@@ -353,7 +353,7 @@ negate(Node *np)
return np;
}
-Node *
+static Node *
exp2cond(Node *np, char neg)
{
np = decay(np);
@@ -977,15 +977,12 @@ expr(void)
}
Node *
-condition(void)
+condexpr(void)
{
Node *np;
- expect('(');
np = exp2cond(expr(), 0);
if (np->constant)
warn("conditional expression is constant");
- expect(')');
-
return np;
}
diff --git a/cc1/stmt.c b/cc1/stmt.c
@@ -49,6 +49,18 @@ stmtexp(Symbol *lbreak, Symbol *lcont, Caselist *lswitch)
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 @@ For(Symbol *lbreak, Symbol *lcont, Caselist *lswitch)
expect('(');
einit = (yytoken != ';') ? expr() : NULL;
expect(';');
- econd = (yytoken != ';') ? exp2cond(expr(), 0) : NULL;
+ econd = (yytoken != ';') ? condexpr() : NULL;
expect(';');
einc = (yytoken != ')') ? expr() : NULL;
expect(')');