Skip to content
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

[cxx-interop] Warning unannotated C++ APIs returning SWIFT_SHARED_REF… #76798

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

fahadnayyar
Copy link
Contributor

Adding a warning when C++ APIs (functions or methods) returning C++ foreign reference types or SWIFT_SHARED_REFERENCE types are not annotated with either of SWIFT_RETURNS_RETAINED and SWIFT_RETURNS_UNRETAINED.

@fahadnayyar
Copy link
Contributor Author

@swift-ci please test

"%0 cannot be annotated with both swift_attr('returns_retained') and swift_attr('returns_unretained') attributes", (const clang::NamedDecl*))
ERROR(both_returns_retained_returns_unretained, none,
"%0 cannot be annotated with both swift_attr('returns_retained') and "
"swift_attr('returns_unretained') attributes",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fahadnayyar I think we should suggest people to use the macro instead of directly using swift_attr. What are other similar diagnostics doing?

Comment on lines +3390 to +3411
static bool isForeignReferenceType(const clang::QualType type) {
if (!type->isPointerType())
return false;

auto pointeeType =
dyn_cast<clang::RecordType>(type->getPointeeType().getCanonicalType());
if (pointeeType == nullptr)
return false;

return hasImportAsRefAttr(pointeeType->getDecl());
}

static bool hasImportAsRefAttr(const clang::RecordDecl *decl) {
return decl->hasAttrs() && llvm::any_of(decl->getAttrs(), [](auto *attr) {
if (auto swiftAttr = dyn_cast<clang::SwiftAttrAttr>(attr))
return swiftAttr->getAttribute() == "import_reference" ||
// TODO: Remove this once libSwift hosttools no longer
// requires it.
swiftAttr->getAttribute() == "import_as_ref";
return false;
});
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like these functions are copied-and-pasted from elsewhere in ClangImporter. Let's use the existing API instead.

@egorzhdan egorzhdan added the c++ interop Feature: Interoperability with C++ label Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ interop Feature: Interoperability with C++
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants