[cfe-dev] Repeat calls to ParseAST from the same compiler instance

Manuel Klimek via cfe-dev cfe-dev at lists.llvm.org
Mon Oct 19 03:59:27 PDT 2015


On Mon, Oct 19, 2015 at 8:59 AM Gabe Svenson via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> Hello,
>
> I'm trying to parse a file to do some minor source to source
> transformations using clang's rewriter. I want to re-use the same compiler
> instance within a parse function but atm I'm triggering this assertion when
> I repeat call parseAST:
>
> Assertion failed: NumEnteredSourceFiles == 0 && "Cannot reenter the main
> file!",
>  file F:\Clanggit\clang\lib\Lex\Preprocessor.cpp, line 483
>
> So I have this in my parsing function, what I want to know is how to reset
> all the parts of the compiler (without deleting/releasing them) so that I
> can repeatedly call ParseAST from the same compiler instance within a
> function without triggering any asserts or having any other issues.
>

I believe the best way to achieve this is to use a completely new
CompilerInstance + SourceManager and feed in the changed source files.


>
> // Set the main file handled by the source manager to the input file.
> const FileEntry *FileIn =
> m_compilerParseInstance->getFileManager().getFile(srcFilePath);
> m_compilerParseInstance->getSourceManager().setMainFileID(
> m_compilerParseInstance->getSourceManager().createFileID(FileIn,
> SourceLocation(), SrcMgr::C_User));
>
> // Create an AST consumer instance which is going to get called by
> // ParseAST.
> MyASTConsumer TheConsumer(m_rewriter);
>
> // Parse the file to AST, registering our consumer as the AST consumer.
> ParseAST(m_compilerParseInstance->getPreprocessor(), &TheConsumer,
> m_compilerParseInstance->getASTContext());
>
> Thanks and best regards,
> Gabe
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20151019/98618124/attachment.html>


More information about the cfe-dev mailing list