[clang] [modules] Implement P3618R0: Allow attaching main to the global module (#146461)

Remove the prior warning for attaching extern "C++" to main.
This commit is contained in:
Ashwin Banwari
2025-07-01 18:52:10 -07:00
committed by GitHub
parent 3deed4211a
commit 2599a9aeb5
7 changed files with 27 additions and 15 deletions

View File

@@ -135,6 +135,8 @@ C++2c Feature Support
- Implemented `P2719R4 Type-aware allocation and deallocation functions <https://wg21.link/P2719>`_.
- Implemented `P3618R0 Allow attaching main to the global module <https://wg21.link/P3618>`_.
C++23 Feature Support
^^^^^^^^^^^^^^^^^^^^^

View File

@@ -1084,9 +1084,9 @@ def warn_main_redefined : Warning<"variable named 'main' with external linkage "
"has undefined behavior">, InGroup<Main>;
def ext_main_used : Extension<
"referring to 'main' within an expression is a Clang extension">, InGroup<Main>;
def ext_main_invalid_linkage_specification : ExtWarn<
"'main' should not be "
"'extern \"%select{C|C++}0\"'">, InGroup<Main>;
def ext_main_invalid_linkage_specification : ExtWarn<"'main' should not be "
"'extern \"C\"'">,
InGroup<Main>;
/// parser diagnostics
def ext_no_declarators : ExtWarn<"declaration does not declare anything">,

View File

@@ -12400,12 +12400,9 @@ bool Sema::CheckFunctionDeclaration(Scope *S, FunctionDecl *NewFD,
void Sema::CheckMain(FunctionDecl *FD, const DeclSpec &DS) {
// [basic.start.main]p3
// The main function shall not be declared with a linkage-specification.
if (FD->isExternCContext() ||
(FD->isExternCXXContext() &&
FD->getDeclContext()->getRedeclContext()->isTranslationUnit()))
Diag(FD->getLocation(), diag::ext_main_invalid_linkage_specification)
<< FD->getLanguageLinkage();
// The main function shall not be declared with C linkage-specification.
if (FD->isExternCContext())
Diag(FD->getLocation(), diag::ext_main_invalid_linkage_specification);
// C++11 [basic.start.main]p3:
// A program that [...] declares main to be inline, static or

View File

@@ -102,11 +102,12 @@ namespace ns {
}
#elif TEST13
// expected-no-diagnostics
extern "C++" {
int main(); // expected-warning {{'main' should not be 'extern "C++"'}}
int main();
}
extern "C++" int main(); // expected-warning {{'main' should not be 'extern "C++"'}}
extern "C++" int main();
namespace ns1 {
extern "C++" int main(); // ok

View File

@@ -68,6 +68,8 @@ int n;
//--- test3.cpp
export module bar;
extern "C++" int main() {}
static int m;
int n;

View File

@@ -317,7 +317,7 @@ C++23, informally referred to as C++26.</p>
<tr>
<td>Attaching main to the global module</td>
<td><a href="https://wg21.link/P3618">P3618R0</a> (<a href="#dr">DR</a>)</td>
<td class="none" align="center">No</td>
<td class="unreleased" align="center">Clang 21</td>
</tr>
<tr>
<td>Expansion Statements</td>

View File

@@ -336,12 +336,22 @@ DEFAULT_FEATURES = [
or platform.system().lower().startswith("aix")
# Avoid building on platforms that don't support modules properly.
or not hasCompileFlag(cfg, "-Wno-reserved-module-identifier")
or not sourceBuilds(
cfg,
"""
# older versions don't support extern "C++", newer versions don't support main in named module.
or not (
sourceBuilds(
cfg,
"""
export module test;
extern "C++" int main(int, char**) { return 0; }
""",
)
or sourceBuilds(
cfg,
"""
export module test;
int main(int, char**) { return 0; }
""",
)
),
),
# The time zone validation tests compare the output of zdump against the