Skip to content

[clang] [modules] Implement P3618R0: Allow attaching main to the global module #146461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

ashwinbanwari
Copy link
Contributor

Remove the prior warning for attaching extern "C++" to main.

@ashwinbanwari ashwinbanwari requested a review from a team as a code owner July 1, 2025 03:46
@llvmbot llvmbot added clang Clang issues not falling into any other category libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jul 1, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 1, 2025

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-libcxx

Author: Ashwin Banwari (ashwinbanwari)

Changes

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 --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; }
           """,
         ),
     ),

@ashwinbanwari
Copy link
Contributor Author

CC @ChuanqiXu9 for review

Copy link

github-actions bot commented Jul 1, 2025

✅ With the latest revision this PR passed the Python code formatter.

Comment on lines +12403 to +12405
// The main function shall not be declared with C linkage-specification.
if (FD->isExternCContext())
Diag(FD->getLocation(), diag::ext_main_invalid_linkage_specification);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel this is not right. We should avoid emitting the diagnostics if it is in a named module.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to proposal, extern "C" for main is still ill-formed regardless if it is named module or not. On other hand, extern "C++" is allowed whether in named module or not.

Copy link
Member

@ChuanqiXu9 ChuanqiXu9 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. I'll merge this after the CI gets green.

@zwuis
Copy link
Contributor

zwuis commented Jul 1, 2025

Please update "clang/www/cxx_status.html".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants