[cfe-dev] How to use ASTImporter

David Come via cfe-dev cfe-dev at lists.llvm.org
Thu Aug 25 01:21:24 PDT 2016


Hi,


I'm trying to merge a bunch of ASTs together using the ASTImporter.

My code is the following :

int main(int argc, const char **argv) {
   cl::OptionCategory MyToolCategory("My tool options");
   CommonOptionsParser op(argc, argv, MyToolCategory);
   ClangTool Tool(op.getCompilations(), op.getSourcePathList());
std::vector<std::unique_ptr<clang::ASTUnit>> asts;
   Tool.buildASTs(asts);

   std::cout <<asts.size() << "\n";
   for(auto it = begin(asts)++; it!=end(asts); ++it){
     auto& first_ast = asts[0];
     ASTImporter Importer(first_ast->getASTContext(), 
first_ast->getFileManager(),
                          (*it)->getASTContext(), (*it)->getFileManager(),
                          /*MinimalImport=*/false);
     TranslationUnitDecl *TU = 
(*it)->getASTContext().getTranslationUnitDecl();
     for (auto *D : TU->decls()) {
       // Don't re-import __va_list_tag, __builtin_va_list.
       if (const auto *ND = dyn_cast<NamedDecl>(D))
         if (IdentifierInfo *II = ND->getIdentifier())
           if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list"))
             continue;

       Importer.Import(D);
     }
   }

   return 0;
}

But is segfaults with the following backtrace :

#0  0x0000000000899e70 in 
clang::DiagnosticRenderer::emitDiagnostic(clang::SourceLocation, 
clang::DiagnosticsEngine::Level, llvm::StringRef, 
llvm::ArrayRef<clang::CharSourceRange>, llvm::ArrayRef<clang::FixI
tHint>, clang::SourceManager const*, 
llvm::PointerUnion<clang::Diagnostic const*, clang::StoredDiagnostic 
const*>) ()
#1  0x0000000000877638 in 
clang::TextDiagnosticPrinter::HandleDiagnostic(clang::DiagnosticsEngine::Level, 
clang::Diagnostic const&) ()
#2  0x00000000005cc710 in 
clang::DiagnosticIDs::EmitDiag(clang::DiagnosticsEngine&, 
clang::DiagnosticIDs::Level) const ()
#3  0x00000000005cc976 in 
clang::DiagnosticIDs::ProcessDiag(clang::DiagnosticsEngine&) const ()
#4  0x00000000005c5d84 in 
clang::DiagnosticsEngine::EmitCurrentDiagnostic(bool) ()
#5  0x00000000005eed9d in clang::ASTNodeImporter::VisitExpr(clang::Expr*) ()
#6  0x00000000005f891f in clang::ASTImporter::Import(clang::Stmt*) ()
#7  0x00000000005f96c6 in 
clang::ASTNodeImporter::VisitDecltypeType(clang::DecltypeType const*) ()
#8  0x00000000005f9795 in clang::TypeVisitor<clang::ASTNodeImporter, 
clang::QualType>::Visit(clang::Type const*) ()
#9  0x00000000005f9a3f in clang::ASTImporter::Import(clang::QualType) ()
#10 0x00000000006017a4 in 
clang::ASTNodeImporter::VisitTypedefNameDecl(clang::TypedefNameDecl*, 
bool) ()
#11 0x00000000005f760a in clang::ASTImporter::Import(clang::Decl*) ()
#12 0x00000000005ff00c in 
clang::ASTNodeImporter::ImportDeclContext(clang::DeclContext*, bool) ()
#13 0x0000000000601271 in 
clang::ASTNodeImporter::VisitNamespaceDecl(clang::NamespaceDecl*) ()
#14 0x00000000005f760a in clang::ASTImporter::Import(clang::Decl*) ()

Can someone tell me what am'I doing wrong ?

Thanks,

David




More information about the cfe-dev mailing list