From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [IPv6:2a00:1450:4864:20::535]) by sourceware.org (Postfix) with ESMTPS id E3AF03858D28 for ; Wed, 28 Sep 2022 20:15:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E3AF03858D28 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-x535.google.com with SMTP id c30so17567173edn.2 for ; Wed, 28 Sep 2022 13:15:46 -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:mime-version:from:to:cc:subject :date; bh=7Fy5u1XABvwc/q8x5eEeYP8RKGxMh9ZkNyrVmSPEPE4=; b=ik9wSql+qY3EytQYFoZM0P7AnumUthnESpe8/b90ph9lnVoQyQQ0GVIPjfIzqPLRSS 3lFgRnJBAqfDlm3yiH2BfmvNWg0Re0/099Izb3gXSd7HaYvaCeysRnj+ztnCu/ZsHH1v q+yYO6rTslpJZEANudg0fdNjSrhAKBBGwv/9wDmXErXx/YVc25ovw0/cdxbSiwR+K4sO JUAmeDuc+B1gZhIQ6hA6EvTekdZJzzPSAJ5SZt+8H5/hY33LlHFHPWLiJX1PFc1fxinS YUMWtNc86THlveYoUwVtYU9M0PXpKhIbtrPmiJTUXmDG8WK6iTKhwGeuKJdEXEkDKzrX U2Pg== 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:mime-version:x-gm-message-state :from:to:cc:subject:date; bh=7Fy5u1XABvwc/q8x5eEeYP8RKGxMh9ZkNyrVmSPEPE4=; b=Yd9zALKZ5roe5UYn0llP0myxEy3FxtHcI3h6s3UBEyPEolNcJujSaMKImkIVq0Jr3I Kj+9k783HFzpuw2rgdw7EySLv9XU+tkBJZ65GCEydrqLKuniHsk3tehiZVsN4fp4El3z i+Y7qCOR/sm4r8iym89DM9NpWH/KXDaINvwXRUz6rnCJnNGAoK9cXVGiD/RVjFQpBA0y h6FRCp0Pb2dEByus6oasXH5RnbyWmGC0yHb5lLG8mgqn0B0poutUYR5nXPJWohXYxoDw 0eJvKrmABDC9DgTUGYqHIY68U0Tz59fFHm6DYmDIZ35RjYEbYg36icctEqxZXrc5xbFd kLpw== X-Gm-Message-State: ACrzQf0QlwrFS9vbntkvAbe52PLtUOZq4/20KRN3VNlp6u3RkkyzxeQw ivkCaO0ZxqsJC3/YRgDZh8u/pmdc1qCUhAcxDLt6AK9Xjcc= X-Google-Smtp-Source: AMsMyM6jcoJW4DA0XA9HYaaUH5bFXvAzET2l9va/5fqmMpqPC2E+5xK+y9yDBwjToFsYXWYCKFQcbVIXuWKtbQ5A6BI= X-Received: by 2002:a05:6402:50d4:b0:451:bf26:8c41 with SMTP id h20-20020a05640250d400b00451bf268c41mr35169257edb.219.1664396145576; Wed, 28 Sep 2022 13:15:45 -0700 (PDT) MIME-Version: 1.0 From: Jonathan Wakely Date: Wed, 28 Sep 2022 21:15:34 +0100 Message-ID: Subject: Handling of main() function for freestanding To: "gcc@gcc.gnu.org" Cc: arsen@aarsen.me, Jason Merrill Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-0.1 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: 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 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