https://github.com/ashwinbanwari updated 
https://github.com/llvm/llvm-project/pull/146247

>From f2ed0c7989d7e181004237a4fa2ba7ae1efe44ed Mon Sep 17 00:00:00 2001
From: Ashwin Banwari <ashwinkbanw...@gmail.com>
Date: Sat, 28 Jun 2025 16:19:55 -0700
Subject: [PATCH 01/10] add err_main_in_named_module

---
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 1 +
 clang/lib/Sema/SemaDecl.cpp                      | 8 ++++++++
 2 files changed, 9 insertions(+)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 5062505cf3c01..94f08300c3dcb 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1062,6 +1062,7 @@ def err_constexpr_main : Error<
   "'main' is not allowed to be declared %select{constexpr|consteval}0">;
 def err_deleted_main : Error<"'main' is not allowed to be deleted">;
 def err_mainlike_template_decl : Error<"%0 cannot be a template">;
+def err_main_in_named_module : Error<"'main' cannot be attached to a named 
module">;
 def err_main_returns_nonint : Error<"'main' must return 'int'">;
 def ext_main_returns_nonint : ExtWarn<"return type of 'main' is not 'int'">,
     InGroup<MainReturnType>;
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index e1cccf068b5aa..c4ddfda9f447f 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -12490,6 +12490,14 @@ void Sema::CheckMain(FunctionDecl *FD, const DeclSpec 
&DS) {
                                 : FixItHint());
       FD->setInvalidDecl(true);
     }
+
+    // In C++ [basic.start.main]p3, it is said a program attaching main to a
+    // named module is ill-formed.
+    if (FD->isInNamedModule()) {
+      Diag(FD->getTypeSpecStartLoc(), diag::err_main_in_named_module)
+          << FixItHint();
+      FD->setInvalidDecl(true);
+    }
   }
 
   // Treat protoless main() as nullary.

>From b721f8ff73a67cb3485876e4b4c0de48cc59d194 Mon Sep 17 00:00:00 2001
From: Ashwin Banwari <ashwinkbanw...@gmail.com>
Date: Sat, 28 Jun 2025 16:32:20 -0700
Subject: [PATCH 02/10] git clang-format HEAD~1

---
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 94f08300c3dcb..ce9017ded0087 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1062,7 +1062,8 @@ def err_constexpr_main : Error<
   "'main' is not allowed to be declared %select{constexpr|consteval}0">;
 def err_deleted_main : Error<"'main' is not allowed to be deleted">;
 def err_mainlike_template_decl : Error<"%0 cannot be a template">;
-def err_main_in_named_module : Error<"'main' cannot be attached to a named 
module">;
+def err_main_in_named_module
+    : Error<"'main' cannot be attached to a named module">;
 def err_main_returns_nonint : Error<"'main' must return 'int'">;
 def ext_main_returns_nonint : ExtWarn<"return type of 'main' is not 'int'">,
     InGroup<MainReturnType>;

>From 6130b53fc4051a618c612f1d852c7d85557644d2 Mon Sep 17 00:00:00 2001
From: Ashwin Banwari <ashwinkbanw...@gmail.com>
Date: Sat, 28 Jun 2025 16:47:02 -0700
Subject: [PATCH 03/10] don't need FixItHint() ?

---
 clang/lib/Sema/SemaDecl.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index c4ddfda9f447f..064e145ff502f 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -12494,8 +12494,7 @@ void Sema::CheckMain(FunctionDecl *FD, const DeclSpec 
&DS) {
     // In C++ [basic.start.main]p3, it is said a program attaching main to a
     // named module is ill-formed.
     if (FD->isInNamedModule()) {
-      Diag(FD->getTypeSpecStartLoc(), diag::err_main_in_named_module)
-          << FixItHint();
+      Diag(FD->getTypeSpecStartLoc(), diag::err_main_in_named_module);
       FD->setInvalidDecl(true);
     }
   }

>From 1f2a548efbf533cc3756e06d85c0d2a0845c7e78 Mon Sep 17 00:00:00 2001
From: Ashwin Banwari <ashwinkbanw...@gmail.com>
Date: Sun, 29 Jun 2025 02:01:57 -0700
Subject: [PATCH 04/10] add release notes and unit test

