From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x533.google.com (mail-ed1-x533.google.com [IPv6:2a00:1450:4864:20::533]) by sourceware.org (Postfix) with ESMTPS id 4EDE53858D37 for ; Thu, 29 Sep 2022 06:00:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4EDE53858D37 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-ed1-x533.google.com with SMTP id c30so563052edn.2 for ; Wed, 28 Sep 2022 23:00:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date; bh=IMttyaghDqh/sLz4ocH6j/03sHtJaNMVCSl5LD57rns=; b=l0iB1kIPQhWsvoV+AxmQBIeCZBMckx0KqbFsOJ7Ru+Vu1+yas2mmy+vydtBlIipLxF IP09JlNHg5dgkxEUyAZe9X7hB+NgSLsJMowOwWtQWfFCZwDfjOCb+qcVKd5P2O/QOohw 2I2Fvm+HA9+lZE5u72nyS8r6gg3zR0I+AwpD/q3PqxCUWVXtU0IeSUkatBUTwJU/F7li bzhHF4+f3jifw0T43M36fLX6XYpKO5Ziy+8AqgIlSfHAjggdFxPcu4FpfNtBdjtkjp1L QLb5diYoeFXqAL1FSZwRMmBrpjq0UeZxhRO1oDivvAL6BxAsXCyK/jYfXtdMN2Ye/wL6 PT9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date; bh=IMttyaghDqh/sLz4ocH6j/03sHtJaNMVCSl5LD57rns=; b=FPhtiv+MeBuN5o5f577qtqInUjuHUu8llU7scnxPeYCvE819jykrIplT1esYY47M+C B+RtjQoOPhJMV1LJpT3Q75UFTzASjBwBMhbRwL46EHDcvBQdGiWjsQpybcbpUS8RbhdP c4tuikjbVA/HS9oRZIJ4Z+r/cBtzoYhHAYhA3k54Ckqmwh8p70bp3PHjnStVYkVh93IQ nH/uiRoMZ0+jVSwJMRG5UMT8b/E8HmMdjERPjyTHuJUqANOiL40h1AYvdGo5zoyms+8L yuvETUZNzsr926aYoH5E/vFI/v502kYjC5dzlWoj9us/zThAJlu8q/ERVZrlEqG6kXGp tfdQ== X-Gm-Message-State: ACrzQf0Rb+2/qXFrep4FNEu6UJsgPbgjwbi4CwAr2iCaIvN0Bi1BUwxR 32AqlkqEX8H2YgnLcYcsnaqstRYXpsaMpj3/dO4= X-Google-Smtp-Source: AMsMyM7l9HEC40EOCiIY02EW9Py3fdDLkeqTU4zOJXqP1JximpQkl7hATyK6RgxqXOcvIn52JXx8JzmJ22Yxui8ZIIs= X-Received: by 2002:a05:6402:550c:b0:443:7d15:d57f with SMTP id fi12-20020a056402550c00b004437d15d57fmr1589008edb.147.1664431227854; Wed, 28 Sep 2022 23:00:27 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Richard Biener Date: Thu, 29 Sep 2022 08:00:15 +0200 Message-ID: Subject: Re: Handling of main() function for freestanding To: Jonathan Wakely Cc: "gcc@gcc.gnu.org" Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,KAM_INFOUSMEBIZ,KAM_SHORT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=no 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 Wed, Sep 28, 2022 at 10:17 PM Jonathan Wakely via Gcc 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. ^^^^^^^^^^^^^^^^^^^^^^ so just document that 'int main(int, char **)' is special to GCC even in freestanding environments and do not emit -Wreturn-type diagnostics? I think that's entirely reasonable (but of course make sure to add an implicit return 0; then as well) Richard. > 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 have no horse in this race, so if the maintainers of bare metal > ports think int main() should not be special for -ffreestanding, so be > it. I hope the first patch to add -fspecial-main would be acceptable > in that case, and libstdc++ will use it when testing with > -ffreestanding. > > [1] https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p1642r11.html > [2] https://eel.is/c++draft/basic.start.main#5.sentence-2 > [3] https://cigix.me/c17#5.1.2.2.3.p1 > [4] https://github.com/ArsenArsen/gcc/commit/7e67edaced33e31a0dd4db4b3dd404c4a8daba59 > [5] https://github.com/ArsenArsen/gcc/commit/c9bf2f9ed6161a38238e9c7f340d2c3bb04fe443