void HandlePragmaSystemHeader(Token &SysHeaderTok);
void HandlePragmaDependency(Token &DependencyTok);
void HandlePragmaComment(Token &CommentTok);
- void HandleComment(SourceRange Comment);
+ // Return true and store the first token only if any CommentHandler
+ // has inserted some tokens and getCommentRetentionState() is false.
+ bool HandleComment(Token &Token, SourceRange Comment);
};
/// \brief Abstract base class that describes a handler that will receive
public:
virtual ~CommentHandler();
- virtual void HandleComment(Preprocessor &PP, SourceRange Comment) = 0;
+ // The handler shall return true if it has pushed any tokens
+ // to be read using e.g. EnterToken or EnterTokenStream.
+ virtual bool HandleComment(Preprocessor &PP, SourceRange Comment) = 0;
};
} // end namespace clang
// SkipBCPLComment - We have just read the // characters from input. Skip until
// we find the newline character thats terminate the comment. Then update
-/// BufferPtr and return. If we're in KeepCommentMode, this will form the token
-/// and return true.
+/// BufferPtr and return.
+///
+/// If we're in KeepCommentMode or any CommentHandler has inserted
+/// some tokens, this will store the first token and return true.
bool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) {
// If BCPL comments aren't explicitly enabled for this language, emit an
// extension warning.
} while (C != '\n' && C != '\r');
// Found but did not consume the newline.
- if (PP)
- PP->HandleComment(SourceRange(getSourceLocation(BufferPtr),
- getSourceLocation(CurPtr)));
+ if (PP && PP->HandleComment(Result,
+ SourceRange(getSourceLocation(BufferPtr),
+ getSourceLocation(CurPtr)))) {
+ BufferPtr = CurPtr;
+ return true; // A token has to be returned.
+ }
// If we are returning comments as tokens, return this comment as a token.
if (inKeepCommentMode())
/// happen is the comment could end with an escaped newline between the */ end
/// of comment.
///
-/// If KeepCommentMode is enabled, this forms a token from the comment and
-/// returns true.
+/// If we're in KeepCommentMode or any CommentHandler has inserted
+/// some tokens, this will store the first token and return true.
bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
// Scan one character past where we should, looking for a '/' character. Once
// we find it, check to see if it was preceeded by a *. This common
C = *CurPtr++;
}
- if (PP)
- PP->HandleComment(SourceRange(getSourceLocation(BufferPtr),
- getSourceLocation(CurPtr)));
+ if (PP && PP->HandleComment(Result,
+ SourceRange(getSourceLocation(BufferPtr),
+ getSourceLocation(CurPtr)))) {
+ BufferPtr = CurPtr;
+ return true; // A token has to be returned.
+ }
// If we are returning comments as tokens, return this comment as a token.
if (inKeepCommentMode()) {
// too (without going through the big switch stmt).
if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() &&
Features.BCPLComment) {
- SkipBCPLComment(Result, CurPtr+2);
+ if (SkipBCPLComment(Result, CurPtr+2))
+ return; // There is a token to return.
goto SkipIgnoredUnits;
} else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) {
- SkipBlockComment(Result, CurPtr+2);
+ if (SkipBlockComment(Result, CurPtr+2))
+ return; // There is a token to return.
goto SkipIgnoredUnits;
} else if (isHorizontalWhitespace(*CurPtr)) {
goto SkipHorizontalWhitespace;
if (Features.BCPLComment ||
getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') {
if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
- return; // KeepCommentMode
+ return; // There is a token to return.
// It is common for the tokens immediately after a // comment to be
// whitespace (indentation for the next line). Instead of going through
if (Char == '*') { // /**/ comment.
if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
- return; // KeepCommentMode
+ return; // There is a token to return.
goto LexNextToken; // GCC isn't tail call eliminating.
}