From patchwork Sat Oct 14 12:28:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Uecker X-Patchwork-Id: 152941 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp2453640vqb; Sat, 14 Oct 2023 05:29:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFkG1+8JwkE0MYAMaUgG/9032QuM8N0qbcqCFonei9VzpvVPGwEdrHC3/yQLFSkZxgRwbS8 X-Received: by 2002:a05:6214:3d8a:b0:65b:31df:df62 with SMTP id om10-20020a0562143d8a00b0065b31dfdf62mr29374109qvb.30.1697286547063; Sat, 14 Oct 2023 05:29:07 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1697286547; cv=pass; d=google.com; s=arc-20160816; b=bxEOM4U3DusU+KeHc/VabJnAPveLUn6IRkiqZ5h1Qog6W6uMonePLVk4Y++VOzUbqo N4NQMWDZLua3JLV+OhSvVtr7uPo+rxQoAFJtfa4OYhbeqMTELagoRk2Ux5DLxJdpa+0R k+EfGdyA+c95TlBhzrqTUlcmxlXmHm1TxF1rQdqgzP6Z4WC4Z+69SUkF31uPvRankirq UUcM1NbmOm9RxDtBSzieR7ByvPqkkRLRA+t+j4/H6pdhaKHOd7C8PqBg3q8U/UoD0uSY lNvUMA92twv8GGmT6g06qMJ00CzZ9NA2a4pRpuoaVg/Oywqda0IbZMTWw06wwVzQWS/f 4abg== 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:user-agent :content-transfer-encoding:date:cc:to:from:subject:message-id :dkim-signature:dkim-filter:dmarc-filter:arc-filter:delivered-to; bh=iZhvZ2JMnD6fr8s0VMKzYGx5zj3RkrtTtBeynzyuYc4=; fh=LIC7hquJdodsoNosKkF6E7I2MQ8yREmMmqLfzxx0xYA=; b=mFg3JL4IC/E6qp3OPMposjO7I/1rCT29LUddBHZ46OQ99xTnnD56e+7V7kka/aW3p4 8fGxC/PqVl/6PrM3+VJmQz7JY3y0E8pNIKUrz5OwO/9f4tYjCeA0QLuny2dZ/vKQjIFv u/mokam8A17jLnf5d/JpDQJxxFBX0RywghbpuNdX0qpcE57b8smYJ0igd5Oq80Tc77uI z25UYyhe2tZWuU/iKgk/qRj4iFIpmID21wshyI6h2ZwP7ZadUxSg4ClDFTSlNjTd2Glo 4UZDWPxZB/SQcoOEfGizgcGVBZnUX3iC6GXmUd4S/m3NFjXnWGmqv5aAbk2DCv1goMil 0QIA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@tugraz.at header.s=mailrelay header.b=ARDR2WE2; arc=pass (i=1); 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=QUARANTINE sp=NONE dis=NONE) header.from=tugraz.at Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id qx9-20020a05620a8b8900b0076c53d5462asi2543967qkn.186.2023.10.14.05.29.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 14 Oct 2023 05:29:07 -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=@tugraz.at header.s=mailrelay header.b=ARDR2WE2; arc=pass (i=1); 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=QUARANTINE sp=NONE dis=NONE) header.from=tugraz.at Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CE0D33857707 for ; Sat, 14 Oct 2023 12:29:06 +0000 (GMT) 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 C7A2F3858C60 for ; Sat, 14 Oct 2023 12:28:40 +0000 (GMT) ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C7A2F3858C60 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=129.27.2.202 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697286523; cv=none; b=vDQvURoQ0Wosan+DExfIkc5ogzpN1S6+nvtsSGyEg75IZq7kiZaTUM+vexcseZfniBzA8LWlZLMIutpVv40Rdwl6lNyPAVg0OkAKXlbVwaSPdfCDAld1sd8MEjusBAfMT0n9lqQU0G0pWghsVpNR0BISxR4kkE5s6uexD1I0mAM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697286523; c=relaxed/simple; bh=ToNOHe69Q3urK99yhHVfl0in26va+H0bi88hZCCDtOM=; h=DKIM-Signature:Message-ID:Subject:From:To:Date:MIME-Version; b=AuvUDKwkfbFSr3R9uBCFIVjX1Ky2cDX992KNHKP9Yvnw9GbqF35G1YP8jltsBXP4d1mCxjRuQ6lN1nhuQbYBRt3Kb+5Guhhc/Uwx6HRFxGadj5CaJSq7Qk/HebZ8Hm20LQ1nLisyU/7lRKJtHa6GOcCsUgCIvaUmS/MlLsfTnD0= ARC-Authentication-Results: i=1; server2.sourceware.org DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C7A2F3858C60 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=tugraz.at Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tugraz.at Received: from vra-172-73.tugraz.at (vra-172-73.tugraz.at [129.27.172.73]) by mailrelay.tugraz.at (Postfix) with ESMTPSA id 4S72jB4WLtz1LLyW; Sat, 14 Oct 2023 14:28:30 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 mailrelay.tugraz.at 4S72jB4WLtz1LLyW DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tugraz.at; s=mailrelay; t=1697286511; bh=iZhvZ2JMnD6fr8s0VMKzYGx5zj3RkrtTtBeynzyuYc4=; h=Subject:From:To:Cc:Date:From; b=ARDR2WE2JWh8FBBTPo8IaTjCagJ3wVtZadZ5UDHmvoIzFfqPwOdWtAbxoOAQlMYZA YFJsizv2YMdou7HTPkm59ujaroszl6+mJ5jRs/KOmlaibZGqHWAzsm02MUPrlPXTWk rcznd1fAP+/8Fn7zEGOR4Is1cMPBKjJxwJOGbj+8= Message-ID: <8642fb1392c8e74c436feaf2b2f4e1d3641530eb.camel@tugraz.at> Subject: [C PATCH] error for function with external and internal linkage [PR111708] From: Martin Uecker To: gcc-patches@gcc.gnu.org Cc: Joseph Myers , Marek Polacek Date: Sat, 14 Oct 2023 14:28:30 +0200 User-Agent: Evolution 3.46.4-2 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.116 X-Spam-Status: No, score=-12.0 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.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: 1779733938412059528 X-GMAIL-MSGID: 1779733938412059528 Bootstrapped and regression tested on x86_64. c: error for function with external and internal linkage [PR111708] Declaring a function with both external and internal linkage in the same TU is translation-time UB. Add an error for this case as already done for objects. PR c/111708 gcc/c/Changelog: * c-decl.cc (grokdeclarator): Add error. gcc/testsuite/Changelog: * gcc.dg/pr111708-1.c: New test. * gcc.dg/pr111708-2.c: New test. diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index 5822faf01b4..52490a784d0 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -8032,6 +8032,28 @@ grokdeclarator (const struct c_declarator *declarator, TREE_THIS_VOLATILE (decl) = 1; } } + + /* C99 6.2.2p7: It is invalid (compile-time undefined + behavior) to create an 'extern' declaration for a + function if there is a global declaration that is + 'static' and the global declaration is not visible. + (If the static declaration _is_ currently visible, + the 'extern' declaration is taken to refer to that decl.) */ + if (!initialized + && storage_class != csc_static + && storage_class != csc_auto + && current_scope != file_scope) + { + tree global_decl = identifier_global_value (declarator->u.id.id); + tree visible_decl = lookup_name (declarator->u.id.id); + + if (global_decl + && global_decl != visible_decl + && VAR_OR_FUNCTION_DECL_P (global_decl) + && !TREE_PUBLIC (global_decl)) + error_at (loc, "function previously declared % " + "redeclared %"); + } } else { diff --git a/gcc/testsuite/gcc.dg/pr111708-1.c b/gcc/testsuite/gcc.dg/pr111708-1.c new file mode 100644 index 00000000000..4af7f53d75f --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr111708-1.c @@ -0,0 +1,42 @@ +/* { dg-do compile } */ +/* { dg-options "" } */ + +extern int a(void); // external linkage (6.2.2p4) +static int a(void); /* { dg-error "static declaration of 'a' follows non-static declaration" } */ + +static int b(void); // internal linkage (6.2.2p3) +extern int b(void); // internal linkage (6.2.2p4) + +static int h0(void); + +void s(void) +{ + extern int h0(void); // internal linkage (6.2.2p4), + extern int h0(void); // internal linkage (6.2.2p4), redeclaration, ok + extern int h2(void); // external linkage (6.2.2p4) + extern int h2(void); // external linkage (6.2.2p4), redeclaration, ok. +} + + +extern int i(void); // external linkage (6.2.2p4) +static int j(void); // internal linkage (6.2.2p3) + +void bar(void) +{ + extern int i(void); // external linkage (6.2.2p4), ok +} + +void foo(void) +{ + extern int j(void); // internal linkage (6.2.2p4), ok, internal +} + +void x(void) +{ + int i(void); // no linkage (6.2.2p6) + int j; // no linkage (6.2.2p6) + { + extern int j(void); /* { dg-error "function previously declared 'static' redeclared 'extern'" } */ + } +} + diff --git a/gcc/testsuite/gcc.dg/pr111708-2.c b/gcc/testsuite/gcc.dg/pr111708-2.c new file mode 100644 index 00000000000..065c0525c2e --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr111708-2.c @@ -0,0 +1,21 @@ +/* { dg-do compile } */ +/* { dg-options "" } */ +/* { dg-require-effective-target trampolines } */ + +static void pp(void) +{ + int pp; + { + auto void pp(void); + void pp(void) { } + } +} + +static void q2(void); + +static void qq(void) +{ + auto void q2(void); + void q2(void) { } +} +