Skip to content

Commit 473769e

Browse files
authored
[clang] [modules] Add err_main_in_named_module (#146247)
Close #146229 As the issue said, main shouldn't be in any modules. new diagnostic output: ``` /my/code/directory/main.cpp:3:1: warning: 'main' should not be attached to a named module; consider adding C++ language linkage [-Wmain] 3 | int main() { | ^ | extern "C++" 1 warning generated. ```
1 parent b42c883 commit 473769e

File tree

4 files changed

+17
-0
lines changed

4 files changed

+17
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,9 @@ Improvements to Clang's diagnostics
650650
#GH69470, #GH59391, #GH58172, #GH46215, #GH45915, #GH45891, #GH44490,
651651
#GH36703, #GH32903, #GH23312, #GH69874.
652652

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

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,10 @@ 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' should not be attached to a named module; consider "
1067+
"adding C++ language linkage">,
1068+
InGroup<DiagGroup<"main-attached-to-named-module">>;
10651069
def err_main_returns_nonint : Error<"'main' must return 'int'">;
10661070
def ext_main_returns_nonint : ExtWarn<"return type of 'main' is not 'int'">,
10671071
InGroup<MainReturnType>;

clang/lib/Sema/SemaDecl.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12489,6 +12489,14 @@ void Sema::CheckMain(FunctionDecl *FD, const DeclSpec &DS) {
1248912489
: FixItHint());
1249012490
FD->setInvalidDecl(true);
1249112491
}
12492+
12493+
// In C++ [basic.start.main]p3, it is said a program attaching main to a
12494+
// named module is ill-formed.
12495+
if (FD->isInNamedModule()) {
12496+
const SourceLocation start = FD->getTypeSpecStartLoc();
12497+
Diag(start, diag::warn_main_in_named_module)
12498+
<< FixItHint::CreateInsertion(start, "extern \"C++\" ", true);
12499+
}
1249212500
}
1249312501

1249412502
// Treat protoless main() as nullary.

clang/test/SemaCXX/modules.cppm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ int n;
6868
//--- test3.cpp
6969
export module bar;
7070

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

7375
int n;

0 commit comments

Comments
 (0)