Skip to content

Commit ab0473b

Browse files
committed
[NFC] Add (better) dump methods for attributes
Now that we have a dumper for DeclAttributes, let’s actually use it to provide `dump()` methods.
1 parent b3b34a4 commit ab0473b

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

include/swift/AST/Attr.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,12 @@ class DeclAttribute : public AttributeBase {
509509
///
510510
static std::optional<DeclAttrKind> getAttrKindFromString(StringRef Str);
511511

512+
SWIFT_DEBUG_DUMPER(dump(const ASTContext &ctx));
513+
void dump(llvm::raw_ostream &out, const ASTContext &ctx) const;
514+
515+
SWIFT_DEBUG_DUMPER(dump(const DeclContext *dc));
516+
void dump(llvm::raw_ostream &out, const DeclContext *dc) const;
517+
512518
static DeclAttribute *createSimple(const ASTContext &context,
513519
DeclAttrKind kind, SourceLoc atLoc,
514520
SourceLoc attrLoc);
@@ -3029,7 +3035,10 @@ class DeclAttributes {
30293035
const BackDeployedAttr *getBackDeployed(const ASTContext &ctx,
30303036
bool forTargetVariant) const;
30313037

3032-
SWIFT_DEBUG_DUMPER(dump(const Decl *D = nullptr));
3038+
SWIFT_DEBUG_DUMPER(dump(const ASTContext &ctx));
3039+
SWIFT_DEBUG_DUMPER(dump(const DeclContext *dc));
3040+
3041+
SWIFT_DEBUG_DUMPER(print(const Decl *D = nullptr));
30333042
void print(ASTPrinter &Printer, const PrintOptions &Options,
30343043
const Decl *D = nullptr) const;
30353044
static void print(ASTPrinter &Printer, const PrintOptions &Options,

lib/AST/ASTDumper.cpp

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4799,6 +4799,10 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
47994799
getTypeOfKeyPathComponent),
48004800
Ctx(ctx), DC(dc) {}
48014801

4802+
bool isTypeChecked() const {
4803+
return PrintBase::isTypeChecked() && DC;
4804+
}
4805+
48024806
void printCommon(DeclAttribute *Attr, StringRef name, Label label) {
48034807
printHead(name, DeclAttributeColor, label);
48044808
printFlag(Attr->isImplicit(), "implicit");
@@ -5027,7 +5031,7 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
50275031

50285032
if (Attr->getType()) {
50295033
printTypeField(Attr->getType(), Label::always("type"));
5030-
} else if (MemberLoading == ASTDumpMemberLoading::TypeChecked) {
5034+
} else if (isTypeChecked()) {
50315035
// If the type is null, it might be a macro reference. Try that if we're
50325036
// dumping the fully type-checked AST.
50335037
auto macroRef =
@@ -5382,6 +5386,43 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
53825386

53835387
} // end anonymous namespace
53845388

5389+
void DeclAttribute::dump(const ASTContext &ctx) const {
5390+
dump(llvm::errs(), ctx);
5391+
llvm::errs() << '\n';
5392+
}
5393+
5394+
void DeclAttribute::dump(llvm::raw_ostream &os, const ASTContext &ctx) const {
5395+
DefaultWriter writer(os, /*indent=*/0);
5396+
PrintAttribute(writer, &ctx, nullptr)
5397+
.visit(const_cast<DeclAttribute*>(this), Label::optional(""));
5398+
}
5399+
5400+
void DeclAttribute::dump(const DeclContext *dc) const {
5401+
dump(llvm::errs(), dc);
5402+
llvm::errs() << '\n';
5403+
}
5404+
5405+
void DeclAttribute::dump(llvm::raw_ostream &os, const DeclContext *dc) const {
5406+
DefaultWriter writer(os, /*indent=*/0);
5407+
PrintAttribute(writer, &dc->getASTContext(), const_cast<DeclContext*>(dc))
5408+
.visit(const_cast<DeclAttribute*>(this), Label::optional(""));
5409+
}
5410+
5411+
5412+
void DeclAttributes::dump(const ASTContext &ctx) const {
5413+
for (auto attr : *this) {
5414+
attr->dump(llvm::errs(), ctx);
5415+
llvm::errs() << '\n';
5416+
}
5417+
}
5418+
5419+
void DeclAttributes::dump(const DeclContext *dc) const {
5420+
for (auto attr : *this) {
5421+
attr->dump(llvm::errs(), dc);
5422+
llvm::errs() << '\n';
5423+
}
5424+
}
5425+
53855426
void PrintBase::printRec(Decl *D, Label label) {
53865427
printRecArbitrary([&](Label label) {
53875428
if (!D) {

lib/AST/Attr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ DeclAttributes::getBackDeployed(const ASTContext &ctx,
425425
return bestAttr;
426426
}
427427

428-
void DeclAttributes::dump(const Decl *D) const {
428+
void DeclAttributes::print(const Decl *D) const {
429429
StreamPrinter P(llvm::errs());
430430
PrintOptions PO = PrintOptions::printDeclarations();
431431
print(P, PO, D);

0 commit comments

Comments
 (0)