Skip to content

Commit f6c9177

Browse files
authored
Merge pull request #79970 from beccadax/these-attributes-are-a-dump
[NFC] Add (better) dump methods for attributes
2 parents 0bccec6 + 11e2461 commit f6c9177

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
@@ -534,6 +534,12 @@ class DeclAttribute : public AttributeBase {
534534
///
535535
static std::optional<DeclAttrKind> getAttrKindFromString(StringRef Str);
536536

537+
SWIFT_DEBUG_DUMPER(dump(const ASTContext &ctx));
538+
void dump(llvm::raw_ostream &out, const ASTContext &ctx) const;
539+
540+
SWIFT_DEBUG_DUMPER(dump(const DeclContext *dc));
541+
void dump(llvm::raw_ostream &out, const DeclContext *dc) const;
542+
537543
static DeclAttribute *createSimple(const ASTContext &context,
538544
DeclAttrKind kind, SourceLoc atLoc,
539545
SourceLoc attrLoc);
@@ -3058,7 +3064,10 @@ class DeclAttributes {
30583064
const BackDeployedAttr *getBackDeployed(const ASTContext &ctx,
30593065
bool forTargetVariant) const;
30603066

3061-
SWIFT_DEBUG_DUMPER(dump(const Decl *D = nullptr));
3067+
SWIFT_DEBUG_DUMPER(dump(const ASTContext &ctx));
3068+
SWIFT_DEBUG_DUMPER(dump(const DeclContext *dc));
3069+
3070+
SWIFT_DEBUG_DUMPER(print(const Decl *D = nullptr));
30623071
void print(ASTPrinter &Printer, const PrintOptions &Options,
30633072
const Decl *D = nullptr) const;
30643073
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
@@ -417,7 +417,7 @@ DeclAttributes::getBackDeployed(const ASTContext &ctx,
417417
return bestAttr;
418418
}
419419

420-
void DeclAttributes::dump(const Decl *D) const {
420+
void DeclAttributes::print(const Decl *D) const {
421421
StreamPrinter P(llvm::errs());
422422
PrintOptions PO = PrintOptions::printDeclarations();
423423
print(P, PO, D);

0 commit comments

Comments
 (0)