[cfe-dev] how to tell if comments or PP directive appears in SourceRange?

Richard via cfe-dev cfe-dev at lists.llvm.org
Mon Dec 21 11:20:31 PST 2015

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

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,

  std::string ReplacementText =
      Lexer::getSourceText(CharRange, *Result.SourceManager,
  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
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
     The Computer Graphics Museum <http://ComputerGraphicsMuseum.org>
         The Terminals Wiki <http://terminals.classiccmp.org>
  Legalize Adulthood! (my blog) <http://LegalizeAdulthood.wordpress.com>

More information about the cfe-dev mailing list