[cfe-dev] How to get source text after preprocessing

Manuel Klimek klimek at google.com
Mon Feb 17 05:34:31 PST 2014


On Fri, Feb 14, 2014 at 8:57 PM, Vladimir <VladimirPlyashkun at yandex.ru>wrote:

> Hello, everyone. I have some trouble. Earlier i'm using Lexer module to
> getting source text. This sample code works fine, if my source file doesnt
> have any #include directive:
> clang::SourceLocation b(d->getLocStart()), _e(d->getLocEnd());
> clang::SourceLocation e(clang::Lexer::getLocForEndOfToken(_e, 0, *sm,
> lopt));
> return std::string(sm->getCharacterData(b),
>          sm->getCharacterData(e)-sm->getCharacterData(b));
> But when i'm trying to get some location on file(which have some includes)
> this code always returns trash. For example, attempt to get source filename
> 'sourceManager->getFilename(location)' returns null string.
> Moreover, above code also returns incorrect values. I know that this fact
> caused by preprocessor macroses and different files, but i dont know
> correct
> way to get source text of AST (which represent preprocessed file). Do you
> have any ideas?
>

There is no "source text of the AST". What will work in basically all cases
where it's possible is to use Lexer::getSourceText. Note that you still
need to be careful to not pass in a bogus range, but if you get it as
start/end from a single AST node, it's usually fine. Also note that not all
cases are resolvable to a single StringRef (but we found it's usually not a
problem in practice).

Cheers,
/Manuel


>
>
>
> --
> View this message in context:
> http://clang-developers.42468.n3.nabble.com/How-to-get-source-text-after-preprocessing-tp4037854.html
> Sent from the Clang Developers mailing list archive at Nabble.com.
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140217/9bbff13b/attachment.html>


More information about the cfe-dev mailing list