Skip to content

[5.9][cxx-interop] evaluate default constructor's unevaluated exception sp… #66477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/IRGen/GenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "clang/Basic/TargetInfo.h"
#include "clang/CodeGen/CodeGenABITypes.h"
#include "clang/CodeGen/ModuleBuilder.h"
#include "clang/Sema/Sema.h"
#include "llvm/IR/GlobalPtrAuthInfo.h"
#include "llvm/IR/GlobalValue.h"
#include "llvm/Support/Compiler.h"
Expand Down Expand Up @@ -4555,6 +4556,23 @@ bool IRGenModule::isForeignExceptionHandlingEnabled() const {
!clangLangOpts.IgnoreExceptions;
}

bool IRGenModule::isCxxNoThrow(clang::FunctionDecl *fd, bool defaultNoThrow) {
auto *fpt = fd->getType()->getAs<clang::FunctionProtoType>();
if (!fpt)
return defaultNoThrow;
if (fpt->getExceptionSpecType() ==
clang::ExceptionSpecificationType::EST_Unevaluated) {
// Clang might not have evaluated the exception spec for
// a constructor, so force the evaluation of it.
auto &clangSema = Context.getClangModuleLoader()->getClangSema();
clangSema.EvaluateImplicitExceptionSpec(fd->getLocation(), fd);
fpt = fd->getType()->getAs<clang::FunctionProtoType>();
if (!fpt)
return defaultNoThrow;
}
return fpt->isNothrow();
}

/// Emit the epilogue for the function.
void IRGenFunction::emitEpilogue() {
if (EarliestIP != AllocaIP)
Expand Down
8 changes: 3 additions & 5 deletions lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3282,11 +3282,9 @@ llvm::CallBase *swift::irgen::emitCXXConstructorCall(
IRGenFunction &IGF, const clang::CXXConstructorDecl *ctor,
llvm::FunctionType *ctorFnType, llvm::Constant *ctorAddress,
llvm::ArrayRef<llvm::Value *> args) {
bool canThrow = IGF.IGM.isForeignExceptionHandlingEnabled();
if (auto *fpt = ctor->getType()->getAs<clang::FunctionProtoType>()) {
if (fpt->isNothrow())
canThrow = false;
}
bool canThrow =
IGF.IGM.isForeignExceptionHandlingEnabled() &&
!IGF.IGM.isCxxNoThrow(const_cast<clang::CXXConstructorDecl *>(ctor));
if (!canThrow)
return IGF.Builder.CreateCall(ctorFnType, ctorAddress, args);
llvm::CallBase *result;
Expand Down
7 changes: 2 additions & 5 deletions lib/IRGen/GenStruct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -720,11 +720,8 @@ namespace {
}
bool canThrow = false;
if (IGF.IGM.isForeignExceptionHandlingEnabled()) {
if (auto *fpt =
destructor->getType()->getAs<clang::FunctionProtoType>()) {
if (!fpt->isNothrow())
canThrow = true;
}
if (!IGF.IGM.isCxxNoThrow(destructor, /*defaultNoThrow=*/true))
canThrow = true;
}
if (canThrow) {
IGF.createExceptionTrapScope([&](llvm::BasicBlock *invokeNormalDest,
Expand Down
3 changes: 3 additions & 0 deletions lib/IRGen/IRGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -1829,6 +1829,9 @@ private: \

bool isForeignExceptionHandlingEnabled() const;

/// Returns true if the given Clang function does not throw exceptions.
bool isCxxNoThrow(clang::FunctionDecl *fd, bool defaultNoThrow = false);

private:
llvm::Constant *
getAddrOfSharedContextDescriptor(LinkEntity entity,
Expand Down
8 changes: 3 additions & 5 deletions lib/IRGen/IRGenSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2887,12 +2887,10 @@ void IRGenSILFunction::visitFunctionRefBaseInst(FunctionRefBaseInst *i) {
FunctionPointer fp =
FunctionPointer::forDirect(fpKind, value, secondaryValue, sig);
// Update the foreign no-throw information if needed.
if (const auto *cd = fn->getClangDecl()) {
if (auto *cd = fn->getClangDecl()) {
if (auto *cfd = dyn_cast<clang::FunctionDecl>(cd)) {
if (auto *cft = cfd->getType()->getAs<clang::FunctionProtoType>()) {
if (cft->isNothrow())
fp.setForeignNoThrow();
}
if (IGM.isCxxNoThrow(const_cast<clang::FunctionDecl *>(cfd)))
fp.setForeignNoThrow();
}
if (IGM.emittedForeignFunctionThunksWithExceptionTraps.count(fnPtr))
fp.setForeignCallCatchesExceptionInThunk();
Expand Down
58 changes: 58 additions & 0 deletions test/Interop/Cxx/exceptions/trap-on-exception-irgen-itanium.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,33 @@ public:
inline ClassWithNoThrowingConstructor() noexcept {}
};

struct StructWithDefaultConstructor {
StructWithDefaultConstructor() = default;

int m = 0;
};


struct NonTrivial {
~NonTrivial() {}
};

struct StructWithDefaultCopyConstructor {
StructWithDefaultCopyConstructor() noexcept {}
StructWithDefaultCopyConstructor(const StructWithDefaultCopyConstructor &) = default;

int m = 0;
NonTrivial _nonTrivialPoison;
};

struct StructWithDefaultDestructor {
StructWithDefaultDestructor() noexcept {}
~StructWithDefaultDestructor() = default;

int m = 0;
NonTrivial _nonTrivialPoison;
};

//--- test.swift

import CxxModule
Expand Down Expand Up @@ -227,6 +254,22 @@ func testClassWithNoThrowingConstructor() -> CInt {
return obj.m
}

func testStructWithDefaultConstructor() -> StructWithDefaultConstructor {
return StructWithDefaultConstructor()
}

func testStructWithDefaultCopyConstructor() -> CInt {
var s = StructWithDefaultCopyConstructor()
let copy = s
return s.m
}

func testStructWithDefaultDestructor() -> CInt {
let s = StructWithDefaultDestructor()
let result = s.m
return result
}

let _ = testFreeFunctionNoThrowOnly()
let _ = testFreeFunctionCalls()
let _ = testMethodCalls()
Expand All @@ -241,6 +284,9 @@ let _ = testClassWithCopyConstructor()
let _ = testClassWithThrowingCopyConstructor()
let _ = testClassWithThrowingConstructor()
let _ = testClassWithNoThrowingConstructor()
let _ = testStructWithDefaultConstructor()
let _ = testStructWithDefaultCopyConstructor()
let _ = testStructWithDefaultDestructor()

// CHECK: define {{.*}} @"$s4test0A23FreeFunctionNoThrowOnlys5Int32VyF"() #[[#SWIFTMETA:]] {
// CHECK-NEXT: :
Expand Down Expand Up @@ -393,6 +439,18 @@ let _ = testClassWithNoThrowingConstructor()
// CHECK-NOT: invoke
// CHECK: }

// CHECK: define {{.*}} @"$s4test0A28StructWithDefaultConstructorSo0bcdE0VyF"() #[[#SWIFTMETA]] {
// CHECK-NOT: invoke
// CHECK: }

// CHECK: define {{.*}} @"$s4test0A32StructWithDefaultCopyConstructors5Int32VyF"() #[[#SWIFTMETA]] {
// CHECK-NOT: invoke
// CHECK: }

// CHECK: define {{.*}} @"$s4test0A27StructWithDefaultDestructors5Int32VyF"() #[[#SWIFTMETA]] {
// CHECK-NOT: invoke
// CHECK: }

// CHECK: i32 @__gxx_personality_v0(...)

// CHECK: attributes #[[#SWIFTMETA]] = {
Expand Down