@@ -853,16 +853,17 @@ class ObjcMethodReferenceCollector: public SourceEntityWalker {
853853 }
854854};
855855
856- static std::optional<int > createObjCMessageTraceFile (const InputFile &input,
857- ModuleDecl *MD,
858- std::vector<SourceFile*> &filesToWalk) {
859- if (input.getLoadedModuleTracePath ().empty ()) {
856+ static void createFineModuleTraceFile (const InputFile &input, ModuleDecl *MD) {
857+ StringRef tracePath = input.getFineModuleTracePath ();
858+ if (tracePath.empty ()) {
860859 // we basically rely on the passing down of module trace file path
861860 // as an indicator that this job needs to emit an ObjC message trace file.
862861 // FIXME: add a separate swift-frontend flag for ObjC message trace path
863862 // specifically.
864- return {} ;
863+ return ;
865864 }
865+ auto &ctx = MD->getASTContext ();
866+ std::vector<SourceFile*> filesToWalk;
866867 for (auto *FU : MD->getFiles ()) {
867868 if (auto *SF = dyn_cast<SourceFile>(FU)) {
868869 switch (SF->Kind ) {
@@ -880,50 +881,48 @@ static std::optional<int> createObjCMessageTraceFile(const InputFile &input,
880881 }
881882 // No source files to walk, abort.
882883 if (filesToWalk.empty ()) {
883- return {} ;
884+ return ;
884885 }
885- llvm::SmallString< 128 > tracePath;
886- if ( const char *P = :: getenv ( " SWIFT_COMPILER_OBJC_MESSAGE_TRACE_DIRECTORY " )) {
887- StringRef DirPath = P ;
888- llvm::sys::path::append ( tracePath, DirPath );
889- } else {
890- llvm::sys::path::append (tracePath, input. getLoadedModuleTracePath ());
891- llvm::sys::path::remove_filename (tracePath );
892- llvm::sys::path::append (tracePath, " .SWIFT_FINE_DEPENDENCY_TRACE " ) ;
886+ // Write output via atomic append.
887+ llvm::vfs::OutputConfig config;
888+ config. setAppend (). setAtomicWrite () ;
889+ auto outputFile = ctx. getOutputBackend (). createFile ( tracePath, config );
890+ if (!outputFile) {
891+ ctx. Diags . diagnose ( SourceLoc (), diag::error_opening_output, tracePath,
892+ toString (outputFile. takeError ()) );
893+ return ;
893894 }
894- if (!llvm::sys::fs::exists (tracePath)) {
895- if (llvm::sys::fs::create_directory (tracePath))
896- return {};
895+ ObjcMethodReferenceCollector collector (MD);
896+ for (auto *SF: filesToWalk) {
897+ collector.setFileBeforeVisiting (SF);
898+ collector.walk (*SF);
897899 }
898- SmallString<32 > fileName (MD->getNameStr ());
899- fileName.append (" -%%%%-%%%%-%%%%.json" );
900- llvm::sys::path::append (tracePath, fileName);
901- int tmpFD;
902- if (llvm::sys::fs::createUniqueFile (tracePath.str (), tmpFD, tracePath)) {
903- return {};
900+
901+ // print this json line.
902+ std::string stringBuffer;
903+ {
904+ llvm::raw_string_ostream memoryBuffer (stringBuffer);
905+ collector.serializeAsJson (memoryBuffer);
906+ }
907+ stringBuffer += " \n " ;
908+
909+ // Write output via atomic append.
910+ *outputFile << stringBuffer;
911+ if (auto err = outputFile->keep ()) {
912+ ctx.Diags .diagnose (SourceLoc (), diag::error_opening_output,
913+ tracePath, toString (std::move (err)));
914+ return ;
904915 }
905- return tmpFD;
906916}
907917
908- bool swift::emitObjCMessageSendTraceIfNeeded (ModuleDecl *mainModule,
909- const FrontendOptions &opts) {
918+ bool swift::emitFineModuleTraceIfNeeded (ModuleDecl *mainModule,
919+ const FrontendOptions &opts) {
910920 ASTContext &ctxt = mainModule->getASTContext ();
911921 assert (!ctxt.hadError () &&
912922 " We should've already exited earlier if there was an error." );
913923
914924 opts.InputsAndOutputs .forEachInput ([&](const InputFile &input) {
915- std::vector<SourceFile*> filesToWalk;
916- auto tmpFD = createObjCMessageTraceFile (input, mainModule, filesToWalk);
917- if (!tmpFD)
918- return false ;
919- // Write the contents of the buffer.
920- llvm::raw_fd_ostream out (*tmpFD, /* shouldClose=*/ true );
921- ObjcMethodReferenceCollector collector (mainModule);
922- for (auto *SF : filesToWalk) {
923- collector.setFileBeforeVisiting (SF);
924- collector.walk (*SF);
925- }
926- collector.serializeAsJson (out);
925+ createFineModuleTraceFile (input, mainModule);
927926 return true ;
928927 });
929928 return false ;
0 commit comments