xref: /llvm-project/mlir/include/mlir/Tools/lsp-server-support/CompilationDatabase.h (revision db791b278a414fb6df1acc1799adcf11d8fb9169)
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 MLIR_TOOLS_LSPSERVERSUPPORT_COMPILATIONDATABASE_H
17 #define 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 #include <vector>
24 
25 namespace mlir {
26 namespace lsp {
27 /// This class contains a collection of compilation information for files
28 /// provided to the language server, such as the available include directories.
29 /// This database acts as an aggregate in-memory form of compilation databases
30 /// used by the current language client. The textual form of a compilation
31 /// database is a YAML file containing documents of the following form:
32 ///
33 /// --- !FileInfo:
34 ///   filepath: <string> - Absolute file path of the file.
35 ///   includes: <string> - Semi-colon delimited list of include directories.
36 ///
37 class CompilationDatabase {
38 public:
39   /// Compilation information for a specific file within the database.
40   struct FileInfo {
41     FileInfo() = default;
FileInfoFileInfo42     FileInfo(std::vector<std::string> &&includeDirs)
43         : includeDirs(std::move(includeDirs)) {}
44 
45     /// The include directories available for the file.
46     std::vector<std::string> includeDirs;
47   };
48 
49   /// Construct a compilation database from the provided files containing YAML
50   /// descriptions of the database.
51   CompilationDatabase(ArrayRef<std::string> databases);
52 
53   /// Get the compilation information for the provided file.
54   const FileInfo &getFileInfo(StringRef filename) const;
55 
56 private:
57   /// Load the given database file into this database.
58   void loadDatabase(StringRef filename);
59 
60   /// A map of filename to file information for each known file within the
61   /// databases.
62   llvm::StringMap<FileInfo> files;
63 
64   /// A default file info that contains basic information for use by files that
65   /// weren't explicitly in the database.
66   FileInfo defaultFileInfo;
67 };
68 } // namespace lsp
69 } // namespace mlir
70 
71 #endif // MLIR_TOOLS_LSPSERVERSUPPORT_COMPILATIONDATABASE_H
72