[Please reply *only* to the list and do not include my email directly
in the To: or Cc: of your reply; otherwise I will not see your reply.
Thanks.]
I am trying to determine if a replacement range contains comments or
preprocessor directives to avoid removing them when generating a
fixit in clang-tidy. I tried the following code:
bool containsDiscardedTokens(
const ast_matchers::MatchFinder::MatchResult &Result, SourceRange Range) {
CharSourceRange CharRange = Lexer::makeFileCharRange(
CharSourceRange::getTokenRange(Range), *Result.SourceManager,
Result.Context->getLangOpts());
std::string ReplacementText =
Lexer::getSourceText(CharRange, *Result.SourceManager,
Result.Context->getLangOpts()).str();
Lexer Lex(CharRange.getBegin(), Result.Context->getLangOpts(),
ReplacementText.data(), ReplacementText.data(),
ReplacementText.data() + ReplacementText.size());
Token Tok;
while (!Lex.LexFromRawLexer(Tok)) {
if (Tok.is(tok::TokenKind::comment) || Tok.is(tok::TokenKind::eod)) {
return true;
}
}
return false;
}
However, when invoked over a source range that contains comments or
preprocessor directives, the lexer isn't returning to me tokens
representing the comments or the preprocessor directives.
Is it possible for the lexer to tell me this, or do I need to hook the
preprocessor?