clang matcher: get Left Hand Side of a VarDecl

I want to rewrite the left hand side (LHS) of a VarDecl. Therefore I need the SourceRange of the LHS.


#define M “macro”

int main(int argc, char** argv) {

char i=“5Test”;

char ii=M"Test";

char iMi=M"Test";

return 0;


In my AST matcher

SourceRange LHSRange = node.getclang::VarDecl()->getSourceRange();

// returns <file:3:1, col:10>, <file:4:1, col:12>, <file:5:1, col:13>

// which is: char i=,char ii=M,char iMi=M

Lexer::getSourceText(Lexer::getAsCharRange(LHSRange,SM,LO),SM, LO);

// returns char i=“5Test”,char ii=M"Test",char iMi=M"Test"


// returns 16,17,18

So the idea was: The definition is the right hand side (RHS) and hence its beginning the end of the LHS.

const Expr * RHS = node.getclang::VarDecl()->getInit();


// returns <file:3:10>,<file:4:11 <Spelling=…>, line:4:12>,<file:5:12 <Spelling=…>, line:5:13>

Lexer::getSourceText(Lexer::getAsCharRange(RHS->getSourceRange(),SM,LO),SM, LO);

// returns “5Test”, M"Test", M"Test"

So, how would I reliably get the LHS SourceRange?