Skip to content

Commit f7a831b

Browse files
committed
Parse module name for obj-c targets
Parse the obj-c module name for targets by looking at the `-fmodule-name` flag.
1 parent 281c3e0 commit f7a831b

File tree

4 files changed

+63
-36
lines changed

4 files changed

+63
-36
lines changed

Sources/SKCore/BuildSystemManager.swift

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import BuildServerProtocol
1414
import Dispatch
1515
import LSPLogging
1616
import LanguageServerProtocol
17+
import SwiftExtensions
1718

1819
import struct TSCBasic.AbsolutePath
1920

@@ -156,14 +157,33 @@ extension BuildSystemManager {
156157

157158
/// Returns the target's module name as parsed from the `ConfiguredTarget`'s compiler arguments.
158159
public func moduleName(for document: DocumentURI, in target: ConfiguredTarget) async -> String? {
159-
guard let buildSettings = await buildSettings(for: document, in: target, language: .swift),
160-
let moduleNameFlagIndex = buildSettings.compilerArguments.firstIndex(of: "-module-name")
160+
guard let language = await self.defaultLanguage(for: document),
161+
let buildSettings = await buildSettings(for: document, in: target, language: language)
161162
else {
162163
return nil
163164
}
164165

165-
let moduleNameIndex = buildSettings.compilerArguments.index(after: moduleNameFlagIndex)
166-
return buildSettings.compilerArguments[moduleNameIndex]
166+
switch language {
167+
case .swift:
168+
// Module name is specified in the form -module-name MyLibrary
169+
guard let moduleNameFlagIndex = buildSettings.compilerArguments.firstIndex(of: "-module-name") else {
170+
return nil
171+
}
172+
return buildSettings.compilerArguments[safe: moduleNameFlagIndex + 1]
173+
case .objective_c:
174+
// Specified in the form -fmodule-name=MyLibrary
175+
guard
176+
let moduleNameArgument = buildSettings.compilerArguments.first(where: {
177+
$0.starts(with: "-fmodule-name=")
178+
}),
179+
let moduleName = moduleNameArgument.split(separator: "=").last
180+
else {
181+
return nil
182+
}
183+
return String(moduleName)
184+
default:
185+
return nil
186+
}
167187
}
168188

169189
/// Returns the build settings for `document` from `buildSystem`.

Sources/SourceKitLSP/TestDiscovery.swift

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -480,38 +480,26 @@ fileprivate extension Array<AnnotatedTestItem> {
480480

481481
func prefixTestsWithModuleName(workspace: Workspace) async -> Self {
482482
return await self.asyncMap({
483-
// If the module name can't be determined we return the test item without a prefixed id.
484-
guard let moduleName = await self.moduleName(from: workspace, for: $0.testItem.location.uri) else {
485-
return $0
486-
}
487-
var newTest = $0.testItem
488-
newTest.id = "\(moduleName).\(newTest.id)"
489-
newTest.children = await prefixTestsWithModuleName(workspace: workspace, newTest.children)
490-
return AnnotatedTestItem(testItem: newTest, isExtension: $0.isExtension)
491-
})
492-
}
493-
494-
private func prefixTestsWithModuleName(workspace: Workspace, _ tests: [TestItem]) async -> [TestItem] {
495-
return await tests.asyncMap({
496-
guard let moduleName = await self.moduleName(from: workspace, for: $0.location.uri) else {
497-
return $0
498-
}
499-
500-
var newTest = $0
501-
newTest.id = "\(moduleName).\(newTest.id)"
502-
newTest.children = await prefixTestsWithModuleName(workspace: workspace, newTest.children)
503-
return newTest
483+
return AnnotatedTestItem(
484+
testItem: await $0.testItem.prefixIDWithModuleName(workspace: workspace),
485+
isExtension: $0.isExtension
486+
)
504487
})
505488
}
489+
}
506490

507-
private func moduleName(from workspace: Workspace, for uri: DocumentURI) async -> String? {
508-
guard let configuredTarget = await workspace.buildSystemManager.canonicalConfiguredTarget(for: uri) else {
509-
return nil
491+
extension TestItem {
492+
fileprivate func prefixIDWithModuleName(workspace: Workspace) async -> TestItem {
493+
guard let configuredTarget = await workspace.buildSystemManager.canonicalConfiguredTarget(for: self.location.uri),
494+
let moduleName = await workspace.buildSystemManager.moduleName(for: self.location.uri, in: configuredTarget)
495+
else {
496+
return self
510497
}
511-
// If for whatever reason we can't get a module name from the build system, fall back
512-
// to using the targetID as this would be used when there is no command line arguments
513-
// to define the module name as something other than the targetID.
514-
return await workspace.buildSystemManager.moduleName(for: uri, in: configuredTarget) ?? configuredTarget.targetID
498+
499+
var newTest = self
500+
newTest.id = "\(moduleName).\(newTest.id)"
501+
newTest.children = await newTest.children.asyncMap({ await $0.prefixIDWithModuleName(workspace: workspace) })
502+
return newTest
515503
}
516504
}
517505

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
extension Array {
14+
/// Returns the element at the specified index if it is within the Array's
15+
/// bounds, otherwise `nil`.
16+
public subscript(safe index: Index) -> Element? {
17+
return index > 0 && index < count ? self[index] : nil
18+
}
19+
}

Tests/SourceKitLSPTests/WorkspaceTestDiscoveryTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -655,12 +655,12 @@ final class WorkspaceTestDiscoveryTests: XCTestCase {
655655
tests,
656656
[
657657
TestItem(
658-
id: "dummy.MyTests",
658+
id: "MyTests",
659659
label: "MyTests",
660660
location: Location(uri: project.fileURI, range: Range(project.positions["1️⃣"])),
661661
children: [
662662
TestItem(
663-
id: "dummy.MyTests/testSomething()",
663+
id: "MyTests/testSomething()",
664664
label: "testSomething()",
665665
location: Location(uri: project.fileURI, range: Range(project.positions["2️⃣"]))
666666
)
@@ -712,12 +712,12 @@ final class WorkspaceTestDiscoveryTests: XCTestCase {
712712
tests,
713713
[
714714
TestItem(
715-
id: "dummy.MyTests",
715+
id: "MyTests",
716716
label: "MyTests",
717717
location: try project.location(from: "1️⃣", to: "4️⃣", in: "MyTests.swift"),
718718
children: [
719719
TestItem(
720-
id: "dummy.MyTests/testSomething()",
720+
id: "MyTests/testSomething()",
721721
label: "testSomething()",
722722
location: try project.location(from: "2️⃣", to: "3️⃣", in: "MyTests.swift")
723723
)

0 commit comments

Comments
 (0)