From patchwork Sat May 11 12:32:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathaniel Shead X-Patchwork-Id: 1934140 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=k2AGNIrm; dkim-atps=neutral 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 4Vc4sb4YmVz20KK for ; Sat, 11 May 2024 22:33:05 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EA595384404C for ; Sat, 11 May 2024 12:32:58 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-oo1-xc29.google.com (mail-oo1-xc29.google.com [IPv6:2607:f8b0:4864:20::c29]) by sourceware.org (Postfix) with ESMTPS id 949873858C41 for ; Sat, 11 May 2024 12:32:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 949873858C41 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 949873858C41 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::c29 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715430761; cv=none; b=Wy2hRcmBwpZZibKGoWD9ffROS6PZ3grV66nnWAu3xSsNbZdl9a4Uaiw7kV0Xrg1tIB38/FooF8B3gdvcu8ZfjoSFsbazjrMjsVtW/KaUSuPlwTiy/fi1wC96jvnoj9CpPIc1F0yt90hZuCLBCyRAPNPaVcDVlRLp6G32C14rbnk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715430761; c=relaxed/simple; bh=QikBZN0qj7ovoOagy7O/cgFEyMBpr0M1o6RAalnTTDY=; h=DKIM-Signature:Message-ID:Date:From:To:Subject:MIME-Version; b=XyX+c4f5B2OUAyfANRlpk9GIGct99sFiZlShrWp49vA/jToThReSyF8JcUvJe6rhQKI1AyS/SRLYkV+a/dFdnXgtnFQXDrRSOnKIeL9ERzkENOwbyr2VJBrVHV9I9zRwJ3XYGHErQ5VitLAogiDVqh8BVgqF6DG2Nx2D+T73Siw= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-oo1-xc29.google.com with SMTP id 006d021491bc7-5b277e17e15so1426322eaf.2 for ; Sat, 11 May 2024 05:32:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715430759; x=1716035559; darn=gcc.gnu.org; h=content-disposition:mime-version:subject:cc:to:from:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=+BLWuVUg3qSJc6RK5i8nz+B8YjIfHxkAwE085k+npo4=; b=k2AGNIrmlswrHuPfsfuDTV5sZlDb/bdOWFDeTpHsbfz4//fe2z+6WIke9M/JJc95YZ JWQmsDJ1h9iinP0zjBsvc6ENy7WofPO7R/O9pqeRexE036f+SbEjBDCu4nUrYli2/OtC 0RiQcE3+iZPmt4saE/L34PfBgiF8rfC//TioaHwhtZFB+eBwsrb1mqVOW9V4YCa2L7v5 bC5qSmOIpM9kM+/6GoTO6koY476L9yUdTU64VoAs1maShbPDQAbfBvo4tgwbHqHvT+R8 3fWOhIxepKZrGWxt7rr0PICT7RKtf1ZMWMSsNVS8sG8R7d3LAja9+S1Fee387jsOSFf4 FO7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715430759; x=1716035559; h=content-disposition:mime-version:subject:cc:to:from:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=+BLWuVUg3qSJc6RK5i8nz+B8YjIfHxkAwE085k+npo4=; b=vaFuxmEFJ4KXbIcy2x5unihgJUcsgxMs7eSOcJ92IRrQJKhwkXAaDnkWr1lZ8P5sDa +37CySwux+bnoBsb4VJu3XMQiv+FK1X8lbE/E5ytdzZ9JwK6w234wRdPV7l9xEf6I6DS fhj68vA3oDOuWdRuwIseTV13VNSX2nwX6vNBGbXCVDClX9uRxfC+NtQ49C9GLfg2nz/8 1TwcA0Y8IeoEdOWHTNG/q1vyagtoTywYYUHKYudbvsAKesg9dJ+pVH9699M25X0on7QH 8+q06kqRGAq2AQgjCK/BOtWRkVZ5qA9sPVKaAO9cdorZKI6v7Zqy8UvcUsWt6Su+k3q2 UOlQ== X-Gm-Message-State: AOJu0YwPXDprRoLIXmOZhLWxI/5lE3LjtbeZZ3/nar3jSoVM9Cc9h/mQ AgC9V82v/K0y2KmVwRSup32yomvJ7Q2ZYjV4dsiK1TwKi9wERIijjvyVDQ== X-Google-Smtp-Source: AGHT+IF2UgTGQ4TRhgGk44MEgGiJ1tqX9stUl64aFGE8UfLvp5U3FctvbvII/pu78I8ue+UNu8j8BA== X-Received: by 2002:a05:6358:2813:b0:192:28de:f6ec with SMTP id e5c5f4694b2df-193bb00f6d2mr590846955d.18.1715430758520; Sat, 11 May 2024 05:32:38 -0700 (PDT) Received: from Thaum. (14-200-72-253.tpgi.com.au. [14.200.72.253]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-634103f69ccsm4629955a12.70.2024.05.11.05.32.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 May 2024 05:32:37 -0700 (PDT) Message-ID: <663f6565.630a0220.1889a.cb64@mx.google.com> X-Google-Original-Message-ID: Date: Sat, 11 May 2024 22:32:33 +1000 From: Nathaniel Shead To: gcc-patches@gcc.gnu.org Cc: Jason Merrill Subject: [PATCH] c++: Strengthen checks on 'main' MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, 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+incoming=patchwork.ozlabs.org@gcc.gnu.org I wasn't entirely sure what to do with the 'abi/main.C' testcase here; is this OK, or should I e.g. lower the linkage error to a pedwarn for the purposes of this test? Bootstrapped and regtested on x86_64-pc-linux-gnu, and lightly checked with a MinGW32 cross. OK for trunk? -- >8 -- This patch adds some missing requirements for legal main declarations, as according to [basic.start.main] p2. gcc/cp/ChangeLog: * decl.cc (grokfndecl): Check for main functions with language linkage or module attachment. (grokvardecl): Check for extern 'C' entities named main. gcc/testsuite/ChangeLog: * g++.dg/abi/main.C: Rework to avoid declaring main with language linkage. * g++.dg/modules/contracts-1_b.C: Don't declare main in named module. * g++.dg/modules/contracts-3_b.C: Likewise. * g++.dg/modules/contracts-4_d.C: Likewise. * g++.dg/modules/horcrux-1_a.C: Export declarations, so that... * g++.dg/modules/horcrux-1_b.C: Don't declare main in named module. * g++.dg/modules/main-1.C: New test. * g++.dg/parse/linkage5.C: New test. * g++.dg/parse/linkage6.C: New test. Signed-off-by: Nathaniel Shead --- gcc/cp/decl.cc | 19 ++++++++++--- gcc/testsuite/g++.dg/abi/main.C | 29 ++++++++------------ gcc/testsuite/g++.dg/modules/contracts-1_b.C | 4 --- gcc/testsuite/g++.dg/modules/contracts-3_b.C | 4 --- gcc/testsuite/g++.dg/modules/contracts-4_d.C | 2 -- gcc/testsuite/g++.dg/modules/horcrux-1_a.C | 3 ++ gcc/testsuite/g++.dg/modules/horcrux-1_b.C | 2 +- gcc/testsuite/g++.dg/modules/main-1.C | 5 ++++ gcc/testsuite/g++.dg/parse/linkage5.C | 14 ++++++++++ gcc/testsuite/g++.dg/parse/linkage6.C | 13 +++++++++ 10 files changed, 63 insertions(+), 32 deletions(-) create mode 100644 gcc/testsuite/g++.dg/modules/main-1.C create mode 100644 gcc/testsuite/g++.dg/parse/linkage5.C create mode 100644 gcc/testsuite/g++.dg/parse/linkage6.C diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index e02562466a7..5ab3b787d6f 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -10781,6 +10781,11 @@ grokfndecl (tree ctype, "cannot declare %<::main%> to be %qs", "consteval"); if (!publicp) error_at (location, "cannot declare %<::main%> to be static"); + if (current_lang_depth () != 0) + error_at (location, "cannot declare %<::main%> with a" + " linkage specification"); + if (module_attach_p ()) + error_at (location, "cannot attach %<::main%> to a named module"); inlinep = 0; publicp = 1; } @@ -11280,10 +11285,16 @@ grokvardecl (tree type, DECL_INTERFACE_KNOWN (decl) = 1; if (DECL_NAME (decl) - && MAIN_NAME_P (DECL_NAME (decl)) - && scope == global_namespace) - error_at (DECL_SOURCE_LOCATION (decl), - "cannot declare %<::main%> to be a global variable"); + && MAIN_NAME_P (DECL_NAME (decl))) + { + if (scope == global_namespace) + error_at (DECL_SOURCE_LOCATION (decl), + "cannot declare %<::main%> to be a global variable"); + else if (DECL_EXTERN_C_P (decl)) + error_at (DECL_SOURCE_LOCATION (decl), + "an entity named % cannot be declared with " + "C language linkage"); + } /* Check that the variable can be safely declared as a concept. Note that this also forbids explicit specializations. */ diff --git a/gcc/testsuite/g++.dg/abi/main.C b/gcc/testsuite/g++.dg/abi/main.C index 4c5f1ea213c..f3882d70612 100644 --- a/gcc/testsuite/g++.dg/abi/main.C +++ b/gcc/testsuite/g++.dg/abi/main.C @@ -1,24 +1,19 @@ /* { dg-do compile } */ -/* Check if entry points get implicit C linkage. If they don't, compiler will - * error on incompatible declarations */ +/* Check if entry points get implicit C linkage. Determined by checking that + the names are unmangled */ -int main(); -extern "C" int main(); +int main() {} #ifdef __MINGW32__ - -int wmain(); -extern "C" int wmain(); - -int DllMain(); -extern "C" int DllMain(); - -int WinMain(); -extern "C" int WinMain(); - -int wWinMain(); -extern "C" int wWinMain(); - +int wmain() {} +int DllMain() {} +int WinMain() {} +int wWinMain() {} #endif +// { dg-final { scan-assembler-not "_Z4mainv" } } +// { dg-final { scan-assembler-not "_Z5wmainv" { target *-*-mingw32 } } } +// { dg-final { scan-assembler-not "_Z7DllMainv" { target *-*-mingw32 } } } +// { dg-final { scan-assembler-not "_Z7WinMainv" { target *-*-mingw32 } } } +// { dg-final { scan-assembler-not "_Z8wWinMainv" { target *-*-mingw32 } } } diff --git a/gcc/testsuite/g++.dg/modules/contracts-1_b.C b/gcc/testsuite/g++.dg/modules/contracts-1_b.C index 30c15f6928b..aa36c8d6b1b 100644 --- a/gcc/testsuite/g++.dg/modules/contracts-1_b.C +++ b/gcc/testsuite/g++.dg/modules/contracts-1_b.C @@ -1,15 +1,11 @@ // { dg-module-do run } // { dg-additional-options "-fmodules-ts -fcontracts -fcontract-continuation-mode=on" } -module; #include -export module bar; -// { dg-module-cmi bar } import foo; template bool bar_fn_pre(T n) { printf("bar fn pre(%d)\n", n); return true; } -export template T bar_fn(T n) [[ pre: bar_fn_pre(n) && n > 0 ]] diff --git a/gcc/testsuite/g++.dg/modules/contracts-3_b.C b/gcc/testsuite/g++.dg/modules/contracts-3_b.C index b1d6375391b..1a29e2c3e5d 100644 --- a/gcc/testsuite/g++.dg/modules/contracts-3_b.C +++ b/gcc/testsuite/g++.dg/modules/contracts-3_b.C @@ -1,15 +1,11 @@ // { dg-module-do run } // { dg-additional-options "-fmodules-ts -fcontracts -fcontract-role=default:ignore,ignore,ignore" } -module; #include -export module bar; -// { dg-module-cmi bar } import foo; template bool bar_fn_pre(T n) { printf("bar fn pre(%d)\n", n); return true; } -export template T bar_fn(T n) [[ pre: bar_fn_pre(n) && n > 0 ]] diff --git a/gcc/testsuite/g++.dg/modules/contracts-4_d.C b/gcc/testsuite/g++.dg/modules/contracts-4_d.C index dc56251d1d8..9e6b7c3d4de 100644 --- a/gcc/testsuite/g++.dg/modules/contracts-4_d.C +++ b/gcc/testsuite/g++.dg/modules/contracts-4_d.C @@ -1,8 +1,6 @@ // { dg-module-do run } // { dg-additional-options "-fmodules-ts -fcontracts" } -module; #include -export module baz; import foo; import bar; diff --git a/gcc/testsuite/g++.dg/modules/horcrux-1_a.C b/gcc/testsuite/g++.dg/modules/horcrux-1_a.C index ff548d039ea..c115bd2965b 100644 --- a/gcc/testsuite/g++.dg/modules/horcrux-1_a.C +++ b/gcc/testsuite/g++.dg/modules/horcrux-1_a.C @@ -3,13 +3,16 @@ export module foo; // { dg-module-cmi foo } +export template struct integral_constant {}; +export template using __bool_constant = integral_constant; +export template struct __is_constructible_impl : public __bool_constant<__is_constructible(_Tp, _Args...)> diff --git a/gcc/testsuite/g++.dg/modules/horcrux-1_b.C b/gcc/testsuite/g++.dg/modules/horcrux-1_b.C index 842bf413585..54aa069436a 100644 --- a/gcc/testsuite/g++.dg/modules/horcrux-1_b.C +++ b/gcc/testsuite/g++.dg/modules/horcrux-1_b.C @@ -1,6 +1,6 @@ // { dg-additional-options -fmodules-ts } -module foo; +import foo; int main () { diff --git a/gcc/testsuite/g++.dg/modules/main-1.C b/gcc/testsuite/g++.dg/modules/main-1.C new file mode 100644 index 00000000000..0d79edddb75 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/main-1.C @@ -0,0 +1,5 @@ +// { dg-additional-options "-fmodules-ts" } +// { dg-prune-output "not writing module" } + +export module M; +int main() {} // { dg-error "attach" } diff --git a/gcc/testsuite/g++.dg/parse/linkage5.C b/gcc/testsuite/g++.dg/parse/linkage5.C new file mode 100644 index 00000000000..451406de69b --- /dev/null +++ b/gcc/testsuite/g++.dg/parse/linkage5.C @@ -0,0 +1,14 @@ +// { dg-do compile } +// The main function shall not be declared with a linkage-specification. + +extern "C" { + int main(); // { dg-error "linkage" } +} + +namespace foo { + extern "C" int main(); // { dg-error "linkage" } +} + +extern "C++" int main(); // { dg-error "linkage" } + +extern "C" struct S { int main(); }; // OK diff --git a/gcc/testsuite/g++.dg/parse/linkage6.C b/gcc/testsuite/g++.dg/parse/linkage6.C new file mode 100644 index 00000000000..44081c1544c --- /dev/null +++ b/gcc/testsuite/g++.dg/parse/linkage6.C @@ -0,0 +1,13 @@ +// { dg-do compile } +// A program that declares an entity named main with C language linkage +// (in any namespace) is ill-formed. + +namespace foo { + extern "C" int main; // { dg-error "linkage" } + extern "C" struct A { + int main; // OK + }; + extern "C" struct B { + int main(); // OK + }; +}