Obtaining file name for functions from included files

Hi,

I got the following problem:
I need to traverse the AST (currently using RecursiveASTVisitor) and get the
file name for every function declaration.
Currently, I only get the filename for function declarations in my main file but
how do I get these for functions in #included files?

My relevant code looks like this (srcMgr is the SourceManager):

bool CMyASTVisitor::VisitFunctionDecl(FunctionDecl* decl)
{
...

const FileEntry* file_entry =
srcMgr.getFileEntryForID(srcMgr.getFileID(decl->getLocStart()));

if (file_entry == NULL)
   std::cout << "no file entry" << std::endl;

...
}

Is there a way to get these filenames?

Regards,
Felix

Hi,

I got the following problem:
I need to traverse the AST (currently using RecursiveASTVisitor) and get the
file name for every function declaration.
Currently, I only get the filename for function declarations in my main file but
how do I get these for functions in #included files?

My relevant code looks like this (srcMgr is the SourceManager):

bool CMyASTVisitor::VisitFunctionDecl(FunctionDecl* decl)
{

const FileEntry* file_entry =
srcMgr.getFileEntryForID(srcMgr.getFileID(decl->getLocStart()));

if (file_entry == NULL)
std::cout << “no file entry” << std::endl;


}

Is there a way to get these filenames?

Why does file_entry->getName() not work?

Cheers,
/Manuel

Manuel Klimek <klimek@...> writes:

Hi,
I got the following problem:
I need to traverse the AST (currently using

RecursiveASTVisitor) and get the

file name for every function declaration.
Currently, I only get the filename for function

declarations in my main file but

how do I get these for functions in #included files?
My relevant code looks like this (srcMgr is the

SourceManager):

bool CMyASTVisitor::VisitFunctionDecl(FunctionDecl*

decl)

{
...
const FileEntry* file_entry =
srcMgr.getFileEntryForID(srcMgr.getFileID(decl-
getLocStart()));
if (file_entry == NULL)
std::cout << "no file entry" << std::endl;
...
}
Is there a way to get these filenames?

Why does file_entry->getName() not work?

Because file_entry is NULL if the declaration found is
not in
the main file but from an included file in the main file.

Felix

Cheers,
/Manuel

Regards,
Felix
_______________________________________________
cfe-dev mailing listcfe-dev-

Tmj1lob9twqVc3sceRu5cw@public.gmane.orghttp://
lists.cs.uiuc.edu/
mailman/listinfo/cfe-dev

Manuel Klimek <klimek@…> writes:

Hi,
I got the following problem:
I need to traverse the AST (currently using
RecursiveASTVisitor) and get the
file name for every function declaration.
Currently, I only get the filename for function
declarations in my main file but
how do I get these for functions in #included files?
My relevant code looks like this (srcMgr is the
SourceManager):
bool CMyASTVisitor::VisitFunctionDecl(FunctionDecl*
decl)
{

const FileEntry* file_entry =
srcMgr.getFileEntryForID(srcMgr.getFileID(decl-
getLocStart()));
if (file_entry == NULL)
std::cout << “no file entry” << std::endl;

}
Is there a way to get these filenames?

Why does file_entry->getName() not work?

Because file_entry is NULL if the declaration found is
not in
the main file but from an included file in the main file.

That’s news to me - the only place where I’ve found file_entry to be NULL is for token-pasted code, for example from macro expansions.

Cheers,
/Manuel

I'm using the following functions from the C bindings:

* clang_getCursorLocation
* clang_getSpellingLocation
* clang_getFileName

May you could have a look at the implementation of those functions if no one else has a quicker solution.

Manuel Klimek <klimek@...> writes:

Manuel Klimek <klimek <at> ...> writes:
>
> Hi,
> I got the following problem:
> I need to traverse the AST (currently using
RecursiveASTVisitor) and get the
> file name for every function declaration.
> Currently, I only get the filename for function
declarations in my main file but
> how do I get these for functions in #included files?
> My relevant code looks like this (srcMgr is the
SourceManager):
> bool CMyASTVisitor::VisitFunctionDecl(FunctionDecl*
decl)
> {
> ...
> const FileEntry* file_entry =
> srcMgr.getFileEntryForID(srcMgr.getFileID(decl-
>getLocStart()));
> if (file_entry == NULL)
> std::cout << "no file entry" << std::endl;
> ...
> }
> Is there a way to get these filenames?
>
>
> Why does file_entry->getName() not work?
>
Because file_entry is NULL if the declaration found is
not in
the main file but from an included file in the main file.

That's news to me - the only place where I've found

file_entry to be NULL is
for token-pasted code, for example from macro expansions.

You are right, but this happened to be the case for me
(I tested with /usr/include/math.h, which does a lot of
nasty stuff). The solution is using this code instead:

SourceLocation loc = srcMgr.getFileLoc(decl->getLocation());
std::pair<FileID, unsigned> loc_info =
  srcMgr.getDecomposedLoc(loc);
FileID file_id = loc_info.first;
const FileEntry* file_entry =
  srcMgr.getFileEntryForID(file_id);

Thx for your help,
Felix

Jacob Carlborg <doob@...> writes:

I'm using the following functions from the C bindings:

* clang_getCursorLocation
* clang_getSpellingLocation
* clang_getFileName

May you could have a look at the implementation of

those functions if no

one else has a quicker solution.

This helped, thx. I'am now using the following code:

SourceLocation loc = srcMgr.getFileLoc(decl->getLocation());
std::pair<FileID, unsigned> loc_info =
  srcMgr.getDecomposedLoc(loc);
FileID file_id = loc_info.first;

const FileEntry* file_entry =
  srcMgr.getFileEntryForID(file_id);

Regards,
Felix

Manuel Klimek <klimek@…> writes:

Manuel Klimek <klimek …> writes:

Hi,
I got the following problem:
I need to traverse the AST (currently using
RecursiveASTVisitor) and get the
file name for every function declaration.
Currently, I only get the filename for function
declarations in my main file but
how do I get these for functions in #included files?
My relevant code looks like this (srcMgr is the
SourceManager):
bool CMyASTVisitor::VisitFunctionDecl(FunctionDecl*
decl)
{

const FileEntry* file_entry =
srcMgr.getFileEntryForID(srcMgr.getFileID(decl-
getLocStart()));
if (file_entry == NULL)
std::cout << “no file entry” << std::endl;

}
Is there a way to get these filenames?

Why does file_entry->getName() not work?

Because file_entry is NULL if the declaration found is
not in
the main file but from an included file in the main file.

That’s news to me - the only place where I’ve found
file_entry to be NULL is
for token-pasted code, for example from macro expansions.

You are right, but this happened to be the case for me
(I tested with /usr/include/math.h, which does a lot of
nasty stuff). The solution is using this code instead:

SourceLocation loc = srcMgr.getFileLoc(decl->getLocation());

Yep, if that’s your problem getFileLoc or getExpansionLoc are what you want.

Cheers,
/Manuel