-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
base: main
Are you sure you want to change the base?
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. |
Close #146229
As the issue said, main shouldn't be in any modules.
new error output: