Author: Scott Linder
Date: 2020-12-09T23:39:32Z
New Revision: 19c56e11fa489cfc461b9ea10faa68e5a1f3eca1

URL: 
https://github.com/llvm/llvm-project/commit/19c56e11fa489cfc461b9ea10faa68e5a1f3eca1
DIFF: 
https://github.com/llvm/llvm-project/commit/19c56e11fa489cfc461b9ea10faa68e5a1f3eca1.diff

LOG: [MC] Fix ICE with non-newline terminated input

There is an explicit option for the lexer to support this, but we crash
when `-preserve-comments` is enabled because it checks for
`getTok().getString().empty()` to detect the case. This doesn't
work currently because the lexer reports this case as a string of length
1, containing a null byte.

Change the lexer to instead report this case via an empty string, as the
null terminator isn't logically a part of the textual input, and the
check for `.empty()` seems natural and obvious in the calling code.

Reviewed By: niravd

Differential Revision: https://reviews.llvm.org/D92681

Added: 
    llvm/test/MC/AsmParser/Inputs/no-newline-at-end-of-file.s

Modified: 
    llvm/lib/MC/MCParser/AsmLexer.cpp
    llvm/test/MC/AsmParser/preserve-comments.s

Removed: 
    


################################################################################
diff  --git a/llvm/lib/MC/MCParser/AsmLexer.cpp 
b/llvm/lib/MC/MCParser/AsmLexer.cpp
index 5c9d1264aaa0..1fa22ab000f0 100644
--- a/llvm/lib/MC/MCParser/AsmLexer.cpp
+++ b/llvm/lib/MC/MCParser/AsmLexer.cpp
@@ -715,7 +715,7 @@ AsmToken AsmLexer::LexToken() {
   if (CurChar == EOF && !IsAtStartOfStatement && EndStatementAtEOF) {
     IsAtStartOfLine = true;
     IsAtStartOfStatement = true;
-    return AsmToken(AsmToken::EndOfStatement, StringRef(TokStart, 1));
+    return AsmToken(AsmToken::EndOfStatement, StringRef(TokStart, 0));
   }
   IsAtStartOfLine = false;
   bool OldIsAtStartOfStatement = IsAtStartOfStatement;

diff  --git a/llvm/test/MC/AsmParser/Inputs/no-newline-at-end-of-file.s 
b/llvm/test/MC/AsmParser/Inputs/no-newline-at-end-of-file.s
new file mode 100644
index 000000000000..a746462708c4
--- /dev/null
+++ b/llvm/test/MC/AsmParser/Inputs/no-newline-at-end-of-file.s
@@ -0,0 +1 @@
+.text
\ No newline at end of file

diff  --git a/llvm/test/MC/AsmParser/preserve-comments.s 
b/llvm/test/MC/AsmParser/preserve-comments.s
index 27c5b2e07071..9cace041fb41 100644
--- a/llvm/test/MC/AsmParser/preserve-comments.s
+++ b/llvm/test/MC/AsmParser/preserve-comments.s
@@ -1,5 +1,6 @@
        #RUN: llvm-mc -preserve-comments -n -triple i386-linux-gnu < %s > %t
        #RUN: 
diff  -b %s %t
+       #RUN: llvm-mc -preserve-comments -n -triple i386-linux-gnu < 
%p/Inputs/no-newline-at-end-of-file.s | FileCheck %s
        .text
 
 foo:   #Comment here
@@ -11,3 +12,6 @@ foo:  #Comment here
        #endif
        .ident  "clang version 3.9.0"
        .section        ".note.GNU-stack","",@progbits
+
+       #Confirm we don't crash on inputs without a terminating newline.
+       #CHECK: .text


        
_______________________________________________
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to