<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body><p style="margin-top:12;margin-bottom:12;margin-left:0;margin-right:0;"><span style="font-size:0.75em;">I want to rewrite the left hand side (LHS) of a VarDecl. Therefore I need the SourceRange of the LHS.</span></p>
<p style="margin-top:12;margin-bottom:12;margin-left:0;margin-right:0;">Example</p>
<p style="margin-top:12;margin-bottom:0;margin-left:0;margin-right:0;"><span style="font-family:Hack;">#define M "macro"</span></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">int main(int argc, char** argv) {</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">char i[]="5Test";</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">char ii[]=M"Test";</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">char iMi[]=M"Test";</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">return 0;</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">}</p>
<br /><p style="margin-top:12;margin-bottom:12;margin-left:0;margin-right:0;">In my AST matcher</p>
<p style="margin-top:12;margin-bottom:0;margin-left:0;margin-right:0;">SourceRange LHSRange = node.get<clang::VarDecl>()->getSourceRange();</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">// returns <file:3:1, col:10>, <file:4:1, col:12>, <file:5:1, col:13></p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">// which is: char i[]=,char ii[]=M,char iMi[]=M</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">Lexer::getSourceText(Lexer::getAsCharRange(LHSRange,SM,LO),SM, LO);</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">// returns char i[]="5Test",char ii[]=M"Test",char iMi[]=M"Test"</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">ASTRewriter->getRangeSize(LHSRange)</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">// returns 16,17,18</p>
<br /><p style="margin-top:12;margin-bottom:12;margin-left:0;margin-right:0;">So the idea was: The definition is the right hand side (RHS) and hence its beginning the end of the LHS.</p>
<p style="margin-top:12;margin-bottom:0;margin-left:0;margin-right:0;">const Expr * RHS = node.get<clang::VarDecl>()->getInit();</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">RHS->getSourceRange()</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">// returns <file:3:10>,<file:4:11 <Spelling=..>, line:4:12>,<file:5:12  <Spelling=..>, line:5:13></p>
<br /><p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">Lexer::getSourceText(Lexer::getAsCharRange(RHS->getSourceRange(),SM,LO),SM, LO);</p>
<p style="margin-top:0;margin-bottom:0;margin-left:0;margin-right:0;">// returns "5Test", M"Test", M"Test"</p>
<br /><p style="margin-top:12;margin-bottom:12;margin-left:0;margin-right:0;">So, how would I reliably get the LHS SourceRange? </p>
</body>
</html>