Line data Source code
1 : //===--- ClangdUnitStore.h - A container of CppFiles -------------*-C++-*-===//
2 : //
3 : // The LLVM Compiler Infrastructure
4 : //
5 : // This file is distributed under the University of Illinois Open Source
6 : // License. See LICENSE.TXT for details.
7 : //
8 : //===---------------------------------------------------------------------===//
9 :
10 : #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDUNITSTORE_H
11 : #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_CLANGDUNITSTORE_H
12 :
13 : #include "ClangdUnit.h"
14 : #include "GlobalCompilationDatabase.h"
15 : #include "Logger.h"
16 : #include "Path.h"
17 : #include "clang/Tooling/CompilationDatabase.h"
18 : #include <mutex>
19 :
20 : namespace clang {
21 : namespace clangd {
22 :
23 : class Logger;
24 :
25 : /// Thread-safe mapping from FileNames to CppFile.
26 0 : class CppFileCollection {
27 : public:
28 : /// \p ASTCallback is called when a file is parsed synchronously. This should
29 : /// not be expensive since it blocks diagnostics.
30 : explicit CppFileCollection(bool StorePreamblesInMemory,
31 : std::shared_ptr<PCHContainerOperations> PCHs,
32 : ASTParsedCallback ASTCallback)
33 : : ASTCallback(std::move(ASTCallback)), PCHs(std::move(PCHs)),
34 : StorePreamblesInMemory(StorePreamblesInMemory) {}
35 :
36 : std::shared_ptr<CppFile> getOrCreateFile(PathRef File) {
37 : std::lock_guard<std::mutex> Lock(Mutex);
38 : auto It = OpenedFiles.find(File);
39 : if (It == OpenedFiles.end()) {
40 : It = OpenedFiles
41 : .try_emplace(File, CppFile::Create(File, StorePreamblesInMemory,
42 : PCHs, ASTCallback))
43 : .first;
44 : }
45 : return It->second;
46 : }
47 :
48 : std::shared_ptr<CppFile> getFile(PathRef File) const {
49 : std::lock_guard<std::mutex> Lock(Mutex);
50 : auto It = OpenedFiles.find(File);
51 : if (It == OpenedFiles.end())
52 : return nullptr;
53 : return It->second;
54 : }
55 :
56 : /// Removes a CppFile, stored for \p File, if it's inside collection and
57 : /// returns it.
58 : std::shared_ptr<CppFile> removeIfPresent(PathRef File);
59 :
60 : /// Gets used memory for each of the stored files.
61 : std::vector<std::pair<Path, std::size_t>> getUsedBytesPerFile() const;
62 :
63 : private:
64 : mutable std::mutex Mutex;
65 : llvm::StringMap<std::shared_ptr<CppFile>> OpenedFiles;
66 : ASTParsedCallback ASTCallback;
67 : std::shared_ptr<PCHContainerOperations> PCHs;
68 : bool StorePreamblesInMemory;
69 : };
70 : } // namespace clangd
71 : } // namespace clang
72 :
73 : #endif
|