---
 clang/docs/ReleaseNotes.rst     | 2 ++
 clang/test/SemaCXX/modules.cppm | 2 ++
 2 files changed, 4 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 9cfe39fca52ee..89e128dc434d0 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -648,6 +648,8 @@ Improvements to Clang's diagnostics
   #GH69470, #GH59391, #GH58172, #GH46215, #GH45915, #GH45891, #GH44490,
   #GH36703, #GH32903, #GH23312, #GH69874.
 
+- An error is now emitted when ``main`` is attached to a named module. 
(#GH146247)
+
   
 Improvements to Clang's time-trace
 ----------------------------------
diff --git a/clang/test/SemaCXX/modules.cppm b/clang/test/SemaCXX/modules.cppm
index 5d0d6da44a2ed..443e7b6eb3296 100644
--- a/clang/test/SemaCXX/modules.cppm
+++ b/clang/test/SemaCXX/modules.cppm
@@ -68,6 +68,8 @@ int n;
 //--- test3.cpp
 export module bar;
 
+int main() {} // expected-error {{'main' cannot be attached to a named module}}
+
 static int m;
 
 int n;

>From 1b83608afdaf02cea75190d84e02163d717006f7 Mon Sep 17 00:00:00 2001
From: Ashwin Banwari <ashwinkbanw...@gmail.com>
Date: Mon, 30 Jun 2025 00:34:36 -0700
Subject: [PATCH 05/10] change error to warning

---
 clang/docs/ReleaseNotes.rst                      | 2 +-
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 4 ++--
 clang/lib/Sema/SemaDecl.cpp                      | 3 +--
 clang/test/SemaCXX/modules.cppm                  | 2 +-
 4 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 7210f8d51361d..028adf73bf6b1 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -650,7 +650,7 @@ Improvements to Clang's diagnostics
   #GH69470, #GH59391, #GH58172, #GH46215, #GH45915, #GH45891, #GH44490,
   #GH36703, #GH32903, #GH23312, #GH69874.
 
-- An error is now emitted when ``main`` is attached to a named module. 
(#GH146247)
+- A warning is now emitted when ``main`` is attached to a named module. 
(#GH146247)
 
 - Clang now avoids issuing `-Wreturn-type` warnings in some cases where
   the final statement of a non-void function is a `throw` expression, or
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index ce9017ded0087..762b19a4cef46 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1062,8 +1062,8 @@ def err_constexpr_main : Error<
   "'main' is not allowed to be declared %select{constexpr|consteval}0">;
 def err_deleted_main : Error<"'main' is not allowed to be deleted">;
 def err_mainlike_template_decl : Error<"%0 cannot be a template">;
-def err_main_in_named_module
-    : Error<"'main' cannot be attached to a named module">;
+def warn_main_in_named_module
+    : Warning<"'main' should not be attached to a named module">;
 def err_main_returns_nonint : Error<"'main' must return 'int'">;
 def ext_main_returns_nonint : ExtWarn<"return type of 'main' is not 'int'">,
     InGroup<MainReturnType>;
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index b622f8e62cb6f..5fa9cdb92c33d 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -12493,8 +12493,7 @@ void Sema::CheckMain(FunctionDecl *FD, const DeclSpec 
&DS) {
     // In C++ [basic.start.main]p3, it is said a program attaching main to a
     // named module is ill-formed.
     if (FD->isInNamedModule()) {
-      Diag(FD->getTypeSpecStartLoc(), diag::err_main_in_named_module);
-      FD->setInvalidDecl(true);
+      Diag(FD->getTypeSpecStartLoc(), diag::warn_main_in_named_module);
     }
   }
 
diff --git a/clang/test/SemaCXX/modules.cppm b/clang/test/SemaCXX/modules.cppm
index 443e7b6eb3296..1acab33d2478a 100644
--- a/clang/test/SemaCXX/modules.cppm
+++ b/clang/test/SemaCXX/modules.cppm
@@ -68,7 +68,7 @@ int n;
 //--- test3.cpp
 export module bar;
 
-int main() {} // expected-error {{'main' cannot be attached to a named module}}
+int main() {} // expected-warning {{'main' should not be attached to a named 
module}}
 
 static int m;
 

>From 96395ec24d71341379b65f8cd75d4051e6b8d29f Mon Sep 17 00:00:00 2001
From: Ashwin Banwari <ashwinkbanw...@gmail.com>
Date: Mon, 30 Jun 2025 01:03:06 -0700
Subject: [PATCH 06/10] put warning in diagnostic group -Wmain

---
 clang/docs/ReleaseNotes.rst                      | 3 ++-
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 028adf73bf6b1..5642912aa9b20 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -650,7 +650,8 @@ Improvements to Clang's diagnostics
   #GH69470, #GH59391, #GH58172, #GH46215, #GH45915, #GH45891, #GH44490,
   #GH36703, #GH32903, #GH23312, #GH69874.
 
-- A warning is now emitted when ``main`` is attached to a named module. 
(#GH146247)
+- A warning is now emitted when ``main`` is attached to a named module,
+  which can be turned off with ``-Wno-main``. (#GH146247)
 
 - Clang now avoids issuing `-Wreturn-type` warnings in some cases where
   the final statement of a non-void function is a `throw` expression, or
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 762b19a4cef46..995fb2fa1a5ea 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1063,7 +1063,8 @@ def err_constexpr_main : Error<
 def err_deleted_main : Error<"'main' is not allowed to be deleted">;
 def err_mainlike_template_decl : Error<"%0 cannot be a template">;
 def warn_main_in_named_module
-    : Warning<"'main' should not be attached to a named module">;
+    : ExtWarn<"'main' should not be attached to a named module">,
+    InGroup<Main>;
 def err_main_returns_nonint : Error<"'main' must return 'int'">;
 def ext_main_returns_nonint : ExtWarn<"return type of 'main' is not 'int'">,
     InGroup<MainReturnType>;

>From 790ecdcfafe6b4bf1509421f02ec64463d322a4e Mon Sep 17 00:00:00 2001
From: Ashwin Banwari <ashwinkbanw...@gmail.com>
Date: Mon, 30 Jun 2025 01:10:13 -0700
Subject: [PATCH 07/10] git clang format

---
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 995fb2fa1a5ea..1210493ef77cb 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1064,7 +1064,7 @@ def err_deleted_main : Error<"'main' is not allowed to be 
deleted">;
 def err_mainlike_template_decl : Error<"%0 cannot be a template">;
 def warn_main_in_named_module
     : ExtWarn<"'main' should not be attached to a named module">,
-    InGroup<Main>;
+      InGroup<Main>;
 def err_main_returns_nonint : Error<"'main' must return 'int'">;
 def ext_main_returns_nonint : ExtWarn<"return type of 'main' is not 'int'">,
     InGroup<MainReturnType>;

>From b6b1b71863720f96c8cdad45a9a780ca74fedc60 Mon Sep 17 00:00:00 2001
From: Ashwin Banwari <ashwinkbanw...@gmail.com>
Date: Mon, 30 Jun 2025 01:22:30 -0700
Subject: [PATCH 08/10] Suggest wrap into language linkage

---
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 2 +-
 clang/lib/Sema/SemaDecl.cpp                      | 4 +++-
 clang/test/SemaCXX/modules.cppm                  | 2 +-
 3 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 1210493ef77cb..52336b6beabd1 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1063,7 +1063,7 @@ def err_constexpr_main : Error<
 def err_deleted_main : Error<"'main' is not allowed to be deleted">;
 def err_mainlike_template_decl : Error<"%0 cannot be a template">;
 def warn_main_in_named_module
-    : ExtWarn<"'main' should not be attached to a named module">,
+    : ExtWarn<"'main' should not be attached to a named module; consider 
adding C++ language linkage">,
       InGroup<Main>;
 def err_main_returns_nonint : Error<"'main' must return 'int'">;
 def ext_main_returns_nonint : ExtWarn<"return type of 'main' is not 'int'">,
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 5fa9cdb92c33d..4da2a1cce5faf 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -12493,7 +12493,9 @@ void Sema::CheckMain(FunctionDecl *FD, const DeclSpec 
&DS) {
     // In C++ [basic.start.main]p3, it is said a program attaching main to a
     // named module is ill-formed.
     if (FD->isInNamedModule()) {
-      Diag(FD->getTypeSpecStartLoc(), diag::warn_main_in_named_module);
+      const SourceLocation start = FD->getTypeSpecStartLoc();
+      Diag(start, diag::warn_main_in_named_module)
+      << FixItHint::CreateInsertion(start, "extern \"C++\" ", true);
     }
   }
 
diff --git a/clang/test/SemaCXX/modules.cppm b/clang/test/SemaCXX/modules.cppm
index 1acab33d2478a..81bc749c58259 100644
--- a/clang/test/SemaCXX/modules.cppm
+++ b/clang/test/SemaCXX/modules.cppm
@@ -68,7 +68,7 @@ int n;
 //--- test3.cpp
 export module bar;
 
-int main() {} // expected-warning {{'main' should not be attached to a named 
module}}
+int main() {} // expected-warning {{'main' should not be attached to a named 
module; consider adding C++ language linkage}}
 
 static int m;
 

