Skip to content

[Driver] - Tokenization for response files #16

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 6 commits into from
Oct 16, 2019

Conversation

JhonnyBillM
Copy link
Contributor

This PR tries to address the "Implement proper tokenization for response files" to-do item that is on the README.

Now we can correctly parse:

  • Double slash comments.
  • Quotes.
  • Escaping characters.
  • Espaces.

This matches the current behavior of the LLVM tokenization method (the Swift compiler is currently using the LLVM method).

Also, after reading LLVM's ExpandResponseFiles method, I think our current implementation has all we need.

Note: we need to update this implementation once we get Windows support.

For "documentation" purposes
What are Response files? Files that contains a list of command line arguments.
For more info search for "response files" in this document

Copy link
Contributor

@harlanhaskins harlanhaskins left a comment

Choose a reason for hiding this comment

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

Thanks, @JhonnyBillM, this looks great! Some small comments, but overall this is good.

///
/// - Complexity: O(*n*), where *n* is the length of the line.
private static func tokenizeResponseFileLine<S: StringProtocol>(_ line: S) -> String {
if line.isEmpty { return "" }
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you return an optional here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Very good suggestion. Thanks!

return content
.split(separator: "\n")
.map { tokenizeResponseFileLine($0) }
.filter { !$0.isEmpty }
Copy link
Contributor

Choose a reason for hiding this comment

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

And turn this map-then-filter into a compactMap?

if line.isEmpty { return nil }

// Support double dash comments only if they start at the beginning of a line.
if line.first == "/", line.count > 1, line[line.index(after: line.startIndex)] == "/" { return nil }
Copy link
Contributor Author

@JhonnyBillM JhonnyBillM Oct 16, 2019

Choose a reason for hiding this comment

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

Inverted the validation order to prevent doing a count operation if there is no chance to have a comment.

Copy link
Contributor

Choose a reason for hiding this comment

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

This might be best as line.hasPrefix(“//“)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🤯 Wonderful. Done.

@JhonnyBillM
Copy link
Contributor Author

Thanks for the review @harlanhaskins
I have pushed the changes.

/// - Parameter content: response file's content to be tokenized.
private static func tokenizeResponseFile(_ content: String) -> [String] {
return content
.split(separator: "\n").map { $0 }
Copy link
Contributor

Choose a reason for hiding this comment

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

Hm, does the result of Split not have a compactMap implementation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed.
Sorry, my bad! That was not supposed to be there and I entirely missed it out.
Thanks for calling that out.

@harlanhaskins
Copy link
Contributor

@swift-ci please test

1 similar comment
@DougGregor
Copy link
Member

@swift-ci please test

@DougGregor DougGregor merged commit 59f5dc3 into swiftlang:master Oct 16, 2019
@JhonnyBillM
Copy link
Contributor Author

Thanks! @DougGregor @harlanhaskins

Can I check this bullet point in the README ?
image
Or is anything else to do in that regard?
(besides the work that we need to do once we have WindowsToolchain support)

fengjixuchui referenced this pull request in fengjixuchui/swift-driver Oct 16, 2019
Merge pull request apple#16 from JhonnyBillM/tokenize-response-files
@harlanhaskins
Copy link
Contributor

Feel free to submit another PR for that, if you want the credit for the commit 😄

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.

3 participants