[PATCH] D90234: [MCParser] Correctly handle Windows line-endings when consuming lexed line comments

Stephen Tozer via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 27 08:09:16 PDT 2020


StephenTozer created this revision.
StephenTozer added reviewers: lattner, caoz, olista01, grosbach.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.
StephenTozer requested review of this revision.

Fixes issue: https://bugs.llvm.org/show_bug.cgi?id=47983

The AsmLexer has a function `LexLineComment` that, as part of the lexing, passes the contents of the comment to a `CommentConsumer` if one exists. The passed comment is meant to exclude newline characters, but it does this by taking the range from the start of the comment inclusive to the last newline exclusive; this works with Unix line-endings, which are a single character, but fails when used with Windows line-endings, in which case the carriage return will be included as part of the passed comment. This causes an issue with llvm-mca, as it reads directives which have no label as directives with the label `\r`, but may result in inconsistent behaviour for any consumer when switching between line ending styles.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D90234

Files:
  llvm/lib/MC/MCParser/AsmLexer.cpp


Index: llvm/lib/MC/MCParser/AsmLexer.cpp
===================================================================
--- llvm/lib/MC/MCParser/AsmLexer.cpp
+++ llvm/lib/MC/MCParser/AsmLexer.cpp
@@ -214,6 +214,7 @@
   int CurChar = getNextChar();
   while (CurChar != '\n' && CurChar != '\r' && CurChar != EOF)
     CurChar = getNextChar();
+  const char *NewlinePtr = CurPtr;
   if (CurChar == '\r' && CurPtr != CurBuf.end() && *CurPtr == '\n')
     ++CurPtr;
 
@@ -221,7 +222,7 @@
   if (CommentConsumer) {
     CommentConsumer->HandleComment(
         SMLoc::getFromPointer(CommentTextStart),
-        StringRef(CommentTextStart, CurPtr - 1 - CommentTextStart));
+        StringRef(CommentTextStart, NewlinePtr - 1 - CommentTextStart));
   }
 
   IsAtStartOfLine = true;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D90234.300986.patch
Type: text/x-patch
Size: 779 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201027/fafcb2af/attachment-0001.bin>


More information about the llvm-commits mailing list