>From 3df1e16ac23dc80c1193115e1d899697d981dcff Mon Sep 17 00:00:00 2001
From: Ashwin Banwari <ashwinkbanw...@gmail.com>
Date: Mon, 30 Jun 2025 01:23:45 -0700
Subject: [PATCH 09/10] add git clang-format

---
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 3 ++-
 clang/lib/Sema/SemaDecl.cpp                      | 2 +-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 52336b6beabd1..ebc4d64e9dcca 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1063,7 +1063,8 @@ def err_constexpr_main : Error<
 def err_deleted_main : Error<"'main' is not allowed to be deleted">;
 def err_mainlike_template_decl : Error<"%0 cannot be a template">;
 def warn_main_in_named_module
-    : ExtWarn<"'main' should not be attached to a named module; consider 
adding C++ language linkage">,
+    : ExtWarn<"'main' should not be attached to a named module; consider "
+              "adding C++ language linkage">,
       InGroup<Main>;
 def err_main_returns_nonint : Error<"'main' must return 'int'">;
 def ext_main_returns_nonint : ExtWarn<"return type of 'main' is not 'int'">,
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 4da2a1cce5faf..33b9ef869746a 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -12495,7 +12495,7 @@ void Sema::CheckMain(FunctionDecl *FD, const DeclSpec 
&DS) {
     if (FD->isInNamedModule()) {
       const SourceLocation start = FD->getTypeSpecStartLoc();
       Diag(start, diag::warn_main_in_named_module)
-      << FixItHint::CreateInsertion(start, "extern \"C++\" ", true);
+          << FixItHint::CreateInsertion(start, "extern \"C++\" ", true);
     }
   }
 

