Skip to content

Commit bb152f1

Browse files
committed
[ASTPrinter] Fix decl/structure nested callbacks
For now, just force any pending callbacks before doing the structure callbacks. This should be fine, since we are always about to print *something* that isn't whitespace, so there is no change in location.
1 parent 9e3fc1c commit bb152f1

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

include/swift/AST/ASTPrinter.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ class ASTPrinter {
126126
const NominalTypeDecl *NTD) {}
127127

128128
/// Called before printing a structured entity.
129+
///
130+
/// Callers should use callPrintStructurePre().
129131
virtual void printStructurePre(PrintStructureKind Kind,
130132
const Decl *D = nullptr) {}
131133
/// Called after printing a structured entity.
@@ -236,6 +238,16 @@ class ASTPrinter {
236238
PendingNamePreCallback = Context;
237239
}
238240

241+
/// Make a callback to printStructurePre(), performing any necessary
242+
/// bookkeeping.
243+
void callPrintStructurePre(PrintStructureKind Kind, const Decl *D = nullptr) {
244+
// FIXME: As a hack, print an empty string to force the pending callbacks.
245+
// The real fix is to incorporate the structure kinds into the pending
246+
// callbacks, interleaved with the decls.
247+
printTextImpl("");
248+
printStructurePre(Kind, D);
249+
}
250+
239251
/// To sanitize a malformed utf8 string to a well-formed one.
240252
static std::string sanitizeUtf8(StringRef Text);
241253
static bool printTypeInterface(Type Ty, DeclContext *DC, std::string &Result);

lib/AST/ASTPrinter.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,7 +1042,7 @@ void PrintAST::printGenericParams(GenericParamList *Params) {
10421042
}
10431043
auto NM = Arg->getAnyNominal();
10441044
assert(NM && "Cannot get nominal type.");
1045-
Printer.printStructurePre(PrintStructureKind::GenericParameter, NM);
1045+
Printer.callPrintStructurePre(PrintStructureKind::GenericParameter, NM);
10461046
Printer << NM->getNameStr(); // FIXME: PrintNameContext::GenericParameter
10471047
Printer.printStructurePost(PrintStructureKind::GenericParameter, NM);
10481048
}
@@ -1053,7 +1053,7 @@ void PrintAST::printGenericParams(GenericParamList *Params) {
10531053
} else {
10541054
Printer << ", ";
10551055
}
1056-
Printer.printStructurePre(PrintStructureKind::GenericParameter, GP);
1056+
Printer.callPrintStructurePre(PrintStructureKind::GenericParameter, GP);
10571057
Printer.printName(GP->getName(), PrintNameContext::GenericParameter);
10581058
printInherited(GP);
10591059
Printer.printStructurePost(PrintStructureKind::GenericParameter, GP);
@@ -1101,7 +1101,7 @@ void PrintAST::printWhereClause(ArrayRef<RequirementRepr> requirements) {
11011101
} else {
11021102
Printer << ", ";
11031103
}
1104-
Printer.printStructurePre(PrintStructureKind::GenericRequirement);
1104+
Printer.callPrintStructurePre(PrintStructureKind::GenericRequirement);
11051105
Printer << std::get<0>(E);
11061106
Printer << (RequirementReprKind::SameType == std::get<2>(E) ? " == " :
11071107
" : ");
@@ -1123,7 +1123,7 @@ void PrintAST::printWhereClause(ArrayRef<RequirementRepr> requirements) {
11231123
Printer << ", ";
11241124
}
11251125

1126-
Printer.printStructurePre(PrintStructureKind::GenericRequirement);
1126+
Printer.callPrintStructurePre(PrintStructureKind::GenericRequirement);
11271127
defer {
11281128
Printer.printStructurePost(PrintStructureKind::GenericRequirement);
11291129
};
@@ -2056,7 +2056,7 @@ void PrintAST::visitParamDecl(ParamDecl *decl) {
20562056

20572057
void PrintAST::printOneParameter(const ParamDecl *param, bool Curried,
20582058
bool ArgNameIsAPIByDefault) {
2059-
Printer.printStructurePre(PrintStructureKind::FunctionParameter, param);
2059+
Printer.callPrintStructurePre(PrintStructureKind::FunctionParameter, param);
20602060
defer {
20612061
Printer.printStructurePost(PrintStructureKind::FunctionParameter, param);
20622062
};

0 commit comments

Comments
 (0)