[cfe-dev] lib clang help request: usage of clang_getIncludedFile()

Douglas Gregor dgregor at apple.com
Sat Dec 3 12:50:53 PST 2011


On Dec 3, 2011, at 6:26 AM, unitydeveloper wrote:

> Hi all,
> 
> I am trying to find out the files included from a file.
> 
> I use clang_tokenize() and clang_annotateTokens() in order to get a set of tokens and cursors.
> 
> When I iterate through them, for #import directives I get a cursor kind of preprocessor directive instead of file inclusion ( which is what I expected if I understood lib clang correctly ) .

Yes, you should get it. Are you specifying CXTranslationUnit_DetailedPreprocessingRecord when you're parsing the translation unit? If not, libclang isn't keeping detailed information about inclusions.

> When I call clang_getIncludedFile() on the correspondent cursor it returns a zero file, so that I cannot get its filename.
> 
> Am I doing something terribly wrong here or is there something wrong with the way clang_annotateTokens() works or anything else?
> 
> What is the correct way to find the files included from a file?


An alternative way is to perform a non-recursive visit of the contents of the translation unit using clang_visitChildren on the translation unit. Then, you'll get all of the inclusion directive cursors. You still need to specify CXTranslationUnit_DetailedPreprocessingRecord, of course. 

	- Doug



More information about the cfe-dev mailing list