Skip to content

[ASTDumper] Avoid parsing decl members #74409

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 2 commits into from
Jun 14, 2024
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
4 changes: 3 additions & 1 deletion include/swift/AST/Stmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -1305,7 +1305,9 @@ class CaseStmt final
SourceLoc getStartLoc() const {
if (UnknownAttrLoc.isValid())
return UnknownAttrLoc;
return getLoc();
if (ItemIntroducerLoc.isValid())
return ItemIntroducerLoc;
return getBody()->getStartLoc();
}
SourceLoc getEndLoc() const { return getBody()->getEndLoc(); }

Expand Down
89 changes: 48 additions & 41 deletions lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,21 +482,22 @@ namespace {
raw_ostream &OS;
unsigned Indent;
public:
bool ParseIfNeeded;
llvm::function_ref<Type(Expr *)> GetTypeOfExpr;
llvm::function_ref<Type(TypeRepr *)> GetTypeOfTypeRepr;
llvm::function_ref<Type(KeyPathExpr *E, unsigned index)>
GetTypeOfKeyPathComponent;
char quote = '"';

explicit PrintBase(raw_ostream &os, unsigned indent = 0,
llvm::function_ref<Type(Expr *)> getTypeOfExpr = defaultGetTypeOfExpr,
llvm::function_ref<Type(TypeRepr *)> getTypeOfTypeRepr = nullptr,
llvm::function_ref<Type(KeyPathExpr *E, unsigned index)>
getTypeOfKeyPathComponent =
defaultGetTypeOfKeyPathComponent)
: OS(os), Indent(indent), GetTypeOfExpr(getTypeOfExpr),
GetTypeOfTypeRepr(getTypeOfTypeRepr),
GetTypeOfKeyPathComponent(getTypeOfKeyPathComponent) { }
explicit PrintBase(
raw_ostream &os, unsigned indent = 0, bool parseIfNeeded = false,
llvm::function_ref<Type(Expr *)> getTypeOfExpr = defaultGetTypeOfExpr,
llvm::function_ref<Type(TypeRepr *)> getTypeOfTypeRepr = nullptr,
llvm::function_ref<Type(KeyPathExpr *E, unsigned index)>
getTypeOfKeyPathComponent = defaultGetTypeOfKeyPathComponent)
: OS(os), Indent(indent), ParseIfNeeded(parseIfNeeded),
GetTypeOfExpr(getTypeOfExpr), GetTypeOfTypeRepr(getTypeOfTypeRepr),
GetTypeOfKeyPathComponent(getTypeOfKeyPathComponent) {}

bool hasNonStandardOutput() {
return &OS != &llvm::errs() && &OS != &llvm::dbgs();
Expand Down Expand Up @@ -1301,7 +1302,9 @@ namespace {
break;
}

for (Decl *D : IDC->getMembers()) {
auto members = ParseIfNeeded ? IDC->getMembers()
: IDC->getCurrentMembersWithoutLoading();
for (Decl *D : members) {
printRec(D);
}
printFoot();
Expand All @@ -1313,7 +1316,9 @@ namespace {
OS << SF.getFilename();
});

if (auto items = SF.getCachedTopLevelItems()) {
auto items =
ParseIfNeeded ? SF.getTopLevelItems() : SF.getCachedTopLevelItems();
if (items) {
for (auto item : *items) {
if (item.isImplicit())
continue;
Expand Down Expand Up @@ -1556,7 +1561,7 @@ namespace {
});
}

if (auto Body = D->getBody(/*canSynthesize=*/false)) {
if (auto Body = D->getBody(/*canSynthesize=*/ParseIfNeeded)) {
printRec(Body, &D->getASTContext());
}
}
Expand Down Expand Up @@ -1858,13 +1863,7 @@ void SourceFile::dump() const {
}

void SourceFile::dump(llvm::raw_ostream &OS, bool parseIfNeeded) const {
// If we're allowed to parse the SourceFile, do so now. We need to force the
// parsing request as by default the dumping logic tries not to kick any
// requests.
if (parseIfNeeded)
(void)getTopLevelItems();

PrintDecl(OS).visitSourceFile(*this);
PrintDecl(OS, /*indent*/ 0, parseIfNeeded).visitSourceFile(*this);
llvm::errs() << '\n';
}

Expand All @@ -1889,13 +1888,16 @@ class PrintStmt : public StmtVisitor<PrintStmt, void, StringRef>,
using PrintBase::PrintBase;
const ASTContext *Ctx;

PrintStmt(raw_ostream &os, const ASTContext *ctx, unsigned indent = 0,
llvm::function_ref<Type(Expr *)> getTypeOfExpr = defaultGetTypeOfExpr,
llvm::function_ref<Type(TypeRepr *)> getTypeOfTypeRepr = nullptr,
llvm::function_ref<Type(KeyPathExpr *E, unsigned index)>
getTypeOfKeyPathComponent = defaultGetTypeOfKeyPathComponent)
: PrintBase(os, indent, getTypeOfExpr, getTypeOfTypeRepr,
getTypeOfKeyPathComponent), Ctx(ctx) { }
PrintStmt(
raw_ostream &os, const ASTContext *ctx, unsigned indent = 0,
bool parseIfNeeded = false,
llvm::function_ref<Type(Expr *)> getTypeOfExpr = defaultGetTypeOfExpr,
llvm::function_ref<Type(TypeRepr *)> getTypeOfTypeRepr = nullptr,
llvm::function_ref<Type(KeyPathExpr *E, unsigned index)>
getTypeOfKeyPathComponent = defaultGetTypeOfKeyPathComponent)
: PrintBase(os, indent, parseIfNeeded, getTypeOfExpr, getTypeOfTypeRepr,
getTypeOfKeyPathComponent),
Ctx(ctx) {}

using PrintBase::printRec;

Expand Down Expand Up @@ -3259,8 +3261,8 @@ void Expr::dump(raw_ostream &OS, llvm::function_ref<Type(Expr *)> getTypeOfExpr,
llvm::function_ref<Type(KeyPathExpr *E, unsigned index)>
getTypeOfKeyPathComponent,
unsigned Indent) const {
PrintExpr(OS, Indent, getTypeOfExpr, getTypeOfTypeRepr,
getTypeOfKeyPathComponent)
PrintExpr(OS, Indent, /*parseIfNeeded*/ false, getTypeOfExpr,
getTypeOfTypeRepr, getTypeOfKeyPathComponent)
.visit(const_cast<Expr *>(this), "");
}

Expand Down Expand Up @@ -3564,8 +3566,9 @@ void PrintBase::printRec(Decl *D, StringRef label) {
printHead("<null decl>", DeclColor, label);
printFoot();
} else {
PrintDecl(OS, Indent, GetTypeOfExpr, GetTypeOfTypeRepr,
GetTypeOfKeyPathComponent).visit(D, label);
PrintDecl(OS, Indent, ParseIfNeeded, GetTypeOfExpr, GetTypeOfTypeRepr,
GetTypeOfKeyPathComponent)
.visit(D, label);
}
}, label);
}
Expand All @@ -3575,8 +3578,9 @@ void PrintBase::printRec(Expr *E, StringRef label) {
printHead("<null expr>", ExprColor, label);
printFoot();
} else {
PrintExpr(OS, Indent, GetTypeOfExpr, GetTypeOfTypeRepr,
GetTypeOfKeyPathComponent).visit(E, label);
PrintExpr(OS, Indent, ParseIfNeeded, GetTypeOfExpr, GetTypeOfTypeRepr,
GetTypeOfKeyPathComponent)
.visit(E, label);
}
}, label);
}
Expand All @@ -3586,8 +3590,9 @@ void PrintBase::printRec(Stmt *S, const ASTContext *Ctx, StringRef label) {
printHead("<null stmt>", ExprColor, label);
printFoot();
} else {
PrintStmt(OS, Ctx, Indent, GetTypeOfExpr, GetTypeOfTypeRepr,
GetTypeOfKeyPathComponent).visit(S, label);
PrintStmt(OS, Ctx, Indent, ParseIfNeeded, GetTypeOfExpr,
GetTypeOfTypeRepr, GetTypeOfKeyPathComponent)
.visit(S, label);
}
}, label);
}
Expand All @@ -3597,8 +3602,9 @@ void PrintBase::printRec(TypeRepr *T, StringRef label) {
printHead("<null typerepr>", TypeReprColor, label);
printFoot();
} else {
PrintTypeRepr(OS, Indent, GetTypeOfExpr, GetTypeOfTypeRepr,
GetTypeOfKeyPathComponent).visit(T, label);
PrintTypeRepr(OS, Indent, ParseIfNeeded, GetTypeOfExpr, GetTypeOfTypeRepr,
GetTypeOfKeyPathComponent)
.visit(T, label);
}
}, label);
}
Expand All @@ -3608,9 +3614,9 @@ void PrintBase::printRec(const Pattern *P, StringRef label) {
printHead("<null pattern>", PatternColor, label);
printFoot();
} else {
PrintPattern(OS, Indent, GetTypeOfExpr, GetTypeOfTypeRepr,
GetTypeOfKeyPathComponent).visit(const_cast<Pattern *>(P),
label);
PrintPattern(OS, Indent, ParseIfNeeded, GetTypeOfExpr, GetTypeOfTypeRepr,
GetTypeOfKeyPathComponent)
.visit(const_cast<Pattern *>(P), label);
}
}, label);
}
Expand Down Expand Up @@ -4536,8 +4542,9 @@ namespace {
printHead("<null type>", DeclColor, label);
printFoot();
} else {
PrintType(OS, Indent, GetTypeOfExpr, GetTypeOfTypeRepr,
GetTypeOfKeyPathComponent).visit(type, label);
PrintType(OS, Indent, ParseIfNeeded, GetTypeOfExpr, GetTypeOfTypeRepr,
GetTypeOfKeyPathComponent)
.visit(type, label);
}
}, label);
}
Expand Down
5 changes: 0 additions & 5 deletions lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1650,11 +1650,6 @@ CompilerInstance::getSourceFileParsingOptions(bool forPrimary) const {
action != ActionType::ScanDependencies) {
opts |= ParsingFlags::DisablePoundIfEvaluation;
}

// If we need to dump the parse tree, disable delayed bodies as we want to
// show everything.
if (action == ActionType::DumpParse)
opts |= ParsingFlags::DisableDelayedBodies;
}

const auto &typeOpts = getASTContext().TypeCheckerOpts;
Expand Down