Skip to content

Commit 2e270a1

Browse files
NFC: Assert that we don't add llbuild commands with the same name twice (#7698)
Add debug assertion to avoid llbuild command name conflicts. Depends on #7695 ### Motivation: One of the reasons we couldn't reveal #7695 issue for long time (at least past 2 releases) is that we overlooked name conflicts on `addWriteLinkFileListCommand`. Looking into the manifest builder, some of the commands also forgot to assert name conflicts, so it was error-prone. ### Modifications: Add a convenience method to perform assertions consistently ### Result: NFC for release build. Just add debug assertions
1 parent e858647 commit 2e270a1

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

Sources/LLBuildManifest/LLBuildManifest.swift

+20-24
Original file line numberDiff line numberDiff line change
@@ -226,51 +226,52 @@ public struct LLBuildManifest {
226226
targets[target, default: Target(name: target, nodes: [])].nodes.append(node)
227227
}
228228

229+
private mutating func addCommand(name: String, tool: ToolProtocol) {
230+
assert(commands[name] == nil, "already had a command named '\(name)'")
231+
commands[name] = Command(name: name, tool: tool)
232+
}
233+
229234
public mutating func addPhonyCmd(
230235
name: String,
231236
inputs: [Node],
232237
outputs: [Node]
233238
) {
234-
assert(commands[name] == nil, "already had a command named '\(name)'")
235239
let tool = PhonyTool(inputs: inputs, outputs: outputs)
236-
commands[name] = Command(name: name, tool: tool)
240+
addCommand(name: name, tool: tool)
237241
}
238242

239243
public mutating func addTestDiscoveryCmd(
240244
name: String,
241245
inputs: [Node],
242246
outputs: [Node]
243247
) {
244-
assert(commands[name] == nil, "already had a command named '\(name)'")
245248
let tool = TestDiscoveryTool(inputs: inputs, outputs: outputs)
246-
commands[name] = Command(name: name, tool: tool)
249+
addCommand(name: name, tool: tool)
247250
}
248251

249252
public mutating func addTestEntryPointCmd(
250253
name: String,
251254
inputs: [Node],
252255
outputs: [Node]
253256
) {
254-
assert(commands[name] == nil, "already had a command named '\(name)'")
255257
let tool = TestEntryPointTool(inputs: inputs, outputs: outputs)
256-
commands[name] = Command(name: name, tool: tool)
258+
addCommand(name: name, tool: tool)
257259
}
258260

259261
public mutating func addCopyCmd(
260262
name: String,
261263
inputs: [Node],
262264
outputs: [Node]
263265
) {
264-
assert(commands[name] == nil, "already had a command named '\(name)'")
265266
let tool = CopyTool(inputs: inputs, outputs: outputs)
266-
commands[name] = Command(name: name, tool: tool)
267+
addCommand(name: name, tool: tool)
267268
}
268269

269270
public mutating func addEntitlementPlistCommand(entitlement: String, outputPath: AbsolutePath) {
270271
let inputs = WriteAuxiliary.EntitlementPlist.computeInputs(entitlement: entitlement)
271272
let tool = WriteAuxiliaryFile(inputs: inputs, outputFilePath: outputPath)
272273
let name = outputPath.pathString
273-
commands[name] = Command(name: name, tool: tool)
274+
addCommand(name: name, tool: tool)
274275
}
275276

