[llvm] [MC] Fix accidentally eating the newline when handling a comment char at the end of the line. (PR #165129)
    Amara Emerson via llvm-commits 
    llvm-commits at lists.llvm.org
       
    Sat Oct 25 20:31:27 PDT 2025
    
    
  
https://github.com/aemerson updated https://github.com/llvm/llvm-project/pull/165129
>From f528592fdd3c970770b092d55b5b24bcca63d59d Mon Sep 17 00:00:00 2001
From: Amara Emerson <amara at apple.com>
Date: Sat, 25 Oct 2025 19:58:13 -0700
Subject: [PATCH 1/2] [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
>From 60e5cffd3dff0b5fd83aefca39691167c1a5cb60 Mon Sep 17 00:00:00 2001
From: Amara Emerson <amara at apple.com>
Date: Sat, 25 Oct 2025 20:31:05 -0700
Subject: [PATCH 2/2] clang-format
---
 llvm/lib/MC/MCParser/AsmLexer.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/MC/MCParser/AsmLexer.cpp b/llvm/lib/MC/MCParser/AsmLexer.cpp
index 8062ce88a154b..a6188f0676937 100644
--- a/llvm/lib/MC/MCParser/AsmLexer.cpp
+++ b/llvm/lib/MC/MCParser/AsmLexer.cpp
@@ -836,9 +836,9 @@ AsmToken AsmLexer::LexToken() {
 
   if (isAtStartOfComment(TokStart)) {
     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.
+    // 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;
    
    
More information about the llvm-commits
mailing list