@@ -36,7 +36,6 @@ import WinSDK
36
36
///
37
37
/// See https://clang.llvm.org/docs/JSONCompilationDatabase.html
38
38
package struct CompilationDatabaseCompileCommand : Equatable , Codable {
39
-
40
39
/// The working directory for the compilation.
41
40
package var directory : String
42
41
@@ -103,84 +102,6 @@ package struct CompilationDatabaseCompileCommand: Equatable, Codable {
103
102
}
104
103
}
105
104
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
-
184
105
/// The JSON clang-compatible compilation database.
185
106
///
186
107
/// Example:
@@ -196,11 +117,9 @@ package struct FixedCompilationDatabase: CompilationDatabase, Equatable {
196
117
/// ```
197
118
///
198
119
/// 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 {
202
121
private var pathToCommands : [ DocumentURI : [ Int ] ] = [ : ]
203
- private var commands : [ CompilationDatabaseCompileCommand ] = [ ]
122
+ var commands : [ CompilationDatabaseCompileCommand ] = [ ]
204
123
205
124
package init ( _ commands: [ CompilationDatabaseCompileCommand ] = [ ] ) {
206
125
for command in commands {
@@ -211,27 +130,22 @@ package struct JSONCompilationDatabase: CompilationDatabase, Equatable, Codable
211
130
package init ( from decoder: Decoder ) throws {
212
131
var container = try decoder. unkeyedContainer ( )
213
132
while !container. isAtEnd {
214
- self . add ( try container. decode ( Command . self) )
133
+ self . add ( try container. decode ( CompilationDatabaseCompileCommand . self) )
215
134
}
216
135
}
217
136
218
137
/// Loads the compilation database located in `directory`, if any.
219
138
///
220
139
/// - 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)
223
142
try self . init ( file: path)
224
143
}
225
144
226
145
/// Loads the compilation database from `file`
227
146
/// - 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)
235
149
self = try JSONDecoder ( ) . decode ( JSONCompilationDatabase . self, from: data)
236
150
}
237
151
0 commit comments