[clang] [modules] Add err_main_in_named_module (#146635)
Revival of https://github.com/llvm/llvm-project/pull/146247 which got reverted for broken test. Now that https://github.com/llvm/llvm-project/pull/146461 is merged to allow `extern "C++"` for main, we can merge this change.
This commit is contained in:
parent
13fddeaf2c
commit
cc801b6570
@ -661,6 +661,9 @@ Improvements to Clang's diagnostics
|
||||
diagnostics when floating-point numbers had both width field and plus or space
|
||||
prefix specified. (#GH143951)
|
||||
|
||||
- A warning is now emitted when ``main`` is attached to a named module,
|
||||
which can be turned off with ``-Wno-main-attached-to-named-module``. (#GH146247)
|
||||
|
||||
- Clang now avoids issuing `-Wreturn-type` warnings in some cases where
|
||||
the final statement of a non-void function is a `throw` expression, or
|
||||
a call to a function that is trivially known to always throw (i.e., its
|
||||
|
@ -1062,6 +1062,9 @@ 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
|
||||
: ExtWarn<"'main' never has module linkage">,
|
||||
InGroup<DiagGroup<"main-attached-to-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>;
|
||||
|
@ -12486,6 +12486,15 @@ void Sema::CheckMain(FunctionDecl *FD, const DeclSpec &DS) {
|
||||
: FixItHint());
|
||||
FD->setInvalidDecl(true);
|
||||
}
|
||||
|
||||
// [basic.start.main]p3:
|
||||
// A program that declares a function main that belongs to the global scope
|
||||
// and is attached to a named module is ill-formed.
|
||||
if (FD->isInNamedModule()) {
|
||||
const SourceLocation start = FD->getTypeSpecStartLoc();
|
||||
Diag(start, diag::warn_main_in_named_module)
|
||||
<< FixItHint::CreateInsertion(start, "extern \"C++\" ", true);
|
||||
}
|
||||
}
|
||||
|
||||
// Treat protoless main() as nullary.
|
||||
|
@ -111,6 +111,7 @@
|
||||
// RUN: %clang --autocomplete=-Wma | FileCheck %s -check-prefix=WARNING
|
||||
// WARNING: -Wmacro-redefined
|
||||
// WARNING-NEXT: -Wmain
|
||||
// WARNING-NEXT: -Wmain-attached-to-named-module
|
||||
// WARNING-NEXT: -Wmain-return-type
|
||||
// WARNING-NEXT: -Wmalformed-warning-check
|
||||
// WARNING-NEXT: -Wmany-braces-around-scalar-init
|
||||
|
@ -41,6 +41,8 @@ struct S {
|
||||
export static int n; // expected-error {{expected member name or ';'}}
|
||||
};
|
||||
|
||||
int main() {} // expected-warning {{'main' never has module linkage}}
|
||||
|
||||
// FIXME: Exports of declarations without external linkage are disallowed.
|
||||
// Exports of declarations with non-external-linkage types are disallowed.
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user