[libcxx-commits] [clang] [libcxx] [clang] [modules] Add err_main_in_named_module (PR #146635)
Ashwin Banwari via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Jul 2 00:52:53 PDT 2025
https://github.com/kish1n updated https://github.com/llvm/llvm-project/pull/146635
>From 76bd9279c0410fa53c8a8ca34229f5ca3a4812e3 Mon Sep 17 00:00:00 2001
From: Ashwin Banwari <ashwinkbanwari at gmail.com>
Date: Mon, 30 Jun 2025 18:10:24 -0700
Subject: [PATCH 1/5] 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 <ashwinkbanwari at gmail.com>
Date: Tue, 1 Jul 2025 22:33:55 -0700
Subject: [PATCH 2/5] 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;
>From 4f635cc99f3f389198571de59eb28e58e3bbd34a Mon Sep 17 00:00:00 2001
From: Ashwin Banwari <ashwinkbanwari at gmail.com>
Date: Wed, 2 Jul 2025 00:35:42 -0700
Subject: [PATCH 3/5] address feedback
---
clang/include/clang/Basic/DiagnosticSemaKinds.td | 4 +---
clang/lib/Sema/SemaDecl.cpp | 4 ++--
clang/test/SemaCXX/modules.cppm | 2 +-
3 files changed, 4 insertions(+), 6 deletions(-)
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 8a05b4b4af31b..b50cddb876fa1 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1062,9 +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 warn_main_in_named_module
- : ExtWarn<"'main' should not be attached to a named module; consider "
- "adding C++ language linkage">,
+def warn_main_in_named_module : ExtWarn<"'main' never has module 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'">,
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index de862e1ad8850..d78dfb2b0bdeb 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -12487,8 +12487,8 @@ void Sema::CheckMain(FunctionDecl *FD, const DeclSpec &DS) {
FD->setInvalidDecl(true);
}
- // In C++ [basic.start.main]p3, it is said a program attaching main to a
- // named module is ill-formed.
+ // [basic.start.main]p3:
+ // A program that declares a function main that belongs to the global scope and is attached to a named module is ill-formed.
if (FD->isInNamedModule()) {
const SourceLocation start = FD->getTypeSpecStartLoc();
Diag(start, diag::warn_main_in_named_module)
diff --git a/clang/test/SemaCXX/modules.cppm b/clang/test/SemaCXX/modules.cppm
index 5e0b3be9870c7..4ef4146df4d9d 100644
--- a/clang/test/SemaCXX/modules.cppm
+++ b/clang/test/SemaCXX/modules.cppm
@@ -41,7 +41,7 @@ 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}}
+int main() {} // expected-warning {{'main' never has module linkage}}
// FIXME: Exports of declarations without external linkage are disallowed.
// Exports of declarations with non-external-linkage types are disallowed.
>From d1ac3f8c8a9a996936ad2c7444ec1b2e89961d9e Mon Sep 17 00:00:00 2001
From: Ashwin Banwari <ashwinkbanwari at gmail.com>
Date: Wed, 2 Jul 2025 00:36:31 -0700
Subject: [PATCH 4/5] clang-format
---
clang/include/clang/Basic/DiagnosticSemaKinds.td | 3 ++-
clang/lib/Sema/SemaDecl.cpp | 3 ++-
2 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index b50cddb876fa1..fac63a500c98f 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 warn_main_in_named_module : ExtWarn<"'main' never has module linkage">,
+def warn_main_in_named_module
+ : ExtWarn<"'main' never has module 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'">,
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index d78dfb2b0bdeb..d0e9bef58ddee 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -12488,7 +12488,8 @@ void Sema::CheckMain(FunctionDecl *FD, const DeclSpec &DS) {
}
// [basic.start.main]p3:
- // A program that declares a function main that belongs to the global scope and is attached to a named module is ill-formed.
+ // A program that declares a function main that belongs to the global scope
+ // and is attached to a named module is ill-formed.
if (FD->isInNamedModule()) {
const SourceLocation start = FD->getTypeSpecStartLoc();
Diag(start, diag::warn_main_in_named_module)
>From 679cc8ceb0f89d8912a02d191d91784ec7edbd0b Mon Sep 17 00:00:00 2001
From: Ashwin Banwari <ashwinkbanwari at gmail.com>
Date: Wed, 2 Jul 2025 00:52:39 -0700
Subject: [PATCH 5/5] add dummy libcxx change for CI checks to run
---
libcxx/utils/libcxx/test/features.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/utils/libcxx/test/features.py b/libcxx/utils/libcxx/test/features.py
index c478d99fbed00..6fbeaae3a48d1 100644
--- a/libcxx/utils/libcxx/test/features.py
+++ b/libcxx/utils/libcxx/test/features.py
@@ -2,7 +2,7 @@
#
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
-# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exceptio
#
# ===----------------------------------------------------------------------===##
More information about the libcxx-commits
mailing list