Skip to content
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,8 @@ 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. (#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
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

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.

: Warning<"'main' should not be attached to a named module">;
Copy link
Member

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.

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
6 changes: 6 additions & 0 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12489,6 +12489,12 @@ 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::warn_main_in_named_module);
}
}

// Treat protoless main() as nullary.
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 @@ -68,6 +68,8 @@ int n;
//--- test3.cpp
export module bar;

int main() {} // expected-warning {{'main' should not be attached to a named module}}

static int m;

int n;
Expand Down