Skip to content

Commit cc801b6

Browse files
authored
[clang] [modules] Add err_main_in_named_module (#146635)
Revival of #146247 which got reverted for broken test. Now that #146461 is merged to allow `extern "C++"` for main, we can merge this change.
1 parent 13fddea commit cc801b6

File tree

5 files changed

+18
-0
lines changed

5 files changed

+18
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,9 @@ Improvements to Clang's diagnostics
661661
diagnostics when floating-point numbers had both width field and plus or space
662662
prefix specified. (#GH143951)
663663

664+
- A warning is now emitted when ``main`` is attached to a named module,
665+
which can be turned off with ``-Wno-main-attached-to-named-module``. (#GH146247)
666+
664667
- Clang now avoids issuing `-Wreturn-type` warnings in some cases where
665668
the final statement of a non-void function is a `throw` expression, or
666669
a call to a function that is trivially known to always throw (i.e., its

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,9 @@ def err_constexpr_main : Error<
10621062
"'main' is not allowed to be declared %select{constexpr|consteval}0">;
10631063
def err_deleted_main : Error<"'main' is not allowed to be deleted">;
10641064
def err_mainlike_template_decl : Error<"%0 cannot be a template">;
1065+
def warn_main_in_named_module
1066+
: ExtWarn<"'main' never has module linkage">,
1067+
InGroup<DiagGroup<"main-attached-to-named-module">>;
10651068
def err_main_returns_nonint : Error<"'main' must return 'int'">;
10661069
def ext_main_returns_nonint : ExtWarn<"return type of 'main' is not 'int'">,
10671070
InGroup<MainReturnType>;

clang/lib/Sema/SemaDecl.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12486,6 +12486,15 @@ void Sema::CheckMain(FunctionDecl *FD, const DeclSpec &DS) {
1248612486
: FixItHint());
1248712487
FD->setInvalidDecl(true);
1248812488
}
12489+
12490+
// [basic.start.main]p3:
12491+
// A program that declares a function main that belongs to the global scope
12492+
// and is attached to a named module is ill-formed.
12493+
if (FD->isInNamedModule()) {
12494+
const SourceLocation start = FD->getTypeSpecStartLoc();
12495+
Diag(start, diag::warn_main_in_named_module)
12496+
<< FixItHint::CreateInsertion(start, "extern \"C++\" ", true);
12497+
}
1248912498
}
1249012499

1249112500
// Treat protoless main() as nullary.

clang/test/Driver/autocomplete.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
// RUN: %clang --autocomplete=-Wma | FileCheck %s -check-prefix=WARNING
112112
// WARNING: -Wmacro-redefined
113113
// WARNING-NEXT: -Wmain
114+
// WARNING-NEXT: -Wmain-attached-to-named-module
114115
// WARNING-NEXT: -Wmain-return-type
115116
// WARNING-NEXT: -Wmalformed-warning-check
116117
// WARNING-NEXT: -Wmany-braces-around-scalar-init

clang/test/SemaCXX/modules.cppm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ struct S {
4141
export static int n; // expected-error {{expected member name or ';'}}
4242
};
4343

44+
int main() {} // expected-warning {{'main' never has module linkage}}
45+
4446
// FIXME: Exports of declarations without external linkage are disallowed.
4547
// Exports of declarations with non-external-linkage types are disallowed.
4648

0 commit comments

Comments
 (0)