https://github.com/a-tarasyuk updated https://github.com/llvm/llvm-project/pull/104594
>From 9ffe47795d3fa25d6ac020b5cebace0c2e758285 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 1/2] [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..0d2c5fbb37afe7 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 -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}} @@ -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 >From 0ea146acc97e2761b401cca5f9b664d39d2a0e9f Mon Sep 17 00:00:00 2001 From: Oleksandr T <oleksandr.taras...@outlook.com> Date: Fri, 16 Aug 2024 18:08:40 +0300 Subject: [PATCH 2/2] cleanup --- clang/docs/ReleaseNotes.rst | 1 - clang/lib/Sema/SemaDecl.cpp | 4 +--- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 1731100fdbcf87..ffdd063ec99037 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -264,7 +264,6 @@ 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 5bba9c1750287d..1d85341bc9a8b6 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -12230,11 +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(); - return; - } // C++11 [basic.start.main]p3: // A program that [...] declares main to be inline, static or _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits