[cfe-dev] parsing multiple source files

Douglas Gregor dgregor at apple.com
Thu Jan 27 07:23:00 PST 2011

On Jan 27, 2011, at 7:09 AM, Hironobu TAKEI wrote:

> Hello
> I'd like to get ASTContext-s for multiple input files (by calling
> ParseAST)
> I've looked into web and clang's sources but I lost my way...
> My understand is that each of FileManager, SourceManager, IdentifierTable,
> SelectorTable and HeaderSearch is unique to MY PROGRAM,
> then, each of Preprocessor and ASTContext is unique to each of INPUT FILES.
> Is it correct?

To. All of these entities are unique to each translation unit.

> My code:
> {
>    ...
>    SourceManager srcMgr(diag);
>    ...
>    foreach input-files {
>        if (file-is-main) {
>            srcMgr.createMainFileID(file);
>        }
>        else {
>            srcMgr.createFileID(file, ...);
>    }
>    ASTContext context(langOpts, srcMgr, ...);
>    Preprocessor cpp(diag, langOpts, targetInfo, srcMgr, headerSearch);
>    ParseAST(cpp, consumer, context);
>    ...
> } parses only the main file.
> Preprocessor and ASTContext must have a reference to its SourceManager
> but never know any specific FileID. How can I tell my program to parse
> other files?
> Could anyone point to or suggest any solutions?

You'll need to create all of these entities from scratch each time to process a new translation unit.

However, I strongly suggest not trying to do all of this by creating contexts, preprocessors, file/source managers, etc., directly. Instead, use CompilerInvocation and CompilerInstance, with your own custom Action, to drive the compilation process.

	- Doug

More information about the cfe-dev mailing list