[cfe-dev] ASTContext: how to get an object or pointer of ASTContext

Douglas Gregor dgregor at apple.com
Tue May 18 07:11:48 PDT 2010


On May 18, 2010, at 6:09 AM, Jiandong Wang wrote:

> Hi all:
>     now I am trying to run my own ASTConsumer against AST, but not quite sure how to do it, especially how to get an object or pointer of ASTContext. BTW, ASTContext is required by ParseAST.  
> here is my code snippet:
> _______________________________________
> 
>         MyDiagnosticClient DiagClient(llvm::errs());
> 	Diagnostic Diags(&DiagClient);
> 	LangOptions Opts;
>         TargetOptions TargetOpts;	
>         TargetInfo* Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
> 	SourceManager SM;
> 	FileManager FM;
> 	HeaderSearch Headers(FM);
> 
> 	llvm::StringRef filename("input.c");
> 	const FileEntry* File = FM.getFile(filename);
> 	if (!File) {
> 	  llvm::errs() << "Failed to open \'" << filename << "\'\n" ;
> 	  return 1;
> 	}
> 
> 	SM.createMainFileID(File, SourceLocation());
> 	Preprocessor PP(Diags, Opts, *Target, SM, Headers);
> 	ASTConsumer* AstConsumer = new MyASTConsumer;
> 	ParseAST(PP, AstConsumer, *here need the third parameter ASTContext*);     //<------------------------- see here
> 	delete Target;
> ______________________________________
> 
> any ideas? Thanks in advance.

You can just create an ASTContext on the stack, passing in the various other pieces (lang options, source manager, target info, etc.) when you construct it.

Have you considered using CompilerInvocation/CompilerInstance rather than building all of the pieces together?

	- Doug
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20100518/c0342d97/attachment.html>


More information about the cfe-dev mailing list