[clang] [libcxx] [clang] [modules] Implement P3618R0: Allow attaching main to the global module (PR #146461)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Jun 30 20:47:25 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libcxx
Author: Ashwin Banwari (ashwinbanwari)
<details>
<summary>Changes</summary>
Remove the prior warning for attaching extern "C++" to main.
---
Full diff: https://github.com/llvm/llvm-project/pull/146461.diff
5 Files Affected:
- (modified) clang/docs/ReleaseNotes.rst (+2)
- (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+1-1)
- (modified) clang/lib/Sema/SemaDecl.cpp (+3-6)
- (modified) clang/test/SemaCXX/modules.cppm (+2)
- (modified) libcxx/utils/libcxx/test/features.py (+1-1)
``````````diff
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index e6c8f9df22170..33dd07179aeea 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 5062505cf3c01..d5d982d20eaa5 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1086,7 +1086,7 @@ 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>;
+ "'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 a34e2c9cbb003..61b82b8377e22 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/SemaCXX/modules.cppm b/clang/test/SemaCXX/modules.cppm
index 5d0d6da44a2ed..ddbbc7cd86360 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/libcxx/utils/libcxx/test/features.py b/libcxx/utils/libcxx/test/features.py
index 74746e37d3bc4..a479ea48ac810 100644
--- a/libcxx/utils/libcxx/test/features.py
+++ b/libcxx/utils/libcxx/test/features.py
@@ -340,7 +340,7 @@ def _mingwSupportsModules(cfg):
cfg,
"""
export module test;
- int main(int, char**) { return 0; }
+ extern "C++" int main(int, char**) { return 0; }
""",
),
),
``````````
</details>
https://github.com/llvm/llvm-project/pull/146461
More information about the cfe-commits
mailing list