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

via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 16 07:25:40 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Oleksandr T. (a-tarasyuk)

<details>
<summary>Changes</summary>

Fixes #<!-- -->104570

---
Full diff: https://github.com/llvm/llvm-project/pull/104594.diff


3 Files Affected:

- (modified) clang/docs/ReleaseNotes.rst (+1) 
- (modified) clang/lib/Sema/SemaDecl.cpp (-1) 
- (modified) clang/test/CXX/basic/basic.start/basic.start.main/p3.cpp (+24-7) 


``````````diff
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

``````````

</details>


https://github.com/llvm/llvm-project/pull/104594


More information about the cfe-commits mailing list