[llvm] ae2aef1 - [AsmParser][SystemZ][z/OS] Reject character and string literals for HLASM
Anirudh Prasad via llvm-commits
llvm-commits at lists.llvm.org
Wed May 5 07:22:01 PDT 2021
Author: Anirudh Prasad
Date: 2021-05-05T10:21:55-04:00
New Revision: ae2aef13618beb8cb86e8b137a8ddbc846461169
URL: https://github.com/llvm/llvm-project/commit/ae2aef13618beb8cb86e8b137a8ddbc846461169
DIFF: https://github.com/llvm/llvm-project/commit/ae2aef13618beb8cb86e8b137a8ddbc846461169.diff
LOG: [AsmParser][SystemZ][z/OS] Reject character and string literals for HLASM
- As per the HLASM support we are providing, i.e. support only for the first parameter of the inline asm block, only pertaining to Z machine instructions defined in LLVM, character literals and string literals are not supported (see Figure 4 - https://www-01.ibm.com/servers/resourcelink/svc00100.nsf/pages/zOSV2R3sc264940/$file/asmr1023.pdf for more information)
- This patch explicitly rejects the usage of char literals and string literals (for example "abc 'a'") when the relevant field is set
- This is achieved by introducing a field called `LexHLASMStrings` in MCAsmLexer similar to `LexMasmStrings`
Reviewed By: abhina.sreeskantharajan, Kai
Differential Revision: https://reviews.llvm.org/D101660
Added:
Modified:
llvm/include/llvm/MC/MCParser/MCAsmLexer.h
llvm/lib/MC/MCParser/AsmLexer.cpp
llvm/unittests/MC/SystemZ/SystemZAsmLexerTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/MC/MCParser/MCAsmLexer.h b/llvm/include/llvm/MC/MCParser/MCAsmLexer.h
index 6a604014a8374..06796979b4fc3 100644
--- a/llvm/include/llvm/MC/MCParser/MCAsmLexer.h
+++ b/llvm/include/llvm/MC/MCParser/MCAsmLexer.h
@@ -57,6 +57,7 @@ class MCAsmLexer {
bool UseMasmDefaultRadix = false;
unsigned DefaultRadix = 10;
bool LexHLASMIntegers = false;
+ bool LexHLASMStrings = false;
AsmCommentConsumer *CommentConsumer = nullptr;
MCAsmLexer();
@@ -180,6 +181,11 @@ class MCAsmLexer {
/// Set whether to lex HLASM-flavour integers. For now this is only [0-9]*
void setLexHLASMIntegers(bool V) { LexHLASMIntegers = V; }
+
+ /// Set whether to "lex" HLASM-flavour character and string literals. For now,
+ /// setting this option to true, will disable lexing for character and string
+ /// literals.
+ void setLexHLASMStrings(bool V) { LexHLASMStrings = V; }
};
} // end namespace llvm
diff --git a/llvm/lib/MC/MCParser/AsmLexer.cpp b/llvm/lib/MC/MCParser/AsmLexer.cpp
index 9d97837d0f606..e328ba5315af5 100644
--- a/llvm/lib/MC/MCParser/AsmLexer.cpp
+++ b/llvm/lib/MC/MCParser/AsmLexer.cpp
@@ -567,6 +567,9 @@ AsmToken AsmLexer::LexDigit() {
AsmToken AsmLexer::LexSingleQuote() {
int CurChar = getNextChar();
+ if (LexHLASMStrings)
+ return ReturnError(TokStart, "invalid usage of character literals");
+
if (LexMasmStrings) {
while (CurChar != EOF) {
if (CurChar != '\'') {
@@ -621,6 +624,9 @@ AsmToken AsmLexer::LexSingleQuote() {
/// LexQuote: String: "..."
AsmToken AsmLexer::LexQuote() {
int CurChar = getNextChar();
+ if (LexHLASMStrings)
+ return ReturnError(TokStart, "invalid usage of string literals");
+
if (LexMasmStrings) {
while (CurChar != EOF) {
if (CurChar != '"') {
diff --git a/llvm/unittests/MC/SystemZ/SystemZAsmLexerTest.cpp b/llvm/unittests/MC/SystemZ/SystemZAsmLexerTest.cpp
index 353084a7a28b3..a93bf8dc710e9 100644
--- a/llvm/unittests/MC/SystemZ/SystemZAsmLexerTest.cpp
+++ b/llvm/unittests/MC/SystemZ/SystemZAsmLexerTest.cpp
@@ -700,4 +700,36 @@ TEST_F(SystemZAsmLexerTest, CheckRejectStarAsCurrentPC) {
EXPECT_EQ(ParsePrimaryExpr, true);
EXPECT_EQ(Parser->hasPendingError(), true);
}
+
+TEST_F(SystemZAsmLexerTest, CheckRejectCharLiterals) {
+ StringRef AsmStr = "abc 'd'";
+
+ // Setup.
+ setupCallToAsmParser(AsmStr);
+ Parser->getLexer().setLexHLASMStrings(true);
+
+ // Lex initially to get the string.
+ Parser->getLexer().Lex();
+
+ SmallVector<AsmToken::TokenKind> ExpectedTokens(
+ {AsmToken::Identifier, AsmToken::Error, AsmToken::Error,
+ AsmToken::EndOfStatement, AsmToken::Eof});
+ lexAndCheckTokens(AsmStr, ExpectedTokens);
+}
+
+TEST_F(SystemZAsmLexerTest, CheckRejectStringLiterals) {
+ StringRef AsmStr = "abc \"ef\"";
+
+ // Setup.
+ setupCallToAsmParser(AsmStr);
+ Parser->getLexer().setLexHLASMStrings(true);
+
+ // Lex initially to get the string.
+ Parser->getLexer().Lex();
+
+ SmallVector<AsmToken::TokenKind> ExpectedTokens(
+ {AsmToken::Identifier, AsmToken::Error, AsmToken::Identifier,
+ AsmToken::Error, AsmToken::EndOfStatement, AsmToken::Eof});
+ lexAndCheckTokens(AsmStr, ExpectedTokens);
+}
} // end anonymous namespace
More information about the llvm-commits
mailing list