Wrong SourceRange for IfStmt ?

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

Wrong SourceRange for IfStmt ?

Eric Fiselier via cfe-dev
Hello, 
I want to emit some text after each if statement in source code. When testing a tool I've found that ifStmt->getSourceRange() sometimes returns wrong end location. 

In RecursiveASTVisitor I have following code to insert "$" before and after each if:

    bool VisitIfStmt(clang::IfStmt* ifStmt)
    {
        clang::tooling::Replacement rep0(compInst.getSourceManager(), ifStmt->getLocStart(), 0, "$");
        handleAllErrors ( replacements.add(rep0) );
        clang::tooling::Replacement rep1(compInst.getSourceManager(), ifStmt->getLocEnd().getLocWithOffset(1), 0, "$");
        handleAllErrors ( replacements.add(rep1) );
        return true;
    }

    Replacements &replacements;
    ASTContext *Context;
    clang::CompilerInstance &compInst;

When testing on following code:

    int a;
    if (a)
       a--;

Result is:

    $if (a)
       a-$-;


So Clang thinks that if statement ends inside decrement operator. Is it a bug, or I misuse API?

Thanks,
-Roman

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

Re: Wrong SourceRange for IfStmt ?

Eric Fiselier via cfe-dev
You're assuming that the length of the end token is 1, but you should use its actual length instead.

So, instead of getting the end location of the statement, you should get the end token, then use its location and length, e.g., tok.getLocation().getLocWithOffset(tok.getLength()).

hth...
don

On Sat, Oct 21, 2017 at 9:28 PM, Roman Popov via cfe-dev <[hidden email]> wrote:
Hello, 
I want to emit some text after each if statement in source code. When testing a tool I've found that ifStmt->getSourceRange() sometimes returns wrong end location. 

In RecursiveASTVisitor I have following code to insert "$" before and after each if:

    bool VisitIfStmt(clang::IfStmt* ifStmt)
    {
        clang::tooling::Replacement rep0(compInst.getSourceManager(), ifStmt->getLocStart(), 0, "$");
        handleAllErrors ( replacements.add(rep0) );
        clang::tooling::Replacement rep1(compInst.getSourceManager(), ifStmt->getLocEnd().getLocWithOffset(1), 0, "$");
        handleAllErrors ( replacements.add(rep1) );
        return true;
    }

    Replacements &replacements;
    ASTContext *Context;
    clang::CompilerInstance &compInst;

When testing on following code:

    int a;
    if (a)
       a--;

Result is:

    $if (a)
       a-$-;


So Clang thinks that if statement ends inside decrement operator. Is it a bug, or I misuse API?

Thanks,
-Roman

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



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

Re: Wrong SourceRange for IfStmt ?

Eric Fiselier via cfe-dev
Thanks you!
So I've misunderstood the meaning of API. 


On Oct 22, 2017 7:41 AM, "Don Hinton" <[hidden email]> wrote:
You're assuming that the length of the end token is 1, but you should use its actual length instead.

So, instead of getting the end location of the statement, you should get the end token, then use its location and length, e.g., tok.getLocation().getLocWithOffset(tok.getLength()).

hth...
don

On Sat, Oct 21, 2017 at 9:28 PM, Roman Popov via cfe-dev <[hidden email]> wrote:
Hello, 
I want to emit some text after each if statement in source code. When testing a tool I've found that ifStmt->getSourceRange() sometimes returns wrong end location. 

In RecursiveASTVisitor I have following code to insert "$" before and after each if:

    bool VisitIfStmt(clang::IfStmt* ifStmt)
    {
        clang::tooling::Replacement rep0(compInst.getSourceManager(), ifStmt->getLocStart(), 0, "$");
        handleAllErrors ( replacements.add(rep0) );
        clang::tooling::Replacement rep1(compInst.getSourceManager(), ifStmt->getLocEnd().getLocWithOffset(1), 0, "$");
        handleAllErrors ( replacements.add(rep1) );
        return true;
    }

    Replacements &replacements;
    ASTContext *Context;
    clang::CompilerInstance &compInst;

When testing on following code:

    int a;
    if (a)
       a--;

Result is:

    $if (a)
       a-$-;


So Clang thinks that if statement ends inside decrement operator. Is it a bug, or I misuse API?

Thanks,
-Roman

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




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

Re: Wrong SourceRange for IfStmt ?

Eric Fiselier via cfe-dev
Looks like semicolon is not considered a token, so it does not have any representation in AST. 

So for example to find location after semicolon in this IfStmt:
    if (a)
       a--;

I have to search for semicolon myself:


    const char* endCharPtr = sourceManager.getCharacterData(locEnd);
    size_t offsetSemicolon = 0;

    while (endCharPtr[offsetSemicolon-1]) != ';' )
        offsetSemicolon ++;


if there is a CompoundStmt in Then body, like
    if (a)
       { a--; }

Location of final curly brace will be detected correctly. 




2017-10-22 10:09 GMT-07:00 Roman Popov <[hidden email]>:
Thanks you!
So I've misunderstood the meaning of API. 


On Oct 22, 2017 7:41 AM, "Don Hinton" <[hidden email]> wrote:
You're assuming that the length of the end token is 1, but you should use its actual length instead.

So, instead of getting the end location of the statement, you should get the end token, then use its location and length, e.g., tok.getLocation().getLocWithOffset(tok.getLength()).

hth...
don

On Sat, Oct 21, 2017 at 9:28 PM, Roman Popov via cfe-dev <[hidden email]> wrote:
Hello, 
I want to emit some text after each if statement in source code. When testing a tool I've found that ifStmt->getSourceRange() sometimes returns wrong end location. 

In RecursiveASTVisitor I have following code to insert "$" before and after each if:

    bool VisitIfStmt(clang::IfStmt* ifStmt)
    {
        clang::tooling::Replacement rep0(compInst.getSourceManager(), ifStmt->getLocStart(), 0, "$");
        handleAllErrors ( replacements.add(rep0) );
        clang::tooling::Replacement rep1(compInst.getSourceManager(), ifStmt->getLocEnd().getLocWithOffset(1), 0, "$");
        handleAllErrors ( replacements.add(rep1) );
        return true;
    }

    Replacements &replacements;
    ASTContext *Context;
    clang::CompilerInstance &compInst;

When testing on following code:

    int a;
    if (a)
       a--;

Result is:

    $if (a)
       a-$-;


So Clang thinks that if statement ends inside decrement operator. Is it a bug, or I misuse API?

Thanks,
-Roman

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





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