<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div>On Apr 13, 2011, at 10:35 PM, aniruddh rao wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">Hi,<div><br></div><div>I am doing source analysis using clang and I need to identify certain function calls. Some of these calls are instantiated through macros.</div><div><br></div><div>Eg:</div><div><br></div><div><1> #define MALLOC(x) malloc((x))</div>

<div><2></div><div><3>int main() {</div><div><4>     char *p;</div><div><5></div><div><6>     p = MALLOC(sizeof(char) *</div><div><7>                                  MAX_SIZE);</div><div>

<8> }</div><div><br></div><div>In the above dummy example, I want to get the end location of malloc invocation. When MALLOC is expanded by the preprocessor, the two line invocation becomes single line and getEndLoc() on the CallExpr object gives line number 6. But I want to get the actual end location of the macro invocation in source file. In the example, it is line 7. Can anyone point me in the right direction to get this from AST?</div></blockquote><div><br></div><div><br></div><div>Let's say 'loc' is the SourceLocation that you know is in a macro (isMacroID() == true) and 'SM' is SourceManager, do this:</div><div><br></div><div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; ">  <span style="color: #056142">FileID</span> FID = SM.getFileID(loc);</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; ">  <span style="color: #911d67">const</span> SrcMgr::<span style="color: #056142">SLocEntry</span> *Entry = &SM.getSLocEntry(FID);</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; min-height: 15px; "><br></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; ">  <span style="color: #911d67">while</span> (Entry->getInstantiation().getInstantiationLocStart().isMacroID()) {</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; ">    loc = Entry->getInstantiation().getInstantiationLocStart();</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; ">    FID = SM.getFileID(loc);</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; ">    Entry = &SM.getSLocEntry(FID);</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; ">  }</div><div><br></div></div><div>Now </div><div><br></div><div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; ">Entry->getInstantiation().getInstantiationLocEnd()</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><br></div></div><div>Will be the SourceLocation of ending ')' of the macro instantiation.</div><br><blockquote type="cite">

<div><br></div><div>Thanks,</div><div>Aniruddh</div><div><br></div>
_______________________________________________<br>cfe-dev mailing list<br><a href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a><br>http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev<br></blockquote></div><br></body></html>