how to tell if comments or PP directive appears in SourceRange?

[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?

I’m curious what the actual problem is. In our experience changing the tokens in the code (as opposed to full ranges of code) is usually sufficient, and will leave all the comments in place. I’m aware that has limits, so I’d like to understand what check you’re working on that requires this.