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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
//===----------------------------------------------------------------------===//
//
// 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 Foundation
import LSPLogging
// MARK: - Entry point
extension RequestInfo {
@MainActor
func reduceCommandLineArguments(
using executor: SourceKitRequestExecutor,
progressUpdate: (_ progress: Double, _ message: String) -> Void
) async throws -> RequestInfo {
try await withoutActuallyEscaping(progressUpdate) { progressUpdate in
let reducer = CommandLineArgumentReducer(sourcekitdExecutor: executor, progressUpdate: progressUpdate)
return try await reducer.run(initialRequestInfo: self)
}
}
}
// MARK: - FileProducer
/// Reduces the compiler arguments needed to reproduce a sourcekitd crash.
fileprivate class CommandLineArgumentReducer {
/// The executor that is used to run a sourcekitd request and check whether it
/// still crashes.
private let sourcekitdExecutor: SourceKitRequestExecutor
/// A callback to be called when the reducer has made progress reducing the request
private let progressUpdate: (_ progress: Double, _ message: String) -> Void
/// The number of command line arguments when the reducer was started.
private var initialCommandLineCount: Int = 0
init(
sourcekitdExecutor: SourceKitRequestExecutor,
progressUpdate: @escaping (_ progress: Double, _ message: String) -> Void
) {
self.sourcekitdExecutor = sourcekitdExecutor
self.progressUpdate = progressUpdate
}
@MainActor
func run(initialRequestInfo: RequestInfo) async throws -> RequestInfo {
var requestInfo = initialRequestInfo
requestInfo = try await reduce(initialRequestInfo: requestInfo, simultaneousRemove: 10)
requestInfo = try await reduce(initialRequestInfo: requestInfo, simultaneousRemove: 1)
return requestInfo
}
/// Reduce the command line arguments of the given `RequestInfo`.
///
/// If `simultaneousRemove` is set, the reducer will try to remove that many arguments at once. This is useful to
/// quickly remove multiple arguments from the request.
@MainActor
private func reduce(initialRequestInfo: RequestInfo, simultaneousRemove: Int) async throws -> RequestInfo {
guard initialRequestInfo.compilerArgs.count > simultaneousRemove else {
// Trying to remove more command line arguments than we have. This isn't going to work.
return initialRequestInfo
}
var requestInfo = initialRequestInfo
self.initialCommandLineCount = requestInfo.compilerArgs.count
var argumentIndexToRemove = requestInfo.compilerArgs.count - 1
while argumentIndexToRemove + 1 >= simultaneousRemove {
defer {
// argumentIndexToRemove can become negative by being decremented in the code below
let progress = 1 - (Double(max(argumentIndexToRemove, 0)) / Double(initialCommandLineCount))
progressUpdate(progress, "Reduced compiler arguments to \(requestInfo.compilerArgs.count)")
}
var numberOfArgumentsToRemove = simultaneousRemove
// If the argument is preceded by -Xswiftc or -Xcxx, we need to remove the `-X` flag as well.
if requestInfo.compilerArgs[safe: argumentIndexToRemove - numberOfArgumentsToRemove]?.hasPrefix("-X") ?? false {
numberOfArgumentsToRemove += 1
}
let rangeToRemove = (argumentIndexToRemove - numberOfArgumentsToRemove + 1)...argumentIndexToRemove
if let reduced = try await tryRemoving(rangeToRemove, from: requestInfo) {
requestInfo = reduced
argumentIndexToRemove -= numberOfArgumentsToRemove
continue
}
// If removing the argument failed and the argument is preceded by an argument starting with `-`, try removing
// that as well. E.g. removing `-F` followed by a search path.
if requestInfo.compilerArgs[safe: argumentIndexToRemove - numberOfArgumentsToRemove]?.hasPrefix("-") ?? false {
numberOfArgumentsToRemove += 1
// If the argument is preceded by -Xswiftc or -Xcxx, we need to remove the `-X` flag as well.
if requestInfo.compilerArgs[safe: argumentIndexToRemove - numberOfArgumentsToRemove]?.hasPrefix("-X") ?? false {
numberOfArgumentsToRemove += 1
}
let rangeToRemove = (argumentIndexToRemove - numberOfArgumentsToRemove + 1)...argumentIndexToRemove
if let reduced = try await tryRemoving(rangeToRemove, from: requestInfo) {
requestInfo = reduced
argumentIndexToRemove -= numberOfArgumentsToRemove
continue
}
}
argumentIndexToRemove -= simultaneousRemove
}
return requestInfo
}
@MainActor
private func tryRemoving(
_ argumentsToRemove: ClosedRange<Int>,
from requestInfo: RequestInfo
) async throws -> RequestInfo? {
logger.debug("Try removing the following compiler arguments:\n\(requestInfo.compilerArgs[argumentsToRemove])")
var reducedRequestInfo = requestInfo
reducedRequestInfo.compilerArgs.removeSubrange(argumentsToRemove)
let result = try await sourcekitdExecutor.run(request: reducedRequestInfo)
if case .reproducesIssue = result {
logger.debug("Reduction successful")
return reducedRequestInfo
} else {
// The reduced request did not crash. We did not find a reduced test case, so return `nil`.
logger.debug("Reduction did not reproduce the issue")
return nil
}
}
}
fileprivate extension Array {
/// Access index in the array if it's in bounds or return `nil` if `index` is outside of the array's bounds.
subscript(safe index: Int) -> Element? {
if index < 0 || index >= count {
return nil
}
return self[index]
}
}
|