276277
public mutating func addWriteLinkFileListCommand(
@@ -280,7 +281,7 @@ public struct LLBuildManifest {
280281
let inputs = WriteAuxiliary.LinkFileList.computeInputs(objects: objects)
281282
let tool = WriteAuxiliaryFile(inputs: inputs, outputFilePath: linkFileListPath)
282283
let name = linkFileListPath.pathString
283-
commands[name] = Command(name: name, tool: tool)
284+
addCommand(name: name, tool: tool)
284285
}
285286

286287
public mutating func addWriteSourcesFileListCommand(
@@ -290,7 +291,7 @@ public struct LLBuildManifest {
290291
let inputs = WriteAuxiliary.SourcesFileList.computeInputs(sources: sources)
291292
let tool = WriteAuxiliaryFile(inputs: inputs, outputFilePath: sourcesFileListPath)
292293
let name = sourcesFileListPath.pathString
293-
commands[name] = Command(name: name, tool: tool)
294+
addCommand(name: name, tool: tool)
294295
}
295296

296297
public mutating func addSwiftGetVersionCommand(
@@ -300,14 +301,14 @@ public struct LLBuildManifest {
300301
let inputs = WriteAuxiliary.SwiftGetVersion.computeInputs(swiftCompilerPath: swiftCompilerPath)
301302
let tool = WriteAuxiliaryFile(inputs: inputs, outputFilePath: swiftVersionFilePath, alwaysOutOfDate: true)
302303
let name = swiftVersionFilePath.pathString
303-
commands[name] = Command(name: name, tool: tool)
304+
addCommand(name: name, tool: tool)
304305
}
305306

306307
public mutating func addWriteInfoPlistCommand(principalClass: String, outputPath: AbsolutePath) {
307308
let inputs = WriteAuxiliary.XCTestInfoPlist.computeInputs(principalClass: principalClass)
308309
let tool = WriteAuxiliaryFile(inputs: inputs, outputFilePath: outputPath)
309310
let name = outputPath.pathString
310-
commands[name] = Command(name: name, tool: tool)
311+
addCommand(name: name, tool: tool)
311312
}
312313

313314
public mutating func addWriteEmbeddedResourcesCommand(
@@ -317,17 +318,16 @@ public struct LLBuildManifest {
317318
let inputs = WriteAuxiliary.EmbeddedResources.computeInputs(resources: resources)
318319
let tool = WriteAuxiliaryFile(inputs: inputs, outputFilePath: outputPath)
319320
let name = outputPath.pathString
320-
commands[name] = Command(name: name, tool: tool)
321+
addCommand(name: name, tool: tool)
321322
}
322323

323324
public mutating func addPkgStructureCmd(
324325
name: String,
325326
inputs: [Node],
326327
outputs: [Node]
327328
) {
328-
assert(commands[name] == nil, "already had a command named '\(name)'")
329329
let tool = PackageStructureTool(inputs: inputs, outputs: outputs)
330-
commands[name] = Command(name: name, tool: tool)
330+
addCommand(name: name, tool: tool)
331331
}
332332

333333
public mutating func addShellCmd(
@@ -340,7 +340,6 @@ public struct LLBuildManifest {
340340
workingDirectory: String? = nil,
341341
allowMissingInputs: Bool = false
342342
) {
343-
assert(commands[name] == nil, "already had a command named '\(name)'")
344343
let tool = ShellTool(
345344
description: description,
346345
inputs: inputs,
@@ -350,7 +349,7 @@ public struct LLBuildManifest {
350349
workingDirectory: workingDirectory,
351350
allowMissingInputs: allowMissingInputs
352351
)
353-
commands[name] = Command(name: name, tool: tool)
352+
addCommand(name: name, tool: tool)
354353
}
355354

356355
public mutating func addSwiftFrontendCmd(
@@ -362,15 +361,14 @@ public struct LLBuildManifest {
362361
outputs: [Node],
363362
arguments: [String]
364363
) {
365-
assert(commands[name] == nil, "already had a command named '\(name)'")
366364
let tool = SwiftFrontendTool(
367365
moduleName: moduleName,
368366
description: description,
369367
inputs: inputs,
370368
outputs: outputs,
371369
arguments: arguments
372370
)
373-
commands[name] = Command(name: name, tool: tool)
371+
addCommand(name: name, tool: tool)
374372
}
375373

376374
public mutating func addClangCmd(
@@ -381,15 +379,14 @@ public struct LLBuildManifest {
381379
arguments: [String],
382380
dependencies: String? = nil
383381
) {
384-
assert(commands[name] == nil, "already had a command named '\(name)'")
385382
let tool = ClangTool(
386383
description: description,
387384
inputs: inputs,
388385
outputs: outputs,
389386
arguments: arguments,
390387
dependencies: dependencies
391388
)
392-
commands[name] = Command(name: name, tool: tool)
389+
addCommand(name: name, tool: tool)
393390
}
394391

395392
public mutating func addSwiftCmd(
@@ -411,7 +408,6 @@ public struct LLBuildManifest {
411408
outputFileMapPath: AbsolutePath,
412409
prepareForIndexing: Bool
413410
) {
414-
assert(commands[name] == nil, "already had a command named '\(name)'")
415411
let tool = SwiftCompilerTool(
416412
inputs: inputs,
417413
outputs: outputs,
@@ -430,6 +426,6 @@ public struct LLBuildManifest {
430426
outputFileMapPath: outputFileMapPath,
431427
prepareForIndexing: prepareForIndexing
432428
)
433-
commands[name] = Command(name: name, tool: tool)
429+
addCommand(name: name, tool: tool)
434430
}
435431
}

0 commit comments

Comments
 (0)