[cfe-users] Location of function name?

Sterling B via cfe-users cfe-users at lists.llvm.org
Fri May 22 19:05:45 PDT 2020


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:

For source code like this:
int f(int x) { return x + 1; }

this is the current code

bool VisitFunctionDecl(clang::FunctionDecl *FD) {
    clang::ASTContext &context = FD->getASTContext();
    int begin = context.getFullLoc(FD->getBeginLoc()).getFileOffset();
    int end = context.getFullLoc(FD->getEndLoc()).getFileOffset();
    std::string path =
context.getFullLoc(FD->getBeginLoc()).getFileEntry()->getName().str();
.   ..
}

will give offsets [0,29], but I'd like to get the location of the "f", i.e.
[5-6].

Thanks!
Sterling.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20200522/1090855b/attachment.html>


More information about the cfe-users mailing list