Skip to content

Commit b14c68f

Browse files
Macro: fix an issue where locations from xcconfig files are lost across condition binding
1 parent f6c2eec commit b14c68f

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

Sources/SWBMacro/MacroValueAssignmentTable.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,15 @@ public struct MacroValueAssignmentTable: Serializable, Sendable {
183183
if effectiveConditionValue.evaluate(condition) == true {
184184
// Condition evaluates to true, so we push an assignment with a condition set that excludes the condition.
185185
let filteredConditions = conditions.conditions.filter{ $0.parameter != parameter }
186-
table.push(macro, assignment.expression, conditions: filteredConditions.isEmpty ? nil : MacroConditionSet(conditions: filteredConditions))
186+
table.push(macro, assignment.expression, conditions: filteredConditions.isEmpty ? nil : MacroConditionSet(conditions: filteredConditions), location: assignment.location)
187187
}
188188
else {
189189
// Condition evaluates to false, so we elide the assignment.
190190
}
191191
}
192192
else {
193193
// Assignment isn't conditioned on the specified parameter, so we just push it as-is.
194-
table.push(macro, assignment.expression, conditions: assignment.conditions)
194+
table.push(macro, assignment.expression, conditions: assignment.conditions, location: assignment.location)
195195
}
196196
}
197197
bindAndPushAssignment(firstAssignment)

Tests/SWBCoreTests/SettingsTests.swift

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4881,6 +4881,72 @@ import SWBMacro
48814881
}
48824882
}
48834883

4884+
@Test func targetConditionalLocation() async throws {
4885+
try await withTemporaryDirectory { (tmpDir: Path) in
4886+
let testWorkspace = TestWorkspace(
4887+
"Workspace",
4888+
sourceRoot: tmpDir.join("Test"),
4889+
projects: [TestPackageProject(
4890+
"aProject",
4891+
groupTree: TestGroup("SomeFiles", children: [
4892+
TestFile("Project.xcconfig"),
4893+
]),
4894+
buildConfigurations: [
4895+
TestBuildConfiguration(
4896+
"Debug",
4897+
baseConfig: "Project.xcconfig",
4898+
buildSettings: [
4899+
"SDKROOT": "macosx",
4900+
"OTHER_CFLAGS": "$(inherited) Project",
4901+
"OTHER_LDFLAGS[target=Target]": "$(inherited) Project",
4902+
"SUPPORTED_PLATFORMS": "$(AVAILABLE_PLATFORMS)",
4903+
"SUPPORTS_MACCATALYST": "YES",
4904+
])
4905+
],
4906+
targets: [
4907+
TestStandardTarget("Target", type: .application),
4908+
])
4909+
])
4910+
let workspace = try await testWorkspace.load(getCore())
4911+
4912+
let context = try await contextForTestData(workspace)
4913+
let buildRequestContext = BuildRequestContext(workspaceContext: context)
4914+
let testProject = context.workspace.projects[0]
4915+
let parameters = BuildParameters(action: .build, configuration: "Debug", activeRunDestination: .macOS)
4916+
4917+
let projectXcconfigPath = testWorkspace.sourceRoot.join("aProject/Project.xcconfig")
4918+
try await context.fs.writeFileContents(projectXcconfigPath) { stream in
4919+
stream <<<
4920+
"""
4921+
OTHER_CFLAGS = XCConfig
4922+
OTHER_LDFLAGS[target=Target] = XCConfig
4923+
"""
4924+
}
4925+
4926+
do {
4927+
let settings = Settings(workspaceContext: context, buildRequestContext: buildRequestContext, parameters: parameters, project: testProject, target: testProject.targets[0])
4928+
4929+
do {
4930+
#expect(settings.globalScope.evaluate(BuiltinMacros.OTHER_CFLAGS) == ["XCConfig", "Project"])
4931+
let macro = settings.globalScope.table.lookupMacro(BuiltinMacros.OTHER_CFLAGS)
4932+
#expect(macro != nil)
4933+
#expect(macro?.location == nil)
4934+
#expect(macro?.next?.location == .init(path: projectXcconfigPath, startLine: 1, endLine: 1, startColumn: 15, endColumn: 24))
4935+
#expect(macro?.next?.next == nil)
4936+
}
4937+
4938+
do {
4939+
#expect(settings.globalScope.evaluate(BuiltinMacros.OTHER_LDFLAGS) == ["XCConfig", "Project"])
4940+
let macro = settings.globalScope.table.lookupMacro(BuiltinMacros.OTHER_LDFLAGS)
4941+
#expect(macro != nil)
4942+
#expect(macro?.location == nil)
4943+
#expect(macro?.next?.location == .init(path: projectXcconfigPath, startLine: 2, endLine: 2, startColumn: 31, endColumn: 40))
4944+
#expect(macro?.next?.next == nil)
4945+
}
4946+
}
4947+
}
4948+
}
4949+
48844950
@Test(.requireSDKs(.macOS, .iOS))
48854951
func platformConditionals() async throws {
48864952
let testWorkspace = try await TestWorkspace(

0 commit comments

Comments
 (0)