Skip to content

[Swiftify] add debug logs for safe interop #81663

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
28 changes: 25 additions & 3 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9106,6 +9106,11 @@ class SwiftifyInfoPrinter {
printAvailabilityOfType("Span");
}

void printDebug() {
printSeparator();
out << "debug: true";
}

private:
ValueDecl *getDecl(StringRef DeclName) {
SmallVector<ValueDecl *, 1> decls;
Expand Down Expand Up @@ -9200,13 +9205,16 @@ static bool SwiftifiablePointerType(Type swiftType) {
(nonnullType->getAnyPointerElementType(PTK) && PTK != PTK_AutoreleasingUnsafeMutablePointer);
}

#define SIW_DBG(x) DEBUG_WITH_TYPE("safe-interop-wrappers", llvm::dbgs() << x)

void ClangImporter::Implementation::swiftify(FuncDecl *MappedDecl) {
if (!SwiftContext.LangOpts.hasFeature(Feature::SafeInteropWrappers))
return;
auto ClangDecl =
dyn_cast_or_null<clang::FunctionDecl>(MappedDecl->getClangDecl());
if (!ClangDecl)
return;
SIW_DBG("Checking " << *ClangDecl << " for bounds and lifetime info\n");

if (ClangDecl->getNumParams() != MappedDecl->getParameters()->size())
return;
Expand Down Expand Up @@ -9239,11 +9247,13 @@ void ClangImporter::Implementation::swiftify(FuncDecl *MappedDecl) {
auto *CAT = ClangDecl->getReturnType()->getAs<clang::CountAttributedType>();
if (SwiftifiableCAT(CAT) && SwiftifiablePointerType(swiftReturnTy)) {
printer.printCountedBy(CAT, SwiftifyInfoPrinter::RETURN_VALUE_INDEX);
SIW_DBG(" Found bounds info '" << clang::QualType(CAT, 0) << "' on return value\n");
attachMacro = true;
}
bool returnHasLifetimeInfo = false;
if (SwiftDeclConverter::getImplicitObjectParamAnnotation<
clang::LifetimeBoundAttr>(ClangDecl)) {
SIW_DBG(" Found lifetimebound attribute on implicit 'this'\n");
printer.printLifetimeboundReturn(SwiftifyInfoPrinter::SELF_PARAM_INDEX,
true);
returnHasLifetimeInfo = true;
Expand All @@ -9256,6 +9266,8 @@ void ClangImporter::Implementation::swiftify(FuncDecl *MappedDecl) {
auto *CAT = clangParamTy->getAs<clang::CountAttributedType>();
if (SwiftifiableCAT(CAT) && SwiftifiablePointerType(swiftParamTy)) {
printer.printCountedBy(CAT, index);
SIW_DBG(" Found bounds info '" << clangParamTy
<< "' on parameter '" << *clangParam << "'\n");
attachMacro = paramHasBoundsInfo = true;
}
bool paramIsStdSpan = registerStdSpanTypeMapping(
Expand All @@ -9264,27 +9276,36 @@ void ClangImporter::Implementation::swiftify(FuncDecl *MappedDecl) {

bool paramHasLifetimeInfo = false;
if (clangParam->hasAttr<clang::NoEscapeAttr>()) {
SIW_DBG(" Found noescape attribute on parameter '" << *clangParam << "'\n");
printer.printNonEscaping(index);
paramHasLifetimeInfo = true;
}
if (clangParam->hasAttr<clang::LifetimeBoundAttr>()) {
SIW_DBG(" Found lifetimebound attribute on parameter '"
<< *clangParam << "'\n");
printer.printLifetimeboundReturn(
index, !paramHasBoundsInfo &&
swiftParamTy->isEscapable());
paramHasLifetimeInfo = true;
returnHasLifetimeInfo = true;
}
if (paramIsStdSpan && paramHasLifetimeInfo)
if (paramIsStdSpan && paramHasLifetimeInfo) {
SIW_DBG(" Found both std::span and lifetime info "
"for parameter '" << *clangParam << "'\n");
attachMacro = true;
}
}
if (returnIsStdSpan && returnHasLifetimeInfo)
if (returnIsStdSpan && returnHasLifetimeInfo) {
SIW_DBG(" Found both std::span and lifetime info for return value\n");
attachMacro = true;
}
printer.printAvailability();
printer.printTypeMapping(typeMapping);

DEBUG_WITH_TYPE("swiftify-import", printer.printDebug());
}

if (attachMacro) {
SIW_DBG("Attaching safe interop macro: " << MacroString << "\n");
if (clang::RawComment *raw =
getClangASTContext().getRawCommentForDeclNoCache(ClangDecl)) {
// swift::RawDocCommentAttr doesn't contain its text directly, but instead
Expand All @@ -9304,6 +9325,7 @@ void ClangImporter::Implementation::swiftify(FuncDecl *MappedDecl) {
}
}
}
#undef SIW_DBG

static bool isUsingMacroName(clang::SourceManager &SM,
clang::SourceLocation loc,
Expand Down
22 changes: 22 additions & 0 deletions lib/Macros/Sources/SwiftMacros/SwiftifyImportMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,19 @@ func parseSpanAvailabilityParam(_ paramAST: LabeledExprSyntax?) throws -> String
return stringLitExpr.representedLiteralValue
}

func parseDebugParam(_ paramAST: LabeledExprSyntax?) throws -> Bool? {
guard let unwrappedParamAST = paramAST else {
return nil
}
guard let label = unwrappedParamAST.label else {
return nil
}
if label.trimmed.text != "debug" {
return nil
}
return try getBoolLiteralValue(unwrappedParamAST.expression)
}

func parseCxxSpansInSignature(
_ signature: FunctionSignatureSyntax,
_ typeMappings: [String: String]?
Expand Down Expand Up @@ -1455,6 +1468,15 @@ public struct SwiftifyImportMacro: PeerMacro {

let argumentList = node.arguments!.as(LabeledExprListSyntax.self)!
var arguments = [LabeledExprSyntax](argumentList)

let debugSetting = try parseDebugParam(arguments.last)
if let debug = debugSetting {
arguments = arguments.dropLast()
if debug {
print("Running _SwiftifyImport on '\(declaration.trimmedDescription)'")
print("with input '\(node.trimmedDescription)'")
}
}
let typeMappings = try parseTypeMappingParam(arguments.last)
if typeMappings != nil {
arguments = arguments.dropLast()
Expand Down
3 changes: 2 additions & 1 deletion stdlib/public/core/SwiftifyImport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public enum _SwiftifyInfo {
@attached(peer, names: overloaded)
public macro _SwiftifyImport(_ paramInfo: _SwiftifyInfo...,
spanAvailability: String? = nil,
typeMappings: [String: String] = [:]) =
typeMappings: [String: String] = [:],
debug: Bool = false) =
#externalMacro(module: "SwiftMacros", type: "SwiftifyImportMacro")
#endif

Expand Down