From 0485e60fb852895e758d80164fef8bb3bfd3e60c Mon Sep 17 00:00:00 2001 From: Matthias Braun Date: Mon, 18 Feb 2008 15:00:22 +0000 Subject: [PATCH] fix is_constant_expression for conditionals [r18892] --- ast.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/ast.c b/ast.c index e1a20f3..ba40144 100644 --- a/ast.c +++ b/ast.c @@ -1464,13 +1464,17 @@ bool is_constant_expression(const expression_t *expression) case EXPR_COMPOUND_LITERAL: return is_constant_initializer(expression->compound_literal.initializer); - case EXPR_CONDITIONAL: - /* TODO: not correct, we only have to test expressions which are - * evaluated, which means either the true or false part might be not - * constant */ - return is_constant_expression(expression->conditional.condition) - && is_constant_expression(expression->conditional.true_expression) - && is_constant_expression(expression->conditional.false_expression); + case EXPR_CONDITIONAL: { + expression_t *condition = expression->conditional.condition; + if(!is_constant_expression(condition)) + return false; + + long val = fold_constant(condition); + if(val != 0) + return is_constant_expression(expression->conditional.true_expression); + else + return is_constant_expression(expression->conditional.false_expression); + } case EXPR_ARRAY_ACCESS: return is_constant_expression(expression->array_access.array_ref) -- 2.20.1