Skip to content

Commit 95f1e9b

Browse files
committed
[NFC] Add (better) dump methods for attributes
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 provie a more “raw” view of the attributes.
1 parent 8c4dea9 commit 95f1e9b

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

include/swift/AST/Attr.h

+10-1
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);
@@ -3048,7 +3054,10 @@ class DeclAttributes {
30483054
const BackDeployedAttr *getBackDeployed(const ASTContext &ctx,
30493055
bool forTargetVariant) const;
30503056

3051-
SWIFT_DEBUG_DUMPER(dump(const Decl *D = nullptr));
3057+
SWIFT_DEBUG_DUMPER(dump(const ASTContext &ctx));
3058+
SWIFT_DEBUG_DUMPER(dump(const DeclContext *dc));
3059+
3060+
SWIFT_DEBUG_DUMPER(print(const Decl *D = nullptr));
30523061
void print(ASTPrinter &Printer, const PrintOptions &Options,
30533062
const Decl *D = nullptr) const;
30543063
static void print(ASTPrinter &Printer, const PrintOptions &Options,

lib/AST/ASTDumper.cpp

+42-1
Original file line numberDiff line numberDiff line change
@@ -4771,6 +4771,10 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
47714771
getTypeOfKeyPathComponent),
47724772
Ctx(ctx), DC(dc) {}
47734773

4774+
bool isTypeChecked() const {
4775+
return PrintBase::isTypeChecked() && DC;
4776+
}
4777+
47744778
void printCommon(DeclAttribute *Attr, StringRef name, Label label) {
47754779
printHead(name, DeclAttributeColor, label);
47764780
printFlag(Attr->isImplicit(), "implicit");
@@ -5005,7 +5009,7 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
50055009

50065010
if (Attr->getType()) {
50075011
printTypeField(Attr->getType(), Label::always("type"));
5008-
} else if (MemberLoading == ASTDumpMemberLoading::TypeChecked) {
5012+
} else if (isTypeChecked()) {
50095013
// If the type is null, it might be a macro reference. Try that if we're
50105014
// dumping the fully type-checked AST.
50115015
auto macroRef =
@@ -5360,6 +5364,43 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
53605364

53615365
} // end anonymous namespace
53625366

5367+
void DeclAttribute::dump(const ASTContext &ctx) const {
5368+
dump(llvm::errs(), ctx);
5369+
llvm::errs() << '\n';
5370+
}
5371+
5372+
void DeclAttribute::dump(llvm::raw_ostream &os, const ASTContext &ctx) const {
5373+
DefaultWriter writer(os, /*indent=*/0);
5374+
PrintAttribute(writer, &ctx, nullptr)
5375+
.visit(const_cast<DeclAttribute*>(this), Label::optional(""));
5376+
}
5377+
5378+
void DeclAttribute::dump(const DeclContext *dc) const {
5379+
dump(llvm::errs(), dc);
5380+
llvm::errs() << '\n';
5381+
}
5382+
5383+
void DeclAttribute::dump(llvm::raw_ostream &os, const DeclContext *dc) const {
5384+
DefaultWriter writer(os, /*indent=*/0);
5385+
PrintAttribute(writer, &dc->getASTContext(), const_cast<DeclContext*>(dc))
5386+
.visit(const_cast<DeclAttribute*>(this), Label::optional(""));
5387+
}
5388+
5389+
5390+
void DeclAttributes::dump(const ASTContext &ctx) const {
5391+
for (auto attr : *this) {
5392+
attr->dump(llvm::errs(), ctx);
5393+
llvm::errs() << '\n';
5394+
}
5395+
}
5396+
5397+
void DeclAttributes::dump(const DeclContext *dc) const {
5398+
for (auto attr : *this) {
5399+
attr->dump(llvm::errs(), dc);
5400+
llvm::errs() << '\n';
5401+
}
5402+
}
5403+
53635404
void PrintBase::printRec(Decl *D, Label label) {
53645405
printRecArbitrary([&](Label label) {
53655406
if (!D) {

lib/AST/Attr.cpp

+1-1
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)