0001    //
0002    //  ClangTranslationUnit.swift
0003    //  SourceKitten
0004    //
0005    //  Created by JP Simard on 2015-01-12.
0006    //  Copyright (c) 2015 SourceKitten. All rights reserved.
0007    //
0008    
0009    #if SWIFT_PACKAGE
0010    import Clang_C
0011    #endif
0012    import Foundation
0013    
0014    extension SequenceType where Generator.Element: Hashable {
0015        func distinct
ClangTranslationUnit.swift:65
            .distinct()
() -> [Generator.Element] { 0016 return Array(Set(self)) 0017 } 0018 } 0019 0020 extension SequenceType { 0021 func groupBy
ClangTranslationUnit.swift:67
            .groupBy { $0.location.file }
<T: Hashable>(keyFn: (Generator.Element) -> T) -> [T: [Generator.Element]] { 0022 var ret = Dictionary<T, [Generator.Element]>() 0023 for val in self { 0024 let key = keyFn(val) 0025 var d = ret[key] ?? [] 0026 d.append(val) 0027 ret[key] = d 0028 } 0029 return ret 0030 } 0031 } 0032 0033 extension Dictionary { 0034 init(_ pairs: [Element]) { 0035 self.init() 0036 for (k, v) in pairs { 0037 self[k] = v 0038 } 0039 } 0040 0041 func map
ClangTranslationUnit.swift:68
            .map { insertMarks($0) }
<OutValue>(@noescape transform: Value throws -> OutValue) rethrows -> [Key: OutValue] { 0042 return Dictionary<Key, OutValue>(try map { (k, v) in (k, try transform(v)) }) 0043 } 0044 } 0045 0046 /// Represents a group of CXTranslationUnits. 0047 public struct ClangTranslationUnit
ClangTranslationUnit.swift:92
extension ClangTranslationUnit: CustomStringConvertible {
{ 0048 /// Array of CXTranslationUnits. 0049 private let clangTranslationUnits
ClangTranslationUnit.swift:62
        clangTranslationUnits = headerFiles.map { clangIndex.open(file: $0, args: cStringCompilerArguments) }
ClangTranslationUnit.swift:63
        declarations = clangTranslationUnits
: [CXTranslationUnit] 0050 0051 public let declarations
ClangTranslationUnit.swift:63
        declarations = clangTranslationUnits
ClangTranslationUnit.swift:95
        return declarationsToJSON(declarations)
: [String: [SourceDeclaration]] 0052 0053 /** 0054 Create a ClangTranslationUnit by passing Objective-C header files and clang compiler arguments. 0055 0056 - parameter headerFiles: Objective-C header files to document. 0057 - parameter compilerArguments: Clang compiler arguments. 0058 */ 0059 public init
ClangTranslationUnit.swift:86
        self.init(headerFiles: headerFiles, compilerArguments: clangArguments)
(headerFiles: [String], compilerArguments: [String]) { 0060 let cStringCompilerArguments = compilerArguments.map { ($0 as NSString).UTF8String } 0061 let clangIndex = ClangIndex() 0062 clangTranslationUnits = headerFiles.map { clangIndex.open(file: $0, args: cStringCompilerArguments) } 0063 declarations = clangTranslationUnits 0064 .flatMap { $0.cursor().flatMap(SourceDeclaration.init) } 0065 .distinct() 0066 .sort() 0067 .groupBy { $0.location.file } 0068 .map { insertMarks($0) } 0069 } 0070 0071 /** 0072 Failable initializer to create a ClangTranslationUnit by passing Objective-C header files and 0073 `xcodebuild` arguments. Optionally pass in a `path`. 0074 0075 - parameter headerFiles: Objective-C header files to document. 0076 - parameter xcodeBuildArguments: The arguments necessary pass in to `xcodebuild` to link these header files. 0077 - parameter path: Path to run `xcodebuild` from. Uses current path by default. 0078 */ 0079 public init?(headerFiles: [String], xcodeBuildArguments: [String], inPath path: String = NSFileManager.defaultManager().currentDirectoryPath) { 0080 let xcodeBuildOutput = runXcodeBuild(xcodeBuildArguments + ["-dry-run"], inPath: path) ?? "" 0081 guard let clangArguments = parseCompilerArguments(xcodeBuildOutput, language: .ObjC, moduleName: nil) else { 0082 fputs("could not parse compiler arguments\n", stderr) 0083 fputs("\(xcodeBuildOutput)\n", stderr) 0084 return nil 0085 } 0086 self.init(headerFiles: headerFiles, compilerArguments: clangArguments) 0087 } 0088 } 0089 0090 // MARK: CustomStringConvertible 0091 0092 extension ClangTranslationUnit: CustomStringConvertible { 0093 /// A textual JSON representation of `ClangTranslationUnit`. 0094 public var description: String { 0095 return declarationsToJSON(declarations) 0096 } 0097 } 0098