Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^^^^^^^^^^^^^^^

Expand Down
6 changes: 3 additions & 3 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -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">,
Expand Down
9 changes: 3 additions & 6 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Comment on lines +12403 to +12405
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel this is not right. We should avoid emitting the diagnostics if it is in a named module.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to proposal, extern "C" for main is still ill-formed regardless if it is named module or not. On other hand, extern "C++" is allowed whether in named module or not.


// C++11 [basic.start.main]p3:
// A program that [...] declares main to be inline, static or
Expand Down
5 changes: 3 additions & 2 deletions clang/test/CXX/basic/basic.start/basic.start.main/p3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions clang/test/SemaCXX/modules.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ int n;
//--- test3.cpp
export module bar;

extern "C++" int main() {}

static int m;

int n;
Expand Down
16 changes: 13 additions & 3 deletions libcxx/utils/libcxx/test/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,22 @@ def _mingwSupportsModules(cfg):
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
Expand Down
Loading