Skip to content

[NFC] Add (better) dump methods for attributes #79970

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

Merged
merged 1 commit into from
Mar 13, 2025
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
11 changes: 10 additions & 1 deletion include/swift/AST/Attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,12 @@ class DeclAttribute : public AttributeBase {
///
static std::optional<DeclAttrKind> 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);
Expand Down Expand Up @@ -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,
Expand Down
43 changes: 42 additions & 1 deletion lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4771,6 +4771,10 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
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");
Expand Down Expand Up @@ -5005,7 +5009,7 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,

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 =
Expand Down Expand Up @@ -5360,6 +5364,43 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,

} // 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<DeclAttribute*>(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<DeclContext*>(dc))
.visit(const_cast<DeclAttribute*>(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) {
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/Attr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down