From patchwork Fri Mar 31 01:44:54 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: 9655403 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 B50B360113 for ; Fri, 31 Mar 2017 01:46:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A639428688 for ; Fri, 31 Mar 2017 01:46:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9A28828696; Fri, 31 Mar 2017 01:46:51 +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 3924D28688 for ; Fri, 31 Mar 2017 01:46:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934857AbdCaBqv (ORCPT ); Thu, 30 Mar 2017 21:46:51 -0400 Received: from mail-wr0-f196.google.com ([209.85.128.196]:33361 "EHLO mail-wr0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934889AbdCaBqs (ORCPT ); Thu, 30 Mar 2017 21:46:48 -0400 Received: by mail-wr0-f196.google.com with SMTP id u18so16611762wrc.0 for ; Thu, 30 Mar 2017 18:46:47 -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=Vb56/q/uSJ9N57cqpwnKjDXLhC0KwyBocVKmZneouBY=; b=m/xBRs1xaS8cfwh+GNsMSPaLt7cL17N1ym5+pi52PRnqNlOuPqtGpDhHeebpLpNySO JxhtMucwhCr+TBrO8+L+HzzWyaNIwQe5+jUpUSX+DG+Ti8n2Pi3Mhn8/RiYd64M2ldyW wBXYJfgJNAuB+IUBgkV46tFm5RjUWj5TrXMO9hZZDCpZg2/yuhbXpSSsB5Vblf7M/5EO f/BhlgSUPgV7JRuW5oafc4iwszNoohFiIIjZuKVWUyRds4fOKi/VxhjbuEIF7BvNTgsI WVrnJV1rzMcmw8WoGiW2VjVpHsxodhjvxxLQfn0W4+Q72+eaRlRxCpyTjBULK7SN9nNA Ouqw== 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=Vb56/q/uSJ9N57cqpwnKjDXLhC0KwyBocVKmZneouBY=; b=aS8FO7e/ia9MCWl7AHCcyFlskr6cV+L/HZzTmQmVlLSt7Pz9T4CB5CKDdL0LZ0IAeg 0GquO82vZgZrvyuNqCvo1psClaHbLfIONcmXmcB/LOZaJViJxfyhiyRXFq8CuBOecvBp RJGo4icBJvQxGS8CosoLJ2JOrYpYRGtdl4OBz7qWDKlIhfxtfHiwYVhZ2QOr7Wwgo57M ba5S5VJvU+iN/UYIopb/ibGVdkJd5HaBJHyyeAYol4Fb+wU1uRI6RW8TZONvQflcCAnJ ydouAyKsF3V00ahT+8sMRt0vxwXOz0KDtG7PEjehV6kaLCiSE4BiHQBw/P9supoWnkqK MIAg== X-Gm-Message-State: AFeK/H3eBGNOAhl8KgtoqmyOSk7KEXAX1v72HqfuxUXEje980tIWqF584Ecu6FOi/sW1zA== X-Received: by 10.28.61.11 with SMTP id k11mr828308wma.119.1490924806909; Thu, 30 Mar 2017 18:46:46 -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.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 30 Mar 2017 18:46:46 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Christopher Li , Nicolai Stange , Luc Van Oostenryck Subject: [PATCH v4 20/25] constexpr: relax some constant expression rules for pointer expressions Date: Fri, 31 Mar 2017 03:44:54 +0200 Message-Id: <20170331014459.9351-21-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 The official constraints on constant expressions [6.6] are insanely strict in that they do not allow some constructs commonly used in the wild. Relax them by treating - address constants cast to different pointer type as address constants again, - address constants cast to arithmetic type as arithmetic constant expressions - conditional expressions whose true and false branches both yield address constants as address constants, - and conditional expressions whose condition is an address constant as an constant expression to the extent their true and false branches allow. Signed-off-by: Nicolai Stange Signed-off-by: Luc Van Oostenryck --- evaluate.c | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/evaluate.c b/evaluate.c index be72b22b9..022c7b5f3 100644 --- a/evaluate.c +++ b/evaluate.c @@ -1136,10 +1136,20 @@ static struct symbol *evaluate_conditional_expression(struct expression *expr) expr->flags = (expr->conditional->flags & (*true)->flags & expr->cond_false->flags & ~CEF_CONST_MASK); /* + * A conditional operator yields a particular constant + * expression type only if all of its three subexpressions are + * of that type [6.6(6), 6.6(8)]. + * As an extension, relax this restriction by allowing any + * constant expression type for the condition expression. + * * A conditional operator never yields an address constant * [6.6(9)]. + * However, as an extension, if the condition is any constant + * expression, and the true and false expressions are both + * address constants, mark the result as an address constant. */ - expr->flags &= ~CEF_ADDR; + if (expr->conditional->flags & (CEF_ACE | CEF_ADDR)) + expr->flags = (*true)->flags & expr->cond_false->flags & ~CEF_CONST_MASK; lclass = classify_type(ltype, <ype); rclass = classify_type(rtype, &rtype); @@ -2762,6 +2772,14 @@ static int cast_flags(struct expression *expr, struct expression *old) * constants [6.6(9)]. */ flags &= ~CEF_ADDR; + + /* + * As an extension, treat address constants cast to + * integer type as an arithmetic constant. + */ + if (old->flags & CEF_ADDR) + flags = CEF_ACE; + /* * Cast to float type -> not an integer constant * expression [6.6(6)]. @@ -2778,8 +2796,15 @@ static int cast_flags(struct expression *expr, struct expression *old) /* * Casts of integer literals to pointer type yield * address constants [6.6(9)]. + * + * As an extension, treat address constants cast to a + * different pointer type as address constants again. + * + * As another extension, treat integer constant + * expressions (in contrast to literals) cast to + * pointer type as address constants. */ - if (old->flags & CEF_INT) + if (old->flags & (CEF_ICE | CEF_ADDR)) flags = CEF_ADDR; }