From patchwork Sun Jan 21 03:58:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sandra Loosemore X-Patchwork-Id: 1888860 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4THfjv48Hmz23fM for ; Sun, 21 Jan 2024 14:59:13 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 250C8385843B for ; Sun, 21 Jan 2024 03:59:10 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from esa2.mentor.iphmx.com (esa2.mentor.iphmx.com [68.232.141.98]) by sourceware.org (Postfix) with ESMTPS id 095F23858D33 for ; Sun, 21 Jan 2024 03:58:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 095F23858D33 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=codesourcery.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mentor.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 095F23858D33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=68.232.141.98 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705809529; cv=none; b=Mm517zhPce7fOJtoPTx1BWAdNXiEz1V/9QYs3zrFLcuMIhCpsZOum6SRyXvc4MxzZ+gsr4FcYUf8bVAn3Y/OqmOLmv358YI3UJZSTJjXMt/i2/+jTvVBxy8kJuZtZtAPpY/hHXBVAS8H5O8BwJehllsV7nTfvoMQoax4Na2l1iY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705809529; c=relaxed/simple; bh=gaR+uDzRfmDdIzp/NAX+sukOW/SAmm5ZBjt532NXg2M=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=tOmGnCzfvar9kSpLgT42+72xtyOBmHOvSFL1AlJlMwYh4FmXWID+ZtQqXu1JesCN4MJeQknR1z9qBilzNsdg7/1gNrqI6wmQCC+zNGK8BlRWF4n+/JRN6hyhO/JBCFSgNrmx7StXxNrElZVhGTZL5XRZZNMAS/0Xwq8QNGBkXA8= ARC-Authentication-Results: i=1; server2.sourceware.org X-CSE-ConnectionGUID: pyxMv44aTNKKhiGtKEvjIQ== X-CSE-MsgGUID: mq40uO99T8OIrcI2f59s2A== X-IronPort-AV: E=Sophos;i="6.05,209,1701158400"; d="scan'208";a="31789444" Received: from orw-gwy-02-in.mentorg.com ([192.94.38.167]) by esa2.mentor.iphmx.com with ESMTP; 20 Jan 2024 19:58:44 -0800 IronPort-SDR: 3JJ2B2UZmIsQakW1ILHE6hYvXrjWzO/c6KKoFt1ZQclK1z3MhXxVtc/1DeR5flmow34CjjAi8v 3ykMG/nArnROBqL2o6qJddhaiCaRxtu/nXWY171SYV3Ff8Z+IJ5qoxyBXfjhWcT0HhKLBIi+ap Lh9MVoxnovGuGr9nBSu4+ToR0ad/fCD/yYW5kuifqumQK7GElhhsXC7tgV0Yy4HsgNUlZYOCVM mG9mx/lpoRGtHr+zzHNcfhIw050LYD/DWaH0wnUuA3AICglJAEp8oPqLSsHH/xXZb47iXAeOBB I8w= From: Sandra Loosemore To: Subject: [COMMITTED] Clean up examples for -Wdangling-pointer [PR109708] Date: Sat, 20 Jan 2024 20:58:28 -0700 Message-ID: <20240121035828.488802-1-sandra@codesourcery.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-ClientProxiedBy: svr-orw-mbx-12.mgc.mentorg.com (147.34.90.212) To svr-orw-mbx-13.mgc.mentorg.com (147.34.90.213) X-Spam-Status: No, score=-10.1 required=5.0 tests=BAYES_00, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, SPF_HELO_PASS, SPF_PASS, 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+incoming=patchwork.ozlabs.org@gcc.gnu.org gcc/ChangeLog PR c/109708 * doc/invoke.texi (Warning Options): Fix broken example and clean up/reorganize the others. Also describe what the short-form options mean. --- gcc/doc/invoke.texi | 56 +++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 930c5dc7236..f7a6e11d20e 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -9423,51 +9423,63 @@ levels but may yield different results with optimization than without. @table @gcctabopt @item -Wdangling-pointer=1 -At level 1 the warning diagnoses only unconditional uses of dangling pointers. -For example +At level 1, the warning diagnoses only unconditional uses of dangling pointers. + +@item -Wdangling-pointer=2 +At level 2, in addition to unconditional uses the warning also diagnoses +conditional uses of dangling pointers. +@end table + +The short form @option{-Wdangling-pointer} is equivalent to +@option{-Wdangling-pointer=2}, while @option{-Wno-dangling-pointer} and +@option{-Wdangling-pointer=0} have the same effect of disabling the warnings. +@option{-Wdangling-pointer=2} is included in @option{-Wall}. + +This example triggers the warning at level 1; the address of the unnamed +temporary is unconditionally referenced outside of its scope. + @smallexample -int f (int c1, int c2, x) +char f (char c1, char c2, char c3) @{ - char *p = strchr ((char[])@{ c1, c2 @}, c3); - // warning: dangling pointer to a compound literal - return p ? *p : 'x'; + char *p; + @{ + p = (char[]) @{ c1, c2, c3 @}; + @} + // warning: using dangling pointer 'p' to an unnamed temporary + return *p; @} @end smallexample + In the following function the store of the address of the local variable -@code{x} in the escaped pointer @code{*p} also triggers the warning. +@code{x} in the escaped pointer @code{*p} triggers the warning at +level 1. + @smallexample void g (int **p) @{ int x = 7; - // warning: storing the address of a local variable in *p + // warning: storing the address of local variable 'x' in '*p' *p = &x; @} @end smallexample -@item -Wdangling-pointer=2 -At level 2, in addition to unconditional uses the warning also diagnoses -conditional uses of dangling pointers. - -For example, because the array @var{a} in the following function is out of -scope when the pointer @var{s} that was set to point is used, the warning -triggers at this level. +In this example, the array @var{a} is out of +scope when the pointer @var{s} is used. Since the code that sets @code{s} +is conditional, the warning triggers at level 2. @smallexample -void f (char *s) +extern void frob (const char *); +void h (char *s) @{ if (!s) @{ char a[12] = "tmpname"; s = a; @} - // warning: dangling pointer to a may be used - strcat (s, ".tmp"); - ... + // warning: dangling pointer 's' to 'a' may be used + frob (s); @} @end smallexample -@end table - -@option{-Wdangling-pointer=2} is included in @option{-Wall}. @opindex Wdate-time @opindex Wno-date-time