Skip to content

Commit 8e802be

Browse files
authored
Merge pull request swiftlang#81663 from hnrklssn/swiftify-debug-logging
[Swiftify] add debug logs for safe interop
2 parents 31b5d55 + ea5a41e commit 8e802be

File tree

5 files changed

+64
-11
lines changed

5 files changed

+64
-11
lines changed

include/swift/AST/Attr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2344,6 +2344,8 @@ class CustomAttr final : public DeclAttribute {
23442344

23452345
bool isEquivalent(const CustomAttr *other, Decl *attachedTo) const;
23462346

2347+
void printCustomAttr(ASTPrinter &Printer, const PrintOptions &Options) const;
2348+
23472349
private:
23482350
friend class CustomAttrNominalRequest;
23492351
void resetTypeInformation(TypeExpr *repr);

lib/AST/ASTPrinter.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,8 @@ class PrintAST : public ASTVisitor<PrintAST> {
11031103
void printRequirement(const InverseRequirement &inverse,
11041104
bool forInherited);
11051105

1106+
void printArgumentList(ArgumentList *args, bool forSubscript = false);
1107+
11061108
private:
11071109
bool shouldPrint(const Decl *D, bool Notify = false);
11081110
bool shouldPrintPattern(const Pattern *P);
@@ -1138,8 +1140,6 @@ class PrintAST : public ASTVisitor<PrintAST> {
11381140
void printFunctionParameters(AbstractFunctionDecl *AFD);
11391141

11401142
void printArgument(const Argument &arg);
1141-
1142-
void printArgumentList(ArgumentList *args, bool forSubscript = false);
11431143

11441144
void printAvailabilitySpec(AvailabilitySpec *spec);
11451145

@@ -4843,6 +4843,19 @@ void PrintAST::visitMacroExpansionDecl(MacroExpansionDecl *decl) {
48434843
Printer << ')';
48444844
}
48454845

4846+
void CustomAttr::printCustomAttr(ASTPrinter &Printer, const PrintOptions &Options) const {
4847+
Printer.callPrintNamePre(PrintNameContext::Attribute);
4848+
Printer << "@";
4849+
if (auto type = getType())
4850+
type.print(Printer, Options);
4851+
else
4852+
getTypeRepr()->print(Printer, Options);
4853+
Printer.printNamePost(PrintNameContext::Attribute);
4854+
if (hasArgs() && Options.PrintExprs) {
4855+
PrintAST(Printer, Options).printArgumentList(argList);
4856+
}
4857+
}
4858+
48464859
void PrintAST::visitIntegerLiteralExpr(IntegerLiteralExpr *expr) {
48474860
Printer << expr->getDigitsText();
48484861
}

lib/AST/Attr.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,14 +1453,8 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
14531453
}
14541454

14551455
case DeclAttrKind::Custom: {
1456-
Printer.callPrintNamePre(PrintNameContext::Attribute);
1457-
Printer << "@";
14581456
auto *attr = cast<CustomAttr>(this);
1459-
if (auto type = attr->getType())
1460-
type.print(Printer, Options);
1461-
else
1462-
attr->getTypeRepr()->print(Printer, Options);
1463-
Printer.printNamePost(PrintNameContext::Attribute);
1457+
attr->printCustomAttr(Printer, Options);
14641458
break;
14651459
}
14661460

lib/ClangImporter/ImportDecl.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9421,13 +9421,16 @@ void ClangImporter::Implementation::addOptionSetTypealiases(
94219421
selfType);
94229422
}
94239423

9424+
#define SIW_DBG(x) DEBUG_WITH_TYPE("safe-interop-wrappers", llvm::dbgs() << x)
9425+
94249426
void ClangImporter::Implementation::swiftify(AbstractFunctionDecl *MappedDecl) {
94259427
if (!SwiftContext.LangOpts.hasFeature(Feature::SafeInteropWrappers))
94269428
return;
94279429
auto ClangDecl =
94289430
dyn_cast_or_null<clang::FunctionDecl>(MappedDecl->getClangDecl());
94299431
if (!ClangDecl)
94309432
return;
9433+
SIW_DBG("Checking " << *ClangDecl << " for bounds and lifetime info\n");
94319434

94329435
// FIXME: for private macro generated functions we do not serialize the
94339436
// SILFunction's body anywhere triggering assertions.
@@ -9483,11 +9486,13 @@ void ClangImporter::Implementation::swiftify(AbstractFunctionDecl *MappedDecl) {
94839486
auto *CAT = ClangDecl->getReturnType()->getAs<clang::CountAttributedType>();
94849487
if (SwiftifiableCAT(getClangASTContext(), CAT, swiftReturnTy)) {
94859488
printer.printCountedBy(CAT, SwiftifyInfoPrinter::RETURN_VALUE_INDEX);
9489+
SIW_DBG(" Found bounds info '" << clang::QualType(CAT, 0) << "' on return value\n");
94869490
attachMacro = true;
94879491
}
94889492
bool returnHasLifetimeInfo = false;
94899493
if (SwiftDeclConverter::getImplicitObjectParamAnnotation<
94909494
clang::LifetimeBoundAttr>(ClangDecl)) {
9495+
SIW_DBG(" Found lifetimebound attribute on implicit 'this'\n");
94919496
printer.printLifetimeboundReturn(SwiftifyInfoPrinter::SELF_PARAM_INDEX,
94929497
true);
94939498
returnHasLifetimeInfo = true;
@@ -9500,6 +9505,8 @@ void ClangImporter::Implementation::swiftify(AbstractFunctionDecl *MappedDecl) {
95009505
auto *CAT = clangParamTy->getAs<clang::CountAttributedType>();
95019506
if (SwiftifiableCAT(getClangASTContext(), CAT, swiftParamTy)) {
95029507
printer.printCountedBy(CAT, index);
9508+
SIW_DBG(" Found bounds info '" << clangParamTy
9509+
<< "' on parameter '" << *clangParam << "'\n");
95039510
attachMacro = paramHasBoundsInfo = true;
95049511
}
95059512
bool paramIsStdSpan = registerStdSpanTypeMapping(
@@ -9508,28 +9515,37 @@ void ClangImporter::Implementation::swiftify(AbstractFunctionDecl *MappedDecl) {
95089515

95099516
bool paramHasLifetimeInfo = false;
95109517
if (clangParam->hasAttr<clang::NoEscapeAttr>()) {
9518+
SIW_DBG(" Found noescape attribute on parameter '" << *clangParam << "'\n");
95119519
printer.printNonEscaping(index);
95129520
paramHasLifetimeInfo = true;
95139521
}
95149522
if (clangParam->hasAttr<clang::LifetimeBoundAttr>()) {
9523+
SIW_DBG(" Found lifetimebound attribute on parameter '"
9524+
<< *clangParam << "'\n");
95159525
// If this parameter has bounds info we will tranform it into a Span,
95169526
// so then it will no longer be Escapable.
95179527
bool willBeEscapable = swiftParamTy->isEscapable() && !paramHasBoundsInfo;
95189528
printer.printLifetimeboundReturn(index, willBeEscapable);
95199529
paramHasLifetimeInfo = true;
95209530
returnHasLifetimeInfo = true;
95219531
}
9522-
if (paramIsStdSpan && paramHasLifetimeInfo)
9532+
if (paramIsStdSpan && paramHasLifetimeInfo) {
9533+
SIW_DBG(" Found both std::span and lifetime info "
9534+
"for parameter '" << *clangParam << "'\n");
95239535
attachMacro = true;
9536+
}
95249537
}
9525-
if (returnIsStdSpan && returnHasLifetimeInfo)
9538+
if (returnIsStdSpan && returnHasLifetimeInfo) {
9539+
SIW_DBG(" Found both std::span and lifetime info for return value\n");
95269540
attachMacro = true;
9541+
}
95279542
printer.printAvailability();
95289543
printer.printTypeMapping(typeMapping);
95299544

95309545
}
95319546

95329547
if (attachMacro) {
9548+
SIW_DBG("Attaching safe interop macro: " << MacroString << "\n");
95339549
if (clang::RawComment *raw =
95349550
getClangASTContext().getRawCommentForDeclNoCache(ClangDecl)) {
95359551
// swift::RawDocCommentAttr doesn't contain its text directly, but instead
@@ -9549,6 +9565,7 @@ void ClangImporter::Implementation::swiftify(AbstractFunctionDecl *MappedDecl) {
95499565
}
95509566
}
95519567
}
9568+
#undef SIW_DBG
95529569

95539570
static bool isUsingMacroName(clang::SourceManager &SM,
95549571
clang::SourceLocation loc,

lib/Sema/TypeCheckMacros.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
#include "swift/Subsystems.h"
5353
#include "llvm/Config/config.h"
5454

55+
#define DEBUG_TYPE "macros"
56+
5557
using namespace swift;
5658

5759
/// Translate an argument provided as a string literal into an identifier,
@@ -1201,6 +1203,11 @@ evaluateFreestandingMacro(FreestandingMacroExpansion *expansion,
12011203

12021204
PrettyStackTraceFreestandingMacroExpansion debugStack(
12031205
"expanding freestanding macro", expansion);
1206+
LLVM_DEBUG(
1207+
llvm::dbgs() << "\nexpanding macro:\n";
1208+
macro->print(llvm::dbgs(), PrintOptions::printEverything());
1209+
llvm::dbgs() << "\n";
1210+
);
12041211

12051212
// Builtin macros are handled via ASTGen.
12061213
auto *astGenSourceFile = sourceFile->getExportedSourceFile();
@@ -1527,6 +1534,18 @@ static SourceFile *evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo,
15271534
auto *astGenAttrSourceFile = attrSourceFile->getExportedSourceFile();
15281535
if (!astGenAttrSourceFile)
15291536
return nullptr;
1537+
LLVM_DEBUG(
1538+
StreamPrinter P(llvm::dbgs());
1539+
llvm::dbgs() << "\nexpanding macro:\n";
1540+
attr->print(P, PrintOptions::printEverything(), attachedTo);
1541+
llvm::dbgs() << "\nattached to:\n";
1542+
attachedTo->print(P, PrintOptions::printEverything());
1543+
if (parentDecl) {
1544+
llvm::dbgs() << "\nwith parent:\n";
1545+
parentDecl->print(P, PrintOptions::printEverything());
1546+
}
1547+
llvm::dbgs() << "\n";
1548+
);
15301549

15311550
auto *astGenDeclSourceFile = declSourceFile->getExportedSourceFile();
15321551
if (!astGenDeclSourceFile)
@@ -1677,6 +1696,14 @@ static SourceFile *evaluateAttachedMacro(MacroDecl *macro,
16771696
#if SWIFT_BUILD_SWIFT_SYNTAX
16781697
PrettyStackTraceExpr debugStack(
16791698
ctx, "expanding attached macro", attachedTo);
1699+
LLVM_DEBUG(
1700+
StreamPrinter P(llvm::dbgs());
1701+
llvm::dbgs() << "\nexpanding macro:\n";
1702+
attr->print(P, PrintOptions::printEverything(), nullptr);
1703+
llvm::dbgs() << "\nattached to:\n";
1704+
attachedTo->print(P, PrintOptions::printEverything());
1705+
llvm::dbgs() << "\n";
1706+
);
16801707

16811708
auto *astGenAttrSourceFile = attrSourceFile->getExportedSourceFile();
16821709
if (!astGenAttrSourceFile)

0 commit comments

Comments
 (0)