From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 1EA503858D3C for ; Fri, 7 Oct 2022 13:51:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1EA503858D3C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1665150695; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=VwQQHukdniZKG08eybGguZZzJ+R953IaOHaSq0pnTjc=; b=FMwsY2TWN7baOTIExp7+GPNSa3WCrCZK3rkMZrO0WAm3SlH8iWbogOSztldzJIki9riWuG zJ2yOfR82aB2xhg8s7B3Sb6jx9zU+Q7j8TNkFY1PHpfYjELlLxomy2jKza4CnFzzyKuR96 1XUpVVLCku/dLDEjpBpFQr3UFlmtcaY= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-669-O4BRtt9POKC_t7ti-FCO2A-1; Fri, 07 Oct 2022 09:51:34 -0400 X-MC-Unique: O4BRtt9POKC_t7ti-FCO2A-1 Received: by mail-qk1-f198.google.com with SMTP id bj40-20020a05620a192800b006e20308f39cso3807379qkb.10 for ; Fri, 07 Oct 2022 06:51:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=VwQQHukdniZKG08eybGguZZzJ+R953IaOHaSq0pnTjc=; b=yi/x5w3oyJUQ/T9JTiVOJNWo+FUbKObPwYvjZE6EEw/7qyev4wcTPBnUkZbNEcSufw 1qto2nfJfZI9xRSkNyWgTH9qy2wEOgZRy68WGyVXjB0nUPS1DFlVxI8ngRHoGKhgb2kK 2fSbvNszkDKN9on1h6K/4NDTYb1r5RH7g6CD258vv/rXQB56+hkAEvOE694/D4d4PQQT /gkEnT4Jgr/droQCZ5zK9OQsGY0ukpMzJO5MqtajhBB88COgozgsCX7lJeyQAF8LifOP 7mz2aU5WKuqhV/TN1jDgXNnJtglKJXoHBj2xdoEMIgB7QaAYJHE3imIEyadY4/7oVq2d /AFg== X-Gm-Message-State: ACrzQf0xKatVHcztLXdpEPV0lwp3yeZcCSGKpGQiVB9m2G8rivLVlI5k l2or/lZxu0X+iiEtUYq7Ycj/C9ZwRV2lu5EuE+QzHxTfb9TLTRVvMWq+pW4S/PAewdDbIMii1Gy kjn/GUNk= X-Received: by 2002:a05:622a:6:b0:394:625e:8c2c with SMTP id x6-20020a05622a000600b00394625e8c2cmr4117589qtw.204.1665150693241; Fri, 07 Oct 2022 06:51:33 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5FzvWNhrsrnPigW3na8BHVt0Gw3YF36DbMIUWBrC5W7tRziE1k+8YLSgT5IDk+RxkB8svMRg== X-Received: by 2002:a05:622a:6:b0:394:625e:8c2c with SMTP id x6-20020a05622a000600b00394625e8c2cmr4117578qtw.204.1665150692875; Fri, 07 Oct 2022 06:51:32 -0700 (PDT) Received: from [192.168.1.101] (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id i5-20020a37b805000000b006cfc9846594sm1959935qkf.93.2022.10.07.06.51.31 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 07 Oct 2022 06:51:32 -0700 (PDT) Message-ID: <2489d88a-e80b-2f89-bac5-07c0b70bc175@redhat.com> Date: Fri, 7 Oct 2022 09:51:31 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.13.1 Subject: Re: Handling of main() function for freestanding To: Jonathan Wakely Cc: "gcc@gcc.gnu.org" , arsen@aarsen.me, Jakub Jelinek , "Joseph S. Myers" References: <44594c0c-db9d-459a-7ecc-29c4f5544b28@redhat.com> From: Jason Merrill In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-8.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,KAM_SHORT,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,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 List-Id: On 10/7/22 07:30, Jonathan Wakely wrote: > On Tue, 4 Oct 2022 at 23:25, Jason Merrill wrote: >> >> On 9/28/22 16:15, Jonathan Wakely wrote: >>> As part of implementing a C++23 proposal [1] to massively increase the >>> scope of the freestanding C++ standard library some questions came up >>> about the special handling of main() that happens for hosted >>> environments. >>> >>> As required by both C++ (all versions) and C (since C99), falling off >>> the end of the main() function is not undefined, the compiler is >>> required to insert an implicit 'return 0' [2][3]. However, this >>> special handling only applies to hosted environments. For freestanding >>> the return type or even the existence of main is >>> implementation-defined. As a result, GCC gives a -Wreturn-type warning >>> for this code with -ffreestanding, but not with -fhosted: >>> >>> int main() { } >>> >>> Arsen (CC'd) has been working on the libstdc++ changes for the >>> freestanding proposal, and several thousand libstdc++ tests were >>> failing when using -ffreestanding, because of the -Wreturn-type >>> warnings. He wrote a patch to the compiler [4] to add a new >>> -fspecial-main flag which defaults to on for -fhosted, but can be used >>> with -ffreestanding to do the implicit 'return 0' (and so disable the >>> -Wreturn-type warnings) for freestanding as well. This fixes the >>> libstdc++ test FAILs. >>> >>> However, after discussing this briefly with Jason it occurred to us >>> that if the user declares an 'int main()' function, it's a pretty big >>> hint that they do want main() to return an int. And so having >>> undefined behaviour do to a missing return isn't really doing anybody >>> any favours. If you're compiling for freestanding and you *don't* want >>> to return a value from main(), then just declare it as void main() >>> instead. So now we're wondering if we need -fspecial-main at all, or >>> if int main() and int main(int, char**) should always be "special", >>> even for freestanding. So Arsen wrote a patch to do that too [5]. >>> >>> The argument against making 'int main()' imply 'special main' is that >>> in a freestanding environment, a function called 'int main()' might be >>> just a normal function, not the program's entry point. And in that >>> case, maybe you really do want -Wreturn-type warnings. I don't know >>> how realistic that is. >>> >>> So the question is, should Arsen continue with his -fspecial-main >>> patch, and propose it along with the libstdc++ changes, or should gcc >>> change to always make 'int main()' "special" even for freestanding? >>> void main() and long main() and other signatures would still be >>> allowed for freestanding, and would not have the implicit 'return 0'. >> >> I would rather not add a flag. No well-defined freestanding program is >> affected by implicit return 0 from main, it should always be enabled. > > There are some tests that fail if we do that. For whatever reason, > they're checking the current semantics. > * gcc.dg/c11-noreturn-4.c: Add -fno-builtin-main to options. > * gcc.dg/inline-10.c: Likewise. IMO we still shouldn't emit these pedwarns when freestanding, we shouldn't require people to add another flag to avoid them. Adding the implicit return 0 unconditionally doesn't mean we also need to adopt all the other special treatment of main. And I guess we shouldn't implicitly return 0 if the function is declared noreturn. > * gcc.dg/noreturn-4.c: Likewise. I'd be inclined to drop this test. > Arsen implemented Jakub's suggestion which is to add the implicit > return by default, but add -fno-builtin-main to restore the previous > behaviour. Is that acceptable? If not, can you and Jakub reach > consensus so that Arsen knows what to do instead? > His -fno-builtin-main patch is at > https://gcc.gnu.org/pipermail/gcc-patches/2022-September/602644.html > (This is the only one of his patch series not committed, and results > in 100s of FAILs for libstdc++ when testing with -fffreestanding).