@@ -36,7 +36,6 @@ import WinSDK
3636///
3737/// See https://clang.llvm.org/docs/JSONCompilationDatabase.html
3838package struct CompilationDatabaseCompileCommand : Equatable , Codable {
39-
4039 /// The working directory for the compilation.
4140 package var directory : String
4241
@@ -103,84 +102,6 @@ package struct CompilationDatabaseCompileCommand: Equatable, Codable {
103102 }
104103}
105104
106- /// A clang-compatible compilation database.
107- ///
108- /// See https://clang.llvm.org/docs/JSONCompilationDatabase.html
109- package protocol CompilationDatabase {
110- typealias Command = CompilationDatabaseCompileCommand
111- subscript( _ uri: DocumentURI ) -> [ Command ] { get }
112- var sourceItems : [ SourceItem ] { get }
113- }
114-
115- /// Loads a compilation database from `file`.
116- package func tryLoadCompilationDatabase(
117- file: URL
118- ) -> CompilationDatabase ? {
119- orLog ( " Failed to load compilation database " ) { ( ) -> CompilationDatabase ? in
120- switch file. lastPathComponent {
121- case JSONCompilationDatabase . dbName:
122- return try JSONCompilationDatabase ( file: file)
123- case FixedCompilationDatabase . dbName:
124- return try FixedCompilationDatabase ( file: file)
125- default :
126- return nil
127- }
128- }
129- }
130-
131- /// Fixed clang-compatible compilation database (compile_flags.txt).
132- ///
133- /// Each line in the file becomes a command line argument. Example:
134- /// ```
135- /// -xc++
136- /// -I
137- /// libwidget/include/
138- /// ```
139- ///
140- /// See https://clang.llvm.org/docs/JSONCompilationDatabase.html under Alternatives
141- package struct FixedCompilationDatabase : CompilationDatabase , Equatable {
142- package static let dbName : String = " compile_flags.txt "
143-
144- private let fixedArgs : [ String ]
145- private let directory : String
146-
147- package subscript( path: DocumentURI ) -> [ CompilationDatabaseCompileCommand ] {
148- [ Command ( directory: directory, filename: path. pseudoPath, commandLine: fixedArgs + [ path. pseudoPath] ) ]
149- }
150-
151- package var sourceItems : [ SourceItem ] {
152- return [
153- SourceItem ( uri: URI ( filePath: directory, isDirectory: true ) , kind: . directory, generated: false )
154- ]
155- }
156-
157- /// Loads the compilation database located in `directory`, if any.
158- /// - Returns: `nil` if `compile_flags.txt` was not found
159- package init ? ( directory: URL ) throws {
160- let path = directory. appendingPathComponent ( Self . dbName)
161- try self . init ( file: path)
162- }
163-
164- /// Loads the compilation database from `file`
165- /// - Returns: `nil` if the file does not exist
166- package init ? ( file: URL ) throws {
167- self . directory = try file. deletingLastPathComponent ( ) . filePath
168-
169- let fileContents : String
170- do {
171- fileContents = try String ( contentsOf: file, encoding: . utf8)
172- } catch {
173- return nil
174- }
175-
176- var fixedArgs : [ String ] = [ " clang " ]
177- fileContents. enumerateLines { line, _ in
178- fixedArgs. append ( line. trimmingCharacters ( in: . whitespacesAndNewlines) )
179- }
180- self . fixedArgs = fixedArgs
181- }
182- }
183-
184105/// The JSON clang-compatible compilation database.
185106///
186107/// Example:
@@ -196,11 +117,9 @@ package struct FixedCompilationDatabase: CompilationDatabase, Equatable {
196117/// ```
197118///
198119/// See https://clang.llvm.org/docs/JSONCompilationDatabase.html
199- package struct JSONCompilationDatabase : CompilationDatabase , Equatable , Codable {
200- package static let dbName : String = " compile_commands.json "
201-
120+ package struct JSONCompilationDatabase : Equatable , Codable {
202121 private var pathToCommands : [ DocumentURI : [ Int ] ] = [ : ]
203- private var commands : [ CompilationDatabaseCompileCommand ] = [ ]
122+ var commands : [ CompilationDatabaseCompileCommand ] = [ ]
204123
205124 package init ( _ commands: [ CompilationDatabaseCompileCommand ] = [ ] ) {
206125 for command in commands {
@@ -211,27 +130,22 @@ package struct JSONCompilationDatabase: CompilationDatabase, Equatable, Codable
211130 package init ( from decoder: Decoder ) throws {
212131 var container = try decoder. unkeyedContainer ( )
213132 while !container. isAtEnd {
214- self . add ( try container. decode ( Command . self) )
133+ self . add ( try container. decode ( CompilationDatabaseCompileCommand . self) )
215134 }
216135 }
217136
218137 /// Loads the compilation database located in `directory`, if any.
219138 ///
220139 /// - Returns: `nil` if `compile_commands.json` was not found
221- package init ? ( directory: URL ) throws {
222- let path = directory. appendingPathComponent ( Self . dbName)
140+ package init ( directory: URL ) throws {
141+ let path = directory. appendingPathComponent ( JSONCompilationDatabaseBuildSystem . dbName)
223142 try self . init ( file: path)
224143 }
225144
226145 /// Loads the compilation database from `file`
227146 /// - Returns: `nil` if the file does not exist
228- package init ? ( file: URL ) throws {
229- let data : Data
230- do {
231- data = try Data ( contentsOf: file)
232- } catch {
233- return nil
234- }
147+ package init ( file: URL ) throws {
148+ let data = try Data ( contentsOf: file)
235149 self = try JSONDecoder ( ) . decode ( JSONCompilationDatabase . self, from: data)
236150 }
237151
0 commit comments