Skip to content

Commit 2599a9a

Browse files
authored
[clang] [modules] Implement P3618R0: Allow attaching main to the global module (#146461)
Remove the prior warning for attaching extern "C++" to main.
1 parent 3deed42 commit 2599a9a

File tree

7 files changed

+27
-15
lines changed

7 files changed

+27
-15
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ C++2c Feature Support
135135

136136
- Implemented `P2719R4 Type-aware allocation and deallocation functions <https://wg21.link/P2719>`_.
137137

138+
- Implemented `P3618R0 Allow attaching main to the global module <https://wg21.link/P3618>`_.
139+
138140
C++23 Feature Support
139141
^^^^^^^^^^^^^^^^^^^^^
140142

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,9 +1084,9 @@ def warn_main_redefined : Warning<"variable named 'main' with external linkage "
10841084
"has undefined behavior">, InGroup<Main>;
10851085
def ext_main_used : Extension<
10861086
"referring to 'main' within an expression is a Clang extension">, InGroup<Main>;
1087-
def ext_main_invalid_linkage_specification : ExtWarn<
1088-
"'main' should not be "
1089-
"'extern \"%select{C|C++}0\"'">, InGroup<Main>;
1087+
def ext_main_invalid_linkage_specification : ExtWarn<"'main' should not be "
1088+
"'extern \"C\"'">,
1089+
InGroup<Main>;
10901090

10911091
/// parser diagnostics
10921092
def ext_no_declarators : ExtWarn<"declaration does not declare anything">,

clang/lib/Sema/SemaDecl.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12400,12 +12400,9 @@ bool Sema::CheckFunctionDeclaration(Scope *S, FunctionDecl *NewFD,
1240012400

1240112401
void Sema::CheckMain(FunctionDecl *FD, const DeclSpec &DS) {
1240212402
// [basic.start.main]p3
12403-
// The main function shall not be declared with a linkage-specification.
12404-
if (FD->isExternCContext() ||
12405-
(FD->isExternCXXContext() &&
12406-
FD->getDeclContext()->getRedeclContext()->isTranslationUnit()))
12407-
Diag(FD->getLocation(), diag::ext_main_invalid_linkage_specification)
12408-
<< FD->getLanguageLinkage();
12403+
// The main function shall not be declared with C linkage-specification.
12404+
if (FD->isExternCContext())
12405+
Diag(FD->getLocation(), diag::ext_main_invalid_linkage_specification);
1240912406

1241012407
// C++11 [basic.start.main]p3:
1241112408
// A program that [...] declares main to be inline, static or

clang/test/CXX/basic/basic.start/basic.start.main/p3.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,12 @@ namespace ns {
102102
}
103103

104104
#elif TEST13
105+
// expected-no-diagnostics
105106
extern "C++" {
106-
int main(); // expected-warning {{'main' should not be 'extern "C++"'}}
107+
int main();
107108
}
108109

109-
extern "C++" int main(); // expected-warning {{'main' should not be 'extern "C++"'}}
110+
extern "C++" int main();
110111

111112
namespace ns1 {
112113
extern "C++" int main(); // ok

clang/test/SemaCXX/modules.cppm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ int n;
6868
//--- test3.cpp
6969
export module bar;
7070

71+
extern "C++" int main() {}
72+
7173
static int m;
7274

7375
int n;

clang/www/cxx_status.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ <h2 id="cxx26">C++2c implementation status</h2>
317317
<tr>
318318
<td>Attaching main to the global module</td>
319319
<td><a href="https://wg21.link/P3618">P3618R0</a> (<a href="#dr">DR</a>)</td>
320-
<td class="none" align="center">No</td>
320+
<td class="unreleased" align="center">Clang 21</td>
321321
</tr>
322322
<tr>
323323
<td>Expansion Statements</td>

libcxx/utils/libcxx/test/features.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,12 +336,22 @@ def _mingwSupportsModules(cfg):
336336
or platform.system().lower().startswith("aix")
337337
# Avoid building on platforms that don't support modules properly.
338338
or not hasCompileFlag(cfg, "-Wno-reserved-module-identifier")
339-
or not sourceBuilds(
340-
cfg,
341-
"""
339+
# older versions don't support extern "C++", newer versions don't support main in named module.
340+
or not (
341+
sourceBuilds(
342+
cfg,
343+
"""
344+
export module test;
345+
extern "C++" int main(int, char**) { return 0; }
346+
""",
347+
)
348+
or sourceBuilds(
349+
cfg,
350+
"""
342351
export module test;
343352
int main(int, char**) { return 0; }
344353
""",
354+
)
345355
),
346356
),
347357
# The time zone validation tests compare the output of zdump against the

0 commit comments

Comments
 (0)