static void indexTranslationUnit(ASTUnit &Unit, IndexingContext &IdxCtx) { // FIXME: Only deserialize stuff from the last chained PCH, not the PCH/Module // that it depends on. bool OnlyLocal = !Unit.isMainFileAST() && Unit.getOnlyLocalDecls(); if (OnlyLocal) { for (ASTUnit::top_level_iterator TL = Unit.top_level_begin(), TLEnd = Unit.top_level_end(); TL != TLEnd; ++TL) { IdxCtx.indexTopLevelDecl(*TL); if (IdxCtx.shouldAbort()) return; } } else { TranslationUnitDecl *TUDecl = Unit.getASTContext().getTranslationUnitDecl(); for (TranslationUnitDecl::decl_iterator I = TUDecl->decls_begin(), E = TUDecl->decls_end(); I != E; ++I) { IdxCtx.indexTopLevelDecl(*I); if (IdxCtx.shouldAbort()) return; } } }
bool SerializationTest::Serialize(llvm::sys::Path& Filename, llvm::sys::Path& FNameDeclPrint, ASTContext &Ctx) { { // Pretty-print the decls to a temp file. std::string Err; llvm::raw_fd_ostream DeclPP(FNameDeclPrint.c_str(), true, Err); assert (Err.empty() && "Could not open file for printing out decls."); llvm::OwningPtr<ASTConsumer> FilePrinter(CreateASTPrinter(&DeclPP)); TranslationUnitDecl *TUD = Ctx.getTranslationUnitDecl(); for (DeclContext::decl_iterator I = TUD->decls_begin(Ctx), E = TUD->decls_end(Ctx); I != E; ++I) FilePrinter->HandleTopLevelDecl(DeclGroupRef(*I)); } // Serialize the translation unit. // Reserve 256K for bitstream buffer. std::vector<unsigned char> Buffer; Buffer.reserve(256*1024); Ctx.EmitASTBitcodeBuffer(Buffer); // Write the bits to disk. if (FILE* fp = fopen(Filename.c_str(),"wb")) { fwrite((char*)&Buffer.front(), sizeof(char), Buffer.size(), fp); fclose(fp); return true; } return false; }
bool FlattenCFGTransformer::execute() { this->renamer = new VarRenamer(this->resMgr); this->preTranser = new StmtPretransformer(this->resMgr); OwningPtr<RefVarToPtrMap> refMap(new RefVarToPtrMap(0)); assert(refMap.get() && "reference variable map alloc failed"); this->dclMover = new LocalDeclMover(this->resMgr, refMap.get()); this->flat = new CFGFlattener(this->resMgr); TranslationUnitDecl *decls = this->resMgr.getCompilerInstance().getASTContext().getTranslationUnitDecl(); for(TranslationUnitDecl::decl_iterator I = decls->decls_begin(), E = decls->decls_end(); I != E; ++I) { Decl *D = *I; DPRINT("TUDecl %s %x | Ctx %x -> p %x", D->getDeclKindName(), (unsigned int)D, (unsigned int)D->getDeclContext(), (unsigned int)decls); SourceLocation Loc = D->getLocation(); if(Loc.isInvalid() || this->compInst.getSourceManager().isInSystemHeader(Loc)){ continue; } if(HandleAnyFunctionDecl(D)) { continue; } else if(HandleAnyClassDecl(D)) { continue; } } delete this->renamer; delete this->preTranser; delete this->dclMover; delete this->flat; refMap.reset(); return true; }
void ObjCMigrateASTConsumer::HandleTranslationUnit(ASTContext &Ctx) { TranslationUnitDecl *TU = Ctx.getTranslationUnitDecl(); if (MigrateProperty) for (DeclContext::decl_iterator D = TU->decls_begin(), DEnd = TU->decls_end(); D != DEnd; ++D) { if (ObjCInterfaceDecl *CDecl = dyn_cast<ObjCInterfaceDecl>(*D)) migrateObjCInterfaceDecl(Ctx, CDecl); else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(*D)) ObjCProtocolDecls.insert(PDecl); else if (const ObjCImplementationDecl *ImpDecl = dyn_cast<ObjCImplementationDecl>(*D)) migrateProtocolConformance(Ctx, ImpDecl); else if (const EnumDecl *ED = dyn_cast<EnumDecl>(*D)) { DeclContext::decl_iterator N = D; ++N; if (N != DEnd) if (const TypedefDecl *TD = dyn_cast<TypedefDecl>(*N)) migrateNSEnumDecl(Ctx, ED, TD); } // migrate methods which can have instancetype as their result type. if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(*D)) migrateInstanceType(Ctx, CDecl); } Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOpts()); RewritesReceiver Rec(rewriter); Editor->applyRewrites(Rec); for (Rewriter::buffer_iterator I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) { FileID FID = I->first; RewriteBuffer &buf = I->second; const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID); assert(file); SmallString<512> newText; llvm::raw_svector_ostream vecOS(newText); buf.write(vecOS); vecOS.flush(); llvm::MemoryBuffer *memBuf = llvm::MemoryBuffer::getMemBufferCopy( StringRef(newText.data(), newText.size()), file->getName()); SmallString<64> filePath(file->getName()); FileMgr.FixupRelativePath(filePath); Remapper.remap(filePath.str(), memBuf); } if (IsOutputFile) { Remapper.flushToFile(MigrateDir, Ctx.getDiagnostics()); } else { Remapper.flushToDisk(MigrateDir, Ctx.getDiagnostics()); } }
void ASTMergeAction::ExecuteAction() { CompilerInstance &CI = getCompilerInstance(); CI.getDiagnostics().getClient()->BeginSourceFile( CI.getASTContext().getLangOpts()); CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument, &CI.getASTContext()); IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(CI.getDiagnostics().getDiagnosticIDs()); for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) { IntrusiveRefCntPtr<DiagnosticsEngine> Diags(new DiagnosticsEngine(DiagIDs, &CI.getDiagnosticOpts(), new ForwardingDiagnosticConsumer( *CI.getDiagnostics().getClient()), /*ShouldOwnClient=*/true)); ASTUnit *Unit = ASTUnit::LoadFromASTFile(ASTFiles[I], Diags, CI.getFileSystemOpts(), false); if (!Unit) continue; ASTImporter Importer(CI.getASTContext(), CI.getFileManager(), Unit->getASTContext(), Unit->getFileManager(), /*MinimalImport=*/false); TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl(); for (DeclContext::decl_iterator D = TU->decls_begin(), DEnd = TU->decls_end(); D != DEnd; ++D) { // Don't re-import __va_list_tag, __builtin_va_list. if (NamedDecl *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); } delete Unit; } AdaptedAction->ExecuteAction(); CI.getDiagnostics().getClient()->EndSourceFile(); }
bool SerializationTest::Deserialize(llvm::sys::Path& Filename, llvm::sys::Path& FNameDeclPrint) { // Deserialize the translation unit. ASTContext *NewCtx; { // Create the memory buffer that contains the contents of the file. llvm::OwningPtr<llvm::MemoryBuffer> MBuffer(llvm::MemoryBuffer::getFile(Filename.c_str())); if (!MBuffer) return false; NewCtx = ASTContext::ReadASTBitcodeBuffer(*MBuffer, FMgr); } if (!NewCtx) return false; { // Pretty-print the deserialized decls to a temp file. std::string Err; llvm::raw_fd_ostream DeclPP(FNameDeclPrint.c_str(), true, Err); assert (Err.empty() && "Could not open file for printing out decls."); llvm::OwningPtr<ASTConsumer> FilePrinter(CreateASTPrinter(&DeclPP)); TranslationUnitDecl *TUD = NewCtx->getTranslationUnitDecl(); for (DeclContext::decl_iterator I = TUD->decls_begin(*NewCtx), E = TUD->decls_end(*NewCtx); I != E; ++I) FilePrinter->HandleTopLevelDecl(DeclGroupRef(*I)); } delete NewCtx; return true; }
void ObjCMigrateASTConsumer::HandleTranslationUnit(ASTContext &Ctx) { TranslationUnitDecl *TU = Ctx.getTranslationUnitDecl(); for (DeclContext::decl_iterator D = TU->decls_begin(), DEnd = TU->decls_end(); D != DEnd; ++D) { if (ObjCInterfaceDecl *CDecl = dyn_cast<ObjCInterfaceDecl>(*D)) migrateObjCInterfaceDecl(Ctx, CDecl); } Rewriter rewriter(Ctx.getSourceManager(), Ctx.getLangOpts()); RewritesReceiver Rec(rewriter); Editor->applyRewrites(Rec); for (Rewriter::buffer_iterator I = rewriter.buffer_begin(), E = rewriter.buffer_end(); I != E; ++I) { FileID FID = I->first; RewriteBuffer &buf = I->second; const FileEntry *file = Ctx.getSourceManager().getFileEntryForID(FID); assert(file); SmallString<512> newText; llvm::raw_svector_ostream vecOS(newText); buf.write(vecOS); vecOS.flush(); llvm::MemoryBuffer *memBuf = llvm::MemoryBuffer::getMemBufferCopy( StringRef(newText.data(), newText.size()), file->getName()); SmallString<64> filePath(file->getName()); FileMgr.FixupRelativePath(filePath); Remapper.remap(filePath.str(), memBuf); } if (IsOutputFile) { Remapper.flushToFile(MigrateDir, Ctx.getDiagnostics()); } else { Remapper.flushToDisk(MigrateDir, Ctx.getDiagnostics()); } }