Skip to content

[Parser] Detect non-breaking space (U+00A0) and offer a fix-it #16090

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 5 commits into from
Apr 24, 2018

Conversation

hashemi
Copy link
Contributor

@hashemi hashemi commented Apr 22, 2018

Detect Unicode non-breaking space character (U+00A0) in the lexer, diagnose it, and offer a fix-it turning it into a regular space. Also treat the non-breaking space as whitespace when considering whether an operator is left/right bound.

Resolves SR-7122.

@CodaFi CodaFi requested a review from jrose-apple April 22, 2018 11:55
@hashemi hashemi force-pushed the non-breaking-spaces branch from 1645493 to 9d28cd5 Compare April 22, 2018 20:15
@@ -716,6 +722,12 @@ static bool isRightBound(const char *tokEnd, bool isLeftBound,
else
return true;

case '\302':
if (tokEnd[1] == '\240')
Copy link
Member

Choose a reason for hiding this comment

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

Could you make these '\xC2' and '\xA0'?

@@ -682,6 +682,12 @@ static bool isLeftBound(const char *tokBegin, const char *bufferBegin) {
else
return true;

case '\240':
if (tokBegin - 1 != bufferBegin && tokBegin[-2] == '\302')
Copy link
Member

Choose a reason for hiding this comment

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

ditto

} else if (Codepoint == 0x000000A0) {
// Non-breaking whitespace (U+00A0)
diagnose(CurPtr - 1, diag::lex_nonbreaking_space)
.fixItReplaceChars(getSourceLoc(CurPtr - 1), getSourceLoc(Tmp), " ");
Copy link
Member

Choose a reason for hiding this comment

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

Nitpick: indent.

Copy link
Member

@rintaro rintaro left a comment

Choose a reason for hiding this comment

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

This looks great! Just a few code style requests.

@rintaro
Copy link
Member

rintaro commented Apr 23, 2018

Also, could you add tests for fix-it in test/Parse ?

@hashemi
Copy link
Contributor Author

hashemi commented Apr 23, 2018

Thanks @rintaro, all suggestions incorporated.
I made a new file for the parse tests. Is there a better spot? Recovery.swift is another possibility.

@rintaro
Copy link
Member

rintaro commented Apr 24, 2018

I think dedicated test file is OK. Since this test is handling invisible characters, it's more safe from accidental (automatic) edits.
@swift-ci Please smoke test

@@ -716,6 +722,12 @@ static bool isRightBound(const char *tokEnd, bool isLeftBound,
else
return true;

case '\xC2':
if (tokEnd[1] == '\xA0')
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you confirm that this dereference is safe? Can we assume that the buffer is always null-terminated?

Copy link
Member

Choose a reason for hiding this comment

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

@rintaro rintaro merged commit 06c528d into swiftlang:master Apr 24, 2018
@rintaro
Copy link
Member

rintaro commented Apr 24, 2018

Thank you Ahmad! Please mark the SR issue RESOLVED.

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.

2 participants