]> granicus.if.org Git - llvm/commitdiff
[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.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@315943 91177308-0d34-0410-b5e6-96231b3b80d8

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

index e9123b9d71420ba491d8da1bf821b5bc587bdcbe..b83b6d3dcf6a1d8a9ef4e8d6fb1c1dd33ec70cf5 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/test/MC/X86/crlf.test b/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