[cfe-dev] incorrect AST

Mark Brown mbrowncu98 at gmail.com
Mon Sep 26 08:39:11 PDT 2011


I wrote a small Plugin that inherits the ASTConsumer and
RecursiveASTVisitor classes and writes out the AST with ASTWriter in
the ASTCOnsumer::HandleTranslationUnit method. When I compile down to
Bitcode everything works fine. When I try to add trival Attributs with
the newish VarDecl::addAttr() method (inherited by Decl) on VarDecls
and then compile down to Bitcode I get different results. Besides from
looking different there is no output. How can I fix this?

I use the VisitCallExpr(), VisitDecl() and VisitStmt() methods from
RecursiveASTVisitor to add the attributes. Here is how I write the AST
out. Does the AST need to be "finalized" or something?

Thank you

virtual void HandleTranslationUnit(ASTContext &ctx) {
                /* Run */
                TraverseDecl(ctx.getTranslationUnitDecl());

                /* Save AST */
                string error_s;
                const char *fileName = astFileName.c_str();

                raw_fd_ostream output(fileName, error_s);
                if(error_s.empty() == true) {
                        vector<unsigned char> buf;
                        BitstreamWriter stream_writer(buf);
                        ASTWriter *ast_writer = new ASTWriter(stream_writer);

                        ast_writer->WriteAST(ci->getSema(), NULL,
fileName, true, StringRef("wtf"));
                        for(vector<unsigned char>::iterator it =
buf.begin(); it != buf.end(); ++it)
                                output << *it;

                        delete ast_writer;
                } else {
                        errs() << "Error writing AST file: ";
                        errs() << error_s << "\n";
                }

                output.close();
}




More information about the cfe-dev mailing list