From: Christoph Mallon Date: Sun, 16 Nov 2008 07:25:43 +0000 (+0000) Subject: Warn about reference address as bool in condition of ?: and if (), too, not only... X-Git-Url: http://nsz.repo.hu/git/?a=commitdiff_plain;h=610d20bcf83b946c7c97835c7bb87707853a44dd;p=cparser Warn about reference address as bool in condition of ?: and if (), too, not only operands of !, && and ||. [r23697] --- diff --git a/parser.c b/parser.c index 12b9d7d..cb0147a 100644 --- a/parser.c +++ b/parser.c @@ -7746,6 +7746,48 @@ static bool same_compound_type(const type_t *type1, const type_t *type2) type1->compound.compound == type2->compound.compound; } +static expression_t const *get_reference_address(expression_t const *expr) +{ + bool regular_take_address = true; + for (;;) { + if (expr->kind == EXPR_UNARY_TAKE_ADDRESS) { + expr = expr->unary.value; + } else { + regular_take_address = false; + } + + if (expr->kind != EXPR_UNARY_DEREFERENCE) + break; + + expr = expr->unary.value; + } + + if (expr->kind != EXPR_REFERENCE) + return NULL; + + /* special case for functions which are automatically converted to a + * pointer to function without an extra TAKE_ADDRESS operation */ + if (!regular_take_address && + expr->reference.entity->kind != ENTITY_FUNCTION) { + return NULL; + } + + return expr; +} + +static void warn_reference_address_as_bool(expression_t const* expr) +{ + if (!warning.address) + return; + + expr = get_reference_address(expr); + if (expr != NULL) { + warningf(&expr->base.source_position, + "the address of '%Y' will always evaluate as 'true'", + expr->reference.entity->base.symbol); + } +} + /** * Parse a conditional expression, ie. 'expression ? ... : ...'. * @@ -7758,6 +7800,8 @@ static expression_t *parse_conditional_expression(expression_t *expression) conditional_expression_t *conditional = &result->conditional; conditional->condition = expression; + warn_reference_address_as_bool(expression); + eat('?'); add_anchor_token(':'); @@ -8096,48 +8140,6 @@ static void semantic_unexpr_plus(unary_expression_t *expression) "traditional C rejects the unary plus operator"); } -static expression_t const *get_reference_address(expression_t const *expr) -{ - bool regular_take_address = true; - for (;;) { - if (expr->kind == EXPR_UNARY_TAKE_ADDRESS) { - expr = expr->unary.value; - } else { - regular_take_address = false; - } - - if (expr->kind != EXPR_UNARY_DEREFERENCE) - break; - - expr = expr->unary.value; - } - - if (expr->kind != EXPR_REFERENCE) - return NULL; - - /* special case for functions which are automatically converted to a - * pointer to function without an extra TAKE_ADDRESS operation */ - if (!regular_take_address && - expr->reference.entity->kind != ENTITY_FUNCTION) { - return NULL; - } - - return expr; -} - -static void warn_reference_address_as_bool(expression_t const* expr) -{ - if (!warning.address) - return; - - expr = get_reference_address(expr); - if (expr != NULL) { - warningf(&expr->base.source_position, - "the address of '%Y' will always evaluate as 'true'", - expr->reference.entity->base.symbol); - } -} - static void semantic_not(unary_expression_t *expression) { type_t *const orig_type = expression->value->base.type; @@ -9541,6 +9543,7 @@ static statement_t *parse_if(void) add_anchor_token(')'); expression_t *const expr = parse_expression(); statement->ifs.condition = expr; + warn_reference_address_as_bool(expr); mark_vars_read(expr, NULL); rem_anchor_token(')'); expect(')');