Obtaining AST Nodes Text

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Obtaining AST Nodes Text

Vassil Vassilev via cfe-dev
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?
 

_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Obtaining AST Nodes Text

Vassil Vassilev via cfe-dev
Hi,

On Tue, Jun 09, 2020 at 09:31:22AM +0300, samins KAlex via cfe-dev <[hidden email]> wrote:
> 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?

Did you try CharSourceRange::getTokenRange()? That's what clang-tidy
uses at a number of places to refer to source in diagnostics.

Regards,

Miklos

_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

signature.asc (201 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Obtaining AST Nodes Text

Vassil Vassilev via cfe-dev
In reply to this post by Vassil Vassilev via cfe-dev
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

On Tue, 2020-06-09 at 09:31 +0300, samins KAlex via cfe-dev wrote:
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?
 
_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev


_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Obtaining AST Nodes Text

Vassil Vassilev via cfe-dev
Thanks a lot, this works for me:
 
...
#include "clang/Tooling/FixIt.h"
...
//in AST MatchCallback:
const CompoundStmt *FS = Result.Nodes.getNodeAs<clang::CompoundStmt>("node_cmpnd");
ASTContext *Context = Result.Context;
StringRef text = clang::tooling::fixit::getText<CompoundStmt>(*FS, *Context);
llvm::outs() << text;


10.06.2020, 14:07, "Nathan James via cfe-dev" <[hidden email]>:
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

On Tue, 2020-06-09 at 09:31 +0300, samins KAlex via cfe-dev wrote:
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?
 
_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

,

_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev


_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev