[clang] [clang] [modules] add err_main_in_named_module (PR #146247)
Ashwin Banwari via cfe-commits
cfe-commits at lists.llvm.org
Sat Jun 28 16:36:41 PDT 2025
https://github.com/ashwinbanwari created https://github.com/llvm/llvm-project/pull/146247
Close https://github.com/llvm/llvm-project/issues/146229
As the issue said, main shouldn't be in any modules.
New Error Output:
```
/my/code/directory/main.cpp:3:1: error: 'main' cannot be attached to a named module
3 | int main() {
| ^
1 error generated.
```
>From f2ed0c7989d7e181004237a4fa2ba7ae1efe44ed Mon Sep 17 00:00:00 2001
From: Ashwin Banwari <ashwinkbanwari at gmail.com>
Date: Sat, 28 Jun 2025 16:19:55 -0700
Subject: [PATCH 1/2] 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 <ashwinkbanwari at gmail.com>
Date: Sat, 28 Jun 2025 16:32:20 -0700
Subject: [PATCH 2/2] 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>;
More information about the cfe-commits
mailing list