mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
This PR refactors the ASTDumper to make it more structured, less mistake-prone, and more amenable to future changes. For example:
```cpp
// Before:
void visitUnresolvedDotExpr(UnresolvedDotExpr *E) {
printCommon(E, "unresolved_dot_expr")
<< " field '" << E->getName() << "'";
PrintWithColorRAII(OS, ExprModifierColor)
<< " function_ref=" << getFunctionRefKindStr(E->getFunctionRefKind());
if (E->getBase()) {
OS << '\n';
printRec(E->getBase());
}
PrintWithColorRAII(OS, ParenthesisColor) << ')';
}
// After:
void visitUnresolvedDotExpr(UnresolvedDotExpr *E, StringRef label) {
printCommon(E, "unresolved_dot_expr", label);
printFieldQuoted(E->getName(), "field");
printField(E->getFunctionRefKind(), "function_ref", ExprModifierColor);
if (E->getBase()) {
printRec(E->getBase());
}
printFoot();
}
```
* Values are printed through calls to base class methods, rather than direct access to the underlying `raw_ostream`.
* These methods tend to reduce the chances of bugs like missing/extra spaces or newlines, too much/too little indentation, etc.
* More values are quoted, and unprintable/non-ASCII characters in quoted values are escaped before printing.
* Infrastructure to label child nodes now exists.
* Some weird breaks from the normal "style", like `PatternBindingDecl`'s original and processed initializers, have been brought into line.
* Some types that previously used ad-hoc dumping functions, like conformances and substitution maps, are now structured similarly to the dumper classes.
* I've fixed the odd dumping bug along the way. For example, distributed actors were only marked `actor`, not `distributed actor`.
This PR doesn't change the overall style of AST dumps; they're still pseudo-S-expressions. But the logic that implements this style is now isolated into a relatively small base class, making it feasible to introduce e.g. JSON dumping in the future.
17 lines
609 B
Swift
17 lines
609 B
Swift
// RUN: %target-swift-frontend -typecheck -parse-as-library -verify %s
|
|
// RUN: %target-swift-frontend -dump-ast -parse-as-library %s | %FileCheck %s --check-prefix=CHECK-AST
|
|
|
|
@main
|
|
struct MyBase {
|
|
static func main() throws {
|
|
}
|
|
}
|
|
|
|
// CHECK-AST: (func_decl implicit "$main()" interface type="(MyBase.Type) -> () throws -> ()" access=internal type
|
|
// CHECK-AST-NEXT: (parameter "self")
|
|
// CHECK-AST-NEXT: (parameter_list)
|
|
// CHECK-AST-NEXT: (brace_stmt implicit
|
|
// CHECK-AST-NEXT: (return_stmt implicit
|
|
// CHECK-AST-NEXT: (try_expr implicit
|
|
// CHECK-AST-NEXT: (call_expr implicit type="()"
|