[PATCH] D101660: [AsmParser][SystemZ][z/OS] Reject character and string literals for HLASM

Anirudh Prasad via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed May 5 07:22:09 PDT 2021


This revision was automatically updated to reflect the committed changes.
Closed by commit rGae2aef13618b: [AsmParser][SystemZ][z/OS] Reject character and string literals for HLASM (authored by anirudhp).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D101660/new/

https://reviews.llvm.org/D101660

Files:
  llvm/include/llvm/MC/MCParser/MCAsmLexer.h
  llvm/lib/MC/MCParser/AsmLexer.cpp
  llvm/unittests/MC/SystemZ/SystemZAsmLexerTest.cpp


Index: llvm/unittests/MC/SystemZ/SystemZAsmLexerTest.cpp
===================================================================
--- llvm/unittests/MC/SystemZ/SystemZAsmLexerTest.cpp
+++ llvm/unittests/MC/SystemZ/SystemZAsmLexerTest.cpp
@@ -700,4 +700,36 @@
   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
Index: llvm/lib/MC/MCParser/AsmLexer.cpp
===================================================================
--- llvm/lib/MC/MCParser/AsmLexer.cpp
+++ llvm/lib/MC/MCParser/AsmLexer.cpp
@@ -567,6 +567,9 @@
 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 @@
 /// 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 != '"') {
Index: llvm/include/llvm/MC/MCParser/MCAsmLexer.h
===================================================================
--- llvm/include/llvm/MC/MCParser/MCAsmLexer.h
+++ llvm/include/llvm/MC/MCParser/MCAsmLexer.h
@@ -57,6 +57,7 @@
   bool UseMasmDefaultRadix = false;
   unsigned DefaultRadix = 10;
   bool LexHLASMIntegers = false;
+  bool LexHLASMStrings = false;
   AsmCommentConsumer *CommentConsumer = nullptr;
 
   MCAsmLexer();
@@ -180,6 +181,11 @@
 
   /// 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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D101660.343037.patch
Type: text/x-patch
Size: 2977 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210505/f85ead14/attachment.bin>


More information about the llvm-commits mailing list