From patchwork Fri Aug 4 06:04:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Uecker X-Patchwork-Id: 130970 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:44a:b0:3f2:4152:657d with SMTP id ez10csp55225vqb; Thu, 3 Aug 2023 23:05:40 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHM4BDRNVr99lcTZudlXrEq1Yi7FbUPoe7qKTNbUks0F667LDrpfqv0k5cS1j/Sj0l+UYOK X-Received: by 2002:a17:906:2cf:b0:994:554b:7f27 with SMTP id 15-20020a17090602cf00b00994554b7f27mr740567ejk.2.1691129140426; Thu, 03 Aug 2023 23:05:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691129140; cv=none; d=google.com; s=arc-20160816; b=zydDAfLj8k2EipEZlQTvZ+A2K4openzCrt1eO03OQ/Cb4NMTH/YLlZUXiRxpjztqFT /PLRH8tSgYsdmqztdOWDGC9tIsbqvBuaMK0BFqMdx4mrn6S3UNLRrRGaVTP3J7gg+mNA sO1LT9pAMjRf8u4IJmia7HZ+lBJQXZ/hLVhq0F5EB9RnaLhQk9LEw8rENhTOK9oB+G6O BPaC/zGjlRLQwmLdyac4M3p0Z/giPYWvy0EeTAPSeiGIeY2PqTk02/LU9EtgOnxvwbuB o5GaKIGP8RmSJc8Dw8xwRCR0V9hlfTxWWvOR0JWVoBCMRQ+cbKB/wx6zltTxDrlN3OtI rQ2g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:user-agent:date:cc:to :subject:message-id:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=/3TFppKbPLNpVy2E0WNDHABk4gsbZ7YGRvdK5TcKTJo=; fh=R/+sD+UPvJdLVX74ua6+DVhn5JOW3tdPUZPuWbX+7uQ=; b=bgQd7CBm08IQAgKlAI3bGLzaB5xipJGMFhzbV9+gShNUkXY6bwsvFdEkQ7vtvqfoax H4unypkmzYxV95v7Bh27cH7mAzm7pF3PJuYF+E4K+TFivscCLaXnePWb8aPpLGEFi0g7 e1JnZKr8vokdT5+Xw000rlFcRKwgw7gPhmQfIVQ2E76A56iaD6/GIcyr3sbWuOB+nP/c C5OPrp1tQi9AvdA9anCeRrYV/2rvu6Zfc+ge9WQrNsMYyF4ivgSAc3KT797d5LQxjauj ZW0p1EbZHUnBScLMyIeMq1Fsx1pbxStOjx5faYQYFRh5qb+XTTA6M/NO+UotnEHwAACe IlWA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=MwraTk9N; 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=gnu.org Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id pg2-20020a170907204200b0098f99532db2si1088952ejb.666.2023.08.03.23.05.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Aug 2023 23:05:40 -0700 (PDT) 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=@gcc.gnu.org header.s=default header.b=MwraTk9N; 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=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 57E523857353 for ; Fri, 4 Aug 2023 06:05:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 57E523857353 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691129108; bh=/3TFppKbPLNpVy2E0WNDHABk4gsbZ7YGRvdK5TcKTJo=; h=Subject:To:Cc:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=MwraTk9N/oQrTkvtMvuJSu8vKw6k3vcTXSUseQonqyunoWlFK3+r4RX24qcc3DWkv tkxJ+xYohKf+/V4DjAuf6gEhQtNASEPZ4HkGILANOkqCPMfDMsf2PVmDs2cnUFSCa1 Y4BScZtgCk0MLeF53kk6g1nyRDaHZ3vJUsE9F8dA= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mailrelay.tugraz.at (mailrelay.tugraz.at [129.27.2.202]) by sourceware.org (Postfix) with ESMTPS id 42F7C3857713 for ; Fri, 4 Aug 2023 06:04:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 42F7C3857713 Received: from vra-170-53.tugraz.at (vra-170-53.tugraz.at [129.27.170.53]) by mailrelay.tugraz.at (Postfix) with ESMTPSA id 4RHFXg6H7Mz3wV7; Fri, 4 Aug 2023 08:04:19 +0200 (CEST) Message-ID: Subject: [C PATCH] _Generic should not warn in non-active branches [PR68193,PR97100] To: gcc-patches@gcc.gnu.org Cc: Joseph Myers Date: Fri, 04 Aug 2023 08:04:14 +0200 User-Agent: Evolution 3.38.3-1+deb11u2 MIME-Version: 1.0 X-TUG-Backscatter-control: G/VXY7/6zeyuAY/PU2/0qw X-Spam-Scanner: SpamAssassin 3.003001 X-Spam-Score-relay: -1.9 X-Scanned-By: MIMEDefang 2.74 on 129.27.10.117 X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Martin Uecker via Gcc-patches From: Martin Uecker Reply-To: Martin Uecker Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773277429512625408 X-GMAIL-MSGID: 1773277429512625408 Here is a patch to reduce false positives in _Generic. Bootstrapped and regression tested on x86_64-linux. Martin c: _Generic should not warn in non-active branches [PR68193,PR97100] To avoid false diagnostics, use c_inhibit_evaluation_warnings when a generic association is known to match during parsing. We may still generate false positives if the default branch comes earler than a specific association that matches. PR c/68193 PR c/97100 gcc/c/: * c-parser.cc (c_parser_generic_selection): Inhibit evaluation warnings branches that are known not be taken during parsing. gcc/testsuite/ChangeLog: * gcc.dg/pr68193.c: New test. diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc index 24a6eb6e459..d1863b301e0 100644 --- a/gcc/c/c-parser.cc +++ b/gcc/c/c-parser.cc @@ -9350,7 +9350,17 @@ c_parser_generic_selection (c_parser *parser) return error_expr; } + bool match = assoc.type == NULL_TREE + || comptypes (assoc.type, selector_type); + + if (!match) + c_inhibit_evaluation_warnings++; + assoc.expression = c_parser_expr_no_commas (parser, NULL); + + if (!match) + c_inhibit_evaluation_warnings--; + if (assoc.expression.value == error_mark_node) { c_parser_skip_until_found (parser, CPP_CLOSE_PAREN, NULL); @@ -9387,7 +9397,7 @@ c_parser_generic_selection (c_parser *parser) match_found = associations.length (); } } - else if (comptypes (assoc.type, selector_type)) + else if (match) { if (match_found < 0 || matched_assoc.type == NULL_TREE) { diff --git a/gcc/testsuite/gcc.dg/pr68193.c b/gcc/testsuite/gcc.dg/pr68193.c new file mode 100644 index 00000000000..2267593e363 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr68193.c @@ -0,0 +1,15 @@ +/* pr69193 */ +/* { dg-do compile } */ +/* { dg-options "-Wall" } */ + +int +main (void) +{ + int i = 0; + int j = _Generic (i, + int: 0, + long int: (i = (long int) 9223372036854775808UL)); + return i + j; +} + +