diff options
author | Ashwin Banwari <ashwinkbanwari@gmail.com> | 2025-07-01 18:52:10 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-07-02 09:52:10 +0800 |
commit | 2599a9aeb543f01cb20e3fdc2713aa8c8cb20fbf (patch) | |
tree | 30524bb486a93595f7011513d852bc0edbe26ca1 | |
parent | 3deed4211a46632a25b1b558b357273a5ce4ee96 (diff) | |
download | llvm-2599a9aeb543f01cb20e3fdc2713aa8c8cb20fbf.zip llvm-2599a9aeb543f01cb20e3fdc2713aa8c8cb20fbf.tar.gz llvm-2599a9aeb543f01cb20e3fdc2713aa8c8cb20fbf.tar.bz2 |
[clang] [modules] Implement P3618R0: Allow attaching main to the global module (#146461)
Remove the prior warning for attaching extern "C++" to main.
-rw-r--r-- | clang/docs/ReleaseNotes.rst | 2 | ||||
-rw-r--r-- | clang/include/clang/Basic/DiagnosticSemaKinds.td | 6 | ||||
-rw-r--r-- | clang/lib/Sema/SemaDecl.cpp | 9 | ||||
-rw-r--r-- | clang/test/CXX/basic/basic.start/basic.start.main/p3.cpp | 5 | ||||
-rw-r--r-- | clang/test/SemaCXX/modules.cppm | 2 | ||||
-rwxr-xr-x | clang/www/cxx_status.html | 2 | ||||
-rw-r--r-- | libcxx/utils/libcxx/test/features.py | 16 |
7 files changed, 27 insertions, 15 deletions
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 7af5f11..3d893e0 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -135,6 +135,8 @@ C++2c Feature Support - Implemented `P2719R4 Type-aware allocation and deallocation functions <https://wg21.link/P2719>`_. +- Implemented `P3618R0 Allow attaching main to the global module <https://wg21.link/P3618>`_. + C++23 Feature Support ^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 5062505..6e48f3b 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -1084,9 +1084,9 @@ def warn_main_redefined : Warning<"variable named 'main' with external linkage " "has undefined behavior">, InGroup<Main>; def ext_main_used : Extension< "referring to 'main' within an expression is a Clang extension">, InGroup<Main>; -def ext_main_invalid_linkage_specification : ExtWarn< - "'main' should not be " - "'extern \"%select{C|C++}0\"'">, InGroup<Main>; +def ext_main_invalid_linkage_specification : ExtWarn<"'main' should not be " + "'extern \"C\"'">, + InGroup<Main>; /// parser diagnostics def ext_no_declarators : ExtWarn<"declaration does not declare anything">, diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index a34e2c9..61b82b8 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -12400,12 +12400,9 @@ bool Sema::CheckFunctionDeclaration(Scope *S, FunctionDecl *NewFD, void Sema::CheckMain(FunctionDecl *FD, const DeclSpec &DS) { // [basic.start.main]p3 - // The main function shall not be declared with a linkage-specification. - if (FD->isExternCContext() || - (FD->isExternCXXContext() && - FD->getDeclContext()->getRedeclContext()->isTranslationUnit())) - Diag(FD->getLocation(), diag::ext_main_invalid_linkage_specification) - << FD->getLanguageLinkage(); + // The main function shall not be declared with C linkage-specification. + if (FD->isExternCContext()) + Diag(FD->getLocation(), diag::ext_main_invalid_linkage_specification); // C++11 [basic.start.main]p3: // A program that [...] declares main to be inline, static or diff --git a/clang/test/CXX/basic/basic.start/basic.start.main/p3.cpp b/clang/test/CXX/basic/basic.start/basic.start.main/p3.cpp index 497a932..a8cd523 100644 --- a/clang/test/CXX/basic/basic.start/basic.start.main/p3.cpp +++ b/clang/test/CXX/basic/basic.start/basic.start.main/p3.cpp @@ -102,11 +102,12 @@ namespace ns { } #elif TEST13 +// expected-no-diagnostics extern "C++" { - int main(); // expected-warning {{'main' should not be 'extern "C++"'}} + int main(); } -extern "C++" int main(); // expected-warning {{'main' should not be 'extern "C++"'}} +extern "C++" int main(); namespace ns1 { extern "C++" int main(); // ok diff --git a/clang/test/SemaCXX/modules.cppm b/clang/test/SemaCXX/modules.cppm index 5d0d6da..ddbbc7c 100644 --- a/clang/test/SemaCXX/modules.cppm +++ b/clang/test/SemaCXX/modules.cppm @@ -68,6 +68,8 @@ int n; //--- test3.cpp export module bar; +extern "C++" int main() {} + static int m; int n; diff --git a/clang/www/cxx_status.html b/clang/www/cxx_status.html index a70e65e..e5f51bf 100755 --- a/clang/www/cxx_status.html +++ b/clang/www/cxx_status.html @@ -317,7 +317,7 @@ C++23, informally referred to as C++26.</p> <tr> <td>Attaching main to the global module</td> <td><a href="https://wg21.link/P3618">P3618R0</a> (<a href="#dr">DR</a>)</td> - <td class="none" align="center">No</td> + <td class="unreleased" align="center">Clang 21</td> </tr> <tr> <td>Expansion Statements</td> diff --git a/libcxx/utils/libcxx/test/features.py b/libcxx/utils/libcxx/test/features.py index 74746e3..c478d99 100644 --- a/libcxx/utils/libcxx/test/features.py +++ b/libcxx/utils/libcxx/test/features.py @@ -336,12 +336,22 @@ DEFAULT_FEATURES = [ or platform.system().lower().startswith("aix") # Avoid building on platforms that don't support modules properly. or not hasCompileFlag(cfg, "-Wno-reserved-module-identifier") - or not sourceBuilds( - cfg, - """ + # older versions don't support extern "C++", newer versions don't support main in named module. + or not ( + sourceBuilds( + cfg, + """ + export module test; + extern "C++" int main(int, char**) { return 0; } + """, + ) + or sourceBuilds( + cfg, + """ export module test; int main(int, char**) { return 0; } """, + ) ), ), # The time zone validation tests compare the output of zdump against the |