From patchwork Thu Oct 27 15:39:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 11853 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp304146wru; Thu, 27 Oct 2022 08:44:10 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7a1FQV20l/QinkWT33aQUgPq/IFP5tVq5Zrw+5cga5ERjPY1lYTDISSY1pZiYCFmEiyeFe X-Received: by 2002:a17:907:2e19:b0:78e:11cc:3bc5 with SMTP id ig25-20020a1709072e1900b0078e11cc3bc5mr43057140ejc.543.1666885450220; Thu, 27 Oct 2022 08:44:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666885450; cv=none; d=google.com; s=arc-20160816; b=NFQAkbAQT6IofB6Sw6eFJfMkaXE//BFYp1XMEwfDFDtpC/RL28CvbZnwLbApLHQIiR sxqXPSBszgUGCNq3Zraxs1Ya4NzUJASaVrehGt8YjnJ+KjW1CAy/HESSxGHlYBRN5G2c St4pdZO4+sSOUwyoa37TZNVv1aR1E/dg1kyiaJtn4CNTcrDl6VMOqu1LXCaT462SU/hq CLv7DJqpNY80yqbEmKHISFgKsRTSFSihExoJWChK0AoDeFK3CEGmyT2ZitgBQfbDgPXp ylEbiYoQZoRwgdxF9JQK3HflFzeyELdYqdqPuaOQb08bMsrpHUQWumoIL2926jfwtE66 LyrQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=MdQn4n6ilM9X15AA/9BFUsSvh+6rL7Swvd7jHcvpZH0=; b=inNL7eF8gdVHNyD1pRNmK6bMV2LbrhHCkTqFfDWd8A5jylLSLeyabfRQhWlfBxB9SG UUfC1NSMbQEgrsFrsHQnsffsQm/A9mbkvGesJ1WHXEEtuBnOi8Gl5YPfCKgsoGjm4wq0 G/DCOBHt9iwgFniMZ2fjfYjIVMWiS1YVyZgNeCqNOPOqnjuJQ78VLrKsOXFC+Ra0tRjQ HFsaI8/a1dqdVjHGESQw5vDOn7+oVQ6DHI9PISDEiKik5TkSSz73nZKbDD8XyX7cwjVp Gqni0H623dK00mlY/gDEQyVPzpFcD103/o0L5ygwSSKTJOKngabV76qt8/CLXwEeiUOI 3UwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=lXLLWuR7; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id s4-20020a50d484000000b004599ae68f98si1705593edi.136.2022.10.27.08.44.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Oct 2022 08:44:10 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=lXLLWuR7; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 42431382459D for ; Thu, 27 Oct 2022 15:43:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 42431382459D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666885416; bh=MdQn4n6ilM9X15AA/9BFUsSvh+6rL7Swvd7jHcvpZH0=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=lXLLWuR7KFK/RKiQvx3PaPibz5CUd3DKjdltZ/dVSkGDJJQyTKY6mN0Jl+ffYsYbb ZQxneKH4p7dnRi2W2y56YMJRv2NSytd4/E6/U881Y4fB6qpJxpNXdjCw5j+5Evl5Qw gzkem1BRETdp+6y5A4PjRffrdg2qYb/xAkVb/6bc= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id C819138B67AA for ; Thu, 27 Oct 2022 15:39:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C819138B67AA Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-329-q-zunJQANQ6NeVkw8Mj77A-1; Thu, 27 Oct 2022 11:39:13 -0400 X-MC-Unique: q-zunJQANQ6NeVkw8Mj77A-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id DDB23884346 for ; Thu, 27 Oct 2022 15:39:12 +0000 (UTC) Received: from pdp-11.redhat.com (unknown [10.22.17.213]) by smtp.corp.redhat.com (Postfix) with ESMTP id B4A2E18EB4; Thu, 27 Oct 2022 15:39:12 +0000 (UTC) To: GCC Patches , Jason Merrill Subject: [PATCH] c++: -Wdangling-reference and system headers Date: Thu, 27 Oct 2022 11:39:06 -0400 Message-Id: <20221027153906.24773-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.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_H2, SPF_HELO_NONE, SPF_NONE, 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: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Cc: Jonathan Wakely Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747856077715112849?= X-GMAIL-MSGID: =?utf-8?q?1747856077715112849?= I got this testcase: auto f() -> std::optional; for (char c : f().value()) { } which has a dangling reference: std::optional::value returns a reference to the contained value, but here it's the f() temporary. We warn, which is great, but only with -Wsystem-headers, because the function comes from a system header and warning_enabled_at used in do_warn_dangling_reference checks diagnostic_report_warnings_p, which in this case returned false so we didn't warn. Fixed as below. I could also override dc_warn_system_headers so that the warning is enabled in system headers always. With that, I found one issue in libstdc++: libstdc++-v3/include/bits/fs_path.h:1265:15: warning: possibly dangling reference to a temporary [-Wdangling-reference] 1265 | auto& __last = *--end(); | ^~~~~~ which looks like a true positive as well. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? gcc/cp/ChangeLog: * call.cc (maybe_warn_dangling_reference): Enable the warning in system headers if the decl isn't in a system header. gcc/testsuite/ChangeLog: * g++.dg/warn/Wdangling-reference4.C: New test. --- gcc/cp/call.cc | 7 +++++++ gcc/testsuite/g++.dg/warn/Wdangling-reference4.C | 14 ++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 gcc/testsuite/g++.dg/warn/Wdangling-reference4.C base-commit: f95d3d5de72a1c43e8d529bad3ef59afc3214705 diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 951b9fd2a88..c7c7a122045 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -13539,6 +13539,13 @@ maybe_warn_dangling_reference (const_tree decl, tree init) return; if (!TYPE_REF_P (TREE_TYPE (decl))) return; + /* Don't suppress the diagnostic just because the call comes from + a system header. If the DECL is not in a system header, or if + -Wsystem-headers was provided, warn. */ + auto wsh + = make_temp_override (global_dc->dc_warn_system_headers, + (!in_system_header_at (DECL_SOURCE_LOCATION (decl)) + || global_dc->dc_warn_system_headers)); if (tree call = do_warn_dangling_reference (init)) { auto_diagnostic_group d; diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-reference4.C b/gcc/testsuite/g++.dg/warn/Wdangling-reference4.C new file mode 100644 index 00000000000..aee7a29019b --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wdangling-reference4.C @@ -0,0 +1,14 @@ +// { dg-do compile { target c++17 } } +// { dg-options "-Wdangling-reference" } +// Check that we warn here even without -Wsystem-headers. + +#include +#include + +auto f() -> std::optional; + +void +g () +{ + for (char c : f().value()) { (void) c; } // { dg-warning "dangling reference" } +}