[cfe-dev] How to find the source file, the called function is defined inside

Georgiou, Andreas via cfe-dev cfe-dev at lists.llvm.org
Sun Jul 24 17:22:38 PDT 2016

Thank you for your answer.

I have found the solution in the Clang Devs archive. This is what I did

if (func_decl->hasBody()) //in order to get the definitions body and then locate it in the corresponding file
const FileEntry *file_entry = astContext->getSourceManager().getFileEntryForID(astContext->getSourceManager().getFileID(func_decl->getLocStart()));
string sourcepath = file_entry->getName();
const int idx = sourcepath.find_last_of("\\/");
if (std::string::npos != idx) //extract the filename from the entire path
  filename = sourcepath.substr(idx + 1);

My answer is just for the archive.

From: Himanshu <himanshu at utexas.edu>
Sent: 22 July 2016 17:37:54
To: Georgiou, Andreas
Cc: cfe-dev at lists.llvm.org
Subject: Re: [cfe-dev] How to find the source file, the called function is defined inside


You could use the getFilename() API by SourceManager. Get the function's startLoc and then pass it to the getFileName( ).


On Thu, Jul 21, 2016 at 6:53 PM, Georgiou, Andreas via cfe-dev <cfe-dev at lists.llvm.org<mailto:cfe-dev at lists.llvm.org>> wrote:

Hi all,

I have an ASTvisitor and when it visits a CallExpr node, if the expression is a function call I would like to retrieve the filename of the source code where the called function is defined within. Is it possible?

Thank you

cfe-dev mailing list
cfe-dev at lists.llvm.org<mailto:cfe-dev at lists.llvm.org>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160725/b685f9bd/attachment.html>

More information about the cfe-dev mailing list