Skip to content

Commit 4ab1d5d

Browse files
committed
[ClangImporter] refactor swiftify before swiftifyProtocol (NFC)
This is a pure refactor to make it easier to follow the upcoming addition of swiftifyProtocol.
1 parent 6f1936f commit 4ab1d5d

File tree

2 files changed

+56
-40
lines changed

2 files changed

+56
-40
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9172,7 +9172,7 @@ class SwiftifyInfoPrinter {
91729172
llvm::StringMap<std::string> typeMapping;
91739173
SwiftifyInfoPrinter(clang::ASTContext &ctx, ASTContext &SwiftContext, llvm::raw_ostream &out, MacroDecl &SwiftifyImportDecl)
91749174
: ctx(ctx), SwiftContext(SwiftContext), out(out), SwiftifyImportDecl(SwiftifyImportDecl) {
9175-
out << "@_SwiftifyImport(";
9175+
out << "(";
91769176
}
91779177
~SwiftifyInfoPrinter() { out << ")"; }
91789178

@@ -9516,43 +9516,30 @@ static StringRef getAttributeName(const clang::CountAttributedType *CAT) {
95169516
}
95179517
}
95189518

9519-
void ClangImporter::Implementation::swiftify(AbstractFunctionDecl *MappedDecl) {
9520-
if (!SwiftContext.LangOpts.hasFeature(Feature::SafeInteropWrappers) ||
9521-
SwiftContext.ClangImporterOpts.DisableSafeInteropWrappers)
9522-
return;
9523-
auto ClangDecl =
9524-
dyn_cast_or_null<clang::FunctionDecl>(MappedDecl->getClangDecl());
9525-
if (!ClangDecl)
9526-
return;
9519+
bool ClangImporter::Implementation::swiftifyImpl(
9520+
SwiftifyInfoPrinter &printer, const AbstractFunctionDecl *MappedDecl,
9521+
const clang::FunctionDecl *ClangDecl) {
95279522
SIW_DBG("Checking " << *ClangDecl << " for bounds and lifetime info\n");
95289523

95299524
// FIXME: for private macro generated functions we do not serialize the
95309525
// SILFunction's body anywhere triggering assertions.
95319526
if (ClangDecl->getAccess() == clang::AS_protected ||
95329527
ClangDecl->getAccess() == clang::AS_private)
9533-
return;
9534-
9535-
MacroDecl *SwiftifyImportDecl = dyn_cast_or_null<MacroDecl>(getKnownSingleDecl(SwiftContext, "_SwiftifyImport"));
9536-
if (!SwiftifyImportDecl)
9537-
return;
9528+
return false;
95389529

95399530
{
95409531
UnaliasedInstantiationVisitor visitor;
95419532
visitor.TraverseType(ClangDecl->getType());
95429533
if (visitor.hasUnaliasedInstantiation)
9543-
return;
9534+
return false;
95449535
}
95459536

9546-
llvm::SmallString<128> MacroString;
95479537
// We only attach the macro if it will produce an overload. Any __counted_by
95489538
// will produce an overload, since UnsafeBufferPointer is still an improvement
95499539
// over UnsafePointer, but std::span will only produce an overload if it also
95509540
// has lifetime information, since std::span already contains bounds info.
95519541
bool attachMacro = false;
95529542
{
9553-
llvm::raw_svector_ostream out(MacroString);
9554-
9555-
SwiftifyInfoPrinter printer(getClangASTContext(), SwiftContext, out, *SwiftifyImportDecl);
95569543
Type swiftReturnTy;
95579544
if (const auto *funcDecl = dyn_cast<FuncDecl>(MappedDecl))
95589545
swiftReturnTy = funcDecl->getResultInterfaceType();
@@ -9568,7 +9555,7 @@ void ClangImporter::Implementation::swiftify(AbstractFunctionDecl *MappedDecl) {
95689555
SIW_DBG(" Found bounds info '" << clang::QualType(CAT, 0) << "' on return value\n");
95699556
attachMacro = true;
95709557
}
9571-
auto dependsOnClass = [](ParamDecl *fromParam) {
9558+
auto dependsOnClass = [](const ParamDecl *fromParam) {
95729559
return fromParam->getInterfaceType()->isAnyClassReferenceType();
95739560
};
95749561
bool returnHasLifetimeInfo = false;
@@ -9600,7 +9587,7 @@ void ClangImporter::Implementation::swiftify(AbstractFunctionDecl *MappedDecl) {
96009587
int mappedIndex = index < selfParamIndex ? index :
96019588
index > selfParamIndex ? index - 1 :
96029589
SwiftifyInfoPrinter::SELF_PARAM_INDEX;
9603-
ParamDecl *swiftParam = nullptr;
9590+
const ParamDecl *swiftParam = nullptr;
96049591
if (mappedIndex == SwiftifyInfoPrinter::SELF_PARAM_INDEX) {
96059592
swiftParam = MappedDecl->getImplicitSelfDecl(/*createIfNeeded*/true);
96069593
} else {
@@ -9659,29 +9646,51 @@ void ClangImporter::Implementation::swiftify(AbstractFunctionDecl *MappedDecl) {
96599646
SIW_DBG(" Found both std::span and lifetime info for return value\n");
96609647
attachMacro = true;
96619648
}
9649+
}
9650+
return attachMacro;
9651+
}
9652+
9653+
void ClangImporter::Implementation::swiftify(AbstractFunctionDecl *MappedDecl) {
9654+
if (!SwiftContext.LangOpts.hasFeature(Feature::SafeInteropWrappers) ||
9655+
SwiftContext.ClangImporterOpts.DisableSafeInteropWrappers)
9656+
return;
9657+
auto ClangDecl = dyn_cast_or_null<clang::FunctionDecl>(MappedDecl->getClangDecl());
9658+
if (!ClangDecl)
9659+
return;
9660+
9661+
MacroDecl *SwiftifyImportDecl = dyn_cast_or_null<MacroDecl>(getKnownSingleDecl(SwiftContext, "_SwiftifyImport"));
9662+
if (!SwiftifyImportDecl)
9663+
return;
9664+
9665+
llvm::SmallString<128> MacroString;
9666+
{
9667+
llvm::raw_svector_ostream out(MacroString);
9668+
out << "@_SwiftifyImport";
9669+
9670+
SwiftifyInfoPrinter printer(getClangASTContext(), SwiftContext, out, *SwiftifyImportDecl);
9671+
if (!swiftifyImpl(printer, MappedDecl, ClangDecl))
9672+
return;
96629673
printer.printAvailability();
96639674
printer.printTypeMapping();
96649675
}
96659676

9666-
if (attachMacro) {
9667-
SIW_DBG("Attaching safe interop macro: " << MacroString << "\n");
9668-
if (clang::RawComment *raw =
9669-
getClangASTContext().getRawCommentForDeclNoCache(ClangDecl)) {
9670-
// swift::RawDocCommentAttr doesn't contain its text directly, but instead
9671-
// references the source range of the parsed comment. Instead of creating
9672-
// a new source file just to parse the doc comment, we can add the
9673-
// comment to the macro invocation attribute, which the macro has access
9674-
// to. Waiting until we know that the macro will be attached before
9675-
// emitting the comment to the string, despite the comment occurring
9676-
// first, avoids copying a bunch of potentially long comments for nodes
9677-
// that don't end up with wrappers.
9678-
auto commentString =
9679-
raw->getRawText(getClangASTContext().getSourceManager());
9680-
importNontrivialAttribute(MappedDecl,
9681-
(commentString + "\n" + MacroString).str());
9682-
} else {
9683-
importNontrivialAttribute(MappedDecl, MacroString);
9684-
}
9677+
SIW_DBG("Attaching safe interop macro: " << MacroString << "\n");
9678+
if (clang::RawComment *raw =
9679+
getClangASTContext().getRawCommentForDeclNoCache(ClangDecl)) {
9680+
// swift::RawDocCommentAttr doesn't contain its text directly, but instead
9681+
// references the source range of the parsed comment. Instead of creating
9682+
// a new source file just to parse the doc comment, we can add the
9683+
// comment to the macro invocation attribute, which the macro has access
9684+
// to. Waiting until we know that the macro will be attached before
9685+
// emitting the comment to the string, despite the comment occurring
9686+
// first, avoids copying a bunch of potentially long comments for nodes
9687+
// that don't end up with wrappers.
9688+
auto commentString =
9689+
raw->getRawText(getClangASTContext().getSourceManager());
9690+
importNontrivialAttribute(MappedDecl,
9691+
(commentString + "\n" + MacroString).str());
9692+
} else {
9693+
importNontrivialAttribute(MappedDecl, MacroString);
96859694
}
96869695
}
96879696
#undef SIW_DBG

lib/ClangImporter/ImporterImpl.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ class QualType;
8080
class TypedefNameDecl;
8181
}
8282

83+
namespace {
84+
class SwiftifyInfoPrinter;
85+
}
86+
8387
namespace swift {
8488

8589
class ASTContext;
@@ -1827,6 +1831,9 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
18271831
void addOptionSetTypealiases(NominalTypeDecl *nominal);
18281832

18291833
void swiftify(AbstractFunctionDecl *MappedDecl);
1834+
bool swiftifyImpl(SwiftifyInfoPrinter &printer,
1835+
const AbstractFunctionDecl *MappedDecl,
1836+
const clang::FunctionDecl *ClangDecl);
18301837

18311838
/// Find the lookup table that corresponds to the given Clang module.
18321839
///

0 commit comments

Comments
 (0)