Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,10 @@ 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' should not be attached to a named module; consider "
"adding C++ language linkage">,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just `'main' never has module linkage;" the "Consider..." is somewhat redundant with the fixit

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>;
Expand Down
8 changes: 8 additions & 0 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12486,6 +12486,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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: generally we we'll cite it directly:

Suggested change
// In C++ [basic.start.main]p3, it is said a program attaching main to a
// named module is ill-formed.
// [basic.start.main]p3:
//
// ....

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.
Expand Down
1 change: 1 addition & 0 deletions clang/test/Driver/autocomplete.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions clang/test/SemaCXX/modules.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ struct S {
export static int n; // expected-error {{expected member name or ';'}}
};

int main() {} // expected-warning {{'main' should not be attached to a named module; consider adding C++ language linkage}}

// FIXME: Exports of declarations without external linkage are disallowed.
// Exports of declarations with non-external-linkage types are disallowed.

Expand Down
Loading