How to find the source file, the called function is defined inside

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

Hi,

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

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.