https://github.com/a-tarasyuk created https://github.com/llvm/llvm-project/pull/104594
Fixes #104570 >From e1821933a1670353c396ada17b82d5bb41599c14 Mon Sep 17 00:00:00 2001 From: Oleksandr T <oleksandr.taras...@outlook.com> Date: Fri, 16 Aug 2024 17:21:38 +0300 Subject: [PATCH] [Clang] fix crash by avoiding invalidation of extern main declaration during strictness checks --- clang/docs/ReleaseNotes.rst | 1 + clang/lib/Sema/SemaDecl.cpp | 1 - .../basic/basic.start/basic.start.main/p3.cpp | 31 ++++++++++++++----- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index ffdd063ec99037..1731100fdbcf87 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -264,6 +264,7 @@ Bug Fixes to C++ Support - Properly reject defaulted copy/move assignment operators that have a non-reference explicit object parameter. - Clang now properly handles the order of attributes in `extern` blocks. (#GH101990). - Fixed an assertion failure by preventing null explicit object arguments from being deduced. (#GH102025). +- Fixed a crash caused by marking the function declaration as invalid during strictness checks on ``extern`` main. (#GH104570). Bug Fixes to AST Handling ^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index d19a16cf2ba150..5bba9c1750287d 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -12233,7 +12233,6 @@ void Sema::CheckMain(FunctionDecl *FD, const DeclSpec &DS) { FD->getDeclContext()->getRedeclContext()->isTranslationUnit())) { Diag(FD->getLocation(), diag::ext_main_invalid_linkage_specification) << FD->getLanguageLinkage(); - FD->setInvalidDecl(); return; } diff --git a/clang/test/CXX/basic/basic.start/basic.start.main/p3.cpp b/clang/test/CXX/basic/basic.start/basic.start.main/p3.cpp index d23d00ccdeebc1..04705a6a365905 100644 --- a/clang/test/CXX/basic/basic.start/basic.start.main/p3.cpp +++ b/clang/test/CXX/basic/basic.start/basic.start.main/p3.cpp @@ -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 -emit-llvm-only -verify -pedantic %s -DTEST15 #if TEST1 int main; // expected-error{{main cannot be declared as a variable in the global scope}} @@ -78,12 +80,12 @@ 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" int main(); // expected-warning {{'main' should not be 'extern "C"'}} } extern "C" { namespace Z { - void main(); // expected-warning {{'main' should not be 'extern "C"'}} + int main(); // expected-warning {{'main' should not be 'extern "C"'}} } } @@ -102,11 +104,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 { @@ -122,6 +119,26 @@ 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"'}} + +using uptr = unsigned long; + +extern bool f(uptr, uptr); +bool InitModuleList() { + uptr me = reinterpret_cast<uptr>(InitModuleList); + uptr exe = reinterpret_cast<uptr>(&main); // expected-warning {{referring to 'main' within an expression is a Clang extension}} + return f(me, exe); +} + #else #error Unknown Test #endif _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits