@@ -911,17 +911,34 @@ class ObjcMethodReferenceCollector: public SourceEntityWalker {
911911};
912912
913913static std::optional<int > createObjCMessageTraceFile (const InputFile &input,
914- ModuleDecl *MD) {
914+ ModuleDecl *MD,
915+ std::vector<SourceFile*> &filesToWalk) {
916+ if (input.getLoadedModuleTracePath ().empty ()) {
917+ // we basically rely on the passing down of module trace file path
918+ // as an indicator that this job needs to emit an ObjC message trace file.
919+ // FIXME: add a separate swift-frontend flag for ObjC message trace path
920+ // specifically.
921+ return {};
922+ }
923+ for (auto *FU : MD->getFiles ()) {
924+ if (auto *SF = dyn_cast<SourceFile>(FU)) {
925+ if (SF->getFilename ().ends_with (" .swift" )) {
926+ filesToWalk.push_back (SF);
927+ }
928+ }
929+ }
930+ // No source files to walk, abort.
931+ if (filesToWalk.empty ()) {
932+ return {};
933+ }
915934 llvm::SmallString<128 > tracePath;
916935 if (const char *P = ::getenv (" SWIFT_COMPILER_OBJC_MESSAGE_TRACE_DIRECTORY" )) {
917936 StringRef DirPath = P;
918937 llvm::sys::path::append (tracePath, DirPath);
919- } else if (!input. getLoadedModuleTracePath (). empty ()) {
938+ } else {
920939 llvm::sys::path::append (tracePath, input.getLoadedModuleTracePath ());
921940 llvm::sys::path::remove_filename (tracePath);
922941 llvm::sys::path::append (tracePath, " .SWIFT_FINE_DEPENDENCY_TRACE" );
923- } else {
924- return {};
925942 }
926943 if (!llvm::sys::fs::exists (tracePath)) {
927944 if (llvm::sys::fs::create_directory (tracePath))
@@ -944,17 +961,16 @@ bool swift::emitObjCMessageSendTraceIfNeeded(ModuleDecl *mainModule,
944961 " We should've already exited earlier if there was an error." );
945962
946963 opts.InputsAndOutputs .forEachInput ([&](const InputFile &input) {
947- auto tmpFD = createObjCMessageTraceFile (input, mainModule);
964+ std::vector<SourceFile*> filesToWalk;
965+ auto tmpFD = createObjCMessageTraceFile (input, mainModule, filesToWalk);
948966 if (!tmpFD)
949967 return false ;
950968 // Write the contents of the buffer.
951969 llvm::raw_fd_ostream out (*tmpFD, /* shouldClose=*/ true );
952970 ObjcMethodReferenceCollector collector (mainModule);
953- for (auto *FU : mainModule->getFiles ()) {
954- if (auto *SF = dyn_cast<SourceFile>(FU)) {
955- collector.setFileBeforeVisiting (SF);
956- collector.walk (*SF);
957- }
971+ for (auto *SF : filesToWalk) {
972+ collector.setFileBeforeVisiting (SF);
973+ collector.walk (*SF);
958974 }
959975 collector.serializeAsJson (out);
960976 return true ;
0 commit comments