[cfe-dev] clang matcher: get Left Hand Side of a VarDecl

chiasa.men via cfe-dev cfe-dev at lists.llvm.org
Thu Sep 2 23:51:45 PDT 2021


I want to rewrite the left hand side (LHS) of a VarDecl. Therefore I need the
SourceRange of the LHS.
Example
#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.get<clang::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"
ASTRewriter->getRangeSize(LHSRange)
// 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.get<clang::VarDecl>()->getInit();
RHS->getSourceRange()
// 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?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20210903/f81d2007/attachment-0001.html>


More information about the cfe-dev mailing list