1 //===- CompilationDatabase.h - LSP Compilation Database ---------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains a definition of a generic compilation database that can be 10 // used to provide information about the compilation of a given source file. It 11 // contains generic components, leaving more complex interpretation to the 12 // specific language servers that consume it. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #ifndef LIB_MLIR_TOOLS_LSPSERVERSUPPORT_COMPILATIONDATABASE_H_ 17 #define LIB_MLIR_TOOLS_LSPSERVERSUPPORT_COMPILATIONDATABASE_H_ 18 19 #include "mlir/Support/LLVM.h" 20 #include "llvm/ADT/StringMap.h" 21 #include <memory> 22 #include <string> 23 24 namespace mlir { 25 namespace lsp { 26 /// This class contains a collection of compilation information for files 27 /// provided to the language server, such as the available include directories. 28 /// This database acts as an aggregate in-memory form of compilation databases 29 /// used by the current language client. The textual form of a compilation 30 /// database is a YAML file containing documents of the following form: 31 /// 32 /// --- !FileInfo: 33 /// filepath: <string> - Absolute file path of the file. 34 /// includes: <string> - Semi-colon delimited list of include directories. 35 /// 36 class CompilationDatabase { 37 public: 38 /// Compilation information for a specific file within the database. 39 struct FileInfo { 40 FileInfo() = default; FileInfoFileInfo41 FileInfo(std::vector<std::string> &&includeDirs) 42 : includeDirs(std::move(includeDirs)) {} 43 44 /// The include directories available for the file. 45 std::vector<std::string> includeDirs; 46 }; 47 48 /// Construct a compilation database from the provided files containing YAML 49 /// descriptions of the database. 50 CompilationDatabase(ArrayRef<std::string> databases); 51 52 /// Get the compilation information for the provided file. 53 const FileInfo &getFileInfo(StringRef filename) const; 54 55 private: 56 /// Load the given database file into this database. 57 void loadDatabase(StringRef filename); 58 59 /// A map of filename to file information for each known file within the 60 /// databases. 61 llvm::StringMap<FileInfo> files; 62 63 /// A default file info that contains basic information for use by files that 64 /// weren't explicitly in the database. 65 FileInfo defaultFileInfo; 66 }; 67 } // namespace lsp 68 } // namespace mlir 69 70 #endif // LIB_MLIR_TOOLS_LSPSERVERSUPPORT_COMPILATIONDATABASE_H_ 71