Skip to content

Commit ebc492a

Browse files
committed
[cxx-interop] Add fix-its to apply 'safe_to_import' and 'self_contained'.
1 parent 40a85f7 commit ebc492a

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

include/swift/AST/ClangModuleLoader.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Preprocessor;
2727
class Sema;
2828
class TargetInfo;
2929
class Type;
30+
class SourceLocation;
3031
} // namespace clang
3132

3233
namespace swift {
@@ -294,6 +295,8 @@ class ClangModuleLoader : public ModuleLoader {
294295

295296
virtual const clang::TypedefType *
296297
getTypeDefForCXXCFOptionsDefinition(const clang::Decl *candidateDecl) = 0;
298+
299+
virtual SourceLoc importSourceLocation(clang::SourceLocation loc) = 0;
297300
};
298301

299302
/// Describes a C++ template instantiation error.

include/swift/ClangImporter/ClangImporter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,8 @@ class ClangImporter final : public ClangModuleLoader {
565565

566566
const clang::TypedefType *getTypeDefForCXXCFOptionsDefinition(
567567
const clang::Decl *candidateDecl) override;
568+
569+
SourceLoc importSourceLocation(clang::SourceLocation loc) override;
568570
};
569571

570572
ImportDecl *createImportDecl(ASTContext &Ctx, DeclContext *DC, ClangNode ClangN,

lib/ClangImporter/ClangImporter.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6316,6 +6316,12 @@ void ClangImporter::diagnoseMemberValue(const DeclName &name,
63166316
}
63176317
}
63186318

6319+
SourceLoc ClangImporter::importSourceLocation(clang::SourceLocation loc) {
6320+
auto &bufferImporter = Impl.getBufferImporterForDiagnostics();
6321+
return bufferImporter.resolveSourceLocation(
6322+
getClangASTContext().getSourceManager(), loc);
6323+
}
6324+
63196325
static bool hasImportAsRefAttr(const clang::RecordDecl *decl) {
63206326
return decl->hasAttrs() && llvm::any_of(decl->getAttrs(), [](auto *attr) {
63216327
if (auto swiftAttr = dyn_cast<clang::SwiftAttrAttr>(attr))

lib/Sema/CSDiagnostics.cpp

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3968,6 +3968,14 @@ void MissingMemberFailure::diagnoseUnsafeCxxMethod(SourceLoc loc,
39683968
->getName()
39693969
.str();
39703970

3971+
auto baseClangLoc = cxxMethod->getParent()->getLocation();
3972+
auto baseSwiftLoc =
3973+
ctx.getClangModuleLoader()->importSourceLocation(baseClangLoc);
3974+
3975+
auto methodClangLoc = cxxMethod->getLocation();
3976+
auto methodSwiftLoc =
3977+
ctx.getClangModuleLoader()->importSourceLocation(methodClangLoc);
3978+
39713979
// Rewrite front() and back() as first and last.
39723980
if ((name.getBaseIdentifier().is("front") ||
39733981
name.getBaseIdentifier().is("back")) &&
@@ -4024,8 +4032,11 @@ void MissingMemberFailure::diagnoseUnsafeCxxMethod(SourceLoc loc,
40244032
name.getBaseIdentifier().str(), returnTypeStr);
40254033
ctx.Diags.diagnose(loc, diag::projection_may_return_interior_ptr,
40264034
name.getBaseIdentifier().str());
4027-
ctx.Diags.diagnose(loc, diag::mark_safe_to_import,
4028-
name.getBaseIdentifier().str());
4035+
ctx.Diags
4036+
.diagnose(methodSwiftLoc, diag::mark_safe_to_import,
4037+
name.getBaseIdentifier().str())
4038+
.fixItInsert(methodSwiftLoc,
4039+
" __attribute__((swift_attr(\"safe_to_import\"))) ");
40294040
} else if (cxxMethod->getReturnType()->isReferenceType()) {
40304041
// Rewrite a call to .at(42) as a subscript.
40314042
if (name.getBaseIdentifier().is("at") &&
@@ -4052,8 +4063,11 @@ void MissingMemberFailure::diagnoseUnsafeCxxMethod(SourceLoc loc,
40524063
name.getBaseIdentifier().str(), returnTypeStr);
40534064
ctx.Diags.diagnose(loc, diag::projection_may_return_interior_ptr,
40544065
name.getBaseIdentifier().str());
4055-
ctx.Diags.diagnose(loc, diag::mark_safe_to_import,
4056-
name.getBaseIdentifier().str());
4066+
ctx.Diags
4067+
.diagnose(methodSwiftLoc, diag::mark_safe_to_import,
4068+
name.getBaseIdentifier().str())
4069+
.fixItInsert(methodSwiftLoc,
4070+
" __attribute__((swift_attr(\"safe_to_import\"))) ");
40574071
}
40584072
} else if (cxxMethod->getReturnType()->isRecordType()) {
40594073
if (auto cxxRecord = dyn_cast<clang::CXXRecordDecl>(
@@ -4071,9 +4085,15 @@ void MissingMemberFailure::diagnoseUnsafeCxxMethod(SourceLoc loc,
40714085
name.getBaseIdentifier().str(), returnTypeStr);
40724086
ctx.Diags.diagnose(loc, diag::projection_may_return_interior_ptr,
40734087
name.getBaseIdentifier().str());
4074-
ctx.Diags.diagnose(loc, diag::mark_safe_to_import,
4075-
name.getBaseIdentifier().str());
4076-
ctx.Diags.diagnose(loc, diag::mark_self_contained, returnTypeStr);
4088+
ctx.Diags
4089+
.diagnose(methodSwiftLoc, diag::mark_safe_to_import,
4090+
name.getBaseIdentifier().str())
4091+
.fixItInsert(methodSwiftLoc,
4092+
" __attribute__((swift_attr(\"safe_to_import\"))) ");
4093+
ctx.Diags
4094+
.diagnose(baseSwiftLoc, diag::mark_self_contained, returnTypeStr)
4095+
.fixItInsert(baseSwiftLoc,
4096+
"__attribute__((swift_attr(\"self_contained\"))) ");
40774097
}
40784098
}
40794099
}

0 commit comments

Comments
 (0)