From 87914fcd1e60460891ce0db020c5ec80ce55ad41 Mon Sep 17 00:00:00 2001 From: Ben Langmuir Date: Mon, 3 Dec 2018 21:49:43 -0800 Subject: [PATCH] [compdb] Log only once per query on failure --- Sources/SKCore/CompilationDatabase.swift | 9 +++------ Sources/SKCore/CompilationDatabaseBuildSystem.swift | 10 ++++++++-- Tests/SKCoreTests/CompilationDatabaseTests.swift | 4 ++-- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/Sources/SKCore/CompilationDatabase.swift b/Sources/SKCore/CompilationDatabase.swift index 768ca771..b6bf93a6 100644 --- a/Sources/SKCore/CompilationDatabase.swift +++ b/Sources/SKCore/CompilationDatabase.swift @@ -58,13 +58,10 @@ public protocol CompilationDatabase { /// Loads the compilation database located in `directory`, if any. public func tryLoadCompilationDatabase( directory: AbsolutePath, - fileSystem: FileSystem = localFileSystem + _ fileSystem: FileSystem = localFileSystem ) -> CompilationDatabase? { - - return orLog("could not open compilation database for \(directory.asString)", level: .error) { - try JSONCompilationDatabase(directory: directory, fileSystem: fileSystem) - } // TODO: Support fixed compilation database (compile_flags.txt). + return try? JSONCompilationDatabase(directory: directory, fileSystem) } /// The JSON clang-compatible compilation database. @@ -128,7 +125,7 @@ extension JSONCompilationDatabase: Codable { } extension JSONCompilationDatabase { - public init(directory: AbsolutePath, fileSystem: FileSystem = localFileSystem) throws { + public init(directory: AbsolutePath, _ fileSystem: FileSystem = localFileSystem) throws { let path = directory.appending(component: "compile_commands.json") let bytes = try fileSystem.readFileContents(path) try bytes.withUnsafeData { data in diff --git a/Sources/SKCore/CompilationDatabaseBuildSystem.swift b/Sources/SKCore/CompilationDatabaseBuildSystem.swift index 313f5ad2..4960bcb7 100644 --- a/Sources/SKCore/CompilationDatabaseBuildSystem.swift +++ b/Sources/SKCore/CompilationDatabaseBuildSystem.swift @@ -10,6 +10,7 @@ // //===----------------------------------------------------------------------===// +import SKSupport import Basic import LanguageServerProtocol @@ -27,7 +28,7 @@ public final class CompilationDatabaseBuildSystem: BuildSettingsProvider { public init(projectRoot: AbsolutePath? = nil, fileSystem: FileSystem = localFileSystem) { self.fileSystem = fileSystem if let path = projectRoot { - self.compdb = tryLoadCompilationDatabase(directory: path, fileSystem: fileSystem) + self.compdb = tryLoadCompilationDatabase(directory: path, fileSystem) } } @@ -53,12 +54,17 @@ public final class CompilationDatabaseBuildSystem: BuildSettingsProvider { var dir = path while !dir.isRoot { dir = dir.parentDirectory - if let db = tryLoadCompilationDatabase(directory: dir, fileSystem: fileSystem) { + if let db = tryLoadCompilationDatabase(directory: dir, fileSystem) { compdb = db break } } } + + if compdb == nil { + log("could not open compilation database for \(path.asString)", level: .warning) + } + return compdb } } diff --git a/Tests/SKCoreTests/CompilationDatabaseTests.swift b/Tests/SKCoreTests/CompilationDatabaseTests.swift index 03f37933..44e015ec 100644 --- a/Tests/SKCoreTests/CompilationDatabaseTests.swift +++ b/Tests/SKCoreTests/CompilationDatabaseTests.swift @@ -169,7 +169,7 @@ final class CompilationDatabaseTests: XCTestCase { func testJSONCompilationDatabaseFromDirectory() { let fs = InMemoryFileSystem() try! fs.createDirectory(AbsolutePath("/a")) - XCTAssertNil(tryLoadCompilationDatabase(directory: AbsolutePath("/a"), fileSystem: fs)) + XCTAssertNil(tryLoadCompilationDatabase(directory: AbsolutePath("/a"), fs)) try! fs.writeFileContents(AbsolutePath("/a/compile_commands.json"), bytes: """ [ @@ -181,7 +181,7 @@ final class CompilationDatabaseTests: XCTestCase { ] """) - XCTAssertNotNil(tryLoadCompilationDatabase(directory: AbsolutePath("/a"), fileSystem: fs)) + XCTAssertNotNil(tryLoadCompilationDatabase(directory: AbsolutePath("/a"), fs)) } func testCompilationDatabaseBuildSystem() {