Skip to content

[Clang] fix crash by avoiding invalidation of extern main declaration during strictness checks #104594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 1 addition & 4 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12230,12 +12230,9 @@ void Sema::CheckMain(FunctionDecl *FD, const DeclSpec &DS) {
// The main function shall not be declared with a linkage-specification.
if (FD->isExternCContext() ||
(FD->isExternCXXContext() &&
FD->getDeclContext()->getRedeclContext()->isTranslationUnit())) {
FD->getDeclContext()->getRedeclContext()->isTranslationUnit()))
Diag(FD->getLocation(), diag::ext_main_invalid_linkage_specification)
<< FD->getLanguageLinkage();
FD->setInvalidDecl();
return;
}

// C++11 [basic.start.main]p3:
// A program that [...] declares main to be inline, static or
Expand Down
28 changes: 21 additions & 7 deletions clang/test/CXX/basic/basic.start/basic.start.main/p3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
// RUN: %clang_cc1 -fsyntax-only -verify -pedantic %s -DTEST11
// RUN: %clang_cc1 -fsyntax-only -verify -pedantic %s -DTEST12
// RUN: %clang_cc1 -fsyntax-only -verify -pedantic %s -DTEST13
// RUN: %clang_cc1 -fsyntax-only -verify -pedantic %s -DTEST14
// RUN: %clang_cc1 -triple x86_64-linux -emit-llvm-only -verify -pedantic %s -DTEST15

#if TEST1
int main; // expected-error{{main cannot be declared as a variable in the global scope}}
Expand Down Expand Up @@ -78,12 +80,14 @@ namespace ns {
extern "C" struct A { int main(); }; // ok

namespace c {
extern "C" void main(); // expected-warning {{'main' should not be 'extern "C"'}}
extern "C" void main(); // expected-error {{'main' must return 'int'}} \
// expected-warning {{'main' should not be 'extern "C"'}}
}

extern "C" {
namespace Z {
void main(); // expected-warning {{'main' should not be 'extern "C"'}}
void main(); // expected-error {{'main' must return 'int'}} \
// expected-warning {{'main' should not be 'extern "C"'}}
}
}

Expand All @@ -102,11 +106,6 @@ extern "C++" {
int main(); // expected-warning {{'main' should not be 'extern "C++"'}}
}

extern "C" {
int main(); // expected-warning {{'main' should not be 'extern "C"'}}
}

extern "C" int main(); // expected-warning {{'main' should not be 'extern "C"'}}
extern "C++" int main(); // expected-warning {{'main' should not be 'extern "C++"'}}

namespace ns1 {
Expand All @@ -122,6 +121,21 @@ namespace ns2 {
extern "C++" void main() {} // ok
}

#elif TEST14
extern "C" {
int main(); // expected-warning {{'main' should not be 'extern "C"'}}
}

extern "C" int main(); // expected-warning {{'main' should not be 'extern "C"'}}

#elif TEST15
extern "C" __attribute__((visibility("default"))) __attribute__((weak))
int main(); // expected-warning {{'main' should not be 'extern "C"'}}

unsigned long g() {
return reinterpret_cast<unsigned long>(&main); // expected-warning {{referring to 'main' within an expression is a Clang extension}}
}

#else
#error Unknown Test
#endif
Loading