From patchwork Thu Feb 8 01:36:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joseph Myers X-Patchwork-Id: 198157 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp2622571dyb; Wed, 7 Feb 2024 17:38:21 -0800 (PST) X-Google-Smtp-Source: AGHT+IFI5t+Md1lOlLymWk2bCice93FfkFMR8v8R0Z55t5E4xd/kKTKOesfDtFt6Kno6rK8367tb X-Received: by 2002:a05:6102:409:b0:46d:2a9c:8f44 with SMTP id d9-20020a056102040900b0046d2a9c8f44mr4447643vsq.8.1707356301124; Wed, 07 Feb 2024 17:38:21 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707356301; cv=pass; d=google.com; s=arc-20160816; b=cuzyOkYzKbBgZuk5FBOKq+9sUbLygdQDZW+XY/nLxUsIaWVHbnUR7kTgqgRnPojpUW N5LlARTHwOdlOJtyZUwS8ygJyfRhPuARItO2ZMUurdMdr9VhQAwrskITBJMcinnSe3+X F2jauP/HVlkN+xoJ2JCMlkNto0EGZy2LOrUhBmlSZqU207kDFT+BB9DchIxLVOLz+10Q cTMLTVQ9QeGbi78KSThC+3dgKpUAU3MUG4JHhWn2Glb56lzCWrz7wwKsOsRGLgbh9fp6 TiREPQ/LuHnSl6d0GVBfUxkhDfAqxZzOxxiscdECz/KohBp+Bq4ikcGuAOb5/PEdJNO4 ngHQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:mime-version:message-id:subject :to:from:date:dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=/6Bja6IIgWYzQJkP/t1t3ALkUzetWtGwqv+R/1in1x0=; fh=97tA5MLpAkmY+uJnyUwBEAh47s3mAkCnn9YqP168pJk=; b=rOEYyr6ZJ5A+rKDpfOzUyl0lnoc/llrh5FbISKuT1eSnkiLIp59AiTT2TH/cm4Cvg9 OJf+adBb67Pz42OaOr66njJs3rfXlRSo7oy7Zkgj2Vap+kwMVtiBTjPqlf9mH5Rza0+f 9FJMmS8HhDBak8y01Mi4sC8/Zkn/nUa7U/b9Yd9O7154OaA4LS4IWX8hCNGC81x5tWHN 2YgBWoQRRoNh0Zy4zi+UUU/Co/dNMKW9JMfcUAlq3+WGXABenLYmhuPerRo9pXaRTJOU XEXzE6Ff/sFA8XQim+A/BPayCpw+IBczHFK6mgAK7wzB+pkb5qzLNkPTGzL0hhyHLYxK 2e8Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=FEqfp1jb; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Forwarded-Encrypted: i=2; AJvYcCVply6YrIgnQ7OZ0glqxeKDD0N3g8pynhgDpZ3Ug6DrFPYCN5etTg6krFxWYlDGGSih7WWl7Ttoqpm3ouu6VRN/0qGZlg== Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id b9-20020ad45189000000b0068c3aa9cc00si2233911qvp.217.2024.02.07.17.38.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 17:38:21 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=FEqfp1jb; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CD0AD3858C33 for ; Thu, 8 Feb 2024 01:38:20 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id BB5083858C5F for ; Thu, 8 Feb 2024 01:37:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BB5083858C5F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org BB5083858C5F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707356263; cv=none; b=Qmi8GAcAXKtWkiYCdjefWBdammSzsYo9z/+u7GC4uy79vasBkV/DziFQ01Nzrf94TgRKblmN29GxpK2ELY+o1BYtGE0Xfg+qgxhlBNAlvxUm9CEqY7bf4eW27ysrIg4wKP0Upu8pFVFGCnwHF9haiuexbOHPmKcodHkDkultM5E= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707356263; c=relaxed/simple; bh=6S2CQkZ5nX9ePL61cYc4Xggx1EyGTNhT5w2OOWB9IAU=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=KfGQZJY7pqjU67XiQnF8V47URn5HtgVcbS9wzVagUZPRW/ZiaBbp4mUb8lKJcrAWq2ctj7gfV+R/7i/39WP3y43pde3nK0imGzmNGZbgu9aBonMJhu1/QdTZisIQej1vnvnK0KTzqjMT/if1DeEE9SVTbn17ZChzCtSD6M2ubPI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707356259; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=/6Bja6IIgWYzQJkP/t1t3ALkUzetWtGwqv+R/1in1x0=; b=FEqfp1jbdSei5Be64vPexLpE9B1agiAxQ3Oth5hv7iBvhxYkPcfQbYd4e93ASV3i9rQ0hA 8GDgEa0IBca1rb1fjWHXhtMhP+dKpgDSNZu1DetrIUY3MkfCygSBVxibLvNXUHzqXyBRZV fu3DR2JAcaw1SyCu6CvIcl6lYkwaTU4= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-66-KDNMiQTmO5a35JjgYf5cCw-1; Wed, 07 Feb 2024 20:37:38 -0500 X-MC-Unique: KDNMiQTmO5a35JjgYf5cCw-1 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-40efad468a5so7801605e9.2 for ; Wed, 07 Feb 2024 17:37:37 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707356256; x=1707961056; h=mime-version:message-id:subject:to:from:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=/6Bja6IIgWYzQJkP/t1t3ALkUzetWtGwqv+R/1in1x0=; b=uRBILPmYfTuRs2xBE1gltltAMNcC18xJDkq2PcUoUJo7AbREcggYloCymsRXljrrLL E8qoIFMF4hhhaw02ezExsCDsaBz9/z/eU/Z8RBZdQG3QOXroBC5FzXFvfVKYXw2wAXM0 fi7EOWiiPrMu6my9AYT26DdIupFaPsinrjOrmLtVvVBdKzIh6EumG5C//n0MBWIJrK+U pNIsVOaBAK7zBhAUdZ6LGnxkpmrkJUO7gqE8qRDBWp7RPIAElzoLuciDLMm65RwZ35eO eGEqgQVwls4SATdFOc2v1mwvKYSm4axIZeVxn2DW42fgOmrnedpiaNcxsfmwvo3XGSY8 D+Zw== X-Gm-Message-State: AOJu0YyZwoidEVsUG7PLQb4E1VD8P14j11+PWcIWJ6ah30qp/VHeMB6r 6AyqAm1flwYxmQsEu15xjuXsySkWDU6wsS4Wzf2FdthZ4y6hxBuK4QeUtjw0OavbwUI0PF+dlPd /uGYrBuKkY1SiMVCdNOsdedX+aT52NN5SnYw0Gc5FS/yGShGIxw8CaHtn7znUV0SI9jaYas6wCQ mXki4yuNFl2DKZ6SfjFEPq1bM99KJnOrFfEH0o+p0= X-Received: by 2002:a05:600c:1d1d:b0:40e:88b6:4eac with SMTP id l29-20020a05600c1d1d00b0040e88b64eacmr5713709wms.3.1707356256293; Wed, 07 Feb 2024 17:37:36 -0800 (PST) X-Received: by 2002:a05:600c:1d1d:b0:40e:88b6:4eac with SMTP id l29-20020a05600c1d1d00b0040e88b64eacmr5713700wms.3.1707356255762; Wed, 07 Feb 2024 17:37:35 -0800 (PST) Received: from digraph.polyomino.org.uk (digraph.polyomino.org.uk. [2001:8b0:bf73:93f7::51bb:e332]) by smtp.gmail.com with ESMTPSA id jh1-20020a05600ca08100b0040fb03f803esm173263wmb.24.2024.02.07.17.37.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 17:37:35 -0800 (PST) Received: from jsm28 (helo=localhost) by digraph.polyomino.org.uk with local-esmtp (Exim 4.95) (envelope-from ) id 1rXtLU-000Yjt-CV for gcc-patches@gcc.gnu.org; Thu, 08 Feb 2024 01:36:56 +0000 Date: Thu, 8 Feb 2024 01:36:56 +0000 (UTC) From: Joseph Myers To: gcc-patches@gcc.gnu.org Subject: [committed] c: Fix boolean conversion of floating constant as integer constant expression [PR113776] Message-ID: <3d84ae10-14a3-ea53-98b9-a1ea43528a14@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790292840755392173 X-GMAIL-MSGID: 1790292840755392173 My fix for bug 111059 and bug 111911 caused a conversion of a floating constant to boolean to wrongly no longer be considered an integer constant expression, because logic to insert a NOP_EXPR in c_objc_common_truthvalue_conversion for an argument not an integer constant expression itself now took place after rather than before the conversion to bool. In the specific case of casting a floating constant to bool, the result is an integer constant expression even though the argument isn't (build_c_cast deals with ensuring that casts to integer type of anything of floating type more complicated than a single floating constant don't get wrongly treated as integer constant expressions even if they fold to constants), so fix the logic in c_objc_common_truthvalue_conversion to handle that special case. Bootstrapped with no regressions for x86_64-pc-linux-gnu. PR c/113776 gcc/c * c-typeck.cc (c_objc_common_truthvalue_conversion): Return an integer constant expression for boolean conversion of floating constant. gcc/testsuite/ * gcc.dg/pr113776-1.c, gcc.dg/pr113776-2.c, gcc.dg/pr113776-3.c, gcc.dg/pr113776-4.c: New tests. --- Applied to mainline. The original commit this is fixing has only gone on mainline so far; any backports of that original commit will need to take care to include this fix (and adjust for GCC 13 branch not having -std=c23 and older versions not having enums with fixed underlying type). diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index 3b519c48ae0..ddeab1e2a8a 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -13572,7 +13572,17 @@ c_objc_common_truthvalue_conversion (location_t location, tree expr, tree type) break; } - int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr)); + /* Conversion of a floating constant to boolean goes through here + and yields an integer constant expression. Otherwise, the result + is only an integer constant expression if the argument is. */ + int_const = ((TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr)) + || ((TREE_CODE (expr) == REAL_CST + || TREE_CODE (expr) == COMPLEX_CST) + && (TREE_CODE (type) == BOOLEAN_TYPE + || (TREE_CODE (type) == ENUMERAL_TYPE + && ENUM_UNDERLYING_TYPE (type) != NULL_TREE + && (TREE_CODE (ENUM_UNDERLYING_TYPE (type)) + == BOOLEAN_TYPE))))); int_operands = EXPR_INT_CONST_OPERANDS (expr); if (int_operands && TREE_CODE (expr) != INTEGER_CST) { diff --git a/gcc/testsuite/gcc.dg/pr113776-1.c b/gcc/testsuite/gcc.dg/pr113776-1.c new file mode 100644 index 00000000000..36190fbc3fe --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr113776-1.c @@ -0,0 +1,5 @@ +/* { dg-do compile } */ +/* { dg-options "-std=c11 -pedantic" } */ + +char d[(_Bool)0.5 == 1 ? 1 : -1]; +char f[(_Bool)0.0 == 0 ? 1 : -1]; diff --git a/gcc/testsuite/gcc.dg/pr113776-2.c b/gcc/testsuite/gcc.dg/pr113776-2.c new file mode 100644 index 00000000000..9e88210892a --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr113776-2.c @@ -0,0 +1,4 @@ +/* { dg-do compile } */ +/* { dg-options "-std=c11 -pedantic" } */ + +enum e { A = (_Bool) 0.0, B = (_Bool) 0.5, C = (_Bool) 1.0 }; diff --git a/gcc/testsuite/gcc.dg/pr113776-3.c b/gcc/testsuite/gcc.dg/pr113776-3.c new file mode 100644 index 00000000000..c615994a89f --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr113776-3.c @@ -0,0 +1,7 @@ +/* { dg-do compile } */ +/* { dg-options "-std=c23 -pedantic" } */ + +enum ebool : bool { BF, BT }; + +char d[(enum ebool)0.5 == 1 ? 1 : -1]; +char f[(enum ebool)0.0 == 0 ? 1 : -1]; diff --git a/gcc/testsuite/gcc.dg/pr113776-4.c b/gcc/testsuite/gcc.dg/pr113776-4.c new file mode 100644 index 00000000000..1b57557746e --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr113776-4.c @@ -0,0 +1,6 @@ +/* { dg-do compile } */ +/* { dg-options "-std=c23 -pedantic" } */ + +enum ebool : bool { BF, BT }; + +enum e { A = (enum ebool) 0.0, B = (enum ebool) 0.5, C = (enum ebool) 1.0 };