On Fri, 22 May 2020 at 19:06, Sterling B via cfe-users <
cfe-users@lists.llvm.org> wrote:

> 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].
>

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.


> Thanks!
> Sterling.
> _______________________________________________
> cfe-users mailing list
> cfe-users@lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users
>
_______________________________________________
cfe-users mailing list
cfe-users@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users

Reply via email to