[cfe-dev] Problem with retrieving the BinaryOperator RHS end location
Nat! via cfe-dev
cfe-dev at lists.llvm.org
Fri May 3 06:28:01 PDT 2019
It turns out the fix suggestion - at least as I implemented it - works
for simple cases, but not in general.
```
endBuf = SM->getCharacterData( Stmt->getRHS()->getEndLoc());
endBuf += Lexer::MeasureTokenLength( Stmt->getEndLoc(), *SM,
TheRewriter.getLangOpts());
```
I believe the problem is that the SourceManager I am using
to "measure" the length is looking at the unmodified sourcecode. But
the RewriteBuffer may already contain changes.
So in my example if I am looking at `x = yyy` at the source but
in the rewrite buffer its now `x=yyy_renamed` it will not take the
extra characters into account.
I should be measuring the contents of the RewriteBuffer instead,
with likely another SourceManager, but I lack the expertise to set
this up.
Ciao
Nat!
Nat! via cfe-dev schrieb:
> I am just trying to print the whole BinaryOperator statement text
> verbatim, but I can not get the end of the statement location.
>
>
> ```
>
> bool VisitBinaryOperator(BinaryOperator *Stmt) {
> const char *startBuf;
> const char *endBuf;
> SourceManager *SM;
>
> SM = &TheRewriter.getSourceMgr(); // based on Rewriter
> startBuf = SM->getCharacterData( Stmt->getLHS()->getBeginLoc());
> endBuf = SM->getCharacterData( Stmt->getRHS()->getEndLoc());
> fprintf( stderr, "write: \"%.*s\"\n", (int) (endBuf -
> startBuf), startBuf);
> return( true);
> }
>
> ```
>
>
> For an input of:
>
> ```
>
> void x( void)
> {
> void (*y)(void);
>
> y = x;
> }
>
> ```
>
>
> I get
>
> ```
>
> write: "y = "
>
> ```
>
>
> This is the dump of the AST:
>
> ```
>
> FunctionDecl 0x19d3bd8 </home/src/srcL/llvm-clang-samples/y.m:1:1,
> line:6:1> line:1:8 referenced x 'void (void)'
> `-CompoundStmt 0x19d3ec8 <line:2:1, line:6:1>
> |-DeclStmt 0x19d3e38 <line:3:4, col:20>
> | `-VarDecl 0x19d3dd8 <col:4, col:19> col:12 used y 'void (*)(void)'
> `-BinaryOperator 0x19d3ea8 <line:5:4, col:8> 'void (*)(void)' '='
> |-DeclRefExpr 0x19d3e50 <col:4> 'void (*)(void)' lvalue Var
> 0x19d3dd8 'y' 'void (*)(void)'
> `-ImplicitCastExpr 0x19d3e90 <col:8> 'void (*)(void)'
> <FunctionToPointerDecay>
> `-DeclRefExpr 0x19d3e70 <col:8> 'void (void)' Function 0x19d3bd8
> 'x' 'void (void)'
> ```
>
> This is my first foray into clang rewriting...
>
>
> Ciao
>
> Nat!
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
More information about the cfe-dev
mailing list