Skip to content

Commit ceb17b8

Browse files
tanadeautkremenek
authored andcommitted
Don't error when lexing UTF-8 BOM
1 parent 2f02635 commit ceb17b8

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

lib/Parse/Lexer.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,15 @@ Lexer::Lexer(const LangOptions &Options,
177177
StringRef contents = SM.extractText(SM.getRangeForBuffer(BufferID));
178178
BufferStart = contents.data();
179179
BufferEnd = contents.data() + contents.size();
180-
CurPtr = BufferStart;
180+
181+
// Check for Unicode BOM at start of file (Only UTF-8 BOM supported now).
182+
size_t BOMLength = llvm::StringSwitch<size_t>(contents)
183+
.StartsWith("\xEF\xBB\xBF", 3)
184+
.Default(0);
185+
186+
// Since the UTF-8 BOM doesn't carry information (UTF-8 has no dependency
187+
// on byte order), throw it away.
188+
CurPtr = BufferStart + BOMLength;
181189

182190
// Initialize code completion.
183191
if (BufferID == SM.getCodeCompletionBufferID()) {
File renamed without changes.

test/Parse/utf8_bom.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// RUN: %target-parse-verify-swift
2+
struct UTF8Test {}

0 commit comments

Comments
 (0)