Skip to content

Commit 94d1333

Browse files
committed
ModuleObjCTrace: walk only Swift source files to collect used ObjC method invocations
1 parent a67eb35 commit 94d1333

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

lib/FrontendTool/LoadedModuleTrace.cpp

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -911,17 +911,34 @@ class ObjcMethodReferenceCollector: public SourceEntityWalker {
911911
};
912912

913913
static 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;

test/IDE/objc_send_collector_1.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// RUN: %target-swift-frontend -I %t/lib/swift -typecheck %s %S/Inputs/objc_send_collector_2.swift -module-name main -swift-version 5 -F %S/Inputs/mock-sdk -emit-loaded-module-trace-path %t/.MODULE_TRACE
55
// RUN: cat %t/.SWIFT_FINE_DEPENDENCY_TRACE/* | %FileCheck %s
66

7-
// RUN: SWIFT_COMPILER_OBJC_MESSAGE_TRACE_DIRECTORY=%t/CUSTOM_DIR %target-swift-frontend -I %t/lib/swift -typecheck %s %S/Inputs/objc_send_collector_2.swift -module-name main -swift-version 5 -F %S/Inputs/mock-sdk
7+
// RUN: SWIFT_COMPILER_OBJC_MESSAGE_TRACE_DIRECTORY=%t/CUSTOM_DIR %target-swift-frontend -I %t/lib/swift -typecheck %s %S/Inputs/objc_send_collector_2.swift -module-name main -swift-version 5 -F %S/Inputs/mock-sdk -emit-loaded-module-trace-path %t/.MODULE_TRACE
88
// RUN: cat %t/CUSTOM_DIR/* | %FileCheck %s
99

1010
// REQUIRES: objc_interop

0 commit comments

Comments
 (0)