>From 088e4a8124ce7b7197bf551ca8328362389db292 Mon Sep 17 00:00:00 2001
From: Ashwin Banwari <ashwinkbanw...@gmail.com>
Date: Mon, 30 Jun 2025 01:41:02 -0700
Subject: [PATCH 10/10] change group to -Wmain-attached-to-named-module

---
 clang/docs/ReleaseNotes.rst                      | 2 +-
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 5642912aa9b20..05ac5e2e679e7 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -651,7 +651,7 @@ Improvements to Clang's diagnostics
   #GH36703, #GH32903, #GH23312, #GH69874.
 
 - A warning is now emitted when ``main`` is attached to a named module,
-  which can be turned off with ``-Wno-main``. (#GH146247)
+  which can be turned off with ``-Wno-main-attached-to-named-module``. 
(#GH146247)
 
 - Clang now avoids issuing `-Wreturn-type` warnings in some cases where
   the final statement of a non-void function is a `throw` expression, or
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index ebc4d64e9dcca..451619709c087 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1065,7 +1065,7 @@ def err_mainlike_template_decl : Error<"%0 cannot be a 
template">;
 def warn_main_in_named_module
     : ExtWarn<"'main' should not be attached to a named module; consider "
               "adding C++ language linkage">,
-      InGroup<Main>;
+      InGroup<DiagGroup<"main-attached-to-named-module">>;
 def err_main_returns_nonint : Error<"'main' must return 'int'">;
 def ext_main_returns_nonint : ExtWarn<"return type of 'main' is not 'int'">,
     InGroup<MainReturnType>;

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

Reply via email to