@@ -68,10 +68,57 @@ public func tryLoadCompilationDatabase(
68
68
directory: AbsolutePath ,
69
69
_ fileSystem: FileSystem = localFileSystem
70
70
) -> CompilationDatabase ? {
71
- // TODO: Support fixed compilation database (compile_flags.txt).
72
- return try ? JSONCompilationDatabase ( directory: directory, fileSystem)
71
+ return
72
+ ( try ? JSONCompilationDatabase ( directory: directory, fileSystem) )
73
+ ?? ( try ? FixedCompilationDatabase ( directory: directory, fileSystem) )
73
74
}
74
75
76
+ /// Fixed clang-compatible compilation database (compile_flags.txt).
77
+ ///
78
+ /// Each line in the file becomes a command line argument. Example:
79
+ /// ```
80
+ /// -xc++
81
+ /// -I
82
+ /// libwidget/include/
83
+ /// ```
84
+ ///
85
+ /// See https://clang.llvm.org/docs/JSONCompilationDatabase.html under Alternatives
86
+ public struct FixedCompilationDatabase : CompilationDatabase , Equatable {
87
+ public var allCommands : AnySequence < Command > { AnySequence ( [ ] ) }
88
+
89
+ private let fixedArgs : [ String ]
90
+ private let directory : String
91
+
92
+ public subscript( path: URL ) -> [ Command ] {
93
+ [ Command ( directory: directory, filename: path. path, commandLine: fixedArgs + [ path. path] ) ]
94
+ }
95
+ }
96
+
97
+ extension FixedCompilationDatabase {
98
+ public init ( directory: AbsolutePath , _ fileSystem: FileSystem = localFileSystem) throws {
99
+ let path = directory. appending ( component: " compile_flags.txt " )
100
+ try self . init ( file: path, fileSystem)
101
+ }
102
+
103
+ public init ( file: AbsolutePath , _ fileSystem: FileSystem = localFileSystem) throws {
104
+ self . directory = file. dirname
105
+ let bytes = try fileSystem. readFileContents ( file)
106
+
107
+ var fixedArgs : [ String ] = [ " clang " ]
108
+ try bytes. withUnsafeData { data in
109
+ guard let fileContents = String ( data: data, encoding: . utf8) else {
110
+ throw CompilationDatabaseDecodingError . fixedDatabaseDecordingError
111
+ }
112
+
113
+ fileContents. enumerateLines { line, _ in
114
+ fixedArgs. append ( line. trimmingCharacters ( in: . whitespacesAndNewlines) )
115
+ }
116
+ }
117
+ self . fixedArgs = fixedArgs
118
+ }
119
+ }
120
+
121
+
75
122
/// The JSON clang-compatible compilation database.
76
123
///
77
124
/// Example:
@@ -150,6 +197,7 @@ extension JSONCompilationDatabase {
150
197
151
198
enum CompilationDatabaseDecodingError : Error {
152
199
case missingCommandOrArguments
200
+ case fixedDatabaseDecordingError
153
201
}
154
202
155
203
extension CompilationDatabase . Command : Codable {
0 commit comments