From f528592fdd3c970770b092d55b5b24bcca63d59d Mon Sep 17 00:00:00 2001 From: Amara Emerson Date: Sat, 25 Oct 2025 19:58:13 -0700 Subject: [PATCH] [MC] Fix accidentally eating the newline when handling a comment char at the end of the line. If we have a target where both # and ## are valid comment strings, a line ending in # would trigger the lexer to eat 2 characters and therefore lex the *next* line as a comment. Oops. This was introduced in 4946db15a74b761c5ac4ead18873639236b4ab5d --- llvm/lib/MC/MCParser/AsmLexer.cpp | 9 ++++++++- .../MC/AsmParser/comments-x86-darwin-eol-dropped.s | 10 ++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 llvm/test/MC/AsmParser/comments-x86-darwin-eol-dropped.s diff --git a/llvm/lib/MC/MCParser/AsmLexer.cpp b/llvm/lib/MC/MCParser/AsmLexer.cpp index 968ccf776440b..8062ce88a154b 100644 --- a/llvm/lib/MC/MCParser/AsmLexer.cpp +++ b/llvm/lib/MC/MCParser/AsmLexer.cpp @@ -835,7 +835,14 @@ AsmToken AsmLexer::LexToken() { } if (isAtStartOfComment(TokStart)) { - CurPtr += MAI.getCommentString().size() - 1; + StringRef CommentString = MAI.getCommentString(); + // For multi-char comment strings, advance CurPtr only if we matched the full + // string. This stops us from accidentally eating the newline if the current + // line ends in a single comment char. + if (CommentString.size() > 1 && + StringRef(TokStart, CommentString.size()) == CommentString) { + CurPtr += CommentString.size() - 1; + } return LexLineComment(); } diff --git a/llvm/test/MC/AsmParser/comments-x86-darwin-eol-dropped.s b/llvm/test/MC/AsmParser/comments-x86-darwin-eol-dropped.s new file mode 100644 index 0000000000000..662e5987db6e2 --- /dev/null +++ b/llvm/test/MC/AsmParser/comments-x86-darwin-eol-dropped.s @@ -0,0 +1,10 @@ +// RUN: llvm-mc -triple i386-apple-darwin %s 2>&1 | FileCheck %s +.p2align 3 +// CHECK: .p2align 3 +test: +// CHECK-LABEL: test: +// CHECK: pushl %ebp +// CHECK: movl %esp, %ebp +# Check that the following line's comment # doesn't drop the movl after + pushl %ebp # + movl %esp, %ebp