Skip to content
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

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

Merged
merged 5 commits into from
Apr 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ ERROR(lex_invalid_curly_quote,none,
NOTE(lex_confusable_character,none,
"unicode character '%0' looks similar to '%1'; did you mean to use '%1'?",
(StringRef, StringRef))
WARNING(lex_nonbreaking_space,none,
"non-breaking space (U+00A0) used instead of regular space", ())

ERROR(lex_unterminated_block_comment,none,
"unterminated '/*' comment", ())
Expand Down
18 changes: 18 additions & 0 deletions lib/Parse/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,12 @@ static bool isLeftBound(const char *tokBegin, const char *bufferBegin) {
else
return true;

case '\xA0':
if (tokBegin - 1 != bufferBegin && tokBegin[-2] == '\xC2')
return false; // Non-breaking whitespace (U+00A0)
else
return true;

default:
return true;
}
Expand Down Expand Up @@ -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.

return false; // Non-breaking whitespace (U+00A0)
else
return true;

default:
return true;
}
Expand Down Expand Up @@ -1894,6 +1906,12 @@ bool Lexer::lexUnknown(bool EmitDiagnosticsIfToken) {
.fixItReplaceChars(getSourceLoc(CurPtr - 1), getSourceLoc(Tmp), " ");
CurPtr = Tmp;
return false; // Skip presumed whitespace.
} else if (Codepoint == 0x000000A0) {
// Non-breaking whitespace (U+00A0)
diagnose(CurPtr - 1, diag::lex_nonbreaking_space)
.fixItReplaceChars(getSourceLoc(CurPtr - 1), getSourceLoc(Tmp), " ");
CurPtr = Tmp;
return false;
} else if (Codepoint == 0x0000201D) {
// If this is an end curly quote, just diagnose it with a fixit hint.
if (EmitDiagnosticsIfToken) {
Expand Down
6 changes: 6 additions & 0 deletions test/Parse/nonbreaking_space.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// RUN: %target-typecheck-verify-swift

let nonBreakingSpace1 = 3 // expected-warning {{non-breaking space (U+00A0) used instead of regular space}} {{22-24= }}

let nonBreakingSpace2 = 3 // expected-warning {{non-breaking space (U+00A0) used instead of regular space}} {{24-26= }}

44 changes: 44 additions & 0 deletions test/Syntax/tokens_nonbreaking_space.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// RUN: cat %s | sed -e 's/'$(echo -ne "\x5a")'/'$(echo -ne "\xc2\xa0")'/g' > %t.tmp
// RUN: cp -f %t.tmp %t
// RUN: %swift-syntax-test -input-source-filename %t -dump-full-tokens 2>&1 | %FileCheck %t
let a =Z3Z // nbsp(Z)
let bZ= 3Z
let cZ=Z3

// CHECK: 4:8: warning: non-breaking space (U+00A0) used instead of regular space
// CHECK: 4:11: warning: non-breaking space (U+00A0) used instead of regular space
// CHECK: 5:6: warning: non-breaking space (U+00A0) used instead of regular space
// CHECK: 5:11: warning: non-breaking space (U+00A0) used instead of regular space
// CHECK: 6:6: warning: non-breaking space (U+00A0) used instead of regular space
// CHECK: 6:9: warning: non-breaking space (U+00A0) used instead of regular space

// CHECK-LABEL: 4:7
// CHECK-NEXT:(Token equal
// CHECK-NEXT: (text="=")
// CHECK-NEXT: (trivia garbageText \302\240))

// CHECK-LABEL: 4:10
// CHECK-NEXT:(Token integer_literal
// CHECK-NEXT: (text="3")
// CHECK-NEXT: (trivia garbageText \302\240)
// CHECK-NEXT: (trivia space 1))

// CHECK-LABEL: 5:5
// CHECK-NEXT:(Token identifier
// CHECK-NEXT: (text="b")
// CHECK-NEXT: (trivia garbageText \302\240))

// CHECK-LABEL: 5:10
// CHECK-NEXT:(Token integer_literal
// CHECK-NEXT: (text="3")
// CHECK-NEXT: (trivia garbageText \302\240)

// CHECK-LABEL: 6:5
// CHECK-NEXT:(Token identifier
// CHECK-NEXT: (text="c")
// CHECK-NEXT: (trivia garbageText \302\240))

// CHECK-LABEL: 6:8
// CHECK-NEXT:(Token equal
// CHECK-NEXT: (text="=")
// CHECK-NEXT: (trivia garbageText \302\240))