[cfe-dev] Obtaining translation unit file names from plugins

Florian Weimer fweimer at redhat.com
Tue Jul 10 08:30:14 PDT 2012


On 07/10/2012 04:39 PM, Tom Honermann wrote:

> Assuming you care only about the source files that are directly read by
> Clang when compiling the TU and not files referenced by preprocessing
> line control directives, a loop like the following will work:
>
>      const clang::SourceManager &source_manager = ...
>      clang::SourceManager::fileinfo_iterator fi_iter;
>      for (fi_iter = source_manager.fileinfo_begin();
>           fi_iter != source_manager.fileinfo_end();
>           ++fi_iter)
>      {
>          const clang::SrcMgr::ContentCache *content_cache =
> fi_iter->second;
>          const clang::FileEntry *file_entry =
>              content_cache->ContentsEntry;
>          if (!file_entry)
>              file_entry = content_cache->OrigEntry;
>          if (!file_entry)
>              continue;
>          const char *file_name = file_entry->getName();
>          // Do something with file_name and/or file_entry
>      }

Thanks, this works.  I didn't realize at first that the CompilerInstance 
and ASTContext have different SourceManagers, and only the latter 
contains the file list.

-- 
Florian Weimer / Red Hat Product Security Team





More information about the cfe-dev mailing list