[cfe-dev] Wrong SourceRange for IfStmt ?

Roman Popov via cfe-dev cfe-dev at lists.llvm.org
Sat Oct 21 21:28:34 PDT 2017

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)

Result is:

    $if (a)

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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20171021/14300d02/attachment.html>

More information about the cfe-dev mailing list