I have just experimented with this a bit. I would consider this a bug, but I am not yet sure what the right fix is. I'll look into it some more..<div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, Jul 14, 2012 at 1:16 AM, Sergejs Belajevs <span dir="ltr"><<a href="mailto:sergejs.belajevs@gmail.com" target="_blank">sergejs.belajevs@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im HOEnZb">Latest version from trunk did the trick, thanks. However, even with<br>
this newer version I ran into another problem:<br>
<br>
test.cpp:<br>
void foo()<br>
{<br>
  int* buf = new int[10];<br>
  buf = new int[10];<br>
}<br>
<br>
AST dump from clang:<br>
<br>
void foo() (CompoundStmt 0x4302b8 <test.cpp:2:1, line:5:1><br>
  (DeclStmt 0x430208 <line:3:3, col:25><br>
    0x42fd00 "int *buf =<br>
      (CXXNewExpr 0x4301c8 <col:14, col:18> 'int *'<br>
        (IntegerLiteral 0x42fd30 <col:22> 'int' 10))")<br>
  (BinaryOperator 0x4302a0 <line:4:3, col:13> 'int *' lvalue '='<br>
    (DeclRefExpr 0x430218 <col:3> 'int *' lvalue Var 0x42fd00 'buf' 'int *')<br>
    (CXXNewExpr 0x430260 <col:9, col:13> 'int *'<br>
      (IntegerLiteral 0x430230 <col:17> 'int' 10))))<br>
<br>
DeclStmt has correct end location, but  binary operator's end location<br>
(and CXXNewExpr as well) points to 'int' token, not to last closing<br>
bracket or semicolon. I suppose it shouldn't be that way.<br>
<br>
<br>
Thanks,<br>
Sergejs<br>
<br>
</div><div class="HOEnZb"><div class="h5">On Fri, Jul 13, 2012 at 12:04 AM, Daniel Jasper <<a href="mailto:djasper@google.com">djasper@google.com</a>> wrote:<br>
> I cannot really reproduces that. With clang 3.2 on a linux machine, I have:<br>
><br>
> declstmt.cc:<br>
> void foo()<br>
> {<br>
>   union { int a; };<br>
> }<br>
><br>
> clang -cc1 -ast-dump declstmt.cc:<br>
> typedef __int128 __int128_t;<br>
> typedef unsigned __int128 __uint128_t;<br>
> typedef __va_list_tag __builtin_va_list[1];<br>
> void foo() (CompoundStmt 0x51ec658 <declstmt.cc:2:1, line:4:1><br>
>   (DeclStmt 0x51ec640 <line:3:3, col:19><br>
>     0x51ec280 "<anonymous union at declstmt.cc:3:3> =<br>
>       (CXXConstructExpr 0x51ec5b8 <col:3> 'union <anonymous at<br>
> declstmt.cc:3:3>''void (void) throw()')"))<br>
><br>
> The DeclStmt appears to have the right source range <line:3:3, col:19>.<br>
> However, you do not seem to print  that range in your dump. Can you check<br>
> that it is not in there? And maybe retry with clang 3.2 based libs?<br>
><br>
><br>
> On Thu, Jul 12, 2012 at 7:57 PM, Sergejs Belajevs<br>
> <<a href="mailto:sergejs.belajevs@gmail.com">sergejs.belajevs@gmail.com</a>> wrote:<br>
>><br>
>> Hi,<br>
>><br>
>> a quick example for Visual Studio 2010: <a href="https://gist.github.com/3099625" target="_blank">https://gist.github.com/3099625</a><br>
>><br>
>> file's a.cpp contents:<br>
>><br>
>> void foo()<br>
>> {<br>
>>    union { int a; };<br>
>> }<br>
>><br>
>> When compiled with libs from clang 3.1, I have the following output:<br>
>><br>
>> top-level-decl: __builtin_va_list<br>
>> top-level-decl: foo<br>
>> (CompoundStmt 0x5ff8f8<br>
>>   (DeclStmt 0x5ff8e8<br>
>>     0x5ff690 "<anonymous union at a.cpp:3:4> =<br>
>>       (CXXConstructExpr 0x5ff888 'union <anonymous at a.cpp:3:4>''void<br>
>> (void) throw()')"))<br>
>> a.cpp:2:1 <=> a.cpp:4:1<br>
>> (DeclStmt 0x5ff8e8<br>
>>   0x5ff690 "<anonymous union at a.cpp:3:4> =<br>
>>     (CXXConstructExpr 0x5ff888 'union <anonymous at a.cpp:3:4>''void<br>
>> (void) throw()')")<br>
>> a.cpp:3:4 <=> <invalid loc><br>
>><br>
>> As you can see, location for last DeclStmt is "a.cpp:3:4 <=> <invalid<br>
>> loc>", that is getLocEnd() returned not what I was expecting.<br>
>><br>
>><br>
>> Sergejs<br>
>><br>
>> On Thu, Jul 12, 2012 at 2:37 AM, Daniel Jasper <<a href="mailto:djasper@google.com">djasper@google.com</a>> wrote:<br>
>> > Could you post a bit more context (a minimal example) of what you are<br>
>> > precisely looking at? I could not reproduce the error for the cases 1-4.<br>
>> > If<br>
>> > I put them outside of a method, they don't lead to a DeclStmt, but a<br>
>> > CXXRecordDec. If I put them into a method, e.g.:<br>
>> ><br>
>> > void f() {<br>
>> >   union { int a; };<br>
>> > }<br>
>> ><br>
>> > I get the DeclStmt but it seems to have the right code range (column 2<br>
>> > to<br>
>> > 19).<br>
>> ><br>
>> > In general, the code location handling is quite inconsistent and we are<br>
>> > currently looking into how to improve this.<br>
>> ><br>
>> ><br>
>> > On Thu, Jul 12, 2012 at 6:45 AM, Sergejs Belajevs<br>
>> > <<a href="mailto:sergejs.belajevs@gmail.com">sergejs.belajevs@gmail.com</a>> wrote:<br>
>> >><br>
>> >> Hi,<br>
>> >><br>
>> >> I am working on source-to-source transformation tool and want to get<br>
>> >> the original source code for a statement token by token. I am using<br>
>> >> statement's getLocStart/getLocEnd, SourceLocation's getLocWithOffset,<br>
>> >> SourceManager's getCharacterData and Lexer::MeasureTokenLength. My<br>
>> >> code worked fine until I ran into some DeclStmts:<br>
>> >><br>
>> >> 1) struct A { int a; } s;<br>
>> >> 2) struct A { int a; };<br>
>> >> 3) union A { int a; };<br>
>> >> 4) union { int a; };<br>
>> >><br>
>> >> For 1) getLocEnd() works fine.<br>
>> >> For 2) my code doesn't work because getLocEnd() is smaller than<br>
>> >> getLocStart(). End's getRawEncoding() returns 0. I found a workaround<br>
>> >> for this case by calling getLocEnd() of DeclStmt's getSingleDecl().<br>
>> >> Case 3) has the same problem as 2), same workaround works fine.<br>
>> >> Case 4) has the same problem as 2), but this time after applying my<br>
>> >> workaround the resulting SourceLocation is the same as getLocStart(),<br>
>> >> that is points to token "union".<br>
>> >><br>
>> >> So I guess the questions are:<br>
>> >> * Is this expected behavior? If yes, then what exactly getLocEnd()<br>
>> >> returns?<br>
>> >> * How could I get the end location for case 4)?<br>
>> >> * Is there a better way to get statement as token strings?<br>
>> >> * As an alternative to previous question, can I somehow find the total<br>
>> >> character length of statement in the original source code?<br>
>> >><br>
>> >><br>
>> >> Thanks,<br>
>> >> Sergejs<br>
>> >> _______________________________________________<br>
>> >> cfe-dev mailing list<br>
>> >> <a href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a><br>
>> >> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br>
>> ><br>
>> ><br>
><br>
><br>
</div></div></blockquote></div><br></div>