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

via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 16 15:01:37 PDT 2024


Author: Oleksandr T.
Date: 2024-08-17T00:01:33+02:00
New Revision: e4f3735d5f600b17b8f86956162d41ce82096685

URL: https://github.com/llvm/llvm-project/commit/e4f3735d5f600b17b8f86956162d41ce82096685
DIFF: https://github.com/llvm/llvm-project/commit/e4f3735d5f600b17b8f86956162d41ce82096685.diff

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

Fixes #104570

Added: 
    

Modified: 
    clang/lib/Sema/SemaDecl.cpp
    clang/test/CXX/basic/basic.start/basic.start.main/p3.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index d19a16cf2ba150..1d85341bc9a8b6 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -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

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..497a9328c11561 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,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"'}}
   }
 }
 
@@ -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 {
@@ -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


        


More information about the cfe-commits mailing list