From 11e246175dfea23d74a88107183f334395c1929c Mon Sep 17 00:00:00 2001 From: Becca Royal-Gordon Date: Thu, 27 Feb 2025 19:51:29 -0800 Subject: [PATCH] [NFC] Add (better) dump methods for attributes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ASTDumper now has the ability to dump attributes in the usual S-expression format, but `DeclAttribute::dump()` and `DeclAttributes::dump()` are still using the printing infrastructure. Use ASTDumper for these functions instead to provide a more “raw” view of the attributes. --- include/swift/AST/Attr.h | 11 +++++++++- lib/AST/ASTDumper.cpp | 43 +++++++++++++++++++++++++++++++++++++++- lib/AST/Attr.cpp | 2 +- 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/include/swift/AST/Attr.h b/include/swift/AST/Attr.h index 37e4fbe7fd64b..a74f1868e9189 100644 --- a/include/swift/AST/Attr.h +++ b/include/swift/AST/Attr.h @@ -524,6 +524,12 @@ class DeclAttribute : public AttributeBase { /// static std::optional getAttrKindFromString(StringRef Str); + SWIFT_DEBUG_DUMPER(dump(const ASTContext &ctx)); + void dump(llvm::raw_ostream &out, const ASTContext &ctx) const; + + SWIFT_DEBUG_DUMPER(dump(const DeclContext *dc)); + void dump(llvm::raw_ostream &out, const DeclContext *dc) const; + static DeclAttribute *createSimple(const ASTContext &context, DeclAttrKind kind, SourceLoc atLoc, SourceLoc attrLoc); @@ -3048,7 +3054,10 @@ class DeclAttributes { const BackDeployedAttr *getBackDeployed(const ASTContext &ctx, bool forTargetVariant) const; - SWIFT_DEBUG_DUMPER(dump(const Decl *D = nullptr)); + SWIFT_DEBUG_DUMPER(dump(const ASTContext &ctx)); + SWIFT_DEBUG_DUMPER(dump(const DeclContext *dc)); + + SWIFT_DEBUG_DUMPER(print(const Decl *D = nullptr)); void print(ASTPrinter &Printer, const PrintOptions &Options, const Decl *D = nullptr) const; static void print(ASTPrinter &Printer, const PrintOptions &Options, diff --git a/lib/AST/ASTDumper.cpp b/lib/AST/ASTDumper.cpp index 17ef8fb2db994..4f4cd4be38ab8 100644 --- a/lib/AST/ASTDumper.cpp +++ b/lib/AST/ASTDumper.cpp @@ -4771,6 +4771,10 @@ class PrintAttribute : public AttributeVisitor, getTypeOfKeyPathComponent), Ctx(ctx), DC(dc) {} + bool isTypeChecked() const { + return PrintBase::isTypeChecked() && DC; + } + void printCommon(DeclAttribute *Attr, StringRef name, Label label) { printHead(name, DeclAttributeColor, label); printFlag(Attr->isImplicit(), "implicit"); @@ -5005,7 +5009,7 @@ class PrintAttribute : public AttributeVisitor, if (Attr->getType()) { printTypeField(Attr->getType(), Label::always("type")); - } else if (MemberLoading == ASTDumpMemberLoading::TypeChecked) { + } else if (isTypeChecked()) { // If the type is null, it might be a macro reference. Try that if we're // dumping the fully type-checked AST. auto macroRef = @@ -5360,6 +5364,43 @@ class PrintAttribute : public AttributeVisitor, } // end anonymous namespace +void DeclAttribute::dump(const ASTContext &ctx) const { + dump(llvm::errs(), ctx); + llvm::errs() << '\n'; +} + +void DeclAttribute::dump(llvm::raw_ostream &os, const ASTContext &ctx) const { + DefaultWriter writer(os, /*indent=*/0); + PrintAttribute(writer, &ctx, nullptr) + .visit(const_cast(this), Label::optional("")); +} + +void DeclAttribute::dump(const DeclContext *dc) const { + dump(llvm::errs(), dc); + llvm::errs() << '\n'; +} + +void DeclAttribute::dump(llvm::raw_ostream &os, const DeclContext *dc) const { + DefaultWriter writer(os, /*indent=*/0); + PrintAttribute(writer, &dc->getASTContext(), const_cast(dc)) + .visit(const_cast(this), Label::optional("")); +} + + +void DeclAttributes::dump(const ASTContext &ctx) const { + for (auto attr : *this) { + attr->dump(llvm::errs(), ctx); + llvm::errs() << '\n'; + } +} + +void DeclAttributes::dump(const DeclContext *dc) const { + for (auto attr : *this) { + attr->dump(llvm::errs(), dc); + llvm::errs() << '\n'; + } +} + void PrintBase::printRec(Decl *D, Label label) { printRecArbitrary([&](Label label) { if (!D) { diff --git a/lib/AST/Attr.cpp b/lib/AST/Attr.cpp index 6192d2be2f8c1..59554a7cfd9dd 100644 --- a/lib/AST/Attr.cpp +++ b/lib/AST/Attr.cpp @@ -428,7 +428,7 @@ DeclAttributes::getBackDeployed(const ASTContext &ctx, return bestAttr; } -void DeclAttributes::dump(const Decl *D) const { +void DeclAttributes::print(const Decl *D) const { StreamPrinter P(llvm::errs()); PrintOptions PO = PrintOptions::printDeclarations(); print(P, PO, D);