From patchwork Fri Mar 31 01:44:47 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: 9655389 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 E3A3A60113 for ; Fri, 31 Mar 2017 01:46:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D60F728688 for ; Fri, 31 Mar 2017 01:46:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CAEF028696; Fri, 31 Mar 2017 01:46:43 +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 7C6ED28693 for ; Fri, 31 Mar 2017 01:46:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934997AbdCaBqn (ORCPT ); Thu, 30 Mar 2017 21:46:43 -0400 Received: from mail-wr0-f195.google.com ([209.85.128.195]:36039 "EHLO mail-wr0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935001AbdCaBqm (ORCPT ); Thu, 30 Mar 2017 21:46:42 -0400 Received: by mail-wr0-f195.google.com with SMTP id k6so15309975wre.3 for ; Thu, 30 Mar 2017 18:46:41 -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=6Hex9YDm8Dn1UscydGuRIXP1bGaiG9D4ltSLjzpW1Lk=; b=UTAOQ+YeVcKHM8qmNZwgTfWpCqnKvvsiKDmQFTF7JwQleSTdiwgYfzHjJSGBNO2mFJ hf8LMPxC63WyGdizJyk06c7pCizixnTw5F/ASJY6alVbcxQ9gY8X7faagKO9ooowA41w Pz4d6q7TIZV/PdFDfUWJyK8NzSMR0ubNOLWsUWvbRvWacyoQLLUNj6ly2tgeXLH9VJsu e09lO9AMc3HvwFNe/CO4qJecNSDOfppVmO+pw/WAVDxUK3twpeKDYnRR5/4/DmhvfHm7 icjzTrBcPXh1HSS2KifILy0HaYJTEsaVxOuTh3XiM3bAZoikylU3gEkGpjZVcfo/jwJl M2Ow== 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=6Hex9YDm8Dn1UscydGuRIXP1bGaiG9D4ltSLjzpW1Lk=; b=Tl5WGxmjQ8P2i3vsLLWDi5Xb3iGDdSOZiRSnUTlotem8Z/1KOoy/MrEdpQVIvh1rYm vO1ooQWCMaG0mvVQa4piEZMbFHjU3y8kjNvksZBUGDP8IKAMHfL0psGCeOdFE5VVT4YT TxEB0FQvPf2uHQH2shAVH08V+XLFUmnMMhH6gZ0XbPLffWr6/ETgy1Yi+5zSM+J7AQ6D y1IcPCXbq2RgPfAwUbbIw0/tjV9k+wOWJIaknQc8JrBOWU9KfuY3lGc7CU4r3ZtWx1Oj 665PSQVaiFyiRpodEjr44Kp/BPNCRwEayLCcWMugc6t62ZaF1r5EG3EGjCAZWfHJAzUq NnLg== X-Gm-Message-State: AFeK/H16BgqMvZUws1ItnRR4ixPvbE7qhfzljEtMW1UearIPeKl2jDx0wSWvEBzmpMBOkw== X-Received: by 10.28.69.147 with SMTP id l19mr846434wmi.55.1490924800326; Thu, 30 Mar 2017 18:46:40 -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.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 30 Mar 2017 18:46:39 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Christopher Li , Nicolai Stange , Luc Van Oostenryck Subject: [PATCH v4 13/25] constexpr: recognize address constants created through casts Date: Fri, 31 Mar 2017 03:44:47 +0200 Message-Id: <20170331014459.9351-14-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 According to 6.6(9), an address constant may get created by casting an integer constant to pointer type. Make evaluate_cast() handle this case, that is tag a cast expression as being an address constant if the target is a integer constant and the destination is of pointer type. Signed-off-by: Nicolai Stange Signed-off-by: Luc Van Oostenryck --- evaluate.c | 7 +++++++ validation/constexpr-pointer-cast.c | 13 +++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 validation/constexpr-pointer-cast.c diff --git a/evaluate.c b/evaluate.c index c111f6d19..ce2e52e15 100644 --- a/evaluate.c +++ b/evaluate.c @@ -2760,6 +2760,13 @@ static int cast_flags(struct expression *expr, struct expression *old) */ else if (old->flags & CEF_FLOAT) flags = CEF_SET_ICE; + } else if (class & TYPE_PTR) { + /* + * Casts of integer literals to pointer type yield + * address constants [6.6(9)]. + */ + if (old->flags & CEF_INT) + flags = CEF_ADDR; } return flags; diff --git a/validation/constexpr-pointer-cast.c b/validation/constexpr-pointer-cast.c new file mode 100644 index 000000000..d19c10828 --- /dev/null +++ b/validation/constexpr-pointer-cast.c @@ -0,0 +1,13 @@ +static int *a = (int*)0; // OK +static int b = 0; +static int *c = (int*)b; // KO + + +/* + * check-name: integer literal cast to pointer type constness verification. + * check-command: sparse -Wconstexpr-not-const $file + * + * check-error-start +constexpr-pointer-cast.c:3:18: warning: non-constant initializer for static object + * check-error-end + */