diff --git a/llvm/include/llvm/MC/MCAsmInfo.h b/llvm/include/llvm/MC/MCAsmInfo.h --- a/llvm/include/llvm/MC/MCAsmInfo.h +++ b/llvm/include/llvm/MC/MCAsmInfo.h @@ -130,6 +130,14 @@ /// at the beginning of statements. Defaults to false. bool RestrictCommentStringToStartOfStatement = false; + /// This indicates whether to allow additional "comment strings" to be lexed + /// as a comment. Setting this attribute to true, will ensure that C-style + /// line comments (// ..), C-style block comments (/* .. */), and "#" are + /// all treated as comments in addition to the string specified by the + /// CommentString attribute. + /// Default is true. + bool AllowAdditionalComments = true; + /// This is appended to emitted labels. Defaults to ":" const char *LabelSuffix; @@ -567,6 +575,7 @@ bool getRestrictCommentStringToStartOfStatement() const { return RestrictCommentStringToStartOfStatement; } + bool shouldAllowAdditionalComments() const { return AllowAdditionalComments; } const char *getLabelSuffix() const { return LabelSuffix; } bool useAssignmentForEHBegin() const { return UseAssignmentForEHBegin; } diff --git a/llvm/lib/MC/MCParser/AsmLexer.cpp b/llvm/lib/MC/MCParser/AsmLexer.cpp --- a/llvm/lib/MC/MCParser/AsmLexer.cpp +++ b/llvm/lib/MC/MCParser/AsmLexer.cpp @@ -175,7 +175,13 @@ /// LexSlash: Slash: / /// C-Style Comment: /* ... */ +/// C-style Comment: // ... AsmToken AsmLexer::LexSlash() { + if (!MAI.shouldAllowAdditionalComments()) { + IsAtStartOfStatement = false; + return AsmToken(AsmToken::Slash, StringRef(TokStart, 1)); + } + switch (*CurPtr) { case '*': IsAtStartOfStatement = false; @@ -729,7 +735,9 @@ UnLex(TokenBuf[0]); return AsmToken(AsmToken::HashDirective, s); } - return LexLineComment(); + + if (MAI.shouldAllowAdditionalComments()) + return LexLineComment(); } if (isAtStartOfComment(TokStart)) diff --git a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmInfo.cpp b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmInfo.cpp --- a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmInfo.cpp +++ b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmInfo.cpp @@ -23,6 +23,7 @@ CommentString = AssemblerDialect == AD_HLASM ? "*" : "#"; RestrictCommentStringToStartOfStatement = (AssemblerDialect == AD_HLASM); + AllowAdditionalComments = (AssemblerDialect == AD_ATT); ZeroDirective = "\t.space\t"; Data64bitsDirective = "\t.quad\t"; UsesELFSectionDirectiveForBSS = true; diff --git a/llvm/unittests/MC/SystemZ/SystemZAsmLexerTest.cpp b/llvm/unittests/MC/SystemZ/SystemZAsmLexerTest.cpp --- a/llvm/unittests/MC/SystemZ/SystemZAsmLexerTest.cpp +++ b/llvm/unittests/MC/SystemZ/SystemZAsmLexerTest.cpp @@ -32,6 +32,9 @@ RestrictCommentStringToStartOfStatement = Value; } void setCommentString(StringRef Value) { CommentString = Value; } + void setAllowAdditionalComments(bool Value) { + AllowAdditionalComments = 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->setAllowAdditionalComments(false); + 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->setAllowAdditionalComments(false); + 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->setAllowAdditionalComments(false); + 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->setAllowAdditionalComments(false); + 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->setAllowAdditionalComments(false); + 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