[clang] Objective C: use C++ exceptions on MinGW+GNUstep (PR #77255)

2024-01-07 Thread Frederik Carlier via cfe-commits
https://github.com/qmfrederik created 
https://github.com/llvm/llvm-project/pull/77255

The GNUstep Objective C runtime (libobjc2) is adding support for MinGW.  This 
runtime uses C++ exceptions in that configuration.

>From 4cbc22e9b7c53547b97c666b24f191b25ae8866f Mon Sep 17 00:00:00 2001
From: Frederik Carlier 
Date: Thu, 4 Jan 2024 11:10:05 -0800
Subject: [PATCH] Objective C: use C++ exceptions on MinGW+GNUstep

The GNUstep Objective C runtime (libobjc2) is adding support for
MinGW.  This runtime uses C++ exceptions in that configuration.
---
 clang/lib/CodeGen/CGException.cpp | 18 ---
 clang/lib/CodeGen/CGObjCGNU.cpp   | 20 +--
 .../test/CodeGenObjC/exceptions-personality.m | 53 +++
 3 files changed, 80 insertions(+), 11 deletions(-)
 create mode 100644 clang/test/CodeGenObjC/exceptions-personality.m

diff --git a/clang/lib/CodeGen/CGException.cpp 
b/clang/lib/CodeGen/CGException.cpp
index 0d507da5c1ba92..939f7962dcc635 100644
--- a/clang/lib/CodeGen/CGException.cpp
+++ b/clang/lib/CodeGen/CGException.cpp
@@ -145,8 +145,6 @@ static const EHPersonality &getCPersonality(const 
TargetInfo &Target,
 static const EHPersonality &getObjCPersonality(const TargetInfo &Target,
const LangOptions &L) {
   const llvm::Triple &T = Target.getTriple();
-  if (T.isWindowsMSVCEnvironment())
-return EHPersonality::MSVC_CxxFrameHandler3;
 
   switch (L.ObjCRuntime.getKind()) {
   case ObjCRuntime::FragileMacOSX:
@@ -156,7 +154,11 @@ static const EHPersonality &getObjCPersonality(const 
TargetInfo &Target,
   case ObjCRuntime::WatchOS:
 return EHPersonality::NeXT_ObjC;
   case ObjCRuntime::GNUstep:
-if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
+if (T.isOSCygMing())
+  return EHPersonality::GNU_CPlusPlus_SEH;
+else if (T.isWindowsMSVCEnvironment())
+  return EHPersonality::MSVC_CxxFrameHandler3;
+else if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
   return EHPersonality::GNUstep_ObjC;
 [[fallthrough]];
   case ObjCRuntime::GCC:
@@ -192,9 +194,6 @@ static const EHPersonality &getCXXPersonality(const 
TargetInfo &Target,
 /// and Objective-C exceptions are being caught.
 static const EHPersonality &getObjCXXPersonality(const TargetInfo &Target,
  const LangOptions &L) {
-  if (Target.getTriple().isWindowsMSVCEnvironment())
-return EHPersonality::MSVC_CxxFrameHandler3;
-
   switch (L.ObjCRuntime.getKind()) {
   // In the fragile ABI, just use C++ exception handling and hope
   // they're not doing crazy exception mixing.
@@ -210,7 +209,12 @@ static const EHPersonality &getObjCXXPersonality(const 
TargetInfo &Target,
 return getObjCPersonality(Target, L);
 
   case ObjCRuntime::GNUstep:
-return EHPersonality::GNU_ObjCXX;
+if (Target.getTriple().isWindowsMSVCEnvironment())
+  return EHPersonality::MSVC_CxxFrameHandler3;
+else if (Target.getTriple().isOSCygMing())
+  return EHPersonality::GNU_CPlusPlus_SEH;
+else
+  return EHPersonality::GNU_ObjCXX;
 
   // The GCC runtime's personality function inherently doesn't support
   // mixed EH.  Use the ObjC personality just to avoid returning null.
diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp
index 4ca1a8cce64d89..5ace469c927045 100644
--- a/clang/lib/CodeGen/CGObjCGNU.cpp
+++ b/clang/lib/CodeGen/CGObjCGNU.cpp
@@ -819,7 +819,15 @@ class CGObjCGNUstep : public CGObjCGNU {
   SlotLookupSuperFn.init(&CGM, "objc_slot_lookup_super", SlotTy,
  PtrToObjCSuperTy, SelectorTy);
   // If we're in ObjC++ mode, then we want to make
-  if (usesSEHExceptions) {
+  if (CGM.getTarget().getTriple().isOSCygMing() && 
isRuntime(ObjCRuntime::GNUstep, 2)) {
+llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
+// void *__cxa_begin_catch(void *e)
+EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy);
+// void __cxa_end_catch(void)
+ExitCatchFn.init(&CGM, "__cxa_end_catch", VoidTy);
+// void objc_exception_rethrow(void*)
+ExceptionReThrowFn.init(&CGM, "__cxa_rethrow", PtrTy);
+  } else if (usesSEHExceptions) {
   llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
   // void objc_exception_rethrow(void)
   ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
@@ -2210,7 +2218,11 @@ CGObjCGNU::CGObjCGNU(CodeGenModule &cgm, unsigned 
runtimeABIVersion,
 
   // void objc_exception_throw(id);
   ExceptionThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
-  ExceptionReThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
+  if ((CGM.getTarget().getTriple().isOSCygMing() && 
isRuntime(ObjCRuntime::GNUstep, 2))) {
+ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy, IdTy);
+  } else {
+ExceptionReThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
+  }
   // int o

[clang] Objective C: use C++ exceptions on MinGW+GNUstep (PR #77255)

2024-01-07 Thread Frederik Carlier via cfe-commits
https://github.com/qmfrederik updated 
https://github.com/llvm/llvm-project/pull/77255

>From 62d7da8476b786f7950ea6bb8d6ac223fc6d0468 Mon Sep 17 00:00:00 2001
From: Frederik Carlier 
Date: Thu, 4 Jan 2024 11:10:05 -0800
Subject: [PATCH] Objective C: use C++ exceptions on MinGW+GNUstep

The GNUstep Objective C runtime (libobjc2) is adding support for
MinGW.  This runtime uses C++ exceptions in that configuration.
---
 clang/lib/CodeGen/CGException.cpp | 18 ---
 clang/lib/CodeGen/CGObjCGNU.cpp   | 34 
 .../test/CodeGenObjC/exceptions-personality.m | 53 +++
 3 files changed, 89 insertions(+), 16 deletions(-)
 create mode 100644 clang/test/CodeGenObjC/exceptions-personality.m

diff --git a/clang/lib/CodeGen/CGException.cpp 
b/clang/lib/CodeGen/CGException.cpp
index 0d507da5c1ba92..939f7962dcc635 100644
--- a/clang/lib/CodeGen/CGException.cpp
+++ b/clang/lib/CodeGen/CGException.cpp
@@ -145,8 +145,6 @@ static const EHPersonality &getCPersonality(const 
TargetInfo &Target,
 static const EHPersonality &getObjCPersonality(const TargetInfo &Target,
const LangOptions &L) {
   const llvm::Triple &T = Target.getTriple();
-  if (T.isWindowsMSVCEnvironment())
-return EHPersonality::MSVC_CxxFrameHandler3;
 
   switch (L.ObjCRuntime.getKind()) {
   case ObjCRuntime::FragileMacOSX:
@@ -156,7 +154,11 @@ static const EHPersonality &getObjCPersonality(const 
TargetInfo &Target,
   case ObjCRuntime::WatchOS:
 return EHPersonality::NeXT_ObjC;
   case ObjCRuntime::GNUstep:
-if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
+if (T.isOSCygMing())
+  return EHPersonality::GNU_CPlusPlus_SEH;
+else if (T.isWindowsMSVCEnvironment())
+  return EHPersonality::MSVC_CxxFrameHandler3;
+else if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
   return EHPersonality::GNUstep_ObjC;
 [[fallthrough]];
   case ObjCRuntime::GCC:
@@ -192,9 +194,6 @@ static const EHPersonality &getCXXPersonality(const 
TargetInfo &Target,
 /// and Objective-C exceptions are being caught.
 static const EHPersonality &getObjCXXPersonality(const TargetInfo &Target,
  const LangOptions &L) {
-  if (Target.getTriple().isWindowsMSVCEnvironment())
-return EHPersonality::MSVC_CxxFrameHandler3;
-
   switch (L.ObjCRuntime.getKind()) {
   // In the fragile ABI, just use C++ exception handling and hope
   // they're not doing crazy exception mixing.
@@ -210,7 +209,12 @@ static const EHPersonality &getObjCXXPersonality(const 
TargetInfo &Target,
 return getObjCPersonality(Target, L);
 
   case ObjCRuntime::GNUstep:
-return EHPersonality::GNU_ObjCXX;
+if (Target.getTriple().isWindowsMSVCEnvironment())
+  return EHPersonality::MSVC_CxxFrameHandler3;
+else if (Target.getTriple().isOSCygMing())
+  return EHPersonality::GNU_CPlusPlus_SEH;
+else
+  return EHPersonality::GNU_ObjCXX;
 
   // The GCC runtime's personality function inherently doesn't support
   // mixed EH.  Use the ObjC personality just to avoid returning null.
diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp
index 4ca1a8cce64d89..3165fabf00a2e7 100644
--- a/clang/lib/CodeGen/CGObjCGNU.cpp
+++ b/clang/lib/CodeGen/CGObjCGNU.cpp
@@ -819,10 +819,19 @@ class CGObjCGNUstep : public CGObjCGNU {
   SlotLookupSuperFn.init(&CGM, "objc_slot_lookup_super", SlotTy,
  PtrToObjCSuperTy, SelectorTy);
   // If we're in ObjC++ mode, then we want to make
-  if (usesSEHExceptions) {
-  llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
-  // void objc_exception_rethrow(void)
-  ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
+  if (CGM.getTarget().getTriple().isOSCygMing() &&
+  isRuntime(ObjCRuntime::GNUstep, 2)) {
+llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
+// void *__cxa_begin_catch(void *e)
+EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy);
+// void __cxa_end_catch(void)
+ExitCatchFn.init(&CGM, "__cxa_end_catch", VoidTy);
+// void objc_exception_rethrow(void*)
+ExceptionReThrowFn.init(&CGM, "__cxa_rethrow", PtrTy);
+  } else if (usesSEHExceptions) {
+llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
+// void objc_exception_rethrow(void)
+ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
   } else if (CGM.getLangOpts().CPlusPlus) {
 llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
 // void *__cxa_begin_catch(void *e)
@@ -2210,7 +2219,11 @@ CGObjCGNU::CGObjCGNU(CodeGenModule &cgm, unsigned 
runtimeABIVersion,
 
   // void objc_exception_throw(id);
   ExceptionThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
-  ExceptionReThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
+  if ((CGM.getTarget().getTriple().isOSCygM

[clang] Objective C: use C++ exceptions on MinGW+GNUstep (PR #77255)

2024-01-07 Thread Frederik Carlier via cfe-commits
https://github.com/qmfrederik updated 
https://github.com/llvm/llvm-project/pull/77255

>From d04a41177e9c39799b73168d397dd87fb948caa1 Mon Sep 17 00:00:00 2001
From: Frederik Carlier 
Date: Thu, 4 Jan 2024 11:10:05 -0800
Subject: [PATCH] Objective C: use C++ exceptions on MinGW+GNUstep

The GNUstep Objective C runtime (libobjc2) is adding support for
MinGW.  This runtime uses C++ exceptions in that configuration.
---
 clang/lib/CodeGen/CGException.cpp | 18 ---
 clang/lib/CodeGen/CGObjCGNU.cpp   | 35 
 .../test/CodeGenObjC/exceptions-personality.m | 53 +++
 3 files changed, 90 insertions(+), 16 deletions(-)
 create mode 100644 clang/test/CodeGenObjC/exceptions-personality.m

diff --git a/clang/lib/CodeGen/CGException.cpp 
b/clang/lib/CodeGen/CGException.cpp
index 0d507da5c1ba92..939f7962dcc635 100644
--- a/clang/lib/CodeGen/CGException.cpp
+++ b/clang/lib/CodeGen/CGException.cpp
@@ -145,8 +145,6 @@ static const EHPersonality &getCPersonality(const 
TargetInfo &Target,
 static const EHPersonality &getObjCPersonality(const TargetInfo &Target,
const LangOptions &L) {
   const llvm::Triple &T = Target.getTriple();
-  if (T.isWindowsMSVCEnvironment())
-return EHPersonality::MSVC_CxxFrameHandler3;
 
   switch (L.ObjCRuntime.getKind()) {
   case ObjCRuntime::FragileMacOSX:
@@ -156,7 +154,11 @@ static const EHPersonality &getObjCPersonality(const 
TargetInfo &Target,
   case ObjCRuntime::WatchOS:
 return EHPersonality::NeXT_ObjC;
   case ObjCRuntime::GNUstep:
-if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
+if (T.isOSCygMing())
+  return EHPersonality::GNU_CPlusPlus_SEH;
+else if (T.isWindowsMSVCEnvironment())
+  return EHPersonality::MSVC_CxxFrameHandler3;
+else if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
   return EHPersonality::GNUstep_ObjC;
 [[fallthrough]];
   case ObjCRuntime::GCC:
@@ -192,9 +194,6 @@ static const EHPersonality &getCXXPersonality(const 
TargetInfo &Target,
 /// and Objective-C exceptions are being caught.
 static const EHPersonality &getObjCXXPersonality(const TargetInfo &Target,
  const LangOptions &L) {
-  if (Target.getTriple().isWindowsMSVCEnvironment())
-return EHPersonality::MSVC_CxxFrameHandler3;
-
   switch (L.ObjCRuntime.getKind()) {
   // In the fragile ABI, just use C++ exception handling and hope
   // they're not doing crazy exception mixing.
@@ -210,7 +209,12 @@ static const EHPersonality &getObjCXXPersonality(const 
TargetInfo &Target,
 return getObjCPersonality(Target, L);
 
   case ObjCRuntime::GNUstep:
-return EHPersonality::GNU_ObjCXX;
+if (Target.getTriple().isWindowsMSVCEnvironment())
+  return EHPersonality::MSVC_CxxFrameHandler3;
+else if (Target.getTriple().isOSCygMing())
+  return EHPersonality::GNU_CPlusPlus_SEH;
+else
+  return EHPersonality::GNU_ObjCXX;
 
   // The GCC runtime's personality function inherently doesn't support
   // mixed EH.  Use the ObjC personality just to avoid returning null.
diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp
index 4ca1a8cce64d89..2b3a5da87fb778 100644
--- a/clang/lib/CodeGen/CGObjCGNU.cpp
+++ b/clang/lib/CodeGen/CGObjCGNU.cpp
@@ -819,10 +819,19 @@ class CGObjCGNUstep : public CGObjCGNU {
   SlotLookupSuperFn.init(&CGM, "objc_slot_lookup_super", SlotTy,
  PtrToObjCSuperTy, SelectorTy);
   // If we're in ObjC++ mode, then we want to make
-  if (usesSEHExceptions) {
-  llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
-  // void objc_exception_rethrow(void)
-  ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
+  if (CGM.getTarget().getTriple().isOSCygMing() &&
+  isRuntime(ObjCRuntime::GNUstep, 2)) {
+llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
+// void *__cxa_begin_catch(void *e)
+EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy);
+// void __cxa_end_catch(void)
+ExitCatchFn.init(&CGM, "__cxa_end_catch", VoidTy);
+// void objc_exception_rethrow(void*)
+ExceptionReThrowFn.init(&CGM, "__cxa_rethrow", PtrTy);
+  } else if (usesSEHExceptions) {
+llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
+// void objc_exception_rethrow(void)
+ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
   } else if (CGM.getLangOpts().CPlusPlus) {
 llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
 // void *__cxa_begin_catch(void *e)
@@ -2210,7 +2219,12 @@ CGObjCGNU::CGObjCGNU(CodeGenModule &cgm, unsigned 
runtimeABIVersion,
 
   // void objc_exception_throw(id);
   ExceptionThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
-  ExceptionReThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
+  if ((CGM.getTarget().getTriple().isOSCygM

[clang] Objective C: use C++ exceptions on MinGW+GNUstep (PR #77255)

2024-01-07 Thread Frederik Carlier via cfe-commits

@@ -156,7 +154,11 @@ static const EHPersonality &getObjCPersonality(const 
TargetInfo &Target,
   case ObjCRuntime::WatchOS:
 return EHPersonality::NeXT_ObjC;
   case ObjCRuntime::GNUstep:
-if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
+if (T.isOSCygMing())

qmfrederik wrote:

I moved the check for `isWindowsMSVCEnvironment` inside the `GNUstep` case, as 
on Windows we'll be using `MSVC_CxxFrameHandler3` when compiling with GNUstep + 
msvc; `GNU_CPlusPlus_SEH` when compiling with GNUstep + mingw and 
`GNU_ObjC_SEH` when compiling iwth GCC + mingw.

https://github.com/llvm/llvm-project/pull/77255
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Objective C: use C++ exceptions on MinGW+GNUstep (PR #77255)

2024-01-07 Thread Frederik Carlier via cfe-commits
https://github.com/qmfrederik edited 
https://github.com/llvm/llvm-project/pull/77255
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Objective C: use C++ exceptions on MinGW+GNUstep (PR #77255)

2024-01-07 Thread Frederik Carlier via cfe-commits
https://github.com/qmfrederik edited 
https://github.com/llvm/llvm-project/pull/77255
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Objective C: use C++ exceptions on MinGW+GNUstep (PR #77255)

2024-01-07 Thread Frederik Carlier via cfe-commits
qmfrederik wrote:

/cc @davidchisnall 

https://github.com/llvm/llvm-project/pull/77255
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Objective C: use C++ exceptions on MinGW+GNUstep (PR #77255)

2024-01-07 Thread Frederik Carlier via cfe-commits

@@ -156,7 +154,11 @@ static const EHPersonality &getObjCPersonality(const 
TargetInfo &Target,
   case ObjCRuntime::WatchOS:
 return EHPersonality::NeXT_ObjC;
   case ObjCRuntime::GNUstep:
-if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
+if (T.isOSCygMing())

qmfrederik wrote:

OK - this broke some tests, so I'll revert that.

https://github.com/llvm/llvm-project/pull/77255
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Objective C: use C++ exceptions on MinGW+GNUstep (PR #77255)

2024-01-07 Thread Frederik Carlier via cfe-commits
https://github.com/qmfrederik updated 
https://github.com/llvm/llvm-project/pull/77255

>From d3e6d552b2502765eff8faccd20b7566a9362491 Mon Sep 17 00:00:00 2001
From: Frederik Carlier 
Date: Thu, 4 Jan 2024 11:10:05 -0800
Subject: [PATCH] Objective C: use C++ exceptions on MinGW+GNUstep

The GNUstep Objective C runtime (libobjc2) is adding support for
MinGW.  This runtime uses C++ exceptions in that configuration.
---
 clang/lib/CodeGen/CGException.cpp |  9 +++-
 clang/lib/CodeGen/CGObjCGNU.cpp   | 35 
 .../test/CodeGenObjC/exceptions-personality.m | 53 +++
 3 files changed, 86 insertions(+), 11 deletions(-)
 create mode 100644 clang/test/CodeGenObjC/exceptions-personality.m

diff --git a/clang/lib/CodeGen/CGException.cpp 
b/clang/lib/CodeGen/CGException.cpp
index 0d507da5c1ba92..cadd6f6ad75daa 100644
--- a/clang/lib/CodeGen/CGException.cpp
+++ b/clang/lib/CodeGen/CGException.cpp
@@ -156,7 +156,9 @@ static const EHPersonality &getObjCPersonality(const 
TargetInfo &Target,
   case ObjCRuntime::WatchOS:
 return EHPersonality::NeXT_ObjC;
   case ObjCRuntime::GNUstep:
-if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
+if (T.isOSCygMing())
+  return EHPersonality::GNU_CPlusPlus_SEH;
+else if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
   return EHPersonality::GNUstep_ObjC;
 [[fallthrough]];
   case ObjCRuntime::GCC:
@@ -210,7 +212,10 @@ static const EHPersonality &getObjCXXPersonality(const 
TargetInfo &Target,
 return getObjCPersonality(Target, L);
 
   case ObjCRuntime::GNUstep:
-return EHPersonality::GNU_ObjCXX;
+if (Target.getTriple().isOSCygMing())
+  return EHPersonality::GNU_CPlusPlus_SEH;
+else
+  return EHPersonality::GNU_ObjCXX;
 
   // The GCC runtime's personality function inherently doesn't support
   // mixed EH.  Use the ObjC personality just to avoid returning null.
diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp
index 4ca1a8cce64d89..2b3a5da87fb778 100644
--- a/clang/lib/CodeGen/CGObjCGNU.cpp
+++ b/clang/lib/CodeGen/CGObjCGNU.cpp
@@ -819,10 +819,19 @@ class CGObjCGNUstep : public CGObjCGNU {
   SlotLookupSuperFn.init(&CGM, "objc_slot_lookup_super", SlotTy,
  PtrToObjCSuperTy, SelectorTy);
   // If we're in ObjC++ mode, then we want to make
-  if (usesSEHExceptions) {
-  llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
-  // void objc_exception_rethrow(void)
-  ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
+  if (CGM.getTarget().getTriple().isOSCygMing() &&
+  isRuntime(ObjCRuntime::GNUstep, 2)) {
+llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
+// void *__cxa_begin_catch(void *e)
+EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy);
+// void __cxa_end_catch(void)
+ExitCatchFn.init(&CGM, "__cxa_end_catch", VoidTy);
+// void objc_exception_rethrow(void*)
+ExceptionReThrowFn.init(&CGM, "__cxa_rethrow", PtrTy);
+  } else if (usesSEHExceptions) {
+llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
+// void objc_exception_rethrow(void)
+ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
   } else if (CGM.getLangOpts().CPlusPlus) {
 llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
 // void *__cxa_begin_catch(void *e)
@@ -2210,7 +2219,12 @@ CGObjCGNU::CGObjCGNU(CodeGenModule &cgm, unsigned 
runtimeABIVersion,
 
   // void objc_exception_throw(id);
   ExceptionThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
-  ExceptionReThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
+  if ((CGM.getTarget().getTriple().isOSCygMing() &&
+   isRuntime(ObjCRuntime::GNUstep, 2))) {
+ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy, IdTy);
+  } else {
+ExceptionReThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
+  }
   // int objc_sync_enter(id);
   SyncEnterFn.init(&CGM, "objc_sync_enter", IntTy, IdTy);
   // int objc_sync_exit(id);
@@ -2387,7 +2401,9 @@ llvm::Constant *CGObjCGNUstep::GetEHType(QualType T) {
   if (usesSEHExceptions)
 return CGM.getCXXABI().getAddrOfRTTIDescriptor(T);
 
-  if (!CGM.getLangOpts().CPlusPlus)
+  if (!CGM.getLangOpts().CPlusPlus &&
+  !(CGM.getTarget().getTriple().isOSCygMing() &&
+isRuntime(ObjCRuntime::GNUstep, 2)))
 return CGObjCGNU::GetEHType(T);
 
   // For Objective-C++, we want to provide the ability to catch both C++ and
@@ -3993,7 +4009,9 @@ void CGObjCGNU::EmitThrowStmt(CodeGenFunction &CGF,
 ExceptionAsObject = CGF.ObjCEHValueStack.back();
 isRethrow = true;
   }
-  if (isRethrow && usesSEHExceptions) {
+  if (isRethrow &&
+  (usesSEHExceptions || (CGM.getTarget().getTriple().isOSCygMing() &&
+ isRuntime(ObjCRuntime::GNUstep, 2 {
 // For SEH, ExceptionAsObject may be undef, beca

[clang] Objective C: use C++ exceptions on MinGW+GNUstep (PR #77255)

2024-01-07 Thread Frederik Carlier via cfe-commits
https://github.com/qmfrederik updated 
https://github.com/llvm/llvm-project/pull/77255

>From 4212c79b5bb9168ae741b05b2c3388aa23e38693 Mon Sep 17 00:00:00 2001
From: Frederik Carlier 
Date: Thu, 4 Jan 2024 11:10:05 -0800
Subject: [PATCH] Objective C: use C++ exceptions on MinGW+GNUstep

The GNUstep Objective C runtime (libobjc2) is adding support for
MinGW.  This runtime uses C++ exceptions in that configuration.
---
 clang/lib/CodeGen/CGException.cpp |  9 +++-
 clang/lib/CodeGen/CGObjCGNU.cpp   | 35 
 .../test/CodeGenObjC/exceptions-personality.m | 53 +++
 clang/test/CodeGenObjC/personality.m  |  5 +-
 clang/test/CodeGenObjCXX/personality.mm   |  5 +-
 5 files changed, 92 insertions(+), 15 deletions(-)
 create mode 100644 clang/test/CodeGenObjC/exceptions-personality.m

diff --git a/clang/lib/CodeGen/CGException.cpp 
b/clang/lib/CodeGen/CGException.cpp
index 0d507da5c1ba92..cadd6f6ad75daa 100644
--- a/clang/lib/CodeGen/CGException.cpp
+++ b/clang/lib/CodeGen/CGException.cpp
@@ -156,7 +156,9 @@ static const EHPersonality &getObjCPersonality(const 
TargetInfo &Target,
   case ObjCRuntime::WatchOS:
 return EHPersonality::NeXT_ObjC;
   case ObjCRuntime::GNUstep:
-if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
+if (T.isOSCygMing())
+  return EHPersonality::GNU_CPlusPlus_SEH;
+else if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
   return EHPersonality::GNUstep_ObjC;
 [[fallthrough]];
   case ObjCRuntime::GCC:
@@ -210,7 +212,10 @@ static const EHPersonality &getObjCXXPersonality(const 
TargetInfo &Target,
 return getObjCPersonality(Target, L);
 
   case ObjCRuntime::GNUstep:
-return EHPersonality::GNU_ObjCXX;
+if (Target.getTriple().isOSCygMing())
+  return EHPersonality::GNU_CPlusPlus_SEH;
+else
+  return EHPersonality::GNU_ObjCXX;
 
   // The GCC runtime's personality function inherently doesn't support
   // mixed EH.  Use the ObjC personality just to avoid returning null.
diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp
index 4ca1a8cce64d89..2b3a5da87fb778 100644
--- a/clang/lib/CodeGen/CGObjCGNU.cpp
+++ b/clang/lib/CodeGen/CGObjCGNU.cpp
@@ -819,10 +819,19 @@ class CGObjCGNUstep : public CGObjCGNU {
   SlotLookupSuperFn.init(&CGM, "objc_slot_lookup_super", SlotTy,
  PtrToObjCSuperTy, SelectorTy);
   // If we're in ObjC++ mode, then we want to make
-  if (usesSEHExceptions) {
-  llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
-  // void objc_exception_rethrow(void)
-  ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
+  if (CGM.getTarget().getTriple().isOSCygMing() &&
+  isRuntime(ObjCRuntime::GNUstep, 2)) {
+llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
+// void *__cxa_begin_catch(void *e)
+EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy);
+// void __cxa_end_catch(void)
+ExitCatchFn.init(&CGM, "__cxa_end_catch", VoidTy);
+// void objc_exception_rethrow(void*)
+ExceptionReThrowFn.init(&CGM, "__cxa_rethrow", PtrTy);
+  } else if (usesSEHExceptions) {
+llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
+// void objc_exception_rethrow(void)
+ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
   } else if (CGM.getLangOpts().CPlusPlus) {
 llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
 // void *__cxa_begin_catch(void *e)
@@ -2210,7 +2219,12 @@ CGObjCGNU::CGObjCGNU(CodeGenModule &cgm, unsigned 
runtimeABIVersion,
 
   // void objc_exception_throw(id);
   ExceptionThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
-  ExceptionReThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
+  if ((CGM.getTarget().getTriple().isOSCygMing() &&
+   isRuntime(ObjCRuntime::GNUstep, 2))) {
+ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy, IdTy);
+  } else {
+ExceptionReThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
+  }
   // int objc_sync_enter(id);
   SyncEnterFn.init(&CGM, "objc_sync_enter", IntTy, IdTy);
   // int objc_sync_exit(id);
@@ -2387,7 +2401,9 @@ llvm::Constant *CGObjCGNUstep::GetEHType(QualType T) {
   if (usesSEHExceptions)
 return CGM.getCXXABI().getAddrOfRTTIDescriptor(T);
 
-  if (!CGM.getLangOpts().CPlusPlus)
+  if (!CGM.getLangOpts().CPlusPlus &&
+  !(CGM.getTarget().getTriple().isOSCygMing() &&
+isRuntime(ObjCRuntime::GNUstep, 2)))
 return CGObjCGNU::GetEHType(T);
 
   // For Objective-C++, we want to provide the ability to catch both C++ and
@@ -3993,7 +4009,9 @@ void CGObjCGNU::EmitThrowStmt(CodeGenFunction &CGF,
 ExceptionAsObject = CGF.ObjCEHValueStack.back();
 isRethrow = true;
   }
-  if (isRethrow && usesSEHExceptions) {
+  if (isRethrow &&
+  (usesSEHExceptions || (CGM.getTarget().getTriple().isOSCygMing() &&
+

[clang] Objective C: use C++ exceptions on MinGW+GNUstep (PR #77255)

2024-01-07 Thread Frederik Carlier via cfe-commits
https://github.com/qmfrederik updated 
https://github.com/llvm/llvm-project/pull/77255

>From d5dec27c1b0eb1999a86035ac458da377132ec7d Mon Sep 17 00:00:00 2001
From: Frederik Carlier 
Date: Thu, 4 Jan 2024 11:10:05 -0800
Subject: [PATCH] Objective C: use C++ exceptions on MinGW+GNUstep

The GNUstep Objective C runtime (libobjc2) is adding support for
MinGW.  This runtime uses C++ exceptions in that configuration.
---
 clang/lib/CodeGen/CGException.cpp |  9 +++-
 clang/lib/CodeGen/CGObjCGNU.cpp   | 35 
 .../test/CodeGenObjC/exceptions-personality.m | 53 +++
 clang/test/CodeGenObjC/personality.m  |  5 +-
 clang/test/CodeGenObjCXX/personality.mm   |  5 +-
 5 files changed, 92 insertions(+), 15 deletions(-)
 create mode 100644 clang/test/CodeGenObjC/exceptions-personality.m

diff --git a/clang/lib/CodeGen/CGException.cpp 
b/clang/lib/CodeGen/CGException.cpp
index 0d507da5c1ba92..cadd6f6ad75daa 100644
--- a/clang/lib/CodeGen/CGException.cpp
+++ b/clang/lib/CodeGen/CGException.cpp
@@ -156,7 +156,9 @@ static const EHPersonality &getObjCPersonality(const 
TargetInfo &Target,
   case ObjCRuntime::WatchOS:
 return EHPersonality::NeXT_ObjC;
   case ObjCRuntime::GNUstep:
-if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
+if (T.isOSCygMing())
+  return EHPersonality::GNU_CPlusPlus_SEH;
+else if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
   return EHPersonality::GNUstep_ObjC;
 [[fallthrough]];
   case ObjCRuntime::GCC:
@@ -210,7 +212,10 @@ static const EHPersonality &getObjCXXPersonality(const 
TargetInfo &Target,
 return getObjCPersonality(Target, L);
 
   case ObjCRuntime::GNUstep:
-return EHPersonality::GNU_ObjCXX;
+if (Target.getTriple().isOSCygMing())
+  return EHPersonality::GNU_CPlusPlus_SEH;
+else
+  return EHPersonality::GNU_ObjCXX;
 
   // The GCC runtime's personality function inherently doesn't support
   // mixed EH.  Use the ObjC personality just to avoid returning null.
diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp
index 4ca1a8cce64d89..2b3a5da87fb778 100644
--- a/clang/lib/CodeGen/CGObjCGNU.cpp
+++ b/clang/lib/CodeGen/CGObjCGNU.cpp
@@ -819,10 +819,19 @@ class CGObjCGNUstep : public CGObjCGNU {
   SlotLookupSuperFn.init(&CGM, "objc_slot_lookup_super", SlotTy,
  PtrToObjCSuperTy, SelectorTy);
   // If we're in ObjC++ mode, then we want to make
-  if (usesSEHExceptions) {
-  llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
-  // void objc_exception_rethrow(void)
-  ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
+  if (CGM.getTarget().getTriple().isOSCygMing() &&
+  isRuntime(ObjCRuntime::GNUstep, 2)) {
+llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
+// void *__cxa_begin_catch(void *e)
+EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy);
+// void __cxa_end_catch(void)
+ExitCatchFn.init(&CGM, "__cxa_end_catch", VoidTy);
+// void objc_exception_rethrow(void*)
+ExceptionReThrowFn.init(&CGM, "__cxa_rethrow", PtrTy);
+  } else if (usesSEHExceptions) {
+llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
+// void objc_exception_rethrow(void)
+ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
   } else if (CGM.getLangOpts().CPlusPlus) {
 llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
 // void *__cxa_begin_catch(void *e)
@@ -2210,7 +2219,12 @@ CGObjCGNU::CGObjCGNU(CodeGenModule &cgm, unsigned 
runtimeABIVersion,
 
   // void objc_exception_throw(id);
   ExceptionThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
-  ExceptionReThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
+  if ((CGM.getTarget().getTriple().isOSCygMing() &&
+   isRuntime(ObjCRuntime::GNUstep, 2))) {
+ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy, IdTy);
+  } else {
+ExceptionReThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
+  }
   // int objc_sync_enter(id);
   SyncEnterFn.init(&CGM, "objc_sync_enter", IntTy, IdTy);
   // int objc_sync_exit(id);
@@ -2387,7 +2401,9 @@ llvm::Constant *CGObjCGNUstep::GetEHType(QualType T) {
   if (usesSEHExceptions)
 return CGM.getCXXABI().getAddrOfRTTIDescriptor(T);
 
-  if (!CGM.getLangOpts().CPlusPlus)
+  if (!CGM.getLangOpts().CPlusPlus &&
+  !(CGM.getTarget().getTriple().isOSCygMing() &&
+isRuntime(ObjCRuntime::GNUstep, 2)))
 return CGObjCGNU::GetEHType(T);
 
   // For Objective-C++, we want to provide the ability to catch both C++ and
@@ -3993,7 +4009,9 @@ void CGObjCGNU::EmitThrowStmt(CodeGenFunction &CGF,
 ExceptionAsObject = CGF.ObjCEHValueStack.back();
 isRethrow = true;
   }
-  if (isRethrow && usesSEHExceptions) {
+  if (isRethrow &&
+  (usesSEHExceptions || (CGM.getTarget().getTriple().isOSCygMing() &&
+

[clang] Objective C: use C++ exceptions on MinGW+GNUstep (PR #77255)

2024-01-08 Thread Frederik Carlier via cfe-commits

@@ -2210,7 +2219,12 @@ CGObjCGNU::CGObjCGNU(CodeGenModule &cgm, unsigned 
runtimeABIVersion,
 
   // void objc_exception_throw(id);
   ExceptionThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
-  ExceptionReThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
+  if ((CGM.getTarget().getTriple().isOSCygMing() &&
+   isRuntime(ObjCRuntime::GNUstep, 2))) {
+ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy, IdTy);
+  } else {

qmfrederik wrote:

On MinGW + objc2, `objc_exception_rethrow` is called; on other targets, 
`objc_exception_throw` is used (**re**throw vs throw).  I find it odd we were 
using `objc_exception_throw` to *re*throw an exception but decided to change it 
for MinGW+objc2 only.

https://github.com/llvm/llvm-project/pull/77255
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Set dllstorage on ObjectiveC ivar offsets (PR #77385)

2024-01-08 Thread Frederik Carlier via cfe-commits
https://github.com/qmfrederik created 
https://github.com/llvm/llvm-project/pull/77385

Mark instance variable offset symbols with `dllexport`/`dllimport` if they are 
not hidden and the interface declaration is marked with 
`dllexport`/`dllimport`, when using the GNUstep 2.x ABI.

/cc @davidchisnall 

>From 3f8094117734b867eaa9bee958e8982cc3687799 Mon Sep 17 00:00:00 2001
From: Frederik Carlier 
Date: Sun, 7 Jan 2024 14:55:48 -0800
Subject: [PATCH] Set dllstorage on ObjectiveC ivar offsets

---
 clang/lib/CodeGen/CGObjCGNU.cpp |  2 ++
 clang/test/CodeGenObjC/dllstorage.m | 29 +
 2 files changed, 31 insertions(+)

diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp
index 4ca1a8cce64d89..9443fecf9b7946 100644
--- a/clang/lib/CodeGen/CGObjCGNU.cpp
+++ b/clang/lib/CodeGen/CGObjCGNU.cpp
@@ -1851,6 +1851,8 @@ class CGObjCGNUstep2 : public CGObjCGNUstep {
 llvm::GlobalValue::HiddenVisibility :
 llvm::GlobalValue::DefaultVisibility;
 OffsetVar->setVisibility(ivarVisibility);
+if (ivarVisibility != llvm::GlobalValue::HiddenVisibility)
+  CGM.setGVProperties(OffsetVar, OID->getClassInterface());
 ivarBuilder.add(OffsetVar);
 // Ivar size
 ivarBuilder.addInt(Int32Ty,
diff --git a/clang/test/CodeGenObjC/dllstorage.m 
b/clang/test/CodeGenObjC/dllstorage.m
index 64ba21f9769ae2..0dbf1881caa9c0 100644
--- a/clang/test/CodeGenObjC/dllstorage.m
+++ b/clang/test/CodeGenObjC/dllstorage.m
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -triple x86_64-unknown-windows-msvc -fdeclspec 
-fobjc-runtime=ios -fobjc-exceptions -S -emit-llvm -o - %s | FileCheck 
-allow-deprecated-dag-overlap -check-prefix CHECK-IR %s
+// RUN: %clang_cc1 -triple x86_64-unknown-windows-msvc -fdeclspec 
-fobjc-runtime=gnustep-2.0 -fobjc-exceptions -S -emit-llvm -o - %s | FileCheck 
-allow-deprecated-dag-overlap -check-prefix CHECK-NF %s
 // RUN: %clang_cc1 -triple i686-windows-itanium -fms-extensions 
-fobjc-runtime=macosx -fdeclspec -fobjc-exceptions -S -emit-llvm -o - %s | 
FileCheck -allow-deprecated-dag-overlap -check-prefix CHECK-IR %s
 // RUN: %clang_cc1 -triple i686-windows-itanium -fms-extensions 
-fobjc-runtime=objfw -fdeclspec -fobjc-exceptions -S -emit-llvm -o - %s | 
FileCheck -allow-deprecated-dag-overlap -check-prefix CHECK-FW %s
 
@@ -12,6 +13,8 @@ + (instancetype) new;
 // CHECK-IR-DAG: @"OBJC_METACLASS_$_I" = external dllimport global 
%struct._class_t
 // CHECK-IR-DAG: @"OBJC_CLASS_$_I" = external dllimport global %struct._class_t
 
+// CHECK-NF-DAG: @"$_OBJC_CLASS_I" = external dllimport global ptr
+
 __declspec(dllexport)
 @interface J : I
 @end
@@ -22,6 +25,9 @@ @interface J : I
 // CHECK-FW-DAG: @_OBJC_METACLASS_J = dso_local dllexport global
 // CHECK-FW-DAG: @_OBJC_CLASS_J = dso_local dllexport global
 
+// CHECK-NF-DAG: @"$_OBJC_METACLASS_J" = internal global
+// CHECK-NF-DAG: @"$_OBJC_CLASS_J" = dllexport global
+
 @implementation J {
   id _ivar;
 }
@@ -29,6 +35,8 @@ @implementation J {
 
 // CHECK-IR-DAG: @"OBJC_IVAR_$_J._ivar" = global i32
 
+// CHECK-NF-DAG: @"__objc_ivar_offset_J._ivar.\01" = hidden global i32
+
 @interface K : J
 @end
 
@@ -38,6 +46,9 @@ @interface K : J
 // CHECK-FW-DAG: @_OBJC_METACLASS_K = dso_local global
 // CHECK-FW-DAG: @_OBJC_CLASS_K = dso_local global
 
+// CHECK-NF-DAG: @"$_OBJC_METACLASS_K" = internal global
+// CHECK-NF-DAG: @"$_OBJC_CLASS_K" = global
+
 @implementation K {
   id _ivar;
 }
@@ -45,6 +56,8 @@ @implementation K {
 
 // CHECK-IR-DAG: @"OBJC_IVAR_$_K._ivar" = global i32
 
+// CHECK-NF-DAG: @"__objc_ivar_offset_K._ivar.\01" = hidden global i32
+
 __declspec(dllexport)
 @interface L : K
 @end
@@ -55,6 +68,9 @@ @interface L : K
 // CHECK-FW-DAG: @_OBJC_METACLASS_L = dso_local dllexport global
 // CHECK-FW-DAG: @_OBJC_CLASS_L = dso_local dllexport global
 
+// CHECK-NF-DAG: @"$_OBJC_METACLASS_L" = internal global
+// CHECK-NF-DAG: @"$_OBJC_CLASS_L" = dllexport global
+
 @implementation L {
   id _none;
 
@@ -78,6 +94,12 @@ @implementation L {
 // CHECK-IR-DAG: @"OBJC_IVAR_$_L._package" = global i32
 // CHECK-IR-DAG: @"OBJC_IVAR_$_L._private" = global i32
 
+// CHECK-NF-DAG: @"__objc_ivar_offset_L._none.\01" = hidden global i32
+// CHECK-NF-DAG: @"__objc_ivar_offset_L._public.\01" = dso_local dllexport 
global i32
+// CHECK-NF-DAG: @"__objc_ivar_offset_L._protected.\01" = dso_local dllexport 
global i32
+// CHECK-NF-DAG: @"__objc_ivar_offset_L._package.\01" = hidden global i32
+// CHECK-NF-DAG: @"__objc_ivar_offset_L._private.\01" = hidden global i32
+
 __declspec(dllimport)
 @interface M : I {
   @public
@@ -89,6 +111,9 @@ @interface M : I {
 
 // CHECK-IR-DAG: @"OBJC_IVAR_$_M._ivar" = external dllimport global i32
 
+// CHECK-NF-DAG: @"$_OBJC_REF_CLASS_M" = external dllimport global ptr
+// CHECK-NF-DAG: @"__objc_ivar_offset_M._ivar.\01" = external global i32
+
 __declspec(dllexport)
 __attribute__((__objc_exception__))
 @interface N : I
@@ -97,6 +122,

[clang] Set dllstorage on ObjectiveC ivar offsets (PR #77385)

2024-01-09 Thread Frederik Carlier via cfe-commits
qmfrederik wrote:

Thanks @davidchisnall .  Do you plan to get this merged?

https://github.com/llvm/llvm-project/pull/77385
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Objective C: use C++ exceptions on MinGW+GNUstep (PR #77255)

2024-01-09 Thread Frederik Carlier via cfe-commits
https://github.com/qmfrederik updated 
https://github.com/llvm/llvm-project/pull/77255

>From 6195e6c57b5b461dda2bffec0e5497ceb659f82c Mon Sep 17 00:00:00 2001
From: Frederik Carlier 
Date: Thu, 4 Jan 2024 11:10:05 -0800
Subject: [PATCH 1/2] Objective C: use C++ exceptions on MinGW+GNUstep

The GNUstep Objective C runtime (libobjc2) is adding support for
MinGW.  This runtime uses C++ exceptions in that configuration.
---
 clang/lib/CodeGen/CGException.cpp |  9 +++-
 clang/lib/CodeGen/CGObjCGNU.cpp   | 35 
 .../test/CodeGenObjC/exceptions-personality.m | 53 +++
 clang/test/CodeGenObjC/personality.m  |  5 +-
 clang/test/CodeGenObjCXX/personality.mm   |  5 +-
 5 files changed, 92 insertions(+), 15 deletions(-)
 create mode 100644 clang/test/CodeGenObjC/exceptions-personality.m

diff --git a/clang/lib/CodeGen/CGException.cpp 
b/clang/lib/CodeGen/CGException.cpp
index 0d507da5c1ba92..cadd6f6ad75daa 100644
--- a/clang/lib/CodeGen/CGException.cpp
+++ b/clang/lib/CodeGen/CGException.cpp
@@ -156,7 +156,9 @@ static const EHPersonality &getObjCPersonality(const 
TargetInfo &Target,
   case ObjCRuntime::WatchOS:
 return EHPersonality::NeXT_ObjC;
   case ObjCRuntime::GNUstep:
-if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
+if (T.isOSCygMing())
+  return EHPersonality::GNU_CPlusPlus_SEH;
+else if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
   return EHPersonality::GNUstep_ObjC;
 [[fallthrough]];
   case ObjCRuntime::GCC:
@@ -210,7 +212,10 @@ static const EHPersonality &getObjCXXPersonality(const 
TargetInfo &Target,
 return getObjCPersonality(Target, L);
 
   case ObjCRuntime::GNUstep:
-return EHPersonality::GNU_ObjCXX;
+if (Target.getTriple().isOSCygMing())
+  return EHPersonality::GNU_CPlusPlus_SEH;
+else
+  return EHPersonality::GNU_ObjCXX;
 
   // The GCC runtime's personality function inherently doesn't support
   // mixed EH.  Use the ObjC personality just to avoid returning null.
diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp
index 4ca1a8cce64d89..2b3a5da87fb778 100644
--- a/clang/lib/CodeGen/CGObjCGNU.cpp
+++ b/clang/lib/CodeGen/CGObjCGNU.cpp
@@ -819,10 +819,19 @@ class CGObjCGNUstep : public CGObjCGNU {
   SlotLookupSuperFn.init(&CGM, "objc_slot_lookup_super", SlotTy,
  PtrToObjCSuperTy, SelectorTy);
   // If we're in ObjC++ mode, then we want to make
-  if (usesSEHExceptions) {
-  llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
-  // void objc_exception_rethrow(void)
-  ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
+  if (CGM.getTarget().getTriple().isOSCygMing() &&
+  isRuntime(ObjCRuntime::GNUstep, 2)) {
+llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
+// void *__cxa_begin_catch(void *e)
+EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy);
+// void __cxa_end_catch(void)
+ExitCatchFn.init(&CGM, "__cxa_end_catch", VoidTy);
+// void objc_exception_rethrow(void*)
+ExceptionReThrowFn.init(&CGM, "__cxa_rethrow", PtrTy);
+  } else if (usesSEHExceptions) {
+llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
+// void objc_exception_rethrow(void)
+ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
   } else if (CGM.getLangOpts().CPlusPlus) {
 llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
 // void *__cxa_begin_catch(void *e)
@@ -2210,7 +2219,12 @@ CGObjCGNU::CGObjCGNU(CodeGenModule &cgm, unsigned 
runtimeABIVersion,
 
   // void objc_exception_throw(id);
   ExceptionThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
-  ExceptionReThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
+  if ((CGM.getTarget().getTriple().isOSCygMing() &&
+   isRuntime(ObjCRuntime::GNUstep, 2))) {
+ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy, IdTy);
+  } else {
+ExceptionReThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
+  }
   // int objc_sync_enter(id);
   SyncEnterFn.init(&CGM, "objc_sync_enter", IntTy, IdTy);
   // int objc_sync_exit(id);
@@ -2387,7 +2401,9 @@ llvm::Constant *CGObjCGNUstep::GetEHType(QualType T) {
   if (usesSEHExceptions)
 return CGM.getCXXABI().getAddrOfRTTIDescriptor(T);
 
-  if (!CGM.getLangOpts().CPlusPlus)
+  if (!CGM.getLangOpts().CPlusPlus &&
+  !(CGM.getTarget().getTriple().isOSCygMing() &&
+isRuntime(ObjCRuntime::GNUstep, 2)))
 return CGObjCGNU::GetEHType(T);
 
   // For Objective-C++, we want to provide the ability to catch both C++ and
@@ -3993,7 +4009,9 @@ void CGObjCGNU::EmitThrowStmt(CodeGenFunction &CGF,
 ExceptionAsObject = CGF.ObjCEHValueStack.back();
 isRethrow = true;
   }
-  if (isRethrow && usesSEHExceptions) {
+  if (isRethrow &&
+  (usesSEHExceptions || (CGM.getTarget().getTriple().isOSCygMing() &&
+

[clang] Set dllstorage on ObjectiveC ivar offsets (PR #77385)

2024-01-09 Thread Frederik Carlier via cfe-commits
qmfrederik wrote:

No worries, thanks! :-)

https://github.com/llvm/llvm-project/pull/77385
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Objective C: use C++ exceptions on MinGW+GNUstep (PR #77255)

2024-01-09 Thread Frederik Carlier via cfe-commits

@@ -819,10 +819,19 @@ class CGObjCGNUstep : public CGObjCGNU {
   SlotLookupSuperFn.init(&CGM, "objc_slot_lookup_super", SlotTy,
  PtrToObjCSuperTy, SelectorTy);
   // If we're in ObjC++ mode, then we want to make
-  if (usesSEHExceptions) {
-  llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
-  // void objc_exception_rethrow(void)
-  ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
+  if (CGM.getTarget().getTriple().isOSCygMing() &&
+  isRuntime(ObjCRuntime::GNUstep, 2)) {
+llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
+// void *__cxa_begin_catch(void *e)
+EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy);
+// void __cxa_end_catch(void)
+ExitCatchFn.init(&CGM, "__cxa_end_catch", VoidTy);
+// void objc_exception_rethrow(void*)
+ExceptionReThrowFn.init(&CGM, "__cxa_rethrow", PtrTy);
+  } else if (usesSEHExceptions) {
+llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);

qmfrederik wrote:

Good remark, thanks.  Fixed this.

https://github.com/llvm/llvm-project/pull/77255
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Objective C: use C++ exceptions on MinGW+GNUstep (PR #77255)

2024-01-09 Thread Frederik Carlier via cfe-commits

@@ -3993,7 +4009,9 @@ void CGObjCGNU::EmitThrowStmt(CodeGenFunction &CGF,
 ExceptionAsObject = CGF.ObjCEHValueStack.back();
 isRethrow = true;
   }
-  if (isRethrow && usesSEHExceptions) {
+  if (isRethrow &&
+  (usesSEHExceptions || (CGM.getTarget().getTriple().isOSCygMing() &&
+ isRuntime(ObjCRuntime::GNUstep, 2 {

qmfrederik wrote:

Yes, that makes the intent clearer.  I changed this.

https://github.com/llvm/llvm-project/pull/77255
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Objective C: use C++ exceptions on MinGW+GNUstep (PR #77255)

2024-01-09 Thread Frederik Carlier via cfe-commits
https://github.com/qmfrederik updated 
https://github.com/llvm/llvm-project/pull/77255

>From 6195e6c57b5b461dda2bffec0e5497ceb659f82c Mon Sep 17 00:00:00 2001
From: Frederik Carlier 
Date: Thu, 4 Jan 2024 11:10:05 -0800
Subject: [PATCH 1/2] Objective C: use C++ exceptions on MinGW+GNUstep

The GNUstep Objective C runtime (libobjc2) is adding support for
MinGW.  This runtime uses C++ exceptions in that configuration.
---
 clang/lib/CodeGen/CGException.cpp |  9 +++-
 clang/lib/CodeGen/CGObjCGNU.cpp   | 35 
 .../test/CodeGenObjC/exceptions-personality.m | 53 +++
 clang/test/CodeGenObjC/personality.m  |  5 +-
 clang/test/CodeGenObjCXX/personality.mm   |  5 +-
 5 files changed, 92 insertions(+), 15 deletions(-)
 create mode 100644 clang/test/CodeGenObjC/exceptions-personality.m

diff --git a/clang/lib/CodeGen/CGException.cpp 
b/clang/lib/CodeGen/CGException.cpp
index 0d507da5c1ba92..cadd6f6ad75daa 100644
--- a/clang/lib/CodeGen/CGException.cpp
+++ b/clang/lib/CodeGen/CGException.cpp
@@ -156,7 +156,9 @@ static const EHPersonality &getObjCPersonality(const 
TargetInfo &Target,
   case ObjCRuntime::WatchOS:
 return EHPersonality::NeXT_ObjC;
   case ObjCRuntime::GNUstep:
-if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
+if (T.isOSCygMing())
+  return EHPersonality::GNU_CPlusPlus_SEH;
+else if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
   return EHPersonality::GNUstep_ObjC;
 [[fallthrough]];
   case ObjCRuntime::GCC:
@@ -210,7 +212,10 @@ static const EHPersonality &getObjCXXPersonality(const 
TargetInfo &Target,
 return getObjCPersonality(Target, L);
 
   case ObjCRuntime::GNUstep:
-return EHPersonality::GNU_ObjCXX;
+if (Target.getTriple().isOSCygMing())
+  return EHPersonality::GNU_CPlusPlus_SEH;
+else
+  return EHPersonality::GNU_ObjCXX;
 
   // The GCC runtime's personality function inherently doesn't support
   // mixed EH.  Use the ObjC personality just to avoid returning null.
diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp
index 4ca1a8cce64d89..2b3a5da87fb778 100644
--- a/clang/lib/CodeGen/CGObjCGNU.cpp
+++ b/clang/lib/CodeGen/CGObjCGNU.cpp
@@ -819,10 +819,19 @@ class CGObjCGNUstep : public CGObjCGNU {
   SlotLookupSuperFn.init(&CGM, "objc_slot_lookup_super", SlotTy,
  PtrToObjCSuperTy, SelectorTy);
   // If we're in ObjC++ mode, then we want to make
-  if (usesSEHExceptions) {
-  llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
-  // void objc_exception_rethrow(void)
-  ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
+  if (CGM.getTarget().getTriple().isOSCygMing() &&
+  isRuntime(ObjCRuntime::GNUstep, 2)) {
+llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
+// void *__cxa_begin_catch(void *e)
+EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy);
+// void __cxa_end_catch(void)
+ExitCatchFn.init(&CGM, "__cxa_end_catch", VoidTy);
+// void objc_exception_rethrow(void*)
+ExceptionReThrowFn.init(&CGM, "__cxa_rethrow", PtrTy);
+  } else if (usesSEHExceptions) {
+llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
+// void objc_exception_rethrow(void)
+ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
   } else if (CGM.getLangOpts().CPlusPlus) {
 llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
 // void *__cxa_begin_catch(void *e)
@@ -2210,7 +2219,12 @@ CGObjCGNU::CGObjCGNU(CodeGenModule &cgm, unsigned 
runtimeABIVersion,
 
   // void objc_exception_throw(id);
   ExceptionThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
-  ExceptionReThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
+  if ((CGM.getTarget().getTriple().isOSCygMing() &&
+   isRuntime(ObjCRuntime::GNUstep, 2))) {
+ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy, IdTy);
+  } else {
+ExceptionReThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
+  }
   // int objc_sync_enter(id);
   SyncEnterFn.init(&CGM, "objc_sync_enter", IntTy, IdTy);
   // int objc_sync_exit(id);
@@ -2387,7 +2401,9 @@ llvm::Constant *CGObjCGNUstep::GetEHType(QualType T) {
   if (usesSEHExceptions)
 return CGM.getCXXABI().getAddrOfRTTIDescriptor(T);
 
-  if (!CGM.getLangOpts().CPlusPlus)
+  if (!CGM.getLangOpts().CPlusPlus &&
+  !(CGM.getTarget().getTriple().isOSCygMing() &&
+isRuntime(ObjCRuntime::GNUstep, 2)))
 return CGObjCGNU::GetEHType(T);
 
   // For Objective-C++, we want to provide the ability to catch both C++ and
@@ -3993,7 +4009,9 @@ void CGObjCGNU::EmitThrowStmt(CodeGenFunction &CGF,
 ExceptionAsObject = CGF.ObjCEHValueStack.back();
 isRethrow = true;
   }
-  if (isRethrow && usesSEHExceptions) {
+  if (isRethrow &&
+  (usesSEHExceptions || (CGM.getTarget().getTriple().isOSCygMing() &&
+

[clang] Objective C: use C++ exceptions on MinGW+GNUstep (PR #77255)

2024-01-09 Thread Frederik Carlier via cfe-commits
qmfrederik wrote:

@compnerd Thanks for you feedback, I addressed your comments.

https://github.com/llvm/llvm-project/pull/77255
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Objective C: use C++ exceptions on MinGW+GNUstep (PR #77255)

2024-01-09 Thread Frederik Carlier via cfe-commits
https://github.com/qmfrederik updated 
https://github.com/llvm/llvm-project/pull/77255

>From 6195e6c57b5b461dda2bffec0e5497ceb659f82c Mon Sep 17 00:00:00 2001
From: Frederik Carlier 
Date: Thu, 4 Jan 2024 11:10:05 -0800
Subject: [PATCH 1/2] Objective C: use C++ exceptions on MinGW+GNUstep

The GNUstep Objective C runtime (libobjc2) is adding support for
MinGW.  This runtime uses C++ exceptions in that configuration.
---
 clang/lib/CodeGen/CGException.cpp |  9 +++-
 clang/lib/CodeGen/CGObjCGNU.cpp   | 35 
 .../test/CodeGenObjC/exceptions-personality.m | 53 +++
 clang/test/CodeGenObjC/personality.m  |  5 +-
 clang/test/CodeGenObjCXX/personality.mm   |  5 +-
 5 files changed, 92 insertions(+), 15 deletions(-)
 create mode 100644 clang/test/CodeGenObjC/exceptions-personality.m

diff --git a/clang/lib/CodeGen/CGException.cpp 
b/clang/lib/CodeGen/CGException.cpp
index 0d507da5c1ba92..cadd6f6ad75daa 100644
--- a/clang/lib/CodeGen/CGException.cpp
+++ b/clang/lib/CodeGen/CGException.cpp
@@ -156,7 +156,9 @@ static const EHPersonality &getObjCPersonality(const 
TargetInfo &Target,
   case ObjCRuntime::WatchOS:
 return EHPersonality::NeXT_ObjC;
   case ObjCRuntime::GNUstep:
-if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
+if (T.isOSCygMing())
+  return EHPersonality::GNU_CPlusPlus_SEH;
+else if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
   return EHPersonality::GNUstep_ObjC;
 [[fallthrough]];
   case ObjCRuntime::GCC:
@@ -210,7 +212,10 @@ static const EHPersonality &getObjCXXPersonality(const 
TargetInfo &Target,
 return getObjCPersonality(Target, L);
 
   case ObjCRuntime::GNUstep:
-return EHPersonality::GNU_ObjCXX;
+if (Target.getTriple().isOSCygMing())
+  return EHPersonality::GNU_CPlusPlus_SEH;
+else
+  return EHPersonality::GNU_ObjCXX;
 
   // The GCC runtime's personality function inherently doesn't support
   // mixed EH.  Use the ObjC personality just to avoid returning null.
diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp
index 4ca1a8cce64d89..2b3a5da87fb778 100644
--- a/clang/lib/CodeGen/CGObjCGNU.cpp
+++ b/clang/lib/CodeGen/CGObjCGNU.cpp
@@ -819,10 +819,19 @@ class CGObjCGNUstep : public CGObjCGNU {
   SlotLookupSuperFn.init(&CGM, "objc_slot_lookup_super", SlotTy,
  PtrToObjCSuperTy, SelectorTy);
   // If we're in ObjC++ mode, then we want to make
-  if (usesSEHExceptions) {
-  llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
-  // void objc_exception_rethrow(void)
-  ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
+  if (CGM.getTarget().getTriple().isOSCygMing() &&
+  isRuntime(ObjCRuntime::GNUstep, 2)) {
+llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
+// void *__cxa_begin_catch(void *e)
+EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy);
+// void __cxa_end_catch(void)
+ExitCatchFn.init(&CGM, "__cxa_end_catch", VoidTy);
+// void objc_exception_rethrow(void*)
+ExceptionReThrowFn.init(&CGM, "__cxa_rethrow", PtrTy);
+  } else if (usesSEHExceptions) {
+llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
+// void objc_exception_rethrow(void)
+ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
   } else if (CGM.getLangOpts().CPlusPlus) {
 llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
 // void *__cxa_begin_catch(void *e)
@@ -2210,7 +2219,12 @@ CGObjCGNU::CGObjCGNU(CodeGenModule &cgm, unsigned 
runtimeABIVersion,
 
   // void objc_exception_throw(id);
   ExceptionThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
-  ExceptionReThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
+  if ((CGM.getTarget().getTriple().isOSCygMing() &&
+   isRuntime(ObjCRuntime::GNUstep, 2))) {
+ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy, IdTy);
+  } else {
+ExceptionReThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
+  }
   // int objc_sync_enter(id);
   SyncEnterFn.init(&CGM, "objc_sync_enter", IntTy, IdTy);
   // int objc_sync_exit(id);
@@ -2387,7 +2401,9 @@ llvm::Constant *CGObjCGNUstep::GetEHType(QualType T) {
   if (usesSEHExceptions)
 return CGM.getCXXABI().getAddrOfRTTIDescriptor(T);
 
-  if (!CGM.getLangOpts().CPlusPlus)
+  if (!CGM.getLangOpts().CPlusPlus &&
+  !(CGM.getTarget().getTriple().isOSCygMing() &&
+isRuntime(ObjCRuntime::GNUstep, 2)))
 return CGObjCGNU::GetEHType(T);
 
   // For Objective-C++, we want to provide the ability to catch both C++ and
@@ -3993,7 +4009,9 @@ void CGObjCGNU::EmitThrowStmt(CodeGenFunction &CGF,
 ExceptionAsObject = CGF.ObjCEHValueStack.back();
 isRethrow = true;
   }
-  if (isRethrow && usesSEHExceptions) {
+  if (isRethrow &&
+  (usesSEHExceptions || (CGM.getTarget().getTriple().isOSCygMing() &&
+

[clang] Objective C: use C++ exceptions on MinGW+GNUstep (PR #77255)

2024-01-09 Thread Frederik Carlier via cfe-commits
https://github.com/qmfrederik updated 
https://github.com/llvm/llvm-project/pull/77255

>From 6195e6c57b5b461dda2bffec0e5497ceb659f82c Mon Sep 17 00:00:00 2001
From: Frederik Carlier 
Date: Thu, 4 Jan 2024 11:10:05 -0800
Subject: [PATCH 1/2] Objective C: use C++ exceptions on MinGW+GNUstep

The GNUstep Objective C runtime (libobjc2) is adding support for
MinGW.  This runtime uses C++ exceptions in that configuration.
---
 clang/lib/CodeGen/CGException.cpp |  9 +++-
 clang/lib/CodeGen/CGObjCGNU.cpp   | 35 
 .../test/CodeGenObjC/exceptions-personality.m | 53 +++
 clang/test/CodeGenObjC/personality.m  |  5 +-
 clang/test/CodeGenObjCXX/personality.mm   |  5 +-
 5 files changed, 92 insertions(+), 15 deletions(-)
 create mode 100644 clang/test/CodeGenObjC/exceptions-personality.m

diff --git a/clang/lib/CodeGen/CGException.cpp 
b/clang/lib/CodeGen/CGException.cpp
index 0d507da5c1ba92..cadd6f6ad75daa 100644
--- a/clang/lib/CodeGen/CGException.cpp
+++ b/clang/lib/CodeGen/CGException.cpp
@@ -156,7 +156,9 @@ static const EHPersonality &getObjCPersonality(const 
TargetInfo &Target,
   case ObjCRuntime::WatchOS:
 return EHPersonality::NeXT_ObjC;
   case ObjCRuntime::GNUstep:
-if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
+if (T.isOSCygMing())
+  return EHPersonality::GNU_CPlusPlus_SEH;
+else if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
   return EHPersonality::GNUstep_ObjC;
 [[fallthrough]];
   case ObjCRuntime::GCC:
@@ -210,7 +212,10 @@ static const EHPersonality &getObjCXXPersonality(const 
TargetInfo &Target,
 return getObjCPersonality(Target, L);
 
   case ObjCRuntime::GNUstep:
-return EHPersonality::GNU_ObjCXX;
+if (Target.getTriple().isOSCygMing())
+  return EHPersonality::GNU_CPlusPlus_SEH;
+else
+  return EHPersonality::GNU_ObjCXX;
 
   // The GCC runtime's personality function inherently doesn't support
   // mixed EH.  Use the ObjC personality just to avoid returning null.
diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp
index 4ca1a8cce64d89..2b3a5da87fb778 100644
--- a/clang/lib/CodeGen/CGObjCGNU.cpp
+++ b/clang/lib/CodeGen/CGObjCGNU.cpp
@@ -819,10 +819,19 @@ class CGObjCGNUstep : public CGObjCGNU {
   SlotLookupSuperFn.init(&CGM, "objc_slot_lookup_super", SlotTy,
  PtrToObjCSuperTy, SelectorTy);
   // If we're in ObjC++ mode, then we want to make
-  if (usesSEHExceptions) {
-  llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
-  // void objc_exception_rethrow(void)
-  ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
+  if (CGM.getTarget().getTriple().isOSCygMing() &&
+  isRuntime(ObjCRuntime::GNUstep, 2)) {
+llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
+// void *__cxa_begin_catch(void *e)
+EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy);
+// void __cxa_end_catch(void)
+ExitCatchFn.init(&CGM, "__cxa_end_catch", VoidTy);
+// void objc_exception_rethrow(void*)
+ExceptionReThrowFn.init(&CGM, "__cxa_rethrow", PtrTy);
+  } else if (usesSEHExceptions) {
+llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
+// void objc_exception_rethrow(void)
+ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
   } else if (CGM.getLangOpts().CPlusPlus) {
 llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
 // void *__cxa_begin_catch(void *e)
@@ -2210,7 +2219,12 @@ CGObjCGNU::CGObjCGNU(CodeGenModule &cgm, unsigned 
runtimeABIVersion,
 
   // void objc_exception_throw(id);
   ExceptionThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
-  ExceptionReThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
+  if ((CGM.getTarget().getTriple().isOSCygMing() &&
+   isRuntime(ObjCRuntime::GNUstep, 2))) {
+ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy, IdTy);
+  } else {
+ExceptionReThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy);
+  }
   // int objc_sync_enter(id);
   SyncEnterFn.init(&CGM, "objc_sync_enter", IntTy, IdTy);
   // int objc_sync_exit(id);
@@ -2387,7 +2401,9 @@ llvm::Constant *CGObjCGNUstep::GetEHType(QualType T) {
   if (usesSEHExceptions)
 return CGM.getCXXABI().getAddrOfRTTIDescriptor(T);
 
-  if (!CGM.getLangOpts().CPlusPlus)
+  if (!CGM.getLangOpts().CPlusPlus &&
+  !(CGM.getTarget().getTriple().isOSCygMing() &&
+isRuntime(ObjCRuntime::GNUstep, 2)))
 return CGObjCGNU::GetEHType(T);
 
   // For Objective-C++, we want to provide the ability to catch both C++ and
@@ -3993,7 +4009,9 @@ void CGObjCGNU::EmitThrowStmt(CodeGenFunction &CGF,
 ExceptionAsObject = CGF.ObjCEHValueStack.back();
 isRethrow = true;
   }
-  if (isRethrow && usesSEHExceptions) {
+  if (isRethrow &&
+  (usesSEHExceptions || (CGM.getTarget().getTriple().isOSCygMing() &&
+

[clang] Objective C: use C++ exceptions on MinGW+GNUstep (PR #77255)

2024-01-09 Thread Frederik Carlier via cfe-commits
qmfrederik wrote:

@compnerd @davidchisnall I've addressed the feedback and CI is (mostly) green.  
The build failure on Windows seems unrelated:


```
# Removing c:/ws\src
# Creating "c:/ws\src"
> git clone -v -- https://github.com/llvm-premerge-tests/llvm-project.git .
Cloning into '.'...
remote: Internal Server Error
fatal: unable to access 
'https://github.com/llvm-premerge-tests/llvm-project.git/': The requested URL 
returned error: 500
```



https://github.com/llvm/llvm-project/pull/77255
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Objective C: use C++ exceptions on MinGW+GNUstep (PR #77255)

2024-01-09 Thread Frederik Carlier via cfe-commits
https://github.com/qmfrederik updated 
https://github.com/llvm/llvm-project/pull/77255

>From be6dccb1691b84a9b84cc78c7bb40fdf2ed53c8a Mon Sep 17 00:00:00 2001
From: Frederik Carlier 
Date: Thu, 4 Jan 2024 11:10:05 -0800
Subject: [PATCH] Objective C: use C++ exceptions on MinGW+GNUstep

The GNUstep Objective C runtime (libobjc2) is adding support for
MinGW.  This runtime uses C++ exceptions in that configuration.
---
 clang/lib/CodeGen/CGException.cpp |  7 ++-
 clang/lib/CodeGen/CGObjCGNU.cpp   | 36 -
 .../test/CodeGenObjC/exceptions-personality.m | 53 +++
 clang/test/CodeGenObjC/personality.m  |  5 +-
 clang/test/CodeGenObjCXX/personality.mm   |  5 +-
 5 files changed, 88 insertions(+), 18 deletions(-)
 create mode 100644 clang/test/CodeGenObjC/exceptions-personality.m

diff --git a/clang/lib/CodeGen/CGException.cpp 
b/clang/lib/CodeGen/CGException.cpp
index 0d507da5c1ba92..56a246eb65e0a8 100644
--- a/clang/lib/CodeGen/CGException.cpp
+++ b/clang/lib/CodeGen/CGException.cpp
@@ -156,7 +156,9 @@ static const EHPersonality &getObjCPersonality(const 
TargetInfo &Target,
   case ObjCRuntime::WatchOS:
 return EHPersonality::NeXT_ObjC;
   case ObjCRuntime::GNUstep:
-if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
+if (T.isOSCygMing())
+  return EHPersonality::GNU_CPlusPlus_SEH;
+else if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
   return EHPersonality::GNUstep_ObjC;
 [[fallthrough]];
   case ObjCRuntime::GCC:
@@ -210,7 +212,8 @@ static const EHPersonality &getObjCXXPersonality(const 
TargetInfo &Target,
 return getObjCPersonality(Target, L);
 
   case ObjCRuntime::GNUstep:
-return EHPersonality::GNU_ObjCXX;
+return Target.getTriple().isOSCygMing() ? EHPersonality::GNU_CPlusPlus_SEH
+: EHPersonality::GNU_ObjCXX;
 
   // The GCC runtime's personality function inherently doesn't support
   // mixed EH.  Use the ObjC personality just to avoid returning null.
diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp
index 4ca1a8cce64d89..9ad1ecf7e52b04 100644
--- a/clang/lib/CodeGen/CGObjCGNU.cpp
+++ b/clang/lib/CodeGen/CGObjCGNU.cpp
@@ -168,6 +168,8 @@ class CGObjCGNU : public CGObjCRuntime {
   /// Does the current target use SEH-based exceptions? False implies
   /// Itanium-style DWARF unwinding.
   bool usesSEHExceptions;
+  /// Does the current target uses C++-based exceptions?
+  bool usesCxxExceptions;
 
   /// Helper to check if we are targeting a specific runtime version or later.
   bool isRuntime(ObjCRuntime::Kind kind, unsigned major, unsigned minor=0) {
@@ -819,12 +821,18 @@ class CGObjCGNUstep : public CGObjCGNU {
   SlotLookupSuperFn.init(&CGM, "objc_slot_lookup_super", SlotTy,
  PtrToObjCSuperTy, SelectorTy);
   // If we're in ObjC++ mode, then we want to make
-  if (usesSEHExceptions) {
-  llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
-  // void objc_exception_rethrow(void)
-  ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
+  llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
+  if (usesCxxExceptions) {
+// void *__cxa_begin_catch(void *e)
+EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy);
+// void __cxa_end_catch(void)
+ExitCatchFn.init(&CGM, "__cxa_end_catch", VoidTy);
+// void objc_exception_rethrow(void*)
+ExceptionReThrowFn.init(&CGM, "__cxa_rethrow", PtrTy);
+  } else if (usesSEHExceptions) {
+// void objc_exception_rethrow(void)
+ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
   } else if (CGM.getLangOpts().CPlusPlus) {
-llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
 // void *__cxa_begin_catch(void *e)
 EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy);
 // void __cxa_end_catch(void)
@@ -833,7 +841,6 @@ class CGObjCGNUstep : public CGObjCGNU {
 ExceptionReThrowFn.init(&CGM, "_Unwind_Resume_or_Rethrow", VoidTy,
 PtrTy);
   } else if (R.getVersion() >= VersionTuple(1, 7)) {
-llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
 // id objc_begin_catch(void *e)
 EnterCatchFn.init(&CGM, "objc_begin_catch", IdTy, PtrTy);
 // void objc_end_catch(void)
@@ -841,7 +848,6 @@ class CGObjCGNUstep : public CGObjCGNU {
 // void _Unwind_Resume_or_Rethrow(void*)
 ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy, PtrTy);
   }
-  llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
   SetPropertyAtomic.init(&CGM, "objc_setProperty_atomic", VoidTy, IdTy,
  SelectorTy, IdTy, PtrDiffTy);
   SetPropertyAtomicCopy.init(&CGM, "objc_setProperty_atomic_copy", VoidTy,
@@ -2124,6 +2130,9 @@ CGObjCGNU::CGObjCGNU(CodeGenModule &cgm, unsigned 

[clang] Objective C: use C++ exceptions on MinGW+GNUstep (PR #77255)

2024-01-09 Thread Frederik Carlier via cfe-commits
qmfrederik wrote:

Looks like there was an intermittent issue with GitHub, which should be 
resolved now.  I've squashed and pushed.  Fingers crossed!

https://github.com/llvm/llvm-project/pull/77255
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Objective C: use C++ exceptions on MinGW+GNUstep (PR #77255)

2024-01-09 Thread Frederik Carlier via cfe-commits
qmfrederik wrote:

CI looks green, let me know if there's anything else you need from me on this 
PR!

https://github.com/llvm/llvm-project/pull/77255
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Objective C: use C++ exceptions on MinGW+GNUstep (PR #77255)

2024-01-10 Thread Frederik Carlier via cfe-commits
https://github.com/qmfrederik updated 
https://github.com/llvm/llvm-project/pull/77255

>From 7048dcba9440bb38ddab6140d6a83839938dae8d Mon Sep 17 00:00:00 2001
From: Frederik Carlier 
Date: Thu, 4 Jan 2024 11:10:05 -0800
Subject: [PATCH 1/2] Objective C: use C++ exceptions on MinGW+GNUstep

The GNUstep Objective C runtime (libobjc2) is adding support for
MinGW.  This runtime uses C++ exceptions in that configuration.
---
 clang/lib/CodeGen/CGException.cpp |  7 ++-
 clang/lib/CodeGen/CGObjCGNU.cpp   | 36 -
 .../test/CodeGenObjC/exceptions-personality.m | 53 +++
 clang/test/CodeGenObjC/personality.m  |  5 +-
 clang/test/CodeGenObjCXX/personality.mm   |  5 +-
 5 files changed, 88 insertions(+), 18 deletions(-)
 create mode 100644 clang/test/CodeGenObjC/exceptions-personality.m

diff --git a/clang/lib/CodeGen/CGException.cpp 
b/clang/lib/CodeGen/CGException.cpp
index 0d507da5c1ba92..56a246eb65e0a8 100644
--- a/clang/lib/CodeGen/CGException.cpp
+++ b/clang/lib/CodeGen/CGException.cpp
@@ -156,7 +156,9 @@ static const EHPersonality &getObjCPersonality(const 
TargetInfo &Target,
   case ObjCRuntime::WatchOS:
 return EHPersonality::NeXT_ObjC;
   case ObjCRuntime::GNUstep:
-if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
+if (T.isOSCygMing())
+  return EHPersonality::GNU_CPlusPlus_SEH;
+else if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
   return EHPersonality::GNUstep_ObjC;
 [[fallthrough]];
   case ObjCRuntime::GCC:
@@ -210,7 +212,8 @@ static const EHPersonality &getObjCXXPersonality(const 
TargetInfo &Target,
 return getObjCPersonality(Target, L);
 
   case ObjCRuntime::GNUstep:
-return EHPersonality::GNU_ObjCXX;
+return Target.getTriple().isOSCygMing() ? EHPersonality::GNU_CPlusPlus_SEH
+: EHPersonality::GNU_ObjCXX;
 
   // The GCC runtime's personality function inherently doesn't support
   // mixed EH.  Use the ObjC personality just to avoid returning null.
diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp
index 9443fecf9b7946..9fc0986b05010e 100644
--- a/clang/lib/CodeGen/CGObjCGNU.cpp
+++ b/clang/lib/CodeGen/CGObjCGNU.cpp
@@ -168,6 +168,8 @@ class CGObjCGNU : public CGObjCRuntime {
   /// Does the current target use SEH-based exceptions? False implies
   /// Itanium-style DWARF unwinding.
   bool usesSEHExceptions;
+  /// Does the current target uses C++-based exceptions?
+  bool usesCxxExceptions;
 
   /// Helper to check if we are targeting a specific runtime version or later.
   bool isRuntime(ObjCRuntime::Kind kind, unsigned major, unsigned minor=0) {
@@ -819,12 +821,18 @@ class CGObjCGNUstep : public CGObjCGNU {
   SlotLookupSuperFn.init(&CGM, "objc_slot_lookup_super", SlotTy,
  PtrToObjCSuperTy, SelectorTy);
   // If we're in ObjC++ mode, then we want to make
-  if (usesSEHExceptions) {
-  llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
-  // void objc_exception_rethrow(void)
-  ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
+  llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
+  if (usesCxxExceptions) {
+// void *__cxa_begin_catch(void *e)
+EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy);
+// void __cxa_end_catch(void)
+ExitCatchFn.init(&CGM, "__cxa_end_catch", VoidTy);
+// void objc_exception_rethrow(void*)
+ExceptionReThrowFn.init(&CGM, "__cxa_rethrow", PtrTy);
+  } else if (usesSEHExceptions) {
+// void objc_exception_rethrow(void)
+ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
   } else if (CGM.getLangOpts().CPlusPlus) {
-llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
 // void *__cxa_begin_catch(void *e)
 EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy);
 // void __cxa_end_catch(void)
@@ -833,7 +841,6 @@ class CGObjCGNUstep : public CGObjCGNU {
 ExceptionReThrowFn.init(&CGM, "_Unwind_Resume_or_Rethrow", VoidTy,
 PtrTy);
   } else if (R.getVersion() >= VersionTuple(1, 7)) {
-llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
 // id objc_begin_catch(void *e)
 EnterCatchFn.init(&CGM, "objc_begin_catch", IdTy, PtrTy);
 // void objc_end_catch(void)
@@ -841,7 +848,6 @@ class CGObjCGNUstep : public CGObjCGNU {
 // void _Unwind_Resume_or_Rethrow(void*)
 ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy, PtrTy);
   }
-  llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
   SetPropertyAtomic.init(&CGM, "objc_setProperty_atomic", VoidTy, IdTy,
  SelectorTy, IdTy, PtrDiffTy);
   SetPropertyAtomicCopy.init(&CGM, "objc_setProperty_atomic_copy", VoidTy,
@@ -2126,6 +2132,9 @@ CGObjCGNU::CGObjCGNU(CodeGenModule &cgm, unsig

[clang] Objective C: use C++ exceptions on MinGW+GNUstep (PR #77255)

2024-01-10 Thread Frederik Carlier via cfe-commits
https://github.com/qmfrederik updated 
https://github.com/llvm/llvm-project/pull/77255

>From 7048dcba9440bb38ddab6140d6a83839938dae8d Mon Sep 17 00:00:00 2001
From: Frederik Carlier 
Date: Thu, 4 Jan 2024 11:10:05 -0800
Subject: [PATCH 1/2] Objective C: use C++ exceptions on MinGW+GNUstep

The GNUstep Objective C runtime (libobjc2) is adding support for
MinGW.  This runtime uses C++ exceptions in that configuration.
---
 clang/lib/CodeGen/CGException.cpp |  7 ++-
 clang/lib/CodeGen/CGObjCGNU.cpp   | 36 -
 .../test/CodeGenObjC/exceptions-personality.m | 53 +++
 clang/test/CodeGenObjC/personality.m  |  5 +-
 clang/test/CodeGenObjCXX/personality.mm   |  5 +-
 5 files changed, 88 insertions(+), 18 deletions(-)
 create mode 100644 clang/test/CodeGenObjC/exceptions-personality.m

diff --git a/clang/lib/CodeGen/CGException.cpp 
b/clang/lib/CodeGen/CGException.cpp
index 0d507da5c1ba92..56a246eb65e0a8 100644
--- a/clang/lib/CodeGen/CGException.cpp
+++ b/clang/lib/CodeGen/CGException.cpp
@@ -156,7 +156,9 @@ static const EHPersonality &getObjCPersonality(const 
TargetInfo &Target,
   case ObjCRuntime::WatchOS:
 return EHPersonality::NeXT_ObjC;
   case ObjCRuntime::GNUstep:
-if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
+if (T.isOSCygMing())
+  return EHPersonality::GNU_CPlusPlus_SEH;
+else if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
   return EHPersonality::GNUstep_ObjC;
 [[fallthrough]];
   case ObjCRuntime::GCC:
@@ -210,7 +212,8 @@ static const EHPersonality &getObjCXXPersonality(const 
TargetInfo &Target,
 return getObjCPersonality(Target, L);
 
   case ObjCRuntime::GNUstep:
-return EHPersonality::GNU_ObjCXX;
+return Target.getTriple().isOSCygMing() ? EHPersonality::GNU_CPlusPlus_SEH
+: EHPersonality::GNU_ObjCXX;
 
   // The GCC runtime's personality function inherently doesn't support
   // mixed EH.  Use the ObjC personality just to avoid returning null.
diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp
index 9443fecf9b7946..9fc0986b05010e 100644
--- a/clang/lib/CodeGen/CGObjCGNU.cpp
+++ b/clang/lib/CodeGen/CGObjCGNU.cpp
@@ -168,6 +168,8 @@ class CGObjCGNU : public CGObjCRuntime {
   /// Does the current target use SEH-based exceptions? False implies
   /// Itanium-style DWARF unwinding.
   bool usesSEHExceptions;
+  /// Does the current target uses C++-based exceptions?
+  bool usesCxxExceptions;
 
   /// Helper to check if we are targeting a specific runtime version or later.
   bool isRuntime(ObjCRuntime::Kind kind, unsigned major, unsigned minor=0) {
@@ -819,12 +821,18 @@ class CGObjCGNUstep : public CGObjCGNU {
   SlotLookupSuperFn.init(&CGM, "objc_slot_lookup_super", SlotTy,
  PtrToObjCSuperTy, SelectorTy);
   // If we're in ObjC++ mode, then we want to make
-  if (usesSEHExceptions) {
-  llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
-  // void objc_exception_rethrow(void)
-  ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
+  llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
+  if (usesCxxExceptions) {
+// void *__cxa_begin_catch(void *e)
+EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy);
+// void __cxa_end_catch(void)
+ExitCatchFn.init(&CGM, "__cxa_end_catch", VoidTy);
+// void objc_exception_rethrow(void*)
+ExceptionReThrowFn.init(&CGM, "__cxa_rethrow", PtrTy);
+  } else if (usesSEHExceptions) {
+// void objc_exception_rethrow(void)
+ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
   } else if (CGM.getLangOpts().CPlusPlus) {
-llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
 // void *__cxa_begin_catch(void *e)
 EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy);
 // void __cxa_end_catch(void)
@@ -833,7 +841,6 @@ class CGObjCGNUstep : public CGObjCGNU {
 ExceptionReThrowFn.init(&CGM, "_Unwind_Resume_or_Rethrow", VoidTy,
 PtrTy);
   } else if (R.getVersion() >= VersionTuple(1, 7)) {
-llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
 // id objc_begin_catch(void *e)
 EnterCatchFn.init(&CGM, "objc_begin_catch", IdTy, PtrTy);
 // void objc_end_catch(void)
@@ -841,7 +848,6 @@ class CGObjCGNUstep : public CGObjCGNU {
 // void _Unwind_Resume_or_Rethrow(void*)
 ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy, PtrTy);
   }
-  llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
   SetPropertyAtomic.init(&CGM, "objc_setProperty_atomic", VoidTy, IdTy,
  SelectorTy, IdTy, PtrDiffTy);
   SetPropertyAtomicCopy.init(&CGM, "objc_setProperty_atomic_copy", VoidTy,
@@ -2126,6 +2132,9 @@ CGObjCGNU::CGObjCGNU(CodeGenModule &cgm, unsig

[clang] Objective C: use C++ exceptions on MinGW+GNUstep (PR #77255)

2024-01-10 Thread Frederik Carlier via cfe-commits
https://github.com/qmfrederik updated 
https://github.com/llvm/llvm-project/pull/77255

>From d0d2e2f72965961712dd2df6fe34bd0f7e2befe8 Mon Sep 17 00:00:00 2001
From: Frederik Carlier 
Date: Thu, 4 Jan 2024 11:10:05 -0800
Subject: [PATCH] Objective C: use C++ exceptions on MinGW+GNUstep

The GNUstep Objective C runtime (libobjc2) is adding support for
MinGW.  This runtime uses C++ exceptions in that configuration.
---
 clang/lib/CodeGen/CGException.cpp |  7 ++-
 clang/lib/CodeGen/CGObjCGNU.cpp   | 36 -
 .../test/CodeGenObjC/exceptions-personality.m | 53 +++
 clang/test/CodeGenObjC/personality.m  |  5 +-
 clang/test/CodeGenObjCXX/personality.mm   |  5 +-
 5 files changed, 87 insertions(+), 19 deletions(-)
 create mode 100644 clang/test/CodeGenObjC/exceptions-personality.m

diff --git a/clang/lib/CodeGen/CGException.cpp 
b/clang/lib/CodeGen/CGException.cpp
index 0d507da5c1ba92..56a246eb65e0a8 100644
--- a/clang/lib/CodeGen/CGException.cpp
+++ b/clang/lib/CodeGen/CGException.cpp
@@ -156,7 +156,9 @@ static const EHPersonality &getObjCPersonality(const 
TargetInfo &Target,
   case ObjCRuntime::WatchOS:
 return EHPersonality::NeXT_ObjC;
   case ObjCRuntime::GNUstep:
-if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
+if (T.isOSCygMing())
+  return EHPersonality::GNU_CPlusPlus_SEH;
+else if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
   return EHPersonality::GNUstep_ObjC;
 [[fallthrough]];
   case ObjCRuntime::GCC:
@@ -210,7 +212,8 @@ static const EHPersonality &getObjCXXPersonality(const 
TargetInfo &Target,
 return getObjCPersonality(Target, L);
 
   case ObjCRuntime::GNUstep:
-return EHPersonality::GNU_ObjCXX;
+return Target.getTriple().isOSCygMing() ? EHPersonality::GNU_CPlusPlus_SEH
+: EHPersonality::GNU_ObjCXX;
 
   // The GCC runtime's personality function inherently doesn't support
   // mixed EH.  Use the ObjC personality just to avoid returning null.
diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp
index 9443fecf9b7946..3902bf3db9a8c0 100644
--- a/clang/lib/CodeGen/CGObjCGNU.cpp
+++ b/clang/lib/CodeGen/CGObjCGNU.cpp
@@ -168,6 +168,8 @@ class CGObjCGNU : public CGObjCRuntime {
   /// Does the current target use SEH-based exceptions? False implies
   /// Itanium-style DWARF unwinding.
   bool usesSEHExceptions;
+  /// Does the current target uses C++-based exceptions?
+  bool usesCxxExceptions;
 
   /// Helper to check if we are targeting a specific runtime version or later.
   bool isRuntime(ObjCRuntime::Kind kind, unsigned major, unsigned minor=0) {
@@ -819,12 +821,18 @@ class CGObjCGNUstep : public CGObjCGNU {
   SlotLookupSuperFn.init(&CGM, "objc_slot_lookup_super", SlotTy,
  PtrToObjCSuperTy, SelectorTy);
   // If we're in ObjC++ mode, then we want to make
-  if (usesSEHExceptions) {
-  llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
-  // void objc_exception_rethrow(void)
-  ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
+  llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
+  if (usesCxxExceptions) {
+// void *__cxa_begin_catch(void *e)
+EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy);
+// void __cxa_end_catch(void)
+ExitCatchFn.init(&CGM, "__cxa_end_catch", VoidTy);
+// void objc_exception_rethrow(void*)
+ExceptionReThrowFn.init(&CGM, "__cxa_rethrow", PtrTy);
+  } else if (usesSEHExceptions) {
+// void objc_exception_rethrow(void)
+ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
   } else if (CGM.getLangOpts().CPlusPlus) {
-llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
 // void *__cxa_begin_catch(void *e)
 EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy);
 // void __cxa_end_catch(void)
@@ -833,7 +841,6 @@ class CGObjCGNUstep : public CGObjCGNU {
 ExceptionReThrowFn.init(&CGM, "_Unwind_Resume_or_Rethrow", VoidTy,
 PtrTy);
   } else if (R.getVersion() >= VersionTuple(1, 7)) {
-llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
 // id objc_begin_catch(void *e)
 EnterCatchFn.init(&CGM, "objc_begin_catch", IdTy, PtrTy);
 // void objc_end_catch(void)
@@ -841,7 +848,6 @@ class CGObjCGNUstep : public CGObjCGNU {
 // void _Unwind_Resume_or_Rethrow(void*)
 ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy, PtrTy);
   }
-  llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
   SetPropertyAtomic.init(&CGM, "objc_setProperty_atomic", VoidTy, IdTy,
  SelectorTy, IdTy, PtrDiffTy);
   SetPropertyAtomicCopy.init(&CGM, "objc_setProperty_atomic_copy", VoidTy,
@@ -2126,6 +2132,9 @@ CGObjCGNU::CGObjCGNU(CodeGenModule &cgm, unsigned 

[clang] Objective C: use C++ exceptions on MinGW+GNUstep (PR #77255)

2024-01-10 Thread Frederik Carlier via cfe-commits
https://github.com/qmfrederik updated 
https://github.com/llvm/llvm-project/pull/77255

>From 4d41874bd13cda572105239c0a26f1667c556085 Mon Sep 17 00:00:00 2001
From: Frederik Carlier 
Date: Thu, 4 Jan 2024 11:10:05 -0800
Subject: [PATCH] Objective C: use C++ exceptions on MinGW+GNUstep

The GNUstep Objective C runtime (libobjc2) is adding support for
MinGW.  This runtime uses C++ exceptions in that configuration.
---
 clang/lib/CodeGen/CGException.cpp |  7 ++-
 clang/lib/CodeGen/CGObjCGNU.cpp   | 35 +++-
 .../test/CodeGenObjC/exceptions-personality.m | 53 +++
 clang/test/CodeGenObjC/personality.m  |  5 +-
 clang/test/CodeGenObjCXX/personality.mm   |  5 +-
 5 files changed, 87 insertions(+), 18 deletions(-)
 create mode 100644 clang/test/CodeGenObjC/exceptions-personality.m

diff --git a/clang/lib/CodeGen/CGException.cpp 
b/clang/lib/CodeGen/CGException.cpp
index 0d507da5c1ba92..56a246eb65e0a8 100644
--- a/clang/lib/CodeGen/CGException.cpp
+++ b/clang/lib/CodeGen/CGException.cpp
@@ -156,7 +156,9 @@ static const EHPersonality &getObjCPersonality(const 
TargetInfo &Target,
   case ObjCRuntime::WatchOS:
 return EHPersonality::NeXT_ObjC;
   case ObjCRuntime::GNUstep:
-if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
+if (T.isOSCygMing())
+  return EHPersonality::GNU_CPlusPlus_SEH;
+else if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
   return EHPersonality::GNUstep_ObjC;
 [[fallthrough]];
   case ObjCRuntime::GCC:
@@ -210,7 +212,8 @@ static const EHPersonality &getObjCXXPersonality(const 
TargetInfo &Target,
 return getObjCPersonality(Target, L);
 
   case ObjCRuntime::GNUstep:
-return EHPersonality::GNU_ObjCXX;
+return Target.getTriple().isOSCygMing() ? EHPersonality::GNU_CPlusPlus_SEH
+: EHPersonality::GNU_ObjCXX;
 
   // The GCC runtime's personality function inherently doesn't support
   // mixed EH.  Use the ObjC personality just to avoid returning null.
diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp
index 9443fecf9b7946..ef78ec2dff43a7 100644
--- a/clang/lib/CodeGen/CGObjCGNU.cpp
+++ b/clang/lib/CodeGen/CGObjCGNU.cpp
@@ -168,6 +168,8 @@ class CGObjCGNU : public CGObjCRuntime {
   /// Does the current target use SEH-based exceptions? False implies
   /// Itanium-style DWARF unwinding.
   bool usesSEHExceptions;
+  /// Does the current target uses C++-based exceptions?
+  bool usesCxxExceptions;
 
   /// Helper to check if we are targeting a specific runtime version or later.
   bool isRuntime(ObjCRuntime::Kind kind, unsigned major, unsigned minor=0) {
@@ -819,12 +821,18 @@ class CGObjCGNUstep : public CGObjCGNU {
   SlotLookupSuperFn.init(&CGM, "objc_slot_lookup_super", SlotTy,
  PtrToObjCSuperTy, SelectorTy);
   // If we're in ObjC++ mode, then we want to make
-  if (usesSEHExceptions) {
-  llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
-  // void objc_exception_rethrow(void)
-  ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
+  llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
+  if (usesCxxExceptions) {
+// void *__cxa_begin_catch(void *e)
+EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy);
+// void __cxa_end_catch(void)
+ExitCatchFn.init(&CGM, "__cxa_end_catch", VoidTy);
+// void objc_exception_rethrow(void*)
+ExceptionReThrowFn.init(&CGM, "__cxa_rethrow", PtrTy);
+  } else if (usesSEHExceptions) {
+// void objc_exception_rethrow(void)
+ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
   } else if (CGM.getLangOpts().CPlusPlus) {
-llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
 // void *__cxa_begin_catch(void *e)
 EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy);
 // void __cxa_end_catch(void)
@@ -833,7 +841,6 @@ class CGObjCGNUstep : public CGObjCGNU {
 ExceptionReThrowFn.init(&CGM, "_Unwind_Resume_or_Rethrow", VoidTy,
 PtrTy);
   } else if (R.getVersion() >= VersionTuple(1, 7)) {
-llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
 // id objc_begin_catch(void *e)
 EnterCatchFn.init(&CGM, "objc_begin_catch", IdTy, PtrTy);
 // void objc_end_catch(void)
@@ -841,7 +848,6 @@ class CGObjCGNUstep : public CGObjCGNU {
 // void _Unwind_Resume_or_Rethrow(void*)
 ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy, PtrTy);
   }
-  llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
   SetPropertyAtomic.init(&CGM, "objc_setProperty_atomic", VoidTy, IdTy,
  SelectorTy, IdTy, PtrDiffTy);
   SetPropertyAtomicCopy.init(&CGM, "objc_setProperty_atomic_copy", VoidTy,
@@ -2126,6 +2132,9 @@ CGObjCGNU::CGObjCGNU(CodeGenModule &cgm, unsigned 

[clang] Objective C: use C++ exceptions on MinGW+GNUstep (PR #77255)

2024-01-10 Thread Frederik Carlier via cfe-commits
https://github.com/qmfrederik updated 
https://github.com/llvm/llvm-project/pull/77255

>From 9ede4ecbd65ee35cd2f2f81fb251debf2df189ee Mon Sep 17 00:00:00 2001
From: Frederik Carlier 
Date: Thu, 4 Jan 2024 11:10:05 -0800
Subject: [PATCH] Objective C: use C++ exceptions on MinGW+GNUstep

The GNUstep Objective C runtime (libobjc2) is adding support for
MinGW.  This runtime uses C++ exceptions in that configuration.
---
 clang/lib/CodeGen/CGException.cpp |  7 ++-
 clang/lib/CodeGen/CGObjCGNU.cpp   | 35 +++-
 .../test/CodeGenObjC/exceptions-personality.m | 53 +++
 clang/test/CodeGenObjC/personality.m  |  5 +-
 clang/test/CodeGenObjCXX/personality.mm   |  5 +-
 5 files changed, 87 insertions(+), 18 deletions(-)
 create mode 100644 clang/test/CodeGenObjC/exceptions-personality.m

diff --git a/clang/lib/CodeGen/CGException.cpp 
b/clang/lib/CodeGen/CGException.cpp
index 0d507da5c1ba92..56a246eb65e0a8 100644
--- a/clang/lib/CodeGen/CGException.cpp
+++ b/clang/lib/CodeGen/CGException.cpp
@@ -156,7 +156,9 @@ static const EHPersonality &getObjCPersonality(const 
TargetInfo &Target,
   case ObjCRuntime::WatchOS:
 return EHPersonality::NeXT_ObjC;
   case ObjCRuntime::GNUstep:
-if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
+if (T.isOSCygMing())
+  return EHPersonality::GNU_CPlusPlus_SEH;
+else if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
   return EHPersonality::GNUstep_ObjC;
 [[fallthrough]];
   case ObjCRuntime::GCC:
@@ -210,7 +212,8 @@ static const EHPersonality &getObjCXXPersonality(const 
TargetInfo &Target,
 return getObjCPersonality(Target, L);
 
   case ObjCRuntime::GNUstep:
-return EHPersonality::GNU_ObjCXX;
+return Target.getTriple().isOSCygMing() ? EHPersonality::GNU_CPlusPlus_SEH
+: EHPersonality::GNU_ObjCXX;
 
   // The GCC runtime's personality function inherently doesn't support
   // mixed EH.  Use the ObjC personality just to avoid returning null.
diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp
index 9443fecf9b7946..cd1a0b6a130ff0 100644
--- a/clang/lib/CodeGen/CGObjCGNU.cpp
+++ b/clang/lib/CodeGen/CGObjCGNU.cpp
@@ -168,6 +168,8 @@ class CGObjCGNU : public CGObjCRuntime {
   /// Does the current target use SEH-based exceptions? False implies
   /// Itanium-style DWARF unwinding.
   bool usesSEHExceptions;
+  /// Does the current target uses C++-based exceptions?
+  bool usesCxxExceptions;
 
   /// Helper to check if we are targeting a specific runtime version or later.
   bool isRuntime(ObjCRuntime::Kind kind, unsigned major, unsigned minor=0) {
@@ -819,12 +821,18 @@ class CGObjCGNUstep : public CGObjCGNU {
   SlotLookupSuperFn.init(&CGM, "objc_slot_lookup_super", SlotTy,
  PtrToObjCSuperTy, SelectorTy);
   // If we're in ObjC++ mode, then we want to make
-  if (usesSEHExceptions) {
-  llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
-  // void objc_exception_rethrow(void)
-  ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
+  llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
+  if (usesCxxExceptions) {
+// void *__cxa_begin_catch(void *e)
+EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy);
+// void __cxa_end_catch(void)
+ExitCatchFn.init(&CGM, "__cxa_end_catch", VoidTy);
+// void objc_exception_rethrow(void*)
+ExceptionReThrowFn.init(&CGM, "__cxa_rethrow", PtrTy);
+  } else if (usesSEHExceptions) {
+// void objc_exception_rethrow(void)
+ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy);
   } else if (CGM.getLangOpts().CPlusPlus) {
-llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
 // void *__cxa_begin_catch(void *e)
 EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy);
 // void __cxa_end_catch(void)
@@ -833,7 +841,6 @@ class CGObjCGNUstep : public CGObjCGNU {
 ExceptionReThrowFn.init(&CGM, "_Unwind_Resume_or_Rethrow", VoidTy,
 PtrTy);
   } else if (R.getVersion() >= VersionTuple(1, 7)) {
-llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
 // id objc_begin_catch(void *e)
 EnterCatchFn.init(&CGM, "objc_begin_catch", IdTy, PtrTy);
 // void objc_end_catch(void)
@@ -841,7 +848,6 @@ class CGObjCGNUstep : public CGObjCGNU {
 // void _Unwind_Resume_or_Rethrow(void*)
 ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy, PtrTy);
   }
-  llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
   SetPropertyAtomic.init(&CGM, "objc_setProperty_atomic", VoidTy, IdTy,
  SelectorTy, IdTy, PtrDiffTy);
   SetPropertyAtomicCopy.init(&CGM, "objc_setProperty_atomic_copy", VoidTy,
@@ -2126,6 +2132,9 @@ CGObjCGNU::CGObjCGNU(CodeGenModule &cgm, unsigned 

[clang] Objective C: use C++ exceptions on MinGW+GNUstep (PR #77255)

2024-01-10 Thread Frederik Carlier via cfe-commits
qmfrederik wrote:

@davidchisnall Apologies, that was a fat-finger mistake while addressing the 
latest feedback.  CI is green now.

https://github.com/llvm/llvm-project/pull/77255
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [ObjC]: Make type encoding safe in symbol names (PR #77797)

2024-01-11 Thread Frederik Carlier via cfe-commits
https://github.com/qmfrederik created 
https://github.com/llvm/llvm-project/pull/77797

Type encodings are part of symbol names in the Objective C ABI.  Replace 
characters which are reseved in symbol names:

- ELF: avoid including '@' characters in type encodings
- Windows: avoid including '=' characters in type encodings

>From 5511454cae83ed61062c49a19c56edaf6b9ee1dd Mon Sep 17 00:00:00 2001
From: Frederik Carlier 
Date: Wed, 10 Jan 2024 16:51:18 +
Subject: [PATCH] [ObjC]: Make type encoding safe in symbol names

Type encodings are part of symbol names in the Objective C ABI.  Replace
characters which are reseved in symbol names:

- ELF: avoid including '@' characters in type encodings
- Windows: avoid including '=' characters in type encodings
---
 clang/lib/CodeGen/CGObjCGNU.cpp| 31 ++
 clang/test/CodeGenObjC/encode-test-6.m | 44 +-
 2 files changed, 54 insertions(+), 21 deletions(-)

diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp
index cd1a0b6a130ff0..6e76765cea3db1 100644
--- a/clang/lib/CodeGen/CGObjCGNU.cpp
+++ b/clang/lib/CodeGen/CGObjCGNU.cpp
@@ -1431,12 +1431,25 @@ class CGObjCGNUstep2 : public CGObjCGNUstep {
 const std::string &TypeEncoding) override {
 return GetConstantSelector(Sel, TypeEncoding);
   }
+  std::string GetSymbolNameForTypeEncoding(const std::string &TypeEncoding) {
+std::string MangledTypes = std::string(TypeEncoding);
+// @ is used as a special character in ELF symbol names (used for symbol
+// versioning), so mangle the name to not include it.  Replace it with a
+// character that is not a valid type encoding character (and, being
+// non-printable, never will be!)
+if (CGM.getTriple().isOSBinFormatELF())
+  std::replace(MangledTypes.begin(), MangledTypes.end(),
+'@', '\1');
+// = in dll exported names causes lld to fail when linking on Windows.
+if (CGM.getTriple().isOSWindows())
+  std::replace(MangledTypes.begin(), MangledTypes.end(),
+'=', '\2');
+return MangledTypes;
+  }
   llvm::Constant  *GetTypeString(llvm::StringRef TypeEncoding) {
 if (TypeEncoding.empty())
   return NULLPtr;
-std::string MangledTypes = std::string(TypeEncoding);
-std::replace(MangledTypes.begin(), MangledTypes.end(),
-  '@', '\1');
+std::string MangledTypes = 
GetSymbolNameForTypeEncoding(std::string(TypeEncoding));
 std::string TypesVarName = ".objc_sel_types_" + MangledTypes;
 auto *TypesGlobal = TheModule.getGlobalVariable(TypesVarName);
 if (!TypesGlobal) {
@@ -1453,13 +1466,7 @@ class CGObjCGNUstep2 : public CGObjCGNUstep {
   }
   llvm::Constant *GetConstantSelector(Selector Sel,
   const std::string &TypeEncoding) 
override {
-// @ is used as a special character in symbol names (used for symbol
-// versioning), so mangle the name to not include it.  Replace it with a
-// character that is not a valid type encoding character (and, being
-// non-printable, never will be!)
-std::string MangledTypes = TypeEncoding;
-std::replace(MangledTypes.begin(), MangledTypes.end(),
-  '@', '\1');
+std::string MangledTypes = GetSymbolNameForTypeEncoding(TypeEncoding);
 auto SelVarName = (StringRef(".objc_selector_") + Sel.getAsString() + "_" +
   MangledTypes).str();
 if (auto *GV = TheModule.getNamedGlobal(SelVarName))
@@ -1671,9 +1678,7 @@ class CGObjCGNUstep2 : public CGObjCGNUstep {
 const ObjCIvarDecl *Ivar) override {
 std::string TypeEncoding;
 CGM.getContext().getObjCEncodingForType(Ivar->getType(), TypeEncoding);
-// Prevent the @ from being interpreted as a symbol version.
-std::replace(TypeEncoding.begin(), TypeEncoding.end(),
-  '@', '\1');
+TypeEncoding = GetSymbolNameForTypeEncoding(TypeEncoding);
 const std::string Name = "__objc_ivar_offset_" + ID->getNameAsString()
   + '.' + Ivar->getNameAsString() + '.' + TypeEncoding;
 return Name;
diff --git a/clang/test/CodeGenObjC/encode-test-6.m 
b/clang/test/CodeGenObjC/encode-test-6.m
index 261eb7fb3368b2..1eb6808c7d12f8 100644
--- a/clang/test/CodeGenObjC/encode-test-6.m
+++ b/clang/test/CodeGenObjC/encode-test-6.m
@@ -1,5 +1,14 @@
 // RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -o %t %s
-// RUN: FileCheck < %t %s
+// RUN: FileCheck -check-prefix CHECK-DWARF < %t %s
+
+// RUN: %clang_cc1 -triple x86_64-w64-windows-gnu -emit-llvm 
-fobjc-runtime=gnustep-2.0 -o %t %s
+// RUN: FileCheck -check-prefix CHECK-MINGW < %t %s
+
+// RUN: %clang_cc1 -triple x86_64-windows-msvc -emit-llvm 
-fobjc-runtime=gnustep-2.0 -o %t %s
+// RUN: FileCheck -check-prefix CHECK-MSVC < %t %s
+
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm 
-fobjc-runtime=gnustep-2.0 -o %t %s
+// RUN: FileCheck -check-prefix CHECK-ELF < %t %s
 
 typedef struct {} Z;
 
@@ -13,8 +22,17 @@ -(void)bar:(Z)a {

[clang] [ObjC]: Make type encoding safe in symbol names (PR #77797)

2024-01-11 Thread Frederik Carlier via cfe-commits
qmfrederik wrote:

/cc @davidchisnall 

https://github.com/llvm/llvm-project/pull/77797
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [ObjC]: Make type encoding safe in symbol names (PR #77797)

2024-01-11 Thread Frederik Carlier via cfe-commits
https://github.com/qmfrederik updated 
https://github.com/llvm/llvm-project/pull/77797

>From e71dd4b65f08e4a552c86a457d4a9aca2b5c Mon Sep 17 00:00:00 2001
From: Frederik Carlier 
Date: Wed, 10 Jan 2024 16:51:18 +
Subject: [PATCH] [ObjC]: Make type encoding safe in symbol names

Type encodings are part of symbol names in the Objective C ABI.  Replace
characters which are reseved in symbol names:

- ELF: avoid including '@' characters in type encodings
- Windows: avoid including '=' characters in type encodings
---
 clang/lib/CodeGen/CGObjCGNU.cpp| 30 ++
 clang/test/CodeGenObjC/encode-test-6.m | 44 +-
 2 files changed, 53 insertions(+), 21 deletions(-)

diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp
index cd1a0b6a130ff0..9cc7f32815f7e9 100644
--- a/clang/lib/CodeGen/CGObjCGNU.cpp
+++ b/clang/lib/CodeGen/CGObjCGNU.cpp
@@ -1431,12 +1431,24 @@ class CGObjCGNUstep2 : public CGObjCGNUstep {
 const std::string &TypeEncoding) override {
 return GetConstantSelector(Sel, TypeEncoding);
   }
+  std::string GetSymbolNameForTypeEncoding(const std::string &TypeEncoding) {
+std::string MangledTypes = std::string(TypeEncoding);
+// @ is used as a special character in ELF symbol names (used for symbol
+// versioning), so mangle the name to not include it.  Replace it with a
+// character that is not a valid type encoding character (and, being
+// non-printable, never will be!)
+if (CGM.getTriple().isOSBinFormatELF())
+  std::replace(MangledTypes.begin(), MangledTypes.end(), '@', '\1');
+// = in dll exported names causes lld to fail when linking on Windows.
+if (CGM.getTriple().isOSWindows())
+  std::replace(MangledTypes.begin(), MangledTypes.end(), '=', '\2');
+return MangledTypes;
+  }
   llvm::Constant  *GetTypeString(llvm::StringRef TypeEncoding) {
 if (TypeEncoding.empty())
   return NULLPtr;
-std::string MangledTypes = std::string(TypeEncoding);
-std::replace(MangledTypes.begin(), MangledTypes.end(),
-  '@', '\1');
+std::string MangledTypes =
+GetSymbolNameForTypeEncoding(std::string(TypeEncoding));
 std::string TypesVarName = ".objc_sel_types_" + MangledTypes;
 auto *TypesGlobal = TheModule.getGlobalVariable(TypesVarName);
 if (!TypesGlobal) {
@@ -1453,13 +1465,7 @@ class CGObjCGNUstep2 : public CGObjCGNUstep {
   }
   llvm::Constant *GetConstantSelector(Selector Sel,
   const std::string &TypeEncoding) 
override {
-// @ is used as a special character in symbol names (used for symbol
-// versioning), so mangle the name to not include it.  Replace it with a
-// character that is not a valid type encoding character (and, being
-// non-printable, never will be!)
-std::string MangledTypes = TypeEncoding;
-std::replace(MangledTypes.begin(), MangledTypes.end(),
-  '@', '\1');
+std::string MangledTypes = GetSymbolNameForTypeEncoding(TypeEncoding);
 auto SelVarName = (StringRef(".objc_selector_") + Sel.getAsString() + "_" +
   MangledTypes).str();
 if (auto *GV = TheModule.getNamedGlobal(SelVarName))
@@ -1671,9 +1677,7 @@ class CGObjCGNUstep2 : public CGObjCGNUstep {
 const ObjCIvarDecl *Ivar) override {
 std::string TypeEncoding;
 CGM.getContext().getObjCEncodingForType(Ivar->getType(), TypeEncoding);
-// Prevent the @ from being interpreted as a symbol version.
-std::replace(TypeEncoding.begin(), TypeEncoding.end(),
-  '@', '\1');
+TypeEncoding = GetSymbolNameForTypeEncoding(TypeEncoding);
 const std::string Name = "__objc_ivar_offset_" + ID->getNameAsString()
   + '.' + Ivar->getNameAsString() + '.' + TypeEncoding;
 return Name;
diff --git a/clang/test/CodeGenObjC/encode-test-6.m 
b/clang/test/CodeGenObjC/encode-test-6.m
index 261eb7fb3368b2..1eb6808c7d12f8 100644
--- a/clang/test/CodeGenObjC/encode-test-6.m
+++ b/clang/test/CodeGenObjC/encode-test-6.m
@@ -1,5 +1,14 @@
 // RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -o %t %s
-// RUN: FileCheck < %t %s
+// RUN: FileCheck -check-prefix CHECK-DWARF < %t %s
+
+// RUN: %clang_cc1 -triple x86_64-w64-windows-gnu -emit-llvm 
-fobjc-runtime=gnustep-2.0 -o %t %s
+// RUN: FileCheck -check-prefix CHECK-MINGW < %t %s
+
+// RUN: %clang_cc1 -triple x86_64-windows-msvc -emit-llvm 
-fobjc-runtime=gnustep-2.0 -o %t %s
+// RUN: FileCheck -check-prefix CHECK-MSVC < %t %s
+
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm 
-fobjc-runtime=gnustep-2.0 -o %t %s
+// RUN: FileCheck -check-prefix CHECK-ELF < %t %s
 
 typedef struct {} Z;
 
@@ -13,8 +22,17 @@ -(void)bar:(Z)a {}
 -(void)foo:(Z)a: (char*)b : (Z)c : (double) d {}
 @end
 
-// CHECK: private unnamed_addr constant [14 x i8] c"v16@0:8{?=}16
-// CHECK: private unnamed_addr constant [26 x i8] c"v32@0:8{?=}16*16{?=}24d24
+// CHECK-DWARF: private unnamed_addr cons

[clang] [ObjC]: Make type encoding safe in symbol names (PR #77797)

2024-01-11 Thread Frederik Carlier via cfe-commits
https://github.com/qmfrederik updated 
https://github.com/llvm/llvm-project/pull/77797

>From 34933c7dbba8168f9d0e568ef1d5c49ce22511fe Mon Sep 17 00:00:00 2001
From: Frederik Carlier 
Date: Wed, 10 Jan 2024 16:51:18 +
Subject: [PATCH] [ObjC]: Make type encoding safe in symbol names

Type encodings are part of symbol names in the Objective C ABI.  Replace
characters which are reseved in symbol names:

- ELF: avoid including '@' characters in type encodings
- Windows: avoid including '=' characters in type encodings
---
 clang/lib/CodeGen/CGObjCGNU.cpp| 30 ++
 clang/test/CodeGenObjC/dllstorage.m| 18 +--
 clang/test/CodeGenObjC/encode-test-6.m | 42 --
 3 files changed, 59 insertions(+), 31 deletions(-)

diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp
index cd1a0b6a130ff0..9cc7f32815f7e9 100644
--- a/clang/lib/CodeGen/CGObjCGNU.cpp
+++ b/clang/lib/CodeGen/CGObjCGNU.cpp
@@ -1431,12 +1431,24 @@ class CGObjCGNUstep2 : public CGObjCGNUstep {
 const std::string &TypeEncoding) override {
 return GetConstantSelector(Sel, TypeEncoding);
   }
+  std::string GetSymbolNameForTypeEncoding(const std::string &TypeEncoding) {
+std::string MangledTypes = std::string(TypeEncoding);
+// @ is used as a special character in ELF symbol names (used for symbol
+// versioning), so mangle the name to not include it.  Replace it with a
+// character that is not a valid type encoding character (and, being
+// non-printable, never will be!)
+if (CGM.getTriple().isOSBinFormatELF())
+  std::replace(MangledTypes.begin(), MangledTypes.end(), '@', '\1');
+// = in dll exported names causes lld to fail when linking on Windows.
+if (CGM.getTriple().isOSWindows())
+  std::replace(MangledTypes.begin(), MangledTypes.end(), '=', '\2');
+return MangledTypes;
+  }
   llvm::Constant  *GetTypeString(llvm::StringRef TypeEncoding) {
 if (TypeEncoding.empty())
   return NULLPtr;
-std::string MangledTypes = std::string(TypeEncoding);
-std::replace(MangledTypes.begin(), MangledTypes.end(),
-  '@', '\1');
+std::string MangledTypes =
+GetSymbolNameForTypeEncoding(std::string(TypeEncoding));
 std::string TypesVarName = ".objc_sel_types_" + MangledTypes;
 auto *TypesGlobal = TheModule.getGlobalVariable(TypesVarName);
 if (!TypesGlobal) {
@@ -1453,13 +1465,7 @@ class CGObjCGNUstep2 : public CGObjCGNUstep {
   }
   llvm::Constant *GetConstantSelector(Selector Sel,
   const std::string &TypeEncoding) 
override {
-// @ is used as a special character in symbol names (used for symbol
-// versioning), so mangle the name to not include it.  Replace it with a
-// character that is not a valid type encoding character (and, being
-// non-printable, never will be!)
-std::string MangledTypes = TypeEncoding;
-std::replace(MangledTypes.begin(), MangledTypes.end(),
-  '@', '\1');
+std::string MangledTypes = GetSymbolNameForTypeEncoding(TypeEncoding);
 auto SelVarName = (StringRef(".objc_selector_") + Sel.getAsString() + "_" +
   MangledTypes).str();
 if (auto *GV = TheModule.getNamedGlobal(SelVarName))
@@ -1671,9 +1677,7 @@ class CGObjCGNUstep2 : public CGObjCGNUstep {
 const ObjCIvarDecl *Ivar) override {
 std::string TypeEncoding;
 CGM.getContext().getObjCEncodingForType(Ivar->getType(), TypeEncoding);
-// Prevent the @ from being interpreted as a symbol version.
-std::replace(TypeEncoding.begin(), TypeEncoding.end(),
-  '@', '\1');
+TypeEncoding = GetSymbolNameForTypeEncoding(TypeEncoding);
 const std::string Name = "__objc_ivar_offset_" + ID->getNameAsString()
   + '.' + Ivar->getNameAsString() + '.' + TypeEncoding;
 return Name;
diff --git a/clang/test/CodeGenObjC/dllstorage.m 
b/clang/test/CodeGenObjC/dllstorage.m
index 0dbf1881caa9c0..f45eb7bb6aee78 100644
--- a/clang/test/CodeGenObjC/dllstorage.m
+++ b/clang/test/CodeGenObjC/dllstorage.m
@@ -35,7 +35,7 @@ @implementation J {
 
 // CHECK-IR-DAG: @"OBJC_IVAR_$_J._ivar" = global i32
 
-// CHECK-NF-DAG: @"__objc_ivar_offset_J._ivar.\01" = hidden global i32
+// CHECK-NF-DAG: @"__objc_ivar_offset_J._ivar.@" = hidden global i32
 
 @interface K : J
 @end
@@ -56,7 +56,7 @@ @implementation K {
 
 // CHECK-IR-DAG: @"OBJC_IVAR_$_K._ivar" = global i32
 
-// CHECK-NF-DAG: @"__objc_ivar_offset_K._ivar.\01" = hidden global i32
+// CHECK-NF-DAG: @"__objc_ivar_offset_K._ivar.@" = hidden global i32
 
 __declspec(dllexport)
 @interface L : K
@@ -94,11 +94,11 @@ @implementation L {
 // CHECK-IR-DAG: @"OBJC_IVAR_$_L._package" = global i32
 // CHECK-IR-DAG: @"OBJC_IVAR_$_L._private" = global i32
 
-// CHECK-NF-DAG: @"__objc_ivar_offset_L._none.\01" = hidden global i32
-// CHECK-NF-DAG: @"__objc_ivar_offset_L._public.\01" = dso_local dllexport 
global i32
-// CHECK-NF-DAG: @

[clang] [ObjC]: Make type encoding safe in symbol names (PR #77797)

2024-01-11 Thread Frederik Carlier via cfe-commits

@@ -1,5 +1,14 @@
 // RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -o %t %s
-// RUN: FileCheck < %t %s
+// RUN: FileCheck -check-prefix CHECK-DWARF < %t %s
+
+// RUN: %clang_cc1 -triple x86_64-w64-windows-gnu -emit-llvm 
-fobjc-runtime=gnustep-2.0 -o %t %s
+// RUN: FileCheck -check-prefix CHECK-MINGW < %t %s
+
+// RUN: %clang_cc1 -triple x86_64-windows-msvc -emit-llvm 
-fobjc-runtime=gnustep-2.0 -o %t %s
+// RUN: FileCheck -check-prefix CHECK-MSVC < %t %s
+
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm 
-fobjc-runtime=gnustep-2.0 -o %t %s
+// RUN: FileCheck -check-prefix CHECK-ELF < %t %s

qmfrederik wrote:

Nope, no need for a temp file here.  Fixed that.

https://github.com/llvm/llvm-project/pull/77797
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Set dllimport on Objective C ivar offsets (PR #107604)

2024-09-06 Thread Frederik Carlier via cfe-commits
https://github.com/qmfrederik created 
https://github.com/llvm/llvm-project/pull/107604

Ensures that offsets for instance variables are marked with `dllimport` if the 
interface to which they belong has this attribute.

>From 83511b91e37a9191fcde289b1f302c79e8533b6f Mon Sep 17 00:00:00 2001
From: Frederik Carlier 
Date: Fri, 6 Sep 2024 11:54:59 +
Subject: [PATCH] Set dllimport on Objective C ivar offsets

This commit ensures that offsets for instance variables are marked with 
`dllimport` if the interface to which they belong have this attribute.
---
 clang/lib/CodeGen/CGObjCGNU.cpp | 11 ---
 clang/test/CodeGenObjC/dllstorage.m |  2 +-
 2 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp
index adc7cdbfded880..c78a3ab9830a1c 100644
--- a/clang/lib/CodeGen/CGObjCGNU.cpp
+++ b/clang/lib/CodeGen/CGObjCGNU.cpp
@@ -1698,12 +1698,17 @@ class CGObjCGNUstep2 : public CGObjCGNUstep {
   }
   llvm::Value *EmitIvarOffset(CodeGenFunction &CGF,
   const ObjCInterfaceDecl *Interface,
-  const ObjCIvarDecl *Ivar) override {
-const std::string Name = 
GetIVarOffsetVariableName(Ivar->getContainingInterface(), Ivar);
+  const ObjCIvarDecl *Ivar) override { 
   
+const ObjCInterfaceDecl *ContainingInterface = 
Ivar->getContainingInterface();
+const std::string Name = GetIVarOffsetVariableName(ContainingInterface, 
Ivar);
 llvm::GlobalVariable *IvarOffsetPointer = TheModule.getNamedGlobal(Name);
-if (!IvarOffsetPointer)
+if (!IvarOffsetPointer) {
   IvarOffsetPointer = new llvm::GlobalVariable(TheModule, IntTy, false,
   llvm::GlobalValue::ExternalLinkage, nullptr, Name);
+  if (Ivar->getAccessControl() != ObjCIvarDecl::Private
+ && Ivar->getAccessControl() != ObjCIvarDecl::Package)
+CGM.setGVProperties(IvarOffsetPointer, ContainingInterface);
+}
 CharUnits Align = CGM.getIntAlign();
 llvm::Value *Offset =
 CGF.Builder.CreateAlignedLoad(IntTy, IvarOffsetPointer, Align);
diff --git a/clang/test/CodeGenObjC/dllstorage.m 
b/clang/test/CodeGenObjC/dllstorage.m
index c94f4c9b5804d0..0801fb049f6b45 100644
--- a/clang/test/CodeGenObjC/dllstorage.m
+++ b/clang/test/CodeGenObjC/dllstorage.m
@@ -151,7 +151,7 @@ id f(Q *q) {
 
 // CHECK-IR-DAG: @"OBJC_IVAR_$_M._ivar" = external dllimport global i32
 
-// CHECK-NF-DAG: @"__objc_ivar_offset_M._ivar.@" = external global i32
+// CHECK-NF-DAG: @"__objc_ivar_offset_M._ivar.@" = external dllimport global 
i32
 
 int g(void) {
   @autoreleasepool {

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Set dllimport on Objective C ivar offsets (PR #107604)

2024-09-06 Thread Frederik Carlier via cfe-commits
qmfrederik wrote:

@davidchisnall Would you mind reviewing this PR?  This came up when trying to 
build gnustep-gui using the Windows-native version of LLVM/clang (i.e., not in 
MSYS).

The problem is that all the ivar references are missing the `dllimport` 
annotation.  This is not a problem on MSYS2 because the linker detects this and 
will use pseudo relocations to correct for this.  This does not happen on a 
'native' Windows toolchain, though.

I can confirm that gnustep-gui builds after implementing this patch.

https://github.com/gnustep/tools-windows-msvc/issues/36#issuecomment-2333950722 
has more information.

https://github.com/llvm/llvm-project/pull/107604
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Set dllimport on Objective C ivar offsets (PR #107604)

2024-09-06 Thread Frederik Carlier via cfe-commits
https://github.com/qmfrederik updated 
https://github.com/llvm/llvm-project/pull/107604

>From 2b42d2048e27d30ad77a6793ae36a9b2204b4435 Mon Sep 17 00:00:00 2001
From: Frederik Carlier 
Date: Fri, 6 Sep 2024 11:54:59 +
Subject: [PATCH] Set dllimport on Objective C ivar offsets

This commit ensures that offsets for instance variables are marked with 
`dllimport` if the interface to which they belong have this attribute.
---
 clang/lib/CodeGen/CGObjCGNU.cpp | 11 +--
 clang/test/CodeGenObjC/dllstorage.m |  2 +-
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp
index adc7cdbfded880..6280e9465ecba6 100644
--- a/clang/lib/CodeGen/CGObjCGNU.cpp
+++ b/clang/lib/CodeGen/CGObjCGNU.cpp
@@ -1699,11 +1699,18 @@ class CGObjCGNUstep2 : public CGObjCGNUstep {
   llvm::Value *EmitIvarOffset(CodeGenFunction &CGF,
   const ObjCInterfaceDecl *Interface,
   const ObjCIvarDecl *Ivar) override {
-const std::string Name = 
GetIVarOffsetVariableName(Ivar->getContainingInterface(), Ivar);
+const ObjCInterfaceDecl *ContainingInterface =
+Ivar->getContainingInterface();
+const std::string Name =
+GetIVarOffsetVariableName(ContainingInterface, Ivar);
 llvm::GlobalVariable *IvarOffsetPointer = TheModule.getNamedGlobal(Name);
-if (!IvarOffsetPointer)
+if (!IvarOffsetPointer) {
   IvarOffsetPointer = new llvm::GlobalVariable(TheModule, IntTy, false,
   llvm::GlobalValue::ExternalLinkage, nullptr, Name);
+  if (Ivar->getAccessControl() != ObjCIvarDecl::Private &&
+  Ivar->getAccessControl() != ObjCIvarDecl::Package)
+CGM.setGVProperties(IvarOffsetPointer, ContainingInterface);
+}
 CharUnits Align = CGM.getIntAlign();
 llvm::Value *Offset =
 CGF.Builder.CreateAlignedLoad(IntTy, IvarOffsetPointer, Align);
diff --git a/clang/test/CodeGenObjC/dllstorage.m 
b/clang/test/CodeGenObjC/dllstorage.m
index c94f4c9b5804d0..0801fb049f6b45 100644
--- a/clang/test/CodeGenObjC/dllstorage.m
+++ b/clang/test/CodeGenObjC/dllstorage.m
@@ -151,7 +151,7 @@ id f(Q *q) {
 
 // CHECK-IR-DAG: @"OBJC_IVAR_$_M._ivar" = external dllimport global i32
 
-// CHECK-NF-DAG: @"__objc_ivar_offset_M._ivar.@" = external global i32
+// CHECK-NF-DAG: @"__objc_ivar_offset_M._ivar.@" = external dllimport global 
i32
 
 int g(void) {
   @autoreleasepool {

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Set dllimport on Objective C ivar offsets (PR #107604)

2024-09-06 Thread Frederik Carlier via cfe-commits
https://github.com/qmfrederik updated 
https://github.com/llvm/llvm-project/pull/107604

>From 64073399d392b187297a6aeb1c6634c271103330 Mon Sep 17 00:00:00 2001
From: Frederik Carlier 
Date: Fri, 6 Sep 2024 11:54:59 +
Subject: [PATCH] Set dllimport on Objective C ivar offsets

This commit ensures that offsets for instance variables are marked with 
`dllimport` if the interface to which they belong have this attribute.
---
 clang/lib/CodeGen/CGObjCGNU.cpp | 11 +--
 clang/test/CodeGenObjC/dllstorage.m |  4 ++--
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp
index adc7cdbfded880..6280e9465ecba6 100644
--- a/clang/lib/CodeGen/CGObjCGNU.cpp
+++ b/clang/lib/CodeGen/CGObjCGNU.cpp
@@ -1699,11 +1699,18 @@ class CGObjCGNUstep2 : public CGObjCGNUstep {
   llvm::Value *EmitIvarOffset(CodeGenFunction &CGF,
   const ObjCInterfaceDecl *Interface,
   const ObjCIvarDecl *Ivar) override {
-const std::string Name = 
GetIVarOffsetVariableName(Ivar->getContainingInterface(), Ivar);
+const ObjCInterfaceDecl *ContainingInterface =
+Ivar->getContainingInterface();
+const std::string Name =
+GetIVarOffsetVariableName(ContainingInterface, Ivar);
 llvm::GlobalVariable *IvarOffsetPointer = TheModule.getNamedGlobal(Name);
-if (!IvarOffsetPointer)
+if (!IvarOffsetPointer) {
   IvarOffsetPointer = new llvm::GlobalVariable(TheModule, IntTy, false,
   llvm::GlobalValue::ExternalLinkage, nullptr, Name);
+  if (Ivar->getAccessControl() != ObjCIvarDecl::Private &&
+  Ivar->getAccessControl() != ObjCIvarDecl::Package)
+CGM.setGVProperties(IvarOffsetPointer, ContainingInterface);
+}
 CharUnits Align = CGM.getIntAlign();
 llvm::Value *Offset =
 CGF.Builder.CreateAlignedLoad(IntTy, IvarOffsetPointer, Align);
diff --git a/clang/test/CodeGenObjC/dllstorage.m 
b/clang/test/CodeGenObjC/dllstorage.m
index c94f4c9b5804d0..a6c591b2d79302 100644
--- a/clang/test/CodeGenObjC/dllstorage.m
+++ b/clang/test/CodeGenObjC/dllstorage.m
@@ -112,7 +112,7 @@ @interface M : I {
 // CHECK-IR-DAG: @"OBJC_IVAR_$_M._ivar" = external dllimport global i32
 
 // CHECK-NF-DAG: @"$_OBJC_REF_CLASS_M" = external dllimport global ptr
-// CHECK-NF-DAG: @"__objc_ivar_offset_M._ivar.@" = external global i32
+// CHECK-NF-DAG: @"__objc_ivar_offset_M._ivar.@" = external dllimport global 
i32
 
 __declspec(dllexport)
 __attribute__((__objc_exception__))
@@ -151,7 +151,7 @@ id f(Q *q) {
 
 // CHECK-IR-DAG: @"OBJC_IVAR_$_M._ivar" = external dllimport global i32
 
-// CHECK-NF-DAG: @"__objc_ivar_offset_M._ivar.@" = external global i32
+// CHECK-NF-DAG: @"__objc_ivar_offset_M._ivar.@" = external dllimport global 
i32
 
 int g(void) {
   @autoreleasepool {

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Set dllimport on Objective C ivar offsets (PR #107604)

2024-09-06 Thread Frederik Carlier via cfe-commits

@@ -1699,11 +1699,18 @@ class CGObjCGNUstep2 : public CGObjCGNUstep {
   llvm::Value *EmitIvarOffset(CodeGenFunction &CGF,
   const ObjCInterfaceDecl *Interface,
   const ObjCIvarDecl *Ivar) override {
-const std::string Name = 
GetIVarOffsetVariableName(Ivar->getContainingInterface(), Ivar);
+const ObjCInterfaceDecl *ContainingInterface =
+Ivar->getContainingInterface();
+const std::string Name =
+GetIVarOffsetVariableName(ContainingInterface, Ivar);
 llvm::GlobalVariable *IvarOffsetPointer = TheModule.getNamedGlobal(Name);
-if (!IvarOffsetPointer)
+if (!IvarOffsetPointer) {
   IvarOffsetPointer = new llvm::GlobalVariable(TheModule, IntTy, false,
   llvm::GlobalValue::ExternalLinkage, nullptr, Name);
+  if (Ivar->getAccessControl() != ObjCIvarDecl::Private &&
+  Ivar->getAccessControl() != ObjCIvarDecl::Package)

qmfrederik wrote:

It appears to be a pattern in both [this 
file](https://github.com/llvm/llvm-project/blob/de88d7db7b77141297fbb5638ee1e18d1fba53b8/clang/lib/CodeGen/CGObjCGNU.cpp#L1871-L1876)
 and in 
[CGObjCMac.cpp](https://github.com/llvm/llvm-project/blob/de88d7db7b77141297fbb5638ee1e18d1fba53b8/clang/lib/CodeGen/CGObjCMac.cpp#L6842-L6844)
 to consider an ivar hidden if the visibility is `ObjCIvarDecl::Private` and 
`ObjCIvarDecl::Package` and visible otherwise.

In Objective C, [a scoping directive applies to all the instance variables 
listed after it, up to the next directive or the end of the list and unmarked 
instance variables are 
`@protected`](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocDefiningClasses.html).
 So I don't think we'd ever a hit case where `getAccessControl()` would return 
`ObjCIvarDecl::None`.

So I left it like this for the sake of consistency, but I'm not particularly 
wed to that opinion -- let me know if you prefer me to make the change!

https://github.com/llvm/llvm-project/pull/107604
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Set dllimport on Objective C ivar offsets (PR #107604)

2024-09-07 Thread Frederik Carlier via cfe-commits
https://github.com/qmfrederik updated 
https://github.com/llvm/llvm-project/pull/107604

>From 64073399d392b187297a6aeb1c6634c271103330 Mon Sep 17 00:00:00 2001
From: Frederik Carlier 
Date: Fri, 6 Sep 2024 11:54:59 +
Subject: [PATCH 1/2] Set dllimport on Objective C ivar offsets

This commit ensures that offsets for instance variables are marked with 
`dllimport` if the interface to which they belong have this attribute.
---
 clang/lib/CodeGen/CGObjCGNU.cpp | 11 +--
 clang/test/CodeGenObjC/dllstorage.m |  4 ++--
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/clang/lib/CodeGen/CGObjCGNU.cpp b/clang/lib/CodeGen/CGObjCGNU.cpp
index adc7cdbfded880..6280e9465ecba6 100644
--- a/clang/lib/CodeGen/CGObjCGNU.cpp
+++ b/clang/lib/CodeGen/CGObjCGNU.cpp
@@ -1699,11 +1699,18 @@ class CGObjCGNUstep2 : public CGObjCGNUstep {
   llvm::Value *EmitIvarOffset(CodeGenFunction &CGF,
   const ObjCInterfaceDecl *Interface,
   const ObjCIvarDecl *Ivar) override {
-const std::string Name = 
GetIVarOffsetVariableName(Ivar->getContainingInterface(), Ivar);
+const ObjCInterfaceDecl *ContainingInterface =
+Ivar->getContainingInterface();
+const std::string Name =
+GetIVarOffsetVariableName(ContainingInterface, Ivar);
 llvm::GlobalVariable *IvarOffsetPointer = TheModule.getNamedGlobal(Name);
-if (!IvarOffsetPointer)
+if (!IvarOffsetPointer) {
   IvarOffsetPointer = new llvm::GlobalVariable(TheModule, IntTy, false,
   llvm::GlobalValue::ExternalLinkage, nullptr, Name);
+  if (Ivar->getAccessControl() != ObjCIvarDecl::Private &&
+  Ivar->getAccessControl() != ObjCIvarDecl::Package)
+CGM.setGVProperties(IvarOffsetPointer, ContainingInterface);
+}
 CharUnits Align = CGM.getIntAlign();
 llvm::Value *Offset =
 CGF.Builder.CreateAlignedLoad(IntTy, IvarOffsetPointer, Align);
diff --git a/clang/test/CodeGenObjC/dllstorage.m 
b/clang/test/CodeGenObjC/dllstorage.m
index c94f4c9b5804d0..a6c591b2d79302 100644
--- a/clang/test/CodeGenObjC/dllstorage.m
+++ b/clang/test/CodeGenObjC/dllstorage.m
@@ -112,7 +112,7 @@ @interface M : I {
 // CHECK-IR-DAG: @"OBJC_IVAR_$_M._ivar" = external dllimport global i32
 
 // CHECK-NF-DAG: @"$_OBJC_REF_CLASS_M" = external dllimport global ptr
-// CHECK-NF-DAG: @"__objc_ivar_offset_M._ivar.@" = external global i32
+// CHECK-NF-DAG: @"__objc_ivar_offset_M._ivar.@" = external dllimport global 
i32
 
 __declspec(dllexport)
 __attribute__((__objc_exception__))
@@ -151,7 +151,7 @@ id f(Q *q) {
 
 // CHECK-IR-DAG: @"OBJC_IVAR_$_M._ivar" = external dllimport global i32
 
-// CHECK-NF-DAG: @"__objc_ivar_offset_M._ivar.@" = external global i32
+// CHECK-NF-DAG: @"__objc_ivar_offset_M._ivar.@" = external dllimport global 
i32
 
 int g(void) {
   @autoreleasepool {

>From cf93b21734e179144dac34751860b0aa9d2aedaa Mon Sep 17 00:00:00 2001
From: Frederik Carlier 
Date: Sat, 7 Sep 2024 03:00:46 -0700
Subject: [PATCH 2/2] Add tests to ensure unmarked instance variables are
 considered protected

---
 clang/test/SemaObjC/ivar-access-tests.m | 10 ++
 1 file changed, 10 insertions(+)

diff --git a/clang/test/SemaObjC/ivar-access-tests.m 
b/clang/test/SemaObjC/ivar-access-tests.m
index cd7e09d406adaa..6060dea5ab0f0e 100644
--- a/clang/test/SemaObjC/ivar-access-tests.m
+++ b/clang/test/SemaObjC/ivar-access-tests.m
@@ -2,6 +2,8 @@
 
 @interface MySuperClass
 {
+  int unmarked;
+
 @private
   int private;
 
@@ -17,6 +19,7 @@ @implementation MySuperClass
 - (void) test {
 int access;
 MySuperClass *s = 0;
+access = s->unmarked;
 access = s->private;   
 access = s->protected;
 }
@@ -30,9 +33,11 @@ @implementation MyClass
 - (void) test {
 int access;
 MySuperClass *s = 0;
+access = s->unmarked;
 access = s->private; // expected-error {{instance variable 'private' is 
private}}
 access = s->protected;
 MyClass *m=0;
+access = m->unmarked;
 access = m->private; // expected-error {{instance variable 'private' is 
private}}
 access = m->protected;
 }
@@ -46,9 +51,11 @@ @implementation Deeper
 - (void) test {
 int access;
 MySuperClass *s = 0;
+access = s->unmarked;
 access = s->private; // expected-error {{instance variable 'private' is 
private}}
 access = s->protected;
 MyClass *m=0;
+access = m->unmarked;
 access = m->private; // expected-error {{instance variable 'private' is 
private}}
 access = m->protected;
 }
@@ -61,9 +68,11 @@ @implementation Unrelated
 - (void) test {
 int access;
 MySuperClass *s = 0;
+access = s->unmarked; // expected-error {{instance variable 'unmarked' is 
protected}}
 access = s->private; // expected-error {{instance variable 'private' is 
private}}
 access = s->protected; // expected-error {{instance variable 'protected' 
is protected}}
 MyClass *m=0;
+access = m->unmarked; // expected-error {{instance v

[clang] Set dllimport on Objective C ivar offsets (PR #107604)

2024-09-07 Thread Frederik Carlier via cfe-commits
qmfrederik wrote:

I've added tests to ensure that unmarked ivars are considered protected -- 
which seems to be the case: 
https://github.com/llvm/llvm-project/pull/107604/commits/cf93b21734e179144dac34751860b0aa9d2aedaa
 

https://github.com/llvm/llvm-project/pull/107604
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Set dllimport on Objective C ivar offsets (PR #107604)

2024-09-07 Thread Frederik Carlier via cfe-commits

@@ -1699,11 +1699,18 @@ class CGObjCGNUstep2 : public CGObjCGNUstep {
   llvm::Value *EmitIvarOffset(CodeGenFunction &CGF,
   const ObjCInterfaceDecl *Interface,
   const ObjCIvarDecl *Ivar) override {
-const std::string Name = 
GetIVarOffsetVariableName(Ivar->getContainingInterface(), Ivar);
+const ObjCInterfaceDecl *ContainingInterface =
+Ivar->getContainingInterface();
+const std::string Name =
+GetIVarOffsetVariableName(ContainingInterface, Ivar);
 llvm::GlobalVariable *IvarOffsetPointer = TheModule.getNamedGlobal(Name);
-if (!IvarOffsetPointer)
+if (!IvarOffsetPointer) {
   IvarOffsetPointer = new llvm::GlobalVariable(TheModule, IntTy, false,
   llvm::GlobalValue::ExternalLinkage, nullptr, Name);
+  if (Ivar->getAccessControl() != ObjCIvarDecl::Private &&
+  Ivar->getAccessControl() != ObjCIvarDecl::Package)

qmfrederik wrote:

I think the added tests demonstrate that unmarked ivars are considered 
protected, so let me know if that resolves this comment?

https://github.com/llvm/llvm-project/pull/107604
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Set dllimport on Objective C ivar offsets (PR #107604)

2024-09-07 Thread Frederik Carlier via cfe-commits
qmfrederik wrote:

Thanks @davidchisnall and @compnerd . Let me know if there's anything else you 
need?

https://github.com/llvm/llvm-project/pull/107604
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Set dllimport on Objective C ivar offsets (PR #107604)

2024-09-10 Thread Frederik Carlier via cfe-commits
qmfrederik wrote:

@davidchisnall and @compnerd -- anything else you need before this can get 
merged?


https://github.com/llvm/llvm-project/pull/107604
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Set dllimport on Objective C ivar offsets (PR #107604)

2024-09-11 Thread Frederik Carlier via cfe-commits
qmfrederik wrote:

@davidchisnall Yes, I don't have merge permissions on this repo; so I'd need 
someone to merge it for me :-)

https://github.com/llvm/llvm-project/pull/107604
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits