-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang] [modules] Implement P3618R0: Allow attaching main to the global module #146461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
@llvm/pr-subscribers-clang @llvm/pr-subscribers-libcxx Author: Ashwin Banwari (ashwinbanwari) ChangesRemove the prior warning for attaching extern "C++" to main. Full diff: https://github.com/llvm/llvm-project/pull/146461.diff 5 Files Affected:
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index e6c8f9df22170..33dd07179aeea 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -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
^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 5062505cf3c01..d5d982d20eaa5 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1086,7 +1086,7 @@ 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>;
+ "'extern \"C\"'">, InGroup<Main>;
/// parser diagnostics
def ext_no_declarators : ExtWarn<"declaration does not declare anything">,
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index a34e2c9cbb003..61b82b8377e22 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -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
diff --git a/clang/test/SemaCXX/modules.cppm b/clang/test/SemaCXX/modules.cppm
index 5d0d6da44a2ed..ddbbc7cd86360 100644
--- a/clang/test/SemaCXX/modules.cppm
+++ b/clang/test/SemaCXX/modules.cppm
@@ -68,6 +68,8 @@ int n;
//--- test3.cpp
export module bar;
+extern "C++" int main() {}
+
static int m;
int n;
diff --git a/libcxx/utils/libcxx/test/features.py b/libcxx/utils/libcxx/test/features.py
index 74746e37d3bc4..a479ea48ac810 100644
--- a/libcxx/utils/libcxx/test/features.py
+++ b/libcxx/utils/libcxx/test/features.py
@@ -340,7 +340,7 @@ def _mingwSupportsModules(cfg):
cfg,
"""
export module test;
- int main(int, char**) { return 0; }
+ extern "C++" int main(int, char**) { return 0; }
""",
),
),
|
CC @ChuanqiXu9 for review |
✅ With the latest revision this PR passed the Python code formatter. |
// The main function shall not be declared with C linkage-specification. | ||
if (FD->isExternCContext()) | ||
Diag(FD->getLocation(), diag::ext_main_invalid_linkage_specification); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. I'll merge this after the CI gets green.
Please update "clang/www/cxx_status.html". |
Remove the prior warning for attaching extern "C++" to main.