How to get source text after preprocessing

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?

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