Skip to content

Commit b0fbff0

Browse files
committed
Addressed some refactoring comments
1 parent 6e776be commit b0fbff0

File tree

3 files changed

+44
-26
lines changed

3 files changed

+44
-26
lines changed

lib/ClangImporter/ImportDecl.cpp

+28-15
Original file line numberDiff line numberDiff line change
@@ -3599,6 +3599,25 @@ namespace {
35993599
std::nullopt);
36003600
}
36013601

3602+
template <typename T>
3603+
std::optional<T>
3604+
matchSwiftAttr(const clang::Decl *decl,
3605+
llvm::ArrayRef<std::pair<llvm::StringRef, T>> patterns) {
3606+
if (!decl || !decl->hasAttrs())
3607+
return std::nullopt;
3608+
3609+
for (const auto *attr : decl->getAttrs()) {
3610+
if (const auto *swiftAttr =
3611+
llvm::dyn_cast<clang::SwiftAttrAttr>(attr)) {
3612+
for (const auto &p : patterns) {
3613+
if (swiftAttr->getAttribute() == p.first)
3614+
return p.second;
3615+
}
3616+
}
3617+
}
3618+
return std::nullopt;
3619+
}
3620+
36023621
/// Emit diagnostics for incorrect usage of SWIFT_RETURNS_RETAINED and
36033622
/// SWIFT_RETURNS_UNRETAINED
36043623
void checkBridgingAttrs(const clang::NamedDecl *decl) {
@@ -3665,21 +3684,15 @@ namespace {
36653684
}
36663685

36673686
if (const auto *returnPtrTy = retType->getAs<clang::PointerType>()) {
3668-
clang::QualType returnPointeeType = returnPtrTy->getPointeeType();
3669-
if (const auto *returnRecordType =
3670-
returnPointeeType->getAs<clang::RecordType>()) {
3671-
clang::RecordDecl *returnRecordDecl = returnRecordType->getDecl();
3672-
for (const auto *attr : returnRecordDecl->getAttrs()) {
3673-
if (const auto *swiftAttr =
3674-
dyn_cast<clang::SwiftAttrAttr>(attr)) {
3675-
if (swiftAttr->getAttribute() ==
3676-
"returns_retained_by_default" ||
3677-
swiftAttr->getAttribute() ==
3678-
"returns_unretained_by_default") {
3679-
unannotatedAPIWarningNeeded = false;
3680-
}
3681-
}
3682-
}
3687+
if (clang::RecordDecl *returnRecordDecl =
3688+
returnPtrTy->getPointeeType()->getAsRecordDecl()) {
3689+
if (auto match = matchSwiftAttr<bool>(
3690+
returnRecordDecl,
3691+
{
3692+
{"returns_retained_by_default", true},
3693+
{"returns_unretained_by_default", true},
3694+
}))
3695+
unannotatedAPIWarningNeeded = false;
36833696
}
36843697
}
36853698

lib/SIL/IR/SILFunctionType.cpp

+15-10
Original file line numberDiff line numberDiff line change
@@ -1376,22 +1376,27 @@ class Conventions {
13761376

13771377
if (auto result = matchSwiftAttr<ResultConvention>(
13781378
decl, {{"returns_unretained", ResultConvention::Unowned},
1379-
{"returns_retained", ResultConvention::Owned}})) {
1379+
{"returns_retained", ResultConvention::Owned}}))
13801380
return result;
1381-
}
13821381

1383-
if (auto *classDecl = tl.getLoweredType()
1384-
.getASTType()
1385-
.getPointer()
1386-
->lookThroughAllOptionalTypes()
1387-
->getClassOrBoundGenericClass()) {
1388-
if (auto *clangRecordDecl = llvm::dyn_cast_or_null<clang::RecordDecl>(
1389-
classDecl->getClangDecl())) {
1382+
const clang::Type *returnTy = nullptr;
1383+
if (const auto *func = llvm::dyn_cast<clang::FunctionDecl>(decl))
1384+
returnTy = func->getReturnType().getTypePtrOrNull();
1385+
else if (const auto *method = llvm::dyn_cast<clang::ObjCMethodDecl>(decl))
1386+
returnTy = method->getReturnType().getTypePtrOrNull();
1387+
if (!returnTy)
1388+
return std::nullopt;
1389+
const clang::Type *desugaredReturnTy =
1390+
returnTy->getUnqualifiedDesugaredType();
1391+
1392+
if (const auto *ptrType =
1393+
llvm::dyn_cast<clang::PointerType>(desugaredReturnTy)) {
1394+
if (clang::RecordDecl *clangRecordDecl =
1395+
ptrType->getPointeeType()->getAsRecordDecl())
13901396
return matchSwiftAttr<ResultConvention>(
13911397
clangRecordDecl,
13921398
{{"returns_unretained_by_default", ResultConvention::Unowned},
13931399
{"returns_retained_by_default", ResultConvention::Owned}});
1394-
}
13951400
}
13961401
return std::nullopt;
13971402
}

test/Interop/Cxx/foreign-reference/Inputs/cxx-functions-and-methods-returning-frt.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -430,4 +430,4 @@ void drelease(
430430
DefaultOwnershipSuppressUnannotatedAPIWarning::RefTypeDefaultRetained *v) {
431431
};
432432

433-
SWIFT_END_NULLABILITY_ANNOTATIONS
433+
SWIFT_END_NULLABILITY_ANNOTATIONS

0 commit comments

Comments
 (0)