[MC] Lex CRLF as one token
authorReid Kleckner <rnk@google.com>
Mon, 16 Oct 2017 22:20:03 +0000 (22:20 +0000)
committerReid Kleckner <rnk@google.com>
Mon, 16 Oct 2017 22:20:03 +0000 (22:20 +0000)
This will prevent doubling of line endings when parsing assembly and
emitting assembly.

Otherwise we'd parse the directive, consume the end of statement, hit
the next end of statement, and emit a fresh newline.

llvm-svn: 315943

llvm/lib/MC/MCParser/AsmLexer.cpp
llvm/test/MC/X86/crlf.test [new file with mode: 0644]

index e9123b9..b83b6d3 100644 (file)
@@ -606,8 +606,16 @@ AsmToken AsmLexer::LexToken() {
       return LexToken(); // Ignore whitespace.
     else
       return AsmToken(AsmToken::Space, StringRef(TokStart, CurPtr - TokStart));
+  case '\r': {
+    IsAtStartOfLine = true;
+    IsAtStartOfStatement = true;
+    // If this is a CR followed by LF, treat that as one token.
+    if (CurPtr != CurBuf.end() && *CurPtr == '\n')
+      ++CurPtr;
+    return AsmToken(AsmToken::EndOfStatement,
+                    StringRef(TokStart, CurPtr - TokStart));
+  }
   case '\n':
-  case '\r':
     IsAtStartOfLine = true;
     IsAtStartOfStatement = true;
     return AsmToken(AsmToken::EndOfStatement, StringRef(TokStart, 1));
diff --git a/llvm/test/MC/X86/crlf.test b/llvm/test/MC/X86/crlf.test
new file mode 100644 (file)
index 0000000..6601445
--- /dev/null
@@ -0,0 +1,5 @@
+RUN: printf '\r\n\r\n' | llvm-mc -as-lex | FileCheck %s
+There should only be two end of statements.
+CHECK: EndOfStatement
+CHECK: EndOfStatement
+CHECK-NOT: EndOfStatement