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.
40 lines
1.2 KiB
Swift
40 lines
1.2 KiB
Swift
/// Test AST with module aliasing.
|
|
///
|
|
/// Module 'Lib' imports module 'XLogging', and 'XLogging' is aliased 'AppleLogging'.
|
|
|
|
// RUN: %empty-directory(%t)
|
|
// RUN: %{python} %utils/split_file.py -o %t %s
|
|
|
|
/// Create AppleLogging.swiftmodule by aliasing XLogging
|
|
// RUN: %target-swift-frontend -module-name AppleLogging -module-alias XLogging=AppleLogging %t/FileLogging.swift -emit-module -emit-module-path %t/AppleLogging.swiftmodule
|
|
// RUN: test -f %t/AppleLogging.swiftmodule
|
|
|
|
/// Verify AST contains AppleLogging as module name
|
|
// RUN: %target-swift-frontend -dump-ast %t/FileLib.swift -module-alias XLogging=AppleLogging -I %t > %t/result-ast.output
|
|
|
|
// RUN: %FileCheck %s -input-file %t/result-ast.output -check-prefix CHECK-AST
|
|
// CHECK-AST-NOT: bind="XLogging"
|
|
// CHECK-AST-NOT: module<XLogging>
|
|
// CHECK-AST-NOT: decl="XLogging"
|
|
// CHECK-AST: type_ident id="XLogging" bind="AppleLogging"
|
|
// CHECK-AST: module<AppleLogging>
|
|
// CHECK-AST: decl="AppleLogging"
|
|
|
|
// BEGIN FileLogging.swift
|
|
public struct Logger {
|
|
public init() {}
|
|
}
|
|
public func setup() -> XLogging.Logger? {
|
|
return Logger()
|
|
}
|
|
|
|
// BEGIN FileLib.swift
|
|
import XLogging
|
|
|
|
public func start() -> XLogging.Logger? {
|
|
return XLogging.setup()
|
|
}
|
|
|
|
public func end(_ arg: XLogging.Logger) {
|
|
}
|