@@ -910,30 +910,45 @@ class ObjcMethodReferenceCollector: public SourceEntityWalker {
910910 }
911911};
912912
913+ static std::optional<int > createObjCMessageTraceFile (const InputFile &input,
914+ ModuleDecl *MD) {
915+ llvm::SmallString<128 > tracePath;
916+ if (const char *P = ::getenv (" SWIFT_COMPILER_OBJC_MESSAGE_TRACE_DIRECTORY" )) {
917+ StringRef DirPath = P;
918+ llvm::sys::path::append (tracePath, DirPath);
919+ } else if (!input.getLoadedModuleTracePath ().empty ()) {
920+ llvm::sys::path::append (tracePath, input.getLoadedModuleTracePath ());
921+ llvm::sys::path::remove_filename (tracePath);
922+ llvm::sys::path::append (tracePath, " .SWIFT_FINE_DEPENDENCY_TRACE" );
923+ } else {
924+ return {};
925+ }
926+ if (!llvm::sys::fs::exists (tracePath)) {
927+ if (llvm::sys::fs::create_directory (tracePath))
928+ return {};
929+ }
930+ SmallString<32 > fileName (MD->getNameStr ());
931+ fileName.append (" -%%%%-%%%%-%%%%.json" );
932+ llvm::sys::path::append (tracePath, fileName);
933+ int tmpFD;
934+ if (llvm::sys::fs::createUniqueFile (tracePath.str (), tmpFD, tracePath)) {
935+ return {};
936+ }
937+ return tmpFD;
938+ }
939+
913940bool swift::emitObjCMessageSendTraceIfNeeded (ModuleDecl *mainModule,
914941 const FrontendOptions &opts) {
915942 ASTContext &ctxt = mainModule->getASTContext ();
916943 assert (!ctxt.hadError () &&
917944 " We should've already exited earlier if there was an error." );
918945
919946 opts.InputsAndOutputs .forEachInput ([&](const InputFile &input) {
920- auto loadedModuleTracePath = input.getLoadedModuleTracePath ();
921- if (loadedModuleTracePath.empty ())
922- return false ;
923- llvm::SmallString<128 > tracePath {loadedModuleTracePath};
924- llvm::sys::path::remove_filename (tracePath);
925- llvm::sys::path::append (tracePath, " .SWIFT_FINE_DEPENDENCY_TRACE" );
926- if (!llvm::sys::fs::exists (tracePath)) {
927- if (llvm::sys::fs::create_directory (tracePath))
928- return false ;
929- }
930- llvm::sys::path::append (tracePath, " %%%%-%%%%-%%%%.json" );
931- int tmpFD;
932- if (llvm::sys::fs::createUniqueFile (tracePath.str (), tmpFD, tracePath)) {
947+ auto tmpFD = createObjCMessageTraceFile (input, mainModule);
948+ if (!tmpFD)
933949 return false ;
934- }
935950 // Write the contents of the buffer.
936- llvm::raw_fd_ostream out (tmpFD, /* shouldClose=*/ true );
951+ llvm::raw_fd_ostream out (* tmpFD, /* shouldClose=*/ true );
937952 ObjcMethodReferenceCollector collector (mainModule);
938953 for (auto *FU : mainModule->getFiles ()) {
939954 if (auto *SF = dyn_cast<SourceFile>(FU)) {
0 commit comments