Skip to content

Commit 96dc89f

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 5f71265 commit 96dc89f

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
@@ -524,6 +524,12 @@ class DeclAttribute : public AttributeBase {
524524
///
525525
static std::optional<DeclAttrKind> getAttrKindFromString(StringRef Str);
526526

527+
SWIFT_DEBUG_DUMPER(dump(const ASTContext &ctx));
528+
void dump(llvm::raw_ostream &out, const ASTContext &ctx) const;
529+
530+
SWIFT_DEBUG_DUMPER(dump(const DeclContext *dc));
531+
void dump(llvm::raw_ostream &out, const DeclContext *dc) const;
532+
527533
static DeclAttribute *createSimple(const ASTContext &context,
528534
DeclAttrKind kind, SourceLoc atLoc,
529535
SourceLoc attrLoc);
@@ -3056,7 +3062,10 @@ class DeclAttributes {
30563062
const BackDeployedAttr *getBackDeployed(const ASTContext &ctx,
30573063
bool forTargetVariant) const;
30583064

3059-
SWIFT_DEBUG_DUMPER(dump(const Decl *D = nullptr));
3065+
SWIFT_DEBUG_DUMPER(dump(const ASTContext &ctx));
3066+
SWIFT_DEBUG_DUMPER(dump(const DeclContext *dc));
3067+
3068+
SWIFT_DEBUG_DUMPER(print(const Decl *D = nullptr));
30603069
void print(ASTPrinter &Printer, const PrintOptions &Options,
30613070
const Decl *D = nullptr) const;
30623071
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
@@ -4781,6 +4781,10 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
47814781
getTypeOfKeyPathComponent),
47824782
Ctx(ctx), DC(dc) {}
47834783

4784+
bool isTypeChecked() const {
4785+
return PrintBase::isTypeChecked() && DC;
4786+
}
4787+
47844788
void printCommon(DeclAttribute *Attr, StringRef name, Label label) {
47854789
printHead(name, DeclAttributeColor, label);
47864790
printFlag(Attr->isImplicit(), "implicit");
@@ -5015,7 +5019,7 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
50155019

50165020
if (Attr->getType()) {
50175021
printTypeField(Attr->getType(), Label::always("type"));
5018-
} else if (MemberLoading == ASTDumpMemberLoading::TypeChecked) {
5022+
} else if (isTypeChecked()) {
50195023
// If the type is null, it might be a macro reference. Try that if we're
50205024
// dumping the fully type-checked AST.
50215025
auto macroRef =
@@ -5370,6 +5374,43 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
53705374

53715375
} // end anonymous namespace
53725376

5377+
void DeclAttribute::dump(const ASTContext &ctx) const {
5378+
dump(llvm::errs(), ctx);
5379+
llvm::errs() << '\n';
5380+
}
5381+
5382+
void DeclAttribute::dump(llvm::raw_ostream &os, const ASTContext &ctx) const {
5383+
DefaultWriter writer(os, /*indent=*/0);
5384+
PrintAttribute(writer, &ctx, nullptr)
5385+
.visit(const_cast<DeclAttribute*>(this), Label::optional(""));
5386+
}
5387+
5388+
void DeclAttribute::dump(const DeclContext *dc) const {
5389+
dump(llvm::errs(), dc);
5390+
llvm::errs() << '\n';
5391+
}
5392+
5393+
void DeclAttribute::dump(llvm::raw_ostream &os, const DeclContext *dc) const {
5394+
DefaultWriter writer(os, /*indent=*/0);
5395+
PrintAttribute(writer, &dc->getASTContext(), const_cast<DeclContext*>(dc))
5396+
.visit(const_cast<DeclAttribute*>(this), Label::optional(""));
5397+
}
5398+
5399+
5400+
void DeclAttributes::dump(const ASTContext &ctx) const {
5401+
for (auto attr : *this) {
5402+
attr->dump(llvm::errs(), ctx);
5403+
llvm::errs() << '\n';
5404+
}
5405+
}
5406+
5407+
void DeclAttributes::dump(const DeclContext *dc) const {
5408+
for (auto attr : *this) {
5409+
attr->dump(llvm::errs(), dc);
5410+
llvm::errs() << '\n';
5411+
}
5412+
}
5413+
53735414
void PrintBase::printRec(Decl *D, Label label) {
53745415
printRecArbitrary([&](Label label) {
53755416
if (!D) {

lib/AST/Attr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ DeclAttributes::getBackDeployed(const ASTContext &ctx,
428428
return bestAttr;
429429
}
430430

431-
void DeclAttributes::dump(const Decl *D) const {
431+
void DeclAttributes::print(const Decl *D) const {
432432
StreamPrinter P(llvm::errs());
433433
PrintOptions PO = PrintOptions::printDeclarations();
434434
print(P, PO, D);

0 commit comments

Comments
 (0)