Skip to content
Merged
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
2 changes: 2 additions & 0 deletions include/swift/AST/Attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -2344,6 +2344,8 @@ class CustomAttr final : public DeclAttribute {

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

void printCustomAttr(ASTPrinter &Printer, const PrintOptions &Options) const;

private:
friend class CustomAttrNominalRequest;
void resetTypeInformation(TypeExpr *repr);
Expand Down
17 changes: 15 additions & 2 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,8 @@ class PrintAST : public ASTVisitor<PrintAST> {
void printRequirement(const InverseRequirement &inverse,
bool forInherited);

void printArgumentList(ArgumentList *args, bool forSubscript = false);

private:
bool shouldPrint(const Decl *D, bool Notify = false);
bool shouldPrintPattern(const Pattern *P);
Expand Down Expand Up @@ -1138,8 +1140,6 @@ class PrintAST : public ASTVisitor<PrintAST> {
void printFunctionParameters(AbstractFunctionDecl *AFD);

void printArgument(const Argument &arg);

void printArgumentList(ArgumentList *args, bool forSubscript = false);

void printAvailabilitySpec(AvailabilitySpec *spec);

Expand Down Expand Up @@ -4843,6 +4843,19 @@ void PrintAST::visitMacroExpansionDecl(MacroExpansionDecl *decl) {
Printer << ')';
}

void CustomAttr::printCustomAttr(ASTPrinter &Printer, const PrintOptions &Options) const {
Printer.callPrintNamePre(PrintNameContext::Attribute);
Printer << "@";
if (auto type = getType())
type.print(Printer, Options);
else
getTypeRepr()->print(Printer, Options);
Printer.printNamePost(PrintNameContext::Attribute);
if (hasArgs() && Options.PrintExprs) {
PrintAST(Printer, Options).printArgumentList(argList);
}
}

void PrintAST::visitIntegerLiteralExpr(IntegerLiteralExpr *expr) {
Printer << expr->getDigitsText();
}
Expand Down
8 changes: 1 addition & 7 deletions lib/AST/Attr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1453,14 +1453,8 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
}

case DeclAttrKind::Custom: {
Printer.callPrintNamePre(PrintNameContext::Attribute);
Printer << "@";
auto *attr = cast<CustomAttr>(this);
if (auto type = attr->getType())
type.print(Printer, Options);
else
attr->getTypeRepr()->print(Printer, Options);
Printer.printNamePost(PrintNameContext::Attribute);
attr->printCustomAttr(Printer, Options);
break;
}

Expand Down
21 changes: 19 additions & 2 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9408,13 +9408,16 @@ void ClangImporter::Implementation::addOptionSetTypealiases(
selfType);
}

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

void ClangImporter::Implementation::swiftify(AbstractFunctionDecl *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");

// FIXME: for private macro generated functions we do not serialize the
// SILFunction's body anywhere triggering assertions.
Expand Down Expand Up @@ -9470,11 +9473,13 @@ void ClangImporter::Implementation::swiftify(AbstractFunctionDecl *MappedDecl) {
auto *CAT = ClangDecl->getReturnType()->getAs<clang::CountAttributedType>();
if (SwiftifiableCAT(getClangASTContext(), CAT, 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 @@ -9487,6 +9492,8 @@ void ClangImporter::Implementation::swiftify(AbstractFunctionDecl *MappedDecl) {
auto *CAT = clangParamTy->getAs<clang::CountAttributedType>();
if (SwiftifiableCAT(getClangASTContext(), CAT, swiftParamTy)) {
printer.printCountedBy(CAT, index);
SIW_DBG(" Found bounds info '" << clangParamTy
<< "' on parameter '" << *clangParam << "'\n");
attachMacro = paramHasBoundsInfo = true;
}
bool paramIsStdSpan = registerStdSpanTypeMapping(
Expand All @@ -9495,28 +9502,37 @@ void ClangImporter::Implementation::swiftify(AbstractFunctionDecl *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");
// If this parameter has bounds info we will tranform it into a Span,
// so then it will no longer be Escapable.
bool willBeEscapable = swiftParamTy->isEscapable() && !paramHasBoundsInfo;
printer.printLifetimeboundReturn(index, willBeEscapable);
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);

}

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 @@ -9536,6 +9552,7 @@ void ClangImporter::Implementation::swiftify(AbstractFunctionDecl *MappedDecl) {
}
}
}
#undef SIW_DBG

static bool isUsingMacroName(clang::SourceManager &SM,
clang::SourceLocation loc,
Expand Down
27 changes: 27 additions & 0 deletions lib/Sema/TypeCheckMacros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
#include "swift/Subsystems.h"
#include "llvm/Config/config.h"

#define DEBUG_TYPE "macros"

using namespace swift;

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

PrettyStackTraceFreestandingMacroExpansion debugStack(
"expanding freestanding macro", expansion);
LLVM_DEBUG(
llvm::dbgs() << "\nexpanding macro:\n";
macro->print(llvm::dbgs(), PrintOptions::printEverything());
llvm::dbgs() << "\n";
);

// Builtin macros are handled via ASTGen.
auto *astGenSourceFile = sourceFile->getExportedSourceFile();
Expand Down Expand Up @@ -1527,6 +1534,18 @@ static SourceFile *evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo,
auto *astGenAttrSourceFile = attrSourceFile->getExportedSourceFile();
if (!astGenAttrSourceFile)
return nullptr;
LLVM_DEBUG(
StreamPrinter P(llvm::dbgs());
llvm::dbgs() << "\nexpanding macro:\n";
attr->print(P, PrintOptions::printEverything(), attachedTo);
llvm::dbgs() << "\nattached to:\n";
attachedTo->print(P, PrintOptions::printEverything());
if (parentDecl) {
llvm::dbgs() << "\nwith parent:\n";
parentDecl->print(P, PrintOptions::printEverything());
}
llvm::dbgs() << "\n";
);

auto *astGenDeclSourceFile = declSourceFile->getExportedSourceFile();
if (!astGenDeclSourceFile)
Expand Down Expand Up @@ -1677,6 +1696,14 @@ static SourceFile *evaluateAttachedMacro(MacroDecl *macro,
#if SWIFT_BUILD_SWIFT_SYNTAX
PrettyStackTraceExpr debugStack(
ctx, "expanding attached macro", attachedTo);
LLVM_DEBUG(
StreamPrinter P(llvm::dbgs());
llvm::dbgs() << "\nexpanding macro:\n";
attr->print(P, PrintOptions::printEverything(), nullptr);
llvm::dbgs() << "\nattached to:\n";
attachedTo->print(P, PrintOptions::printEverything());
llvm::dbgs() << "\n";
);

auto *astGenAttrSourceFile = attrSourceFile->getExportedSourceFile();
if (!astGenAttrSourceFile)
Expand Down