From patchwork Fri Mar 31 01:44:52 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: 9655399 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 B168860113 for ; Fri, 31 Mar 2017 01:46:48 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A342528688 for ; Fri, 31 Mar 2017 01:46:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 984F828696; Fri, 31 Mar 2017 01:46:48 +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 383C528688 for ; Fri, 31 Mar 2017 01:46:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934642AbdCaBqs (ORCPT ); Thu, 30 Mar 2017 21:46:48 -0400 Received: from mail-wr0-f194.google.com ([209.85.128.194]:34267 "EHLO mail-wr0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935038AbdCaBqr (ORCPT ); Thu, 30 Mar 2017 21:46:47 -0400 Received: by mail-wr0-f194.google.com with SMTP id w43so16583345wrb.1 for ; Thu, 30 Mar 2017 18:46:45 -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=Sl18iIVYXZvD7U4sjziMIIsBACQ6CXuMtGqc6SvsINE=; b=i7YkEi3TnX1VSKIYmMEpbApIw5buffFppmfVWH6LkaEwoRTaaUC/Pk2QQgDE2oTmPn XnvRvus+h32fDTlJCAmAz+tsRWYrAC4eKoEkSyQznDORicWWfN4fOpYqpv1s4nASZgLM MLonnnrRS2V57Ra08wzM8ZZOXN34ReN76BoOfPn8OX1QkdC9isHTOpT129a2NshQR+Hg 9amhe1hORJmUnPRFtm+b7Ed8q8a4z1yXil0mKrdTSgNgjqfTXI3EmnRovKXOAcnM02I8 pgPfjAs7xlkhSItm8QuKclM+zcSGu+BxJkwYHU6OuIVB3MV6/pCauTaVvkxhgyx7q35b bLlA== 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=Sl18iIVYXZvD7U4sjziMIIsBACQ6CXuMtGqc6SvsINE=; b=oSLIYTVj/yFN9NpC8qYxfTtPiVMlNT9xSqOAf4WNLDvaZWL/Wd22IHwe0tBWskI/gk eYLnAlK5+r8lhT+Aq5Ho+c/q1oiS8cjALVn9Y1g6wPA4IkPtIBx/BkR0YWR7f+i6l0UX 6P8dHnj/8NTcY9JjuOSJHUUeCrgdgFFDfjIJRs98XYIBy23Kq2Wc/K4mzBMz5Wjo50sB zoA8aOoRhqVjgeeX68io7nPsDvTQT16XGO0fhlrIMSUtvLuTCEGyWXYJ+/tThFrIFCiG zavm9rwgezFdQeDjLIIHCh3tRouxZ6Scv9rv+MTjq8FfC7IqV+LC5eibnlMmmXnLqiAc UbBA== X-Gm-Message-State: AFeK/H0/YiaLqd7JeLN9HjtAjJ/KlnJppDG0OxwytvncXSmCJEcGZo+L+iMoF00+y0vr5Q== X-Received: by 10.28.54.137 with SMTP id y9mr407671wmh.90.1490924805051; Thu, 30 Mar 2017 18:46:45 -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.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 30 Mar 2017 18:46:44 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Christopher Li , Nicolai Stange , Luc Van Oostenryck Subject: [PATCH v4 18/25] constexpr: examine constness of __builtin_offsetof at evaluation only Date: Fri, 31 Mar 2017 03:44:52 +0200 Message-Id: <20170331014459.9351-19-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 Currently, the determination of a __builtin_offsetof() expressions' constness flags is done in two steps: - Several flags are speculatively set at expression parsing time - and possibly cleared again at evaluation if the member expression includes a non-const array index like in __builtin_offsetof(struct A, a.b[non_const_foo]) For consistency with other expression types' evaluation, defer the determination of a __builtin_offsetof() expression's constness to evaluation time, too. Furthermore, carry an array index expression's constness flags through the implicit cast to size_t type. Signed-off-by: Nicolai Stange Signed-off-by: Luc Van Oostenryck --- evaluate.c | 8 ++++++-- expression.c | 3 --- validation/constexpr-offsetof.c | 21 +++++++++++++++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 validation/constexpr-offsetof.c diff --git a/evaluate.c b/evaluate.c index cdbc08ef2..be72b22b9 100644 --- a/evaluate.c +++ b/evaluate.c @@ -3056,23 +3056,27 @@ static struct symbol *evaluate_offsetof(struct expression *expr) } else { struct expression *idx = expr->index, *m; struct symbol *i_type = evaluate_expression(idx); + unsigned old_idx_flags; int i_class = classify_type(i_type, &i_type); + if (!is_int(i_class)) { expression_error(expr, "non-integer index"); return NULL; } unrestrict(idx, i_class, &i_type); + old_idx_flags = idx->flags; idx = cast_to(idx, size_t_ctype); + idx->flags = old_idx_flags; m = alloc_const_expression(expr->pos, bits_to_bytes(ctype->bit_size)); m->ctype = size_t_ctype; - m->flags |= CEF_SET_ICE; + m->flags = CEF_SET_INT; expr->type = EXPR_BINOP; expr->left = idx; expr->right = m; expr->op = '*'; expr->ctype = size_t_ctype; - expr->flags = m->flags & idx->flags; + expr->flags = m->flags & idx->flags & ~CEF_CONST_MASK; } } if (e) { diff --git a/expression.c b/expression.c index 241ab7bad..3f1ea2141 100644 --- a/expression.c +++ b/expression.c @@ -199,7 +199,6 @@ static struct token *builtin_offsetof_expr(struct token *token, return expect(token, ')', "at end of __builtin_offset"); case SPECIAL_DEREFERENCE: e = alloc_expression(token->pos, EXPR_OFFSETOF); - e->flags = CEF_SET_ICE; e->op = '['; *p = e; p = &e->down; @@ -207,7 +206,6 @@ static struct token *builtin_offsetof_expr(struct token *token, case '.': token = token->next; e = alloc_expression(token->pos, EXPR_OFFSETOF); - e->flags = CEF_SET_ICE; e->op = '.'; if (token_type(token) != TOKEN_IDENT) { sparse_error(token->pos, "Expected member name"); @@ -219,7 +217,6 @@ static struct token *builtin_offsetof_expr(struct token *token, case '[': token = token->next; e = alloc_expression(token->pos, EXPR_OFFSETOF); - e->flags = CEF_SET_ICE; e->op = '['; token = parse_expression(token, &e->index); token = expect(token, ']', diff --git a/validation/constexpr-offsetof.c b/validation/constexpr-offsetof.c new file mode 100644 index 000000000..d1697b0c5 --- /dev/null +++ b/validation/constexpr-offsetof.c @@ -0,0 +1,21 @@ +struct A { + int a[1]; + int b; +}; + +extern int c; + +static int o[] = { + [__builtin_offsetof(struct A, b)] = 0, // OK + [__builtin_offsetof(struct A, a[0])] = 0, // OK + [__builtin_offsetof(struct A, a[0*0])] = 0, // OK + [__builtin_offsetof(struct A, a[c])] = 0 // KO +}; + +/* + * check-name: __builtin_offsetof() constness verification. + * + * check-error-start +constexpr-offsetof.c:12:39: error: bad constant expression + * check-error-end + */