summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOliver Stannard <oliver.stannard@arm.com>2016-12-08 10:31:21 +0000
committerOliver Stannard <oliver.stannard@arm.com>2016-12-08 10:31:21 +0000
commit68e7c21ca0d3fc26ccbefe4214ff4c1b34484c4f (patch)
tree0b5415e412f72fbb1047b3390412b99835d932ef
parentd9c53710d55cc583f94e3cb439275b4fca99f50d (diff)
downloadbcm5719-llvm-68e7c21ca0d3fc26ccbefe4214ff4c1b34484c4f.tar.gz
bcm5719-llvm-68e7c21ca0d3fc26ccbefe4214ff4c1b34484c4f.zip
Add a comment consumer mechanism to MCAsmLexer
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 llvm-svn: 289036
-rw-r--r--llvm/include/llvm/MC/MCParser/MCAsmLexer.h19
-rw-r--r--llvm/lib/MC/MCParser/AsmLexer.cpp15
-rw-r--r--llvm/lib/MC/MCParser/MCAsmLexer.cpp3
3 files changed, 36 insertions, 1 deletions
diff --git a/llvm/include/llvm/MC/MCParser/MCAsmLexer.h b/llvm/include/llvm/MC/MCParser/MCAsmLexer.h
index ad5bcc481cc..56da6f85c19 100644
--- a/llvm/include/llvm/MC/MCParser/MCAsmLexer.h
+++ b/llvm/include/llvm/MC/MCParser/MCAsmLexer.h
@@ -128,6 +128,20 @@ public:
}
};
+/// 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 {
@@ -145,6 +159,7 @@ protected: // Can only create subclasses.
bool SkipSpace;
bool AllowAtInIdentifier;
bool IsAtStartOfStatement;
+ AsmCommentConsumer *CommentConsumer;
MCAsmLexer();
@@ -234,6 +249,10 @@ public:
bool getAllowAtInIdentifier() { return AllowAtInIdentifier; }
void setAllowAtInIdentifier(bool v) { AllowAtInIdentifier = v; }
+
+ void setCommentConsumer(AsmCommentConsumer *CommentConsumer) {
+ this->CommentConsumer = CommentConsumer;
+ }
};
} // End llvm namespace
diff --git a/llvm/lib/MC/MCParser/AsmLexer.cpp b/llvm/lib/MC/MCParser/AsmLexer.cpp
index 0fa7fbdc7b6..87ecf9e0227 100644
--- a/llvm/lib/MC/MCParser/AsmLexer.cpp
+++ b/llvm/lib/MC/MCParser/AsmLexer.cpp
@@ -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));
@@ -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)
diff --git a/llvm/lib/MC/MCParser/MCAsmLexer.cpp b/llvm/lib/MC/MCParser/MCAsmLexer.cpp
index 336e6413e38..63c0daba09a 100644
--- a/llvm/lib/MC/MCParser/MCAsmLexer.cpp
+++ b/llvm/lib/MC/MCParser/MCAsmLexer.cpp
@@ -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());
}
OpenPOWER on IntegriCloud