Skip to content

Include source ranges for statements, declarations, and parameter lists in the AST dump #16473

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 4 commits into from
May 17, 2018

Conversation

bdash
Copy link
Contributor

@bdash bdash commented May 9, 2018

Source ranges are currently missing from statements, declarations, and parameter lists in the AST dump. This PR addresses that.

For some background: we have a Swift-based domain-specific language that works by analyzing AST dumps and transforming them into new Swift code. In cases where the input doesn't match the constraints of our DSL, we emit errors that point to the problems in the original Swift source. The lack of source range information on these node types makes this difficult for certain types of errors.

@slavapestov
Copy link
Contributor

@jrose-apple Want to take a look?

@slavapestov slavapestov requested a review from jrose-apple May 15, 2018 02:22
Copy link
Contributor

@jrose-apple jrose-apple left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I almost want to say "no" on principle—please please don't parse AST dumps! They are not expected to be stable or complete or machine-readable. But this is a good change regardless, so thank you for doing it. I have one review question and two style nitpicks.

@@ -128,7 +128,7 @@ class alignas(8) Stmt {
LLVM_ATTRIBUTE_DEPRECATED(
void dump() const LLVM_ATTRIBUTE_USED,
"only for use within the debugger");
void print(raw_ostream &OS, unsigned Indent = 0) const;
void print(raw_ostream &OS, const ASTContext * = nullptr, unsigned Indent = 0) const;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: please provide a parameter name here, even if it's just "Ctx".

OS.indent(Indent);
PrintWithColorRAII(OS, ParenthesisColor) << "(";
PrintWithColorRAII(OS, ASTNodeColor) << Name;
void printASTNodes(const ArrayRef<ASTNode> &Elements) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change was to allow visitBraceStmt to delegate to printCommon, so it can reuse the logic for printing the source range.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right, thanks!

@@ -424,7 +424,7 @@ namespace {

void printRec(Decl *D) { D->dump(OS, Indent + 2); }
void printRec(Expr *E) { E->print(OS, Indent + 2); }
void printRec(Stmt *S) { S->print(OS, Indent + 2); }
void printRec(Stmt *S, const ASTContext& Ctx) { S->print(OS, &Ctx, Indent + 2); }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style nitpick: please attach the & to the declaration name, not the type.

Copy link
Contributor Author

@bdash bdash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that the AST dump format is unstable and isn't intended to be consumed, but it's the best option we've found for what we're trying to do.

OS.indent(Indent);
PrintWithColorRAII(OS, ParenthesisColor) << "(";
PrintWithColorRAII(OS, ASTNodeColor) << Name;
void printASTNodes(const ArrayRef<ASTNode> &Elements) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change was to allow visitBraceStmt to delegate to printCommon, so it can reuse the logic for printing the source range.

@bdash
Copy link
Contributor Author

bdash commented May 15, 2018

I've addressed the style issues.

@jrose-apple
Copy link
Contributor

@swift-ci Please test

@jrose-apple jrose-apple self-assigned this May 15, 2018
@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - c58dcfc

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - c58dcfc

@jrose-apple jrose-apple merged commit cb6c8f0 into swiftlang:master May 17, 2018
@jrose-apple
Copy link
Contributor

Merged. Thanks, Mark!

@benlangmuir
Copy link
Contributor

@jrose-apple @bdash I reverted this because it appears to have broken a test in CI: https://ci.swift.org/job/oss-swift-incremental-RA-linux-ubuntu-16_04/4126/

Please take a look

@jrose-apple
Copy link
Contributor

How did this pass the regular testing? Weird. Thanks, Ben.

@bdash
Copy link
Contributor Author

bdash commented May 17, 2018

#16622 was merged after this was tested, and introduced a new func_decl check in test/Driver/dump-parse.swift that needs to be updated to accommodate the inclusion of source ranges in the AST dump. I'll take care of that now.

@bdash
Copy link
Contributor Author

bdash commented May 17, 2018

#16682 is this PR rebased on master, with the necessary fixes made to tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants