-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[clang] [modules] Add err_main_in_named_module #146247
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
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-clang Author: Ashwin Banwari (ashwinbanwari) ChangesClose #146229 As the issue said, main shouldn't be in any modules. New Error Output:
Full diff: https://github.com/llvm/llvm-project/pull/146247.diff 2 Files Affected:
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.
|
You should add a test case and a release note for this patch to be accepted. |
It seems that clang trunk warns on I think it will be helpful for the UX to land this change after that warning is fixed. (Disclaimer: I'm not clang contributor, just clang user) |
I prefer a warning instead of hard error. There are already users who uses this. |
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.
LGTM. Thanks.
@@ -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 warn_main_in_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.
Let's add a warning option for it.
@@ -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 warn_main_in_named_module | |||
: Warning<"'main' should not be attached to 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.
And suggest them to wrap it into language linkage.
clang/docs/ReleaseNotes.rst
Outdated
@@ -650,6 +650,9 @@ Improvements to Clang's diagnostics | |||
#GH69470, #GH59391, #GH58172, #GH46215, #GH45915, #GH45891, #GH44490, | |||
#GH36703, #GH32903, #GH23312, #GH69874. | |||
|
|||
- A warning is now emitted when ``main`` is attached to a named module, | |||
which can be turned off with ``-Wno-main``. (#GH146247) |
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 prefer to change the warning name to Wmain-attached-to-named-modules
. There were a lot examples declaring a warning with a warning name in place.
Please use a new email name. We don't accept github.noreply mail. |
@ashwinbanwari Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/10/builds/8364 Here is the relevant piece of the build log for the reference
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/144/builds/28925 Here is the relevant piece of the build log for the reference
|
It seems I need to add the line What is the process to amend this PR? |
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/190/builds/22459 Here is the relevant piece of the build log for the reference
|
I think you can open a new PR that fixes the issue. |
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/153/builds/36443 Here is the relevant piece of the build log for the reference
|
FWIW, I fixed it in 5186d4a to avoid affecting more people longer. |
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/56/builds/29711 Here is the relevant piece of the build log for the reference
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/60/builds/31648 Here is the relevant piece of the build log for the reference
|
Hello @ashwinbanwari With this patch I get
any idea what's happening? |
Hi, can you take a look at the buildbot failure? |
Didn't know it. I'll revert this. @ashwinbanwari it looks like you need to remove the diagnostics with |
…46247)" This reverts commit 473769e. It breaks test in libc++ See llvm/llvm-project#146247
Close llvm#146229 As the issue said, main shouldn't be in any modules. new diagnostic output: ``` /my/code/directory/main.cpp:3:1: warning: 'main' should not be attached to a named module; consider adding C++ language linkage [-Wmain] 3 | int main() { | ^ | extern "C++" 1 warning generated. ```
This reverts commit 473769e. It breaks test in libc++ See llvm#146247
Close llvm#146229 As the issue said, main shouldn't be in any modules. new diagnostic output: ``` /my/code/directory/main.cpp:3:1: warning: 'main' should not be attached to a named module; consider adding C++ language linkage [-Wmain] 3 | int main() { | ^ | extern "C++" 1 warning generated. ```
This reverts commit 473769e. It breaks test in libc++ See llvm#146247
This reverts commit 8a5b97a.
Revival of llvm/llvm-project#146247 which got reverted for broken test. Now that llvm/llvm-project#146461 is merged to allow `extern "C++"` for main, we can merge this change.
Close #146229
As the issue said, main shouldn't be in any modules.
new diagnostic output: