[llvm] r215615 - MC: AsmLexer: handle multi-character CommentStrings correctly
Saleem Abdulrasool
compnerd at compnerd.org
Wed Aug 13 19:51:43 PDT 2014
Author: compnerd
Date: Wed Aug 13 21:51:43 2014
New Revision: 215615
URL: http://llvm.org/viewvc/llvm-project?rev=215615&view=rev
Log:
MC: AsmLexer: handle multi-character CommentStrings correctly
As X86MCAsmInfoDarwin uses '##' as CommentString although a single '#' starts a
comment a workaround for this special case is added.
Fixes divisions in constant expressions for the AArch64 assembler and other
targets which use '//' as CommentString.
Patch by Janne Grunau!
Added:
llvm/trunk/test/MC/AArch64/single-slash.s
llvm/trunk/test/MC/AsmParser/comments-x86-darwin.s
Modified:
llvm/trunk/include/llvm/MC/MCParser/AsmLexer.h
llvm/trunk/lib/MC/MCParser/AsmLexer.cpp
Modified: llvm/trunk/include/llvm/MC/MCParser/AsmLexer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/MC/MCParser/AsmLexer.h?rev=215615&r1=215614&r2=215615&view=diff
==============================================================================
--- llvm/trunk/include/llvm/MC/MCParser/AsmLexer.h (original)
+++ llvm/trunk/include/llvm/MC/MCParser/AsmLexer.h Wed Aug 13 21:51:43 2014
@@ -49,7 +49,7 @@ public:
const AsmToken peekTok(bool ShouldSkipSpace = true) override;
- bool isAtStartOfComment(char Char);
+ bool isAtStartOfComment(const char *Ptr);
bool isAtStatementSeparator(const char *Ptr);
const MCAsmInfo &getMAI() const { return MAI; }
Modified: llvm/trunk/lib/MC/MCParser/AsmLexer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCParser/AsmLexer.cpp?rev=215615&r1=215614&r2=215615&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCParser/AsmLexer.cpp (original)
+++ llvm/trunk/lib/MC/MCParser/AsmLexer.cpp Wed Aug 13 21:51:43 2014
@@ -417,7 +417,7 @@ AsmToken AsmLexer::LexQuote() {
StringRef AsmLexer::LexUntilEndOfStatement() {
TokStart = CurPtr;
- while (!isAtStartOfComment(*CurPtr) && // Start of line comment.
+ while (!isAtStartOfComment(CurPtr) && // Start of line comment.
!isAtStatementSeparator(CurPtr) && // End of statement marker.
*CurPtr != '\n' && *CurPtr != '\r' &&
(*CurPtr != 0 || CurPtr != CurBuf.end())) {
@@ -458,9 +458,17 @@ const AsmToken AsmLexer::peekTok(bool Sh
return Token;
}
-bool AsmLexer::isAtStartOfComment(char Char) {
- // FIXME: This won't work for multi-character comment indicators like "//".
- return Char == *MAI.getCommentString();
+bool AsmLexer::isAtStartOfComment(const char *Ptr) {
+ const char *CommentString = MAI.getCommentString();
+
+ if (CommentString[1] == '\0')
+ return CommentString[0] == Ptr[0];
+
+ // FIXME: special case for the bogus "##" comment string in X86MCAsmInfoDarwin
+ if (CommentString[1] == '#')
+ return CommentString[0] == Ptr[0];
+
+ return strncmp(Ptr, CommentString, strlen(CommentString)) == 0;
}
bool AsmLexer::isAtStatementSeparator(const char *Ptr) {
@@ -473,7 +481,7 @@ AsmToken AsmLexer::LexToken() {
// This always consumes at least one character.
int CurChar = getNextChar();
- if (isAtStartOfComment(CurChar)) {
+ if (isAtStartOfComment(TokStart)) {
// If this comment starts with a '#', then return the Hash token and let
// the assembler parser see if it can be parsed as a cpp line filename
// comment. We do this only if we are at the start of a line.
Added: llvm/trunk/test/MC/AArch64/single-slash.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/AArch64/single-slash.s?rev=215615&view=auto
==============================================================================
--- llvm/trunk/test/MC/AArch64/single-slash.s (added)
+++ llvm/trunk/test/MC/AArch64/single-slash.s Wed Aug 13 21:51:43 2014
@@ -0,0 +1,6 @@
+// RUN: llvm-mc -triple aarch64-none-linux-gnu < %s | FileCheck %s
+
+// Test that a single slash is not mistaken as the start of comment.
+
+//CHECK: movz x0, #0x10
+ movz x0, #(32 / 2)
Added: llvm/trunk/test/MC/AsmParser/comments-x86-darwin.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/AsmParser/comments-x86-darwin.s?rev=215615&view=auto
==============================================================================
--- llvm/trunk/test/MC/AsmParser/comments-x86-darwin.s (added)
+++ llvm/trunk/test/MC/AsmParser/comments-x86-darwin.s Wed Aug 13 21:51:43 2014
@@ -0,0 +1,14 @@
+// RUN: llvm-mc -triple x86_64-apple-darwin %s 2>&1 | FileCheck %s
+# ensure that single '#' comments are worink as expected on x86 darwin
+.align 3 # test single hash after align
+// CHECK: .align 3
+foo: # single hash should be ignored as comment
+// CHECK-LABEL: foo:
+ movl %esp, %ebp # same after an instruction
+// CHECK: movl %esp, %ebp
+# movl %esp, %ebp ## start of the line
+// CHECK-NOT: movl %esp, %ebp
+ # movl %esp, %ebp ## not quite start of the line
+// CHECK-NOT: movl %esp, %ebp
+bar:
+// CHECK-LABEL: bar:
More information about the llvm-commits
mailing list