From 68eb63ba040fdaa79c573dd3e3d1411f608d8710 Mon Sep 17 00:00:00 2001 From: Myriad-Dreamin Date: Mon, 26 Jan 2026 17:27:13 +0800 Subject: [PATCH] ActiveFile --- include/Server/Server.h | 6 +++--- src/Server/Document.cpp | 4 ++-- src/Server/Server.cpp | 7 +++---- tests/unit/Server/ActiveFileManagerTests.cpp | 6 +++--- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/include/Server/Server.h b/include/Server/Server.h index 68149e90..b204f185 100644 --- a/include/Server/Server.h +++ b/include/Server/Server.h @@ -79,10 +79,10 @@ public: } /// Try get OpenFile from manager, default construct one if not exists. - [[nodiscard]] ActiveFile& get_or_add(llvm::StringRef path); + [[nodiscard]] ActiveFile get_or_add(llvm::StringRef path); /// Add a OpenFile to the manager. - ActiveFile& add(llvm::StringRef path, OpenFile file); + ActiveFile add(llvm::StringRef path, OpenFile file); [[nodiscard]] bool contains(llvm::StringRef path) const { return index.contains(path); @@ -97,7 +97,7 @@ public: } private: - ActiveFile& lru_put_impl(llvm::StringRef path, OpenFile file); + ActiveFile lru_put_impl(llvm::StringRef path, OpenFile file); private: /// The maximum size of the cache. diff --git a/src/Server/Document.cpp b/src/Server/Document.cpp index 07540a4c..65024a88 100644 --- a/src/Server/Document.cpp +++ b/src/Server/Document.cpp @@ -249,7 +249,7 @@ async::Task Server::build_pch(std::string file, std::string content) { auto info = database.lookup(file, options); auto bound = compute_preamble_bound(content); - auto& open_file = opening_files.get_or_add(file); + auto open_file = opening_files.get_or_add(file); /// Check update ... if(open_file->pch && !check_pch_update(content, bound, info, *open_file->pch)) { @@ -347,7 +347,7 @@ async::Task<> Server::build_ast(std::string path, std::string content) { } async::Task> Server::add_document(std::string path, std::string content) { - auto& openFile = opening_files.get_or_add(path); + auto openFile = opening_files.get_or_add(path); openFile->version += 1; openFile->content = content; diff --git a/src/Server/Server.cpp b/src/Server/Server.cpp index 5d8fe4a8..5e3bb3b7 100644 --- a/src/Server/Server.cpp +++ b/src/Server/Server.cpp @@ -9,8 +9,7 @@ namespace clice { -ActiveFileManager::ActiveFile& ActiveFileManager::lru_put_impl(llvm::StringRef path, - OpenFile file) { +ActiveFileManager::ActiveFile ActiveFileManager::lru_put_impl(llvm::StringRef path, OpenFile file) { /// If the file is not in the chain, create a new OpenFile. if(items.size() >= capability) { /// If the size exceeds the maximum size, remove the last element. @@ -26,7 +25,7 @@ ActiveFileManager::ActiveFile& ActiveFileManager::lru_put_impl(llvm::StringRef p return items.front().second; } -ActiveFileManager::ActiveFile& ActiveFileManager::get_or_add(llvm::StringRef path) { +ActiveFileManager::ActiveFile ActiveFileManager::get_or_add(llvm::StringRef path) { auto iter = index.find(path); if(iter == index.end()) { return lru_put_impl(path, OpenFile{}); @@ -37,7 +36,7 @@ ActiveFileManager::ActiveFile& ActiveFileManager::get_or_add(llvm::StringRef pat return iter->second->second; } -ActiveFileManager::ActiveFile& ActiveFileManager::add(llvm::StringRef path, OpenFile file) { +ActiveFileManager::ActiveFile ActiveFileManager::add(llvm::StringRef path, OpenFile file) { auto iter = index.find(path); if(iter == index.end()) { return lru_put_impl(path, std::move(file)); diff --git a/tests/unit/Server/ActiveFileManagerTests.cpp b/tests/unit/Server/ActiveFileManagerTests.cpp index afd21a38..bf85a307 100644 --- a/tests/unit/Server/ActiveFileManagerTests.cpp +++ b/tests/unit/Server/ActiveFileManagerTests.cpp @@ -32,12 +32,12 @@ TEST_CASE(LruAlgorithm) { ASSERT_EQ(actives.size(), 0U); - auto& first = actives.add("first", OpenFile{.version = 1}); + auto first = actives.add("first", OpenFile{.version = 1}); ASSERT_EQ(actives.size(), 1U); ASSERT_TRUE(actives.contains("first")); ASSERT_EQ(first->version, 1U); - auto& second = actives.add("second", OpenFile{.version = 2}); + auto second = actives.add("second", OpenFile{.version = 2}); ASSERT_EQ(actives.size(), 1U); } @@ -82,7 +82,7 @@ TEST_CASE(IteratorCheck) { std::string fpath = std::format("{}", i); OpenFile object{.version = i}; - auto& inseted = manager.add(fpath, std::move(object)); + auto inseted = manager.add(fpath, std::move(object)); std::optional new_added_entry = manager.get_or_add(fpath); ASSERT_TRUE(new_added_entry.has_value()); auto new_added = std::move(new_added_entry).value();