Skip to content

Commit 2e2e993

Browse files
authored
Merge pull request #2988 from keith/ks/improve-sanitizer-usage
Improve sanitizer usage
2 parents a5824b1 + 7995bc9 commit 2e2e993

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

Sources/Commands/Options.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,27 @@ enum BuildSystemKind: String, ExpressibleByArgument, CaseIterable {
9090
case xcode
9191
}
9292

93+
public extension Sanitizer {
94+
init(argument: String) throws {
95+
if let sanitizer = Sanitizer(rawValue: argument) {
96+
self = sanitizer
97+
return
98+
}
99+
100+
for sanitizer in Sanitizer.allCases where sanitizer.shortName == argument {
101+
self = sanitizer
102+
return
103+
}
104+
105+
throw ArgumentConversionError.custom("valid sanitizers: \(Sanitizer.formattedValues)")
106+
}
107+
108+
/// All sanitizer options in a comma separated string
109+
fileprivate static var formattedValues: String {
110+
return Sanitizer.allCases.map(\.rawValue).joined(separator: ", ")
111+
}
112+
}
113+
93114
public struct SwiftToolOptions: ParsableArguments {
94115
@OptionGroup()
95116
var buildFlagsGroup: BuildFlagsGroup
@@ -180,7 +201,7 @@ public struct SwiftToolOptions: ParsableArguments {
180201

181202
/// Which compile-time sanitizers should be enabled.
182203
@Option(name: .customLong("sanitize"),
183-
help: "Turn on runtime checks for erroneous behavior",
204+
help: "Turn on runtime checks for erroneous behavior, possible values: \(Sanitizer.formattedValues)",
184205
transform: { try Sanitizer(argument: $0) })
185206
var sanitizers: [Sanitizer] = []
186207

Sources/SPMBuildCore/Sanitizers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import TSCBasic
1212
import TSCUtility
1313

1414
/// Available runtime sanitizers.
15-
public enum Sanitizer: String, Encodable {
15+
public enum Sanitizer: String, Encodable, CaseIterable {
1616
case address
1717
case thread
1818
case undefined

Tests/BuildTests/BuildPlanTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2464,6 +2464,22 @@ final class BuildPlanTests: XCTestCase {
24642464
try testBinaryTargets(platform: "macos", arch: "arm64e", destinationTriple: arm64eTriple)
24652465
}
24662466

2467+
func testCreatingSanitizers() throws {
2468+
for sanitizer in Sanitizer.allCases {
2469+
XCTAssertEqual(sanitizer, try Sanitizer(argument: sanitizer.shortName))
2470+
}
2471+
}
2472+
2473+
func testInvalidSanitizer() throws {
2474+
do {
2475+
_ = try Sanitizer(argument: "invalid")
2476+
XCTFail("Should have failed to create Sanitizer")
2477+
} catch let error as ArgumentConversionError {
2478+
XCTAssertEqual(
2479+
error.description, "valid sanitizers: address, thread, undefined, scudo")
2480+
}
2481+
}
2482+
24672483
func testAddressSanitizer() throws {
24682484
try sanitizerTest(.address, expectedName: "address")
24692485
}

0 commit comments

Comments
 (0)