Obtaining AST Nodes Text

Good day!
How can I retreive matched node text to some variable, not dump?
I have
MatchResult &R;
ASTContext *C;
SourceManager &SM;

I don’t want to get location and re-read source file. Is there other way?

Hi,

Hi,

Have you tried using:
clang::tooling::fixit::getText(const T &Node, const ASTContext &);
Internally that uses
clang::Lexer::getSourceText(CharSourceRange, const SourceManager &, const LangOptions &, bool *Invalid);

Hope this helps,
Nathan

Thanks a lot, this works for me:


#include “clang/Tooling/FixIt.h”

//in AST MatchCallback:
const CompoundStmt *FS = Result.Nodes.getNodeAsclang::CompoundStmt(“node_cmpnd”);
ASTContext *Context = Result.Context;
StringRef text = clang::tooling::fixit::getText(*FS, *Context);
llvm::outs() << text;