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

Manuel Klimek via cfe-dev cfe-dev at lists.llvm.org
Mon Dec 21 23:49:59 PST 2015


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.

On Mon, Dec 21, 2015, 8:20 PM Richard via cfe-dev <cfe-dev at lists.llvm.org>
wrote:

> [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?
> --
> "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>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20151222/83e22003/attachment.html>


More information about the cfe-dev mailing list