Skip to content

Commit

Permalink
Fix running analyze on Xcode 11 build outputs (realm#2912)
Browse files Browse the repository at this point in the history
Xcode 11 started using _response files_ [0] to avoid exceeding command
line length limits. For SwiftLint's analyzer rules to find which
compiler arguments are needed for a given file being analyzed, we first
need to expand these response files to their contents.

This was ported over from jpsim/SourceKitten#613

[0]: swiftlang/swift#16362
  • Loading branch information
jpsim authored Oct 20, 2019
1 parent 3903127 commit 3706f31
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@

#### Bug Fixes

* None.
* Fix running analyzer rules on the output of builds performed with
Xcode 11 or later.
[JP Simard](https://github.com/jpsim)

## 0.35.0: Secondary Lint Trap

Expand Down
23 changes: 22 additions & 1 deletion Source/swiftlint/Helpers/CompilerArgumentsExtractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ struct CompilerArgumentsExtractor {
var compilerInvocations = [String]()
compilerLogs.enumerateLines { line, _ in
if let swiftcIndex = line.range(of: "swiftc ")?.upperBound, line.contains(" -module-name ") {
compilerInvocations.append(String(line[swiftcIndex...]))
let invocation = line[swiftcIndex...]
.components(separatedBy: " ")
.expandingResponseFiles
.joined(separator: " ")
compilerInvocations.append(invocation)
}
}

Expand Down Expand Up @@ -118,3 +122,20 @@ private func filter(arguments args: [String]) -> [String] {
return $0
}
}

private extension Array where Element == String {
/// Return the full list of compiler arguments, replacing any response files with their contents.
var expandingResponseFiles: [String] {
return flatMap { arg -> [String] in
guard arg.starts(with: "@") else {
return [arg]
}
let responseFile = String(arg.dropFirst())
return (try? String(contentsOf: URL(fileURLWithPath: responseFile))).flatMap {
$0.trimmingCharacters(in: .newlines)
.components(separatedBy: "\n")
.expandingResponseFiles
} ?? [arg]
}
}
}

0 comments on commit 3706f31

Please sign in to comment.