Skip to content

Commit

Permalink
Add a comment consumer mechanism to MCAsmLexer
Browse files Browse the repository at this point in the history
This allows clients to register an AsmCommentConsumer with the MCAsmLexer,
which receives a callback each time a comment is parsed.

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



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@289036 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
ostannard committed Dec 8, 2016
1 parent 4f38f40 commit c7bb88e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
19 changes: 19 additions & 0 deletions include/llvm/MC/MCParser/MCAsmLexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@ class AsmToken {
}
};

/// A callback class which is notified of each comment in an assembly file as
/// it is lexed.
class AsmCommentConsumer {
public:
virtual ~AsmCommentConsumer() {};

/// Callback function for when a comment is lexed. Loc is the start of the
/// comment text (excluding the comment-start marker). CommentText is the text
/// of the comment, excluding the comment start and end markers, and the
/// newline for single-line comments.
virtual void HandleComment(SMLoc Loc, StringRef CommentText) = 0;
};


/// Generic assembler lexer interface, for use by target specific assembly
/// lexers.
class MCAsmLexer {
Expand All @@ -145,6 +159,7 @@ class MCAsmLexer {
bool SkipSpace;
bool AllowAtInIdentifier;
bool IsAtStartOfStatement;
AsmCommentConsumer *CommentConsumer;

MCAsmLexer();

Expand Down Expand Up @@ -234,6 +249,10 @@ class MCAsmLexer {

bool getAllowAtInIdentifier() { return AllowAtInIdentifier; }
void setAllowAtInIdentifier(bool v) { AllowAtInIdentifier = v; }

void setCommentConsumer(AsmCommentConsumer *CommentConsumer) {
this->CommentConsumer = CommentConsumer;
}
};

} // End llvm namespace
Expand Down
15 changes: 15 additions & 0 deletions lib/MC/MCParser/AsmLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,19 @@ AsmToken AsmLexer::LexSlash() {

// C Style comment.
++CurPtr; // skip the star.
const char *CommentTextStart = CurPtr;
while (CurPtr != CurBuf.end()) {
switch (*CurPtr++) {
case '*':
// End of the comment?
if (*CurPtr != '/')
break;
// If we have a CommentConsumer, notify it about the comment.
if (CommentConsumer) {
CommentConsumer->HandleComment(
SMLoc::getFromPointer(CommentTextStart),
StringRef(CommentTextStart, CurPtr - 1 - CommentTextStart));
}
++CurPtr; // End the */.
return AsmToken(AsmToken::Comment,
StringRef(TokStart, CurPtr - TokStart));
Expand All @@ -202,10 +209,18 @@ AsmToken AsmLexer::LexLineComment() {
// comment. While it would be nicer to leave this two tokens,
// backwards compatability with TargetParsers makes keeping this in this form
// better.
const char *CommentTextStart = CurPtr;
int CurChar = getNextChar();
while (CurChar != '\n' && CurChar != '\r' && CurChar != EOF)
CurChar = getNextChar();

// If we have a CommentConsumer, notify it about the comment.
if (CommentConsumer) {
CommentConsumer->HandleComment(
SMLoc::getFromPointer(CommentTextStart),
StringRef(CommentTextStart, CurPtr - 1 - CommentTextStart));
}

IsAtStartOfLine = true;
// This is a whole line comment. leave newline
if (IsAtStartOfStatement)
Expand Down
3 changes: 2 additions & 1 deletion lib/MC/MCParser/MCAsmLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
using namespace llvm;

MCAsmLexer::MCAsmLexer()
: TokStart(nullptr), SkipSpace(true), IsAtStartOfStatement(true) {
: TokStart(nullptr), SkipSpace(true), IsAtStartOfStatement(true),
CommentConsumer(nullptr) {
CurTok.emplace_back(AsmToken::Space, StringRef());
}

Expand Down

0 comments on commit c7bb88e

Please sign in to comment.