[clang] [clang] [modules] add err_main_in_named_module (PR #146247)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Jun 28 16:37:19 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Ashwin Banwari (ashwinbanwari)
<details>
<summary>Changes</summary>
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.
```
---
Full diff: https://github.com/llvm/llvm-project/pull/146247.diff
2 Files Affected:
- (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+2)
- (modified) clang/lib/Sema/SemaDecl.cpp (+8)
``````````diff
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 5062505cf3c01..ce9017ded0087 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1062,6 +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_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.
``````````
</details>
https://github.com/llvm/llvm-project/pull/146247
More information about the cfe-commits
mailing list