From patchwork Fri Mar 31 01:44:38 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 9655371 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 664A760352 for ; Fri, 31 Mar 2017 01:46:35 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5815028617 for ; Fri, 31 Mar 2017 01:46:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4BC9A28688; Fri, 31 Mar 2017 01:46:35 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D8FAB28693 for ; Fri, 31 Mar 2017 01:46:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934872AbdCaBqf (ORCPT ); Thu, 30 Mar 2017 21:46:35 -0400 Received: from mail-wr0-f194.google.com ([209.85.128.194]:35122 "EHLO mail-wr0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934742AbdCaBqd (ORCPT ); Thu, 30 Mar 2017 21:46:33 -0400 Received: by mail-wr0-f194.google.com with SMTP id p52so16621133wrc.2 for ; Thu, 30 Mar 2017 18:46:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=4bcajv1MFqCKnf5evsEYGkhJ/g3LbsgTMWmoP4e2e6s=; b=KAZJ5IDhUkrQCDLW9hvMUjCZXTBnV+/I+xBzvnXwgksmmJayhsaVlxNYHSMuzu8zvy iS7bQDjgb11jr8vb8GMfkyajCnan5YnZYLEBNLzJ6RL1nWeS3lIIw0nVPwPu+d2mqnZA aWAFvs/vbQc4w7PttF6MTWyO5wVOJYVb8YBo5UCoJ8ggluZjqQntCyu/8ioFKa3m5pyK 7NsW8XBFjHh4XMUBhrGY4xlQ2a32lsH1rl1308LzUVnyw+r+5RxV8MSfof1TzlheH8xm 1o0qJm1fewD2gF+BRoQanojH7evAIEmC59GUgMeBw12OYnarW1oUm91k+K9PrylM6GKl 7bmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=4bcajv1MFqCKnf5evsEYGkhJ/g3LbsgTMWmoP4e2e6s=; b=L0YMF1i+OgVHHUO00pV9iVcnjV5mLv6yAGEyotGd4DHREeKsM5JT0g9faa/STPJHoG uRwQxcWcwdhc0drI7yRovCXaWEdGC0r4MxQ3dAbuKV7u2xGdo+lMt+2BVtGD0Ci1Z5LN gSsQYyb+uYv/Bx4pTMAtgRSmtP4E1jnXsAB7Hp/QyvBPn9BanfX8CiVvYGeuqQhAG8FC 4WNrIUfnOlmsQVDMcAopGHzk5OqGbe/peBzozTnLc5MAolrUlfCaWiHTYfJJp3mqOeKS fJmZpSP0dxVXvAhE2YMpxOfVCxTc1EOXA+CPFcRqOSsPehsmEUQzT9v/Q8r7dVw+LhNa 0keA== X-Gm-Message-State: AFeK/H09d6JaV14yFFvgKeQb5JGRKTqIVRAoghM60Po7pKV2X3UkCOQKOAOwMOinCiYkUg== X-Received: by 10.28.178.142 with SMTP id b136mr397072wmf.57.1490924791432; Thu, 30 Mar 2017 18:46:31 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:88d:1900:cdad:92b8:5279:ae19]) by smtp.gmail.com with ESMTPSA id 189sm917023wmm.31.2017.03.30.18.46.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 30 Mar 2017 18:46:30 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Christopher Li , Nicolai Stange , Luc Van Oostenryck Subject: [PATCH v4 04/25] constexpr: examine constness of binops and alike at evaluation only Date: Fri, 31 Mar 2017 03:44:38 +0200 Message-Id: <20170331014459.9351-5-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.12.0 In-Reply-To: <20170331014459.9351-1-luc.vanoostenryck@gmail.com> References: <20170331014459.9351-1-luc.vanoostenryck@gmail.com> Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Nicolai Stange Move the whole calculation of binary operations', compare and logical expressions' constness flags to the evaluation phase such that expressions like 0 + __builtin_choose_expr(0, 0, 0) 0 < __builtin_choose_expr(0, 0, 0) 0 && __builtin_choose_expr(0, 0, 0) can now be recognized as qualifying as integer constant expressions. Signed-off-by: Nicolai Stange Signed-off-by: Luc Van Oostenryck --- evaluate.c | 19 ++++++------------- expression.c | 3 --- validation/constexpr-binop.c | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 16 deletions(-) create mode 100644 validation/constexpr-binop.c diff --git a/evaluate.c b/evaluate.c index 5725e898f..48530f28c 100644 --- a/evaluate.c +++ b/evaluate.c @@ -889,10 +889,8 @@ static struct symbol *evaluate_logical(struct expression *expr) /* the result is int [6.5.13(3), 6.5.14(3)] */ expr->ctype = &int_ctype; - if (expr->flags) { - if (!(expr->left->flags & expr->right->flags & CEF_ICE)) - expr->flags = CEF_NONE; - } + expr->flags = expr->left->flags & expr->right->flags; + expr->flags &= ~CEF_CONST_MASK; return &int_ctype; } @@ -903,13 +901,11 @@ static struct symbol *evaluate_binop(struct expression *expr) int rclass = classify_type(expr->right->ctype, &rtype); int op = expr->op; - if (expr->flags) { - if (!(expr->left->flags & expr->right->flags & CEF_ICE)) - expr->flags = CEF_NONE; - } - /* number op number */ if (lclass & rclass & TYPE_NUM) { + expr->flags = expr->left->flags & expr->right->flags; + expr->flags &= ~CEF_CONST_MASK; + if ((lclass | rclass) & TYPE_FLOAT) { switch (op) { case '+': case '-': case '*': case '/': @@ -1010,10 +1006,7 @@ static struct symbol *evaluate_compare(struct expression *expr) struct symbol *ctype; const char *typediff; - if (expr->flags) { - if (!(expr->left->flags & expr->right->flags & CEF_ICE)) - expr->flags = CEF_NONE; - } + expr->flags = left->flags & right->flags & ~CEF_CONST_MASK; /* Type types? */ if (is_type_type(ltype) && is_type_type(rtype)) diff --git a/expression.c b/expression.c index 3262cf22c..b59af8886 100644 --- a/expression.c +++ b/expression.c @@ -146,7 +146,6 @@ static struct token *builtin_types_compatible_p_expr(struct token *token, { struct expression *expr = alloc_expression( token->pos, EXPR_COMPARE); - expr->flags = CEF_SET_ICE; expr->op = SPECIAL_EQUAL; token = token->next; if (!match_op(token, '(')) @@ -758,8 +757,6 @@ static struct token *cast_expression(struct token *token, struct expression **tr sparse_error(next->pos, "No right hand side of '%s'-expression", show_special(op)); \ break; \ } \ - top->flags = left->flags & right->flags \ - & ~CEF_CONST_MASK; \ top->op = op; \ top->left = left; \ top->right = right; \ diff --git a/validation/constexpr-binop.c b/validation/constexpr-binop.c new file mode 100644 index 000000000..85a88e3cd --- /dev/null +++ b/validation/constexpr-binop.c @@ -0,0 +1,33 @@ +static int a[] = { + [0 + 0] = 0, // OK + [0 + 0.] = 0, // KO + [(void*)0 + 0] = 0, // KO + [0 + __builtin_choose_expr(0, 0, 0)] = 0, // OK + [0 + __builtin_choose_expr(0, 0., 0)] = 0, // OK + [0 + __builtin_choose_expr(0, 0, 0.)] = 0, // KO + [0 < 0] = 0, // OK + [0 < 0.] = 0, // KO + [0 < __builtin_choose_expr(0, 0, 0)] = 0, // OK + [0 < __builtin_choose_expr(0, 0., 0)] = 0, // OK + [0 < __builtin_choose_expr(0, 0, 0.)] = 0, // KO + [0 && 0] = 0, // OK + [0 && 0.] = 0, // KO + [0 && __builtin_choose_expr(0, 0, 0)] = 0, // OK + [0 && __builtin_choose_expr(0, 0., 0)] = 0, // OK + [0 && __builtin_choose_expr(0, 0, 0.)] = 0, // KO + [0 + __builtin_types_compatible_p(int, float)] = 0, // OK +}; + +/* + * check-name: Expression constness propagation in binops and alike + * + * check-error-start +constexpr-binop.c:3:12: error: bad constant expression +constexpr-binop.c:4:19: error: bad integer constant expression +constexpr-binop.c:7:12: error: bad constant expression +constexpr-binop.c:9:12: error: bad integer constant expression +constexpr-binop.c:12:12: error: bad integer constant expression +constexpr-binop.c:14:12: error: bad integer constant expression +constexpr-binop.c:17:12: error: bad integer constant expression + * check-error-end + */