Skip to content

Commit 38dc77b

Browse files
authored
Merge pull request #77373 from nkcsgexi/env-var-objc-message
ModuleObjCTrace: allow configuring the directory path to where the objc message trace file will be emitted to via an environment variable
2 parents fcdec96 + 3cb5fbd commit 38dc77b

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

lib/FrontendTool/LoadedModuleTrace.cpp

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
913940
bool 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)) {

test/IDE/objc_send_collector_1.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
// RUN: %empty-directory(%t)
2+
// RUN: %empty-directory(%t/CUSTOM_DIR)
23

34
// 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
45
// RUN: cat %t/.SWIFT_FINE_DEPENDENCY_TRACE/* | %FileCheck %s
56

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
8+
// RUN: cat %t/CUSTOM_DIR/* | %FileCheck %s
9+
610
// REQUIRES: objc_interop
711

812
import Foo

0 commit comments

Comments
 (0)