https://github.com/ashwinbanwari created 
https://github.com/llvm/llvm-project/pull/146635

Revival of https://github.com/llvm/llvm-project/pull/146247.

Now that https://github.com/llvm/llvm-project/pull/146461 is merged to allow 
`extern "C++"` for main, we can merge this change.

>From 76bd9279c0410fa53c8a8ca34229f5ca3a4812e3 Mon Sep 17 00:00:00 2001
From: Ashwin Banwari <ashwinkbanw...@gmail.com>
Date: Mon, 30 Jun 2025 18:10:24 -0700
Subject: [PATCH 1/2] Reapply "[clang] [modules] Add err_main_in_named_module
 (#146247)"

This reverts commit 8a5b97a7205db189ca82f44dec7a399c2b5da546.
---
 clang/docs/ReleaseNotes.rst                      | 3 +++
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 4 ++++
 clang/lib/Sema/SemaDecl.cpp                      | 8 ++++++++
 clang/test/Driver/autocomplete.c                 | 1 +
 clang/test/SemaCXX/modules.cppm                  | 2 ++
 5 files changed, 18 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index e6c8f9df22170..db99cc9233377 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -659,6 +659,9 @@ Improvements to Clang's diagnostics
   diagnostics when floating-point numbers had both width field and plus or 
space
   prefix specified. (#GH143951)
 
+- A warning is now emitted when ``main`` is attached to a named module,
+  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
   a call to a function that is trivially known to always throw (i.e., its
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 5062505cf3c01..451619709c087 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1062,6 +1062,10 @@ 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 warn_main_in_named_module
+    : ExtWarn<"'main' should not be attached to a named module; consider "
+              "adding C++ language linkage">,
+      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>;
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index a34e2c9cbb003..f4bc191d1dae6 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -12489,6 +12489,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()) {
+      const SourceLocation start = FD->getTypeSpecStartLoc();
+      Diag(start, diag::warn_main_in_named_module)
+          << FixItHint::CreateInsertion(start, "extern \"C++\" ", true);
+    }
   }
 
   // Treat protoless main() as nullary.
diff --git a/clang/test/Driver/autocomplete.c b/clang/test/Driver/autocomplete.c
index 8cc604dbff875..4983b71496834 100644
--- a/clang/test/Driver/autocomplete.c
+++ b/clang/test/Driver/autocomplete.c
@@ -111,6 +111,7 @@
 // RUN: %clang --autocomplete=-Wma | FileCheck %s -check-prefix=WARNING
 // WARNING: -Wmacro-redefined
 // WARNING-NEXT: -Wmain
+// WARNING-NEXT: -Wmain-attached-to-named-module
 // WARNING-NEXT: -Wmain-return-type
 // WARNING-NEXT: -Wmalformed-warning-check
 // WARNING-NEXT: -Wmany-braces-around-scalar-init
diff --git a/clang/test/SemaCXX/modules.cppm b/clang/test/SemaCXX/modules.cppm
index 5d0d6da44a2ed..81bc749c58259 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-warning {{'main' should not be attached to a named 
module; consider adding C++ language linkage}}
+
 static int m;
 
 int n;

>From 195b81a312fed2b13bea14666feb45f3c1f599ed Mon Sep 17 00:00:00 2001
From: Ashwin Banwari <ashwinkbanw...@gmail.com>
Date: Tue, 1 Jul 2025 22:33:55 -0700
Subject: [PATCH 2/2] update test

---
 clang/test/SemaCXX/modules.cppm | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/test/SemaCXX/modules.cppm b/clang/test/SemaCXX/modules.cppm
index 5d7ea6bacf7e0..5e0b3be9870c7 100644
--- a/clang/test/SemaCXX/modules.cppm
+++ b/clang/test/SemaCXX/modules.cppm
@@ -41,6 +41,8 @@ struct S {
   export static int n; // expected-error {{expected member name or ';'}}
 };
 
+int main() {} // expected-warning {{'main' should not be attached to a named 
module; consider adding C++ language linkage}}
+
 // FIXME: Exports of declarations without external linkage are disallowed.
 // Exports of declarations with non-external-linkage types are disallowed.
 
@@ -68,8 +70,6 @@ int n;
 //--- test3.cpp
 export module bar;
 
-int main() {} // expected-warning {{'main' should not be attached to a named 
module; consider adding C++ language linkage}}
-
 extern "C++" int main() {}
 
 static int m;

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

Reply via email to