<div dir="ltr"><div dir="ltr">On Fri, 22 May 2020 at 19:06, Sterling B via cfe-users <<a href="mailto:cfe-users@lists.llvm.org">cfe-users@lists.llvm.org</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr">Hi Clang Experts, could you kindly advice how to get location of a function *name* when visiting FunctionDecl in RecursiveASTVisitor (getBeginLoc and getEndLoc return the whole range for the definition, not just the name). Example:</div><div dir="ltr"><br></div><div dir="ltr">For source code like this:<div>int f(int x) { return x + 1; }<br></div><div><br></div><div>this is the current code</div><div><br></div><div>bool VisitFunctionDecl(clang::FunctionDecl *FD) {<br></div><div><div>    clang::ASTContext &context = FD->getASTContext();<br></div><div>    int begin = context.getFullLoc(FD->getBeginLoc()).getFileOffset();</div><div>    int end = context.getFullLoc(FD->getEndLoc()).getFileOffset();</div><div>    std::string path = context.getFullLoc(FD->getBeginLoc()).getFileEntry()->getName().str();</div><div>.   ..</div></div><div>}</div><div><br></div><div>will give offsets [0,29], but I'd like to get the location of the "f", i.e. [5-6].</div></div></div></div></blockquote><div><br></div><div>In general, you can use getLocation() to get the location of the name in a NamedDecl. For a FunctionDecl, there's also getNameInfo(), which will return more detailed information about the name of the function (allowing you to handle the case where the function name is more than a single token, such as for C++ functions with non-identifier names like "operator int*"). If you want to also include the preceding C++ nested name specifier as part of the name, you can use getQualifierLoc to determine its location.</div><div> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div>Thanks!</div><div>Sterling.</div></div></div></div>
_______________________________________________<br>
cfe-users mailing list<br>
<a href="mailto:cfe-users@lists.llvm.org" target="_blank">cfe-users@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users</a><br>
</blockquote></div></div>