Consider lazy evaluation for && and || in is_constant_expression().
authorChristoph Mallon <christoph.mallon@gmx.de>
Sat, 13 Dec 2008 10:18:45 +0000 (10:18 +0000)
committerChristoph Mallon <christoph.mallon@gmx.de>
Sat, 13 Dec 2008 10:18:45 +0000 (10:18 +0000)
[r24588]

ast.c

diff --git a/ast.c b/ast.c
index 4152eaf..36338a6 100644 (file)
--- a/ast.c
+++ b/ast.c
@@ -1927,8 +1927,6 @@ bool is_constant_expression(const expression_t *expression)
        case EXPR_BINARY_BITWISE_AND:
        case EXPR_BINARY_BITWISE_OR:
        case EXPR_BINARY_BITWISE_XOR:
-       case EXPR_BINARY_LOGICAL_AND:
-       case EXPR_BINARY_LOGICAL_OR:
        case EXPR_BINARY_SHIFTLEFT:
        case EXPR_BINARY_SHIFTRIGHT:
        case EXPR_BINARY_ISGREATER:
@@ -1940,6 +1938,24 @@ bool is_constant_expression(const expression_t *expression)
                return is_constant_expression(expression->binary.left)
                        && is_constant_expression(expression->binary.right);
 
+       case EXPR_BINARY_LOGICAL_AND: {
+               expression_t const *const left = expression->binary.left;
+               if (!is_constant_expression(left))
+                       return false;
+               if (fold_constant(left) == 0)
+                       return true;
+               return is_constant_expression(expression->binary.right);
+       }
+
+       case EXPR_BINARY_LOGICAL_OR: {
+               expression_t const *const left = expression->binary.left;
+               if (!is_constant_expression(left))
+                       return false;
+               if (fold_constant(left) != 0)
+                       return true;
+               return is_constant_expression(expression->binary.right);
+       }
+
        case EXPR_COMPOUND_LITERAL:
                return is_constant_initializer(expression->compound_literal.initializer);