Getting precise SourceLocation for VarDecl

Hi All,

I am working on a rewriter that needs to replace certain variable declarations. Let me create a simplified example here. Consider the following DeclStmt:

size_t v, *u = NULL, w = 10;

In my RecursiveASTVisitor I implement VisitVarDecl, and use the rewriter.replace(SourceRange, …) API. I create the SourceRange using VarDecl::getLocStart and getLocEnd. But this leads to the whole DeclStmt being replaced even if only want to replace u or w. I tried using the getInnerLocStart, but it leads to the same issue. How should I get the correct start and end locations for each individual VarDecl?


Actually I found out that getLoc works as desired in this case.

For such things you might also want to take a look at clang-rename tool in clang/tools/extra.