Author: Ashwin Banwari
Date: 2025-07-02T09:52:10+08:00
New Revision: 2599a9aeb543f01cb20e3fdc2713aa8c8cb20fbf

URL: 
https://github.com/llvm/llvm-project/commit/2599a9aeb543f01cb20e3fdc2713aa8c8cb20fbf
DIFF: 
https://github.com/llvm/llvm-project/commit/2599a9aeb543f01cb20e3fdc2713aa8c8cb20fbf.diff

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

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

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/include/clang/Basic/DiagnosticSemaKinds.td
    clang/lib/Sema/SemaDecl.cpp
    clang/test/CXX/basic/basic.start/basic.start.main/p3.cpp
    clang/test/SemaCXX/modules.cppm
    clang/www/cxx_status.html
    libcxx/utils/libcxx/test/features.py

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 7af5f116ee98f..3d893e0aa8e2c 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..6e48f3bc88bab 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 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/CXX/basic/basic.start/basic.start.main/p3.cpp 
b/clang/test/CXX/basic/basic.start/basic.start.main/p3.cpp
index 497a9328c1156..a8cd523b0837f 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 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/clang/www/cxx_status.html b/clang/www/cxx_status.html
index a70e65e35d5ee..e5f51bfb2e36e 100755
--- a/clang/www/cxx_status.html
+++ b/clang/www/cxx_status.html
@@ -317,7 +317,7 @@ <h2 id="cxx26">C++2c implementation status</h2>
  <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 74746e37d3bc4..c478d99fbed00 100644
--- a/libcxx/utils/libcxx/test/features.py
+++ b/libcxx/utils/libcxx/test/features.py
@@ -336,12 +336,22 @@ def _mingwSupportsModules(cfg):
         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


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to