[cfe-dev] loading and traversing an AST

josef maxim pohl josef.m.pohl at gmail.com
Thu Oct 27 08:46:48 PDT 2011


Hi all,
Here is yet another new person question.
I have, after a long while, cobbled together an example that I thought would
simply traverse create an AST and then traverse it printing out function and
variable names. Sadly, I am getting the following error:

~/llvm/tools/clang/lib/Lex/Preprocessor.cpp:386: void
clang::Preprocessor::EnterMainSourceFile(): Assertion `NumEnteredSourceFiles
== 0 && "Cannot reenter the main file!"' failed.
Stack dump:
0.    <eof> parser at end of file
Aborted

Which makes me believe that my AST is not being created properly
(obviously).

I create the ASTUnit after an example sent by Guoping using:

llvm::OwningPtr<ASTUnit> CUnit(
                  ASTUnit::LoadFromCommandLine(argv,argv+argc,Diags,
                  ResPath,false,CaptureDiagnostics,NULL,0,false,false,
                  TUKind,false,false));

And then have a ASTConsumer instance that implements HandleTranslationUnit
and two methods, VisitFunctionDecl and VisitVarDecl.  But I suspect the
error is coming before the call to any of these.

Perhaps I have not set up the call to ParseAST properly?

  clang::ParseAST(TU.getPreprocessor(), consumer, TU.getASTContext());


TU is a wrapper around ASTUnit as in the wpa example.

Any thoughts I what I might try to remedy this situation?

Thanks in advance,
Joe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20111027/3260c49c/attachment.html>


More information about the cfe-dev mailing list