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

Gabe Svenson via cfe-dev cfe-dev at lists.llvm.org
Sat Oct 17 23:33:52 PDT 2015


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.

// 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20151018/187c645d/attachment.html>


More information about the cfe-dev mailing list