Index: llvm/include/llvm/MC/MCAsmInfo.h =================================================================== --- llvm/include/llvm/MC/MCAsmInfo.h +++ llvm/include/llvm/MC/MCAsmInfo.h @@ -130,6 +130,15 @@ /// at the beginning of statements. Defaults to false. bool RestrictCommentStringToStartOfStatement = false; + /// This indicates whether to only treat the string specified by the + /// CommentString attribute as a possible comment in AsmLexer. + /// Currently, regular C-style block level comments and line level + /// comments are accepted. + /// Furthermore, if a string starts with "#", it is treated as a possible + /// comment, even if the CommentString attribute is specified to be something + /// other than "#". + bool OnlyUseCommentStringForComment = false; + /// This is appended to emitted labels. Defaults to ":" const char *LabelSuffix; @@ -564,6 +573,9 @@ bool getRestrictCommentStringToStartOfStatement() const { return RestrictCommentStringToStartOfStatement; } + bool shouldOnlyUseCommentStringForComment() const { + return OnlyUseCommentStringForComment; + } const char *getLabelSuffix() const { return LabelSuffix; } bool useAssignmentForEHBegin() const { return UseAssignmentForEHBegin; } Index: llvm/lib/MC/MCParser/AsmLexer.cpp =================================================================== --- llvm/lib/MC/MCParser/AsmLexer.cpp +++ llvm/lib/MC/MCParser/AsmLexer.cpp @@ -175,6 +175,11 @@ /// LexSlash: Slash: / /// C-Style Comment: /* ... */ AsmToken AsmLexer::LexSlash() { + if (MAI.shouldOnlyUseCommentStringForComment()) { + IsAtStartOfStatement = false; + return AsmToken(AsmToken::Slash, StringRef(TokStart, 1)); + } + switch (*CurPtr) { case '*': IsAtStartOfStatement = false; @@ -700,7 +705,9 @@ UnLex(TokenBuf[0]); return AsmToken(AsmToken::HashDirective, s); } - return LexLineComment(); + + if (!MAI.shouldOnlyUseCommentStringForComment()) + return LexLineComment(); } if (isAtStartOfComment(TokStart)) Index: llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmInfo.cpp =================================================================== --- llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmInfo.cpp +++ llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmInfo.cpp @@ -23,6 +23,7 @@ CommentString = AssemblerDialect == AD_HLASM ? "*" : "#"; RestrictCommentStringToStartOfStatement = (AssemblerDialect == AD_HLASM); + OnlyUseCommentStringForComment = (AssemblerDialect == AD_HLASM); ZeroDirective = "\t.space\t"; Data64bitsDirective = "\t.quad\t"; UsesELFSectionDirectiveForBSS = true; Index: llvm/unittests/MC/SystemZ/SystemZAsmLexerTest.cpp =================================================================== --- llvm/unittests/MC/SystemZ/SystemZAsmLexerTest.cpp +++ llvm/unittests/MC/SystemZ/SystemZAsmLexerTest.cpp @@ -32,6 +32,9 @@ RestrictCommentStringToStartOfStatement = Value; } void setCommentString(StringRef Value) { CommentString = Value; } + void setOnlyUseCommentStringForComment(bool Value) { + OnlyUseCommentStringForComment = Value; + } }; // Setup a testing class that the GTest framework can call. @@ -213,4 +216,155 @@ AsmToken::EndOfStatement, AsmToken::Eof}); lexAndCheckTokens(AsmStr, ExpectedTokens); } + +TEST_F(SystemZAsmLexerTest, DontCheckStrictCommentString) { + StringRef AsmStr = "# abc\n/* def */// xyz"; + + // Setup. + setupCallToAsmParser(AsmStr); + + // Lex initially to get the string. + Parser->getLexer().Lex(); + + SmallVector ExpectedTokens( + {AsmToken::EndOfStatement, AsmToken::Comment, AsmToken::EndOfStatement, + AsmToken::Eof}); + lexAndCheckTokens(AsmStr, ExpectedTokens); +} + +TEST_F(SystemZAsmLexerTest, DontCheckStrictCommentString2) { + StringRef AsmStr = "# abc\n/* def */// xyz\n* rst"; + + // Setup. + MUPMAI->setCommentString("*"); + setupCallToAsmParser(AsmStr); + + // Lex initially to get the string. + Parser->getLexer().Lex(); + + SmallVector ExpectedTokens( + {AsmToken::EndOfStatement, AsmToken::Comment, AsmToken::EndOfStatement, + AsmToken::EndOfStatement, AsmToken::Eof}); + lexAndCheckTokens(AsmStr, ExpectedTokens); +} + +TEST_F(SystemZAsmLexerTest, CheckStrictCommentString) { + StringRef AsmStr = "# abc\n/* def */// xyz"; + + // Setup. + MUPMAI->setOnlyUseCommentStringForComment(true); + setupCallToAsmParser(AsmStr); + + // Lex initially to get the string. + Parser->getLexer().Lex(); + + // "# abc" -> still treated as a comment, since CommentString + // is set to "#" + SmallVector ExpectedTokens; + ExpectedTokens.push_back(AsmToken::EndOfStatement); // "# abc\n" + ExpectedTokens.push_back(AsmToken::Slash); // "/" + ExpectedTokens.push_back(AsmToken::Star); // "*" + ExpectedTokens.push_back(AsmToken::Identifier); // "def" + ExpectedTokens.push_back(AsmToken::Star); // "*" + ExpectedTokens.push_back(AsmToken::Slash); // "/" + ExpectedTokens.push_back(AsmToken::Slash); // "/" + ExpectedTokens.push_back(AsmToken::Slash); // "/" + ExpectedTokens.push_back(AsmToken::Identifier); // "xyz" + ExpectedTokens.push_back(AsmToken::EndOfStatement); + ExpectedTokens.push_back(AsmToken::Eof); + + lexAndCheckTokens(AsmStr, ExpectedTokens); +} + +TEST_F(SystemZAsmLexerTest, CheckStrictCommentString2) { + StringRef AsmStr = "// abc"; + + // Setup. + MUPMAI->setOnlyUseCommentStringForComment(true); + MUPMAI->setCommentString("//"); + setupCallToAsmParser(AsmStr); + + // Lex initially to get the string. + Parser->getLexer().Lex(); + + // "// abc" -> will still be treated as a comment because "//" is the + // CommentString + SmallVector ExpectedTokens( + {AsmToken::EndOfStatement, AsmToken::Eof}); + lexAndCheckTokens(AsmStr /* "// abc" */, ExpectedTokens); +} + +TEST_F(SystemZAsmLexerTest, CheckStrictCommentString3) { + StringRef AsmStr = "/* abc */"; + + // Setup. + MUPMAI->setOnlyUseCommentStringForComment(true); + setupCallToAsmParser(AsmStr); + + // Lex initially to get the string. + Parser->getLexer().Lex(); + + SmallVector ExpectedTokens; + ExpectedTokens.push_back(AsmToken::Slash); + ExpectedTokens.push_back(AsmToken::Star); + ExpectedTokens.push_back(AsmToken::Identifier); + ExpectedTokens.push_back(AsmToken::Star); + ExpectedTokens.push_back(AsmToken::Slash); + ExpectedTokens.push_back(AsmToken::EndOfStatement); + ExpectedTokens.push_back(AsmToken::Eof); + + lexAndCheckTokens(AsmStr, ExpectedTokens); +} + +TEST_F(SystemZAsmLexerTest, CheckStrictCommentString4) { + StringRef AsmStr = "# abc\n/* def */// xyz"; + + // Setup. + MUPMAI->setCommentString("*"); + MUPMAI->setOnlyUseCommentStringForComment(true); + MUPMAI->setRestrictCommentStringToStartOfStatement(true); + setupCallToAsmParser(AsmStr); + + // Lex initially to get the string. + Parser->getLexer().Lex(); + + SmallVector ExpectedTokens; + ExpectedTokens.push_back(AsmToken::Hash); // "#" + ExpectedTokens.push_back(AsmToken::Identifier); // "abc" + ExpectedTokens.push_back(AsmToken::EndOfStatement); // "\n" + ExpectedTokens.push_back(AsmToken::Slash); // "/" + ExpectedTokens.push_back(AsmToken::Star); // "*" + ExpectedTokens.push_back(AsmToken::Identifier); // "def" + ExpectedTokens.push_back(AsmToken::Star); // "*" + ExpectedTokens.push_back(AsmToken::Slash); // "/" + ExpectedTokens.push_back(AsmToken::Slash); // "/" + ExpectedTokens.push_back(AsmToken::Slash); // "/" + ExpectedTokens.push_back(AsmToken::Identifier); // "xyz" + ExpectedTokens.push_back(AsmToken::EndOfStatement); + ExpectedTokens.push_back(AsmToken::Eof); + + lexAndCheckTokens(AsmStr, ExpectedTokens); +} + +TEST_F(SystemZAsmLexerTest, CheckStrictCommentString5) { + StringRef AsmStr = "#abc\n/* def */// xyz"; + + // Setup. + MUPMAI->setCommentString("*"); + MUPMAI->setOnlyUseCommentStringForComment(true); + setupCallToAsmParser(AsmStr); + + // Lex initially to get the string. + Parser->getLexer().Lex(); + + SmallVector ExpectedTokens; + ExpectedTokens.push_back(AsmToken::Hash); // "#" + ExpectedTokens.push_back(AsmToken::Identifier); // "abc" + ExpectedTokens.push_back(AsmToken::EndOfStatement); // "\n" + ExpectedTokens.push_back(AsmToken::Slash); // "/" + ExpectedTokens.push_back(AsmToken::EndOfStatement); // "* def */// xyz" + ExpectedTokens.push_back(AsmToken::Eof); + + lexAndCheckTokens(AsmStr, ExpectedTokens); +} } // end anonymous namespace