1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import ArgumentParser
import Foundation
import SKCore
import struct TSCBasic.AbsolutePath
import class TSCBasic.Process
import class TSCUtility.PercentProgressAnimation
public struct ReduceFrontendCommand: AsyncParsableCommand {
public static let configuration: CommandConfiguration = CommandConfiguration(
commandName: "reduce-frontend",
abstract: "Reduce a single swift-frontend crash"
)
#if canImport(Darwin)
// Creating an NSPredicate from a string is not supported in corelibs-foundation.
@Option(
help: """
If the sourcekitd response matches this predicate, consider it as reproducing the issue.
sourcekitd crashes are always considered as reproducers.
The predicate is an NSPredicate. `stdout` and `stderr` are standard output and standard error of the \
swift-frontend execution, respectively.
Example:
- stderr CONTAINS "failed to produce diagnostic for expression"
"""
)
var predicate: String?
private var nsPredicate: NSPredicate? { predicate.map { NSPredicate(format: $0) } }
#else
private var nsPredicate: NSPredicate? { nil }
#endif
@Option(
name: .customLong("toolchain"),
help: """
The toolchain used to reduce the swift-frontend issue. \
If not specified, the toolchain is found in the same way that sourcekit-lsp finds it
"""
)
var toolchainOverride: String?
@Option(
parsing: .remaining,
help: """
The swift-frontend arguments that exhibit the issue that should be reduced.
"""
)
var frontendArgs: [String]
@MainActor
var toolchain: Toolchain? {
get async throws {
if let toolchainOverride {
return Toolchain(try AbsolutePath(validating: toolchainOverride))
}
let installPath = try AbsolutePath(validating: Bundle.main.bundlePath)
return await ToolchainRegistry(installPath: installPath).default
}
}
public init() {}
@MainActor
public func run() async throws {
guard let sourcekitd = try await toolchain?.sourcekitd else {
throw ReductionError("Unable to find sourcekitd.framework")
}
guard let swiftFrontend = try await toolchain?.swiftFrontend else {
throw ReductionError("Unable to find swift-frontend")
}
let progressBar = PercentProgressAnimation(
stream: stderrStreamConcurrencySafe,
header: "Reducing swift-frontend crash"
)
let executor = OutOfProcessSourceKitRequestExecutor(
sourcekitd: sourcekitd.asURL,
swiftFrontend: swiftFrontend,
reproducerPredicate: nsPredicate
)
defer {
progressBar.complete(success: true)
}
let reducedRequestInfo = try await reduceFrontendIssue(
frontendArgs: frontendArgs,
using: executor
) { progress, message in
progressBar.update(step: Int(progress * 100), total: 100, text: message)
}
print("Reduced compiler arguments:")
print(reducedRequestInfo.compilerArgs.joined(separator: " "))
print("")
print("Reduced file contents:")
print(reducedRequestInfo.fileContents)
}
}
|