Skip to content

Commit 792c65c

Browse files
authored
[MC] Fix accidentally eating the newline when handling a comment char at the end of the line. (#165129)
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 4946db1 rdar://162635338
1 parent cd27741 commit 792c65c

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

llvm/lib/MC/MCParser/AsmLexer.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,14 @@ AsmToken AsmLexer::LexToken() {
835835
}
836836

837837
if (isAtStartOfComment(TokStart)) {
838-
CurPtr += MAI.getCommentString().size() - 1;
838+
StringRef CommentString = MAI.getCommentString();
839+
// For multi-char comment strings, advance CurPtr only if we matched the
840+
// full string. This stops us from accidentally eating the newline if the
841+
// current line ends in a single comment char.
842+
if (CommentString.size() > 1 &&
843+
StringRef(TokStart, CommentString.size()) == CommentString) {
844+
CurPtr += CommentString.size() - 1;
845+
}
839846
return LexLineComment();
840847
}
841848

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: llvm-mc -triple i386-apple-darwin %s 2>&1 | FileCheck %s
2+
.p2align 3
3+
// CHECK: .p2align 3
4+
test:
5+
// CHECK-LABEL: test:
6+
// CHECK: pushl %ebp
7+
// CHECK: movl %esp, %ebp
8+
# Check that the following line's comment # doesn't drop the movl after
9+
pushl %ebp #
10+
movl %esp, %ebp

0 commit comments

Comments
 (0)