diff --git a/clang-tools-extra/clangd/GlobalCompilationDatabase.h b/clang-tools-extra/clangd/GlobalCompilationDatabase.h --- a/clang-tools-extra/clangd/GlobalCompilationDatabase.h +++ b/clang-tools-extra/clangd/GlobalCompilationDatabase.h @@ -81,13 +81,15 @@ llvm::Optional getProjectInfo(PathRef File) const override; private: - /// Caches compilation databases loaded from directories. - struct CachedCDB { - std::string Path; // Not case-folded. - std::unique_ptr CDB = nullptr; - bool SentBroadcast = false; - }; - CachedCDB &getCDBInDirLocked(PathRef File) const; + class DirectoryCache; + // Keyed by possibly-case-folded directory path. + // We can hand out pointers as they're stable and entries are never removed. + mutable llvm::StringMap DirCaches; + mutable std::mutex Mutex; + mutable std::unique_ptr OnlyDirCache; + + std::vector + getDirectoryCaches(llvm::ArrayRef Dirs) const; struct CDBLookupRequest { PathRef FileName; @@ -95,21 +97,13 @@ bool ShouldBroadcast = false; }; struct CDBLookupResult { - tooling::CompilationDatabase *CDB = nullptr; + std::shared_ptr CDB; ProjectInfo PI; }; llvm::Optional lookupCDB(CDBLookupRequest Request) const; // Performs broadcast on governed files. void broadcastCDB(CDBLookupResult Res) const; - - mutable std::mutex Mutex; - // Keyed by possibly-case-folded directory path. - mutable llvm::StringMap CompilationDatabases; - - /// Used for command argument pointing to folder where compile_commands.json - /// is located. - llvm::Optional CompileCommandsDir; }; /// Extracts system include search path from drivers matching QueryDriverGlobs diff --git a/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp b/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp --- a/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp +++ b/clang-tools-extra/clangd/GlobalCompilationDatabase.cpp @@ -16,11 +16,13 @@ #include "llvm/ADT/None.h" #include "llvm/ADT/Optional.h" #include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/ScopeExit.h" #include "llvm/ADT/SmallString.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/FileUtilities.h" #include "llvm/Support/Path.h" #include "llvm/Support/Program.h" +#include #include #include #include @@ -58,10 +60,112 @@ return Cmd; } +// Loads and caches the CDB from a single directory. +// +// This class is threadsafe, which is to say we have independent locks for each +// directory we're searching for a CDB. +// Loading is deferred until first access. +// +// The DirectoryBasedCDB keeps a map from path => DirectoryCache. +// Typical usage is to: +// - 1) determine all the paths that might be searched +// - 2) acquire the map lock and get-or-create all the DirectoryCache entries +// - 3) release the map lock and query the caches as desired +// +// FIXME: this should revalidate the cache sometimes +// FIXME: IO should go through a VFS +class DirectoryBasedGlobalCompilationDatabase::DirectoryCache { + // Absolute canonical path that we're the cache for. (Not case-folded). + std::string Path; + + // True if we've looked for a CDB here and found none. + // (This makes it possible for get() to return without taking a lock) + // FIXME: this should have an expiry time instead of lasting forever. + std::atomic FinalizedNoCDB = {false}; + + // Guards following cache state. + std::mutex Mu; + // Has cache been filled from disk? FIXME: this should be an expiry time. + bool CachePopulated = false; + // Whether a new CDB has been loaded but not broadcast yet. + bool Dirty = false; + // Last loaded CDB, meaningful if CachePopulated is set. + // shared_ptr so we can overwrite this when callers are still using the CDB. + std::shared_ptr CDB; + +public: + DirectoryCache(llvm::StringRef Path) : Path(Path) { + assert(llvm::sys::path::is_absolute(Path)); + } + + // Get the CDB associated with this directory. + // ShouldBroadcast: + // - as input, signals whether the caller is willing to broadcast a + // newly-discovered CDB. (e.g. to trigger background indexing) + // - as output, signals whether the caller should do so. + // (If a new CDB is discovered and ShouldBroadcast is false, we mark the + // CDB as dirty and broadcast it next time we can). + std::shared_ptr + get(bool &ShouldBroadcast) { + // Fast path for common case without taking lock. + if (FinalizedNoCDB.load()) { + ShouldBroadcast = false; + return nullptr; + } + std::lock_guard Lock(Mu); + + // For now, we never actually attempt to revalidate a populated cache. + if (CachePopulated) { + ShouldBroadcast = Dirty; + Dirty = false; + return CDB; + } + assert(CDB == nullptr); + + load(); + CachePopulated = true; + + if (CDB) { + if (!ShouldBroadcast) + Dirty = true; + } else { + FinalizedNoCDB.store(true); + ShouldBroadcast = false; + } + return CDB; + } + + llvm::StringRef path() const { return Path; } + +private: + // Updates `CDB` from disk state. + void load() { + std::string Error; // ignored, because it's often "didn't find anything". + CDB = tooling::CompilationDatabase::loadFromDirectory(Path, Error); + if (!CDB) { + // Fallback: check for $src/build, the conventional CMake build root. + // Probe existence first to avoid each plugin doing IO if it doesn't + // exist. + llvm::SmallString<256> BuildDir(Path); + llvm::sys::path::append(BuildDir, "build"); + if (llvm::sys::fs::is_directory(BuildDir)) { + vlog("Found candidate build directory {0}", BuildDir); + CDB = tooling::CompilationDatabase::loadFromDirectory(BuildDir, Error); + } + } + if (CDB) + log("Loaded compilation database from {0}", Path); + else + vlog("No compilation database at {0}", Path); + } +}; + DirectoryBasedGlobalCompilationDatabase:: DirectoryBasedGlobalCompilationDatabase( - llvm::Optional CompileCommandsDir) - : CompileCommandsDir(std::move(CompileCommandsDir)) {} + llvm::Optional CompileCommandsDir) { + if (CompileCommandsDir) + OnlyDirCache = std::make_unique(*CompileCommandsDir); +} DirectoryBasedGlobalCompilationDatabase:: ~DirectoryBasedGlobalCompilationDatabase() = default; @@ -107,31 +211,21 @@ #endif } -DirectoryBasedGlobalCompilationDatabase::CachedCDB & -DirectoryBasedGlobalCompilationDatabase::getCDBInDirLocked(PathRef Dir) const { - // FIXME(ibiryukov): Invalidate cached compilation databases on changes - auto Key = maybeCaseFoldPath(Dir); - auto R = CompilationDatabases.try_emplace(Key); - if (R.second) { // Cache miss, try to load CDB. - CachedCDB &Entry = R.first->second; - std::string Error; - Entry.Path = std::string(Dir); - Entry.CDB = tooling::CompilationDatabase::loadFromDirectory(Dir, Error); - // Check for $src/build, the conventional CMake build root. - // Probe existence first to avoid each plugin doing IO if it doesn't exist. - if (!CompileCommandsDir && !Entry.CDB) { - llvm::SmallString<256> BuildDir = Dir; - llvm::sys::path::append(BuildDir, "build"); - if (llvm::sys::fs::is_directory(BuildDir)) { - vlog("Found candidate build directory {0}", BuildDir); - Entry.CDB = - tooling::CompilationDatabase::loadFromDirectory(BuildDir, Error); - } - } - if (Entry.CDB) - log("Loaded compilation database from {0}", Dir); - } - return R.first->second; +std::vector +DirectoryBasedGlobalCompilationDatabase::getDirectoryCaches( + llvm::ArrayRef Dirs) const { + std::vector FoldedDirs; + FoldedDirs.reserve(Dirs.size()); + for (const auto &Dir : Dirs) + FoldedDirs.push_back(maybeCaseFoldPath(Dir)); + + std::vector Ret; + Ret.reserve(Dirs.size()); + + std::lock_guard Lock(Mutex); + for (unsigned I = 0; I < Dirs.size(); ++I) + Ret.push_back(&DirCaches.try_emplace(FoldedDirs[I], Dirs[I]).first->second); + return Ret; } llvm::Optional @@ -140,40 +234,39 @@ assert(llvm::sys::path::is_absolute(Request.FileName) && "path must be absolute"); - bool ShouldBroadcast = false; - CDBLookupResult Result; - - { - std::lock_guard Lock(Mutex); - CachedCDB *Entry = nullptr; - if (CompileCommandsDir) { - Entry = &getCDBInDirLocked(*CompileCommandsDir); - } else { - // Traverse the canonical version to prevent false positives. i.e.: - // src/build/../a.cc can detect a CDB in /src/build if not canonicalized. - // FIXME(sammccall): this loop is hot, use a union-find-like structure. - actOnAllParentDirectories(removeDots(Request.FileName), - [&](PathRef Path) { - Entry = &getCDBInDirLocked(Path); - return Entry->CDB != nullptr; - }); + bool ShouldBroadcast = Request.ShouldBroadcast; + DirectoryCache *DirCache = nullptr; + std::shared_ptr CDB = nullptr; + if (OnlyDirCache) { + DirCache = OnlyDirCache.get(); + CDB = DirCache->get(ShouldBroadcast); + } else { + // Traverse the canonical version to prevent false positives. i.e.: + // src/build/../a.cc can detect a CDB in /src/build if not canonicalized. + std::string CanonicalPath = removeDots(Request.FileName); + std::vector SearchDirs; + actOnAllParentDirectories(CanonicalPath, [&](PathRef Path) { + SearchDirs.push_back(Path); + return false; + }); + for (DirectoryCache *Candidate : getDirectoryCaches(SearchDirs)) { + if ((CDB = Candidate->get(ShouldBroadcast))) { + DirCache = Candidate; + break; + } } + } - if (!Entry || !Entry->CDB) - return llvm::None; - - // Mark CDB as broadcasted to make sure discovery is performed once. - if (Request.ShouldBroadcast && !Entry->SentBroadcast) { - Entry->SentBroadcast = true; - ShouldBroadcast = true; - } + if (!CDB) + return llvm::None; - Result.CDB = Entry->CDB.get(); - Result.PI.SourceRoot = Entry->Path; - } + // Mark CDB as broadcasted to make sure discovery is performed once. + CDBLookupResult Result; + Result.CDB = std::move(CDB); + Result.PI.SourceRoot = DirCache->path().str(); - // FIXME: Maybe make the following part async, since this can block retrieval - // of compile commands. + // FIXME: Maybe make the following part async, since this can block + // retrieval of compile commands. if (ShouldBroadcast) broadcastCDB(Result); return Result; @@ -186,29 +279,32 @@ std::vector AllFiles = Result.CDB->getAllFiles(); // We assume CDB in CompileCommandsDir owns all of its entries, since we don't // perform any search in parent paths whenever it is set. - if (CompileCommandsDir) { - assert(*CompileCommandsDir == Result.PI.SourceRoot && + if (OnlyDirCache) { + assert(OnlyDirCache->path() == Result.PI.SourceRoot && "Trying to broadcast a CDB outside of CompileCommandsDir!"); OnCommandChanged.broadcast(std::move(AllFiles)); return; } + // Uniquify all parent directories of all files. llvm::StringMap DirectoryHasCDB; - { - std::lock_guard Lock(Mutex); - // Uniquify all parent directories of all files. - for (llvm::StringRef File : AllFiles) { - actOnAllParentDirectories(File, [&](PathRef Path) { - auto It = DirectoryHasCDB.try_emplace(Path); - // Already seen this path, and all of its parents. - if (!It.second) - return true; - - CachedCDB &Entry = getCDBInDirLocked(Path); - It.first->second = Entry.CDB != nullptr; - return pathEqual(Path, Result.PI.SourceRoot); - }); - } + std::vector FileAncestors; + for (llvm::StringRef File : AllFiles) { + actOnAllParentDirectories(File, [&](PathRef Path) { + auto It = DirectoryHasCDB.try_emplace(Path); + // Already seen this path, and all of its parents. + if (!It.second) + return true; + + FileAncestors.push_back(It.first->getKey()); + return pathEqual(Path, Result.PI.SourceRoot); + }); + } + // Work out which ones have CDBs in them. + for (DirectoryCache *Dir : getDirectoryCaches(FileAncestors)) { + bool ShouldBroadcast = false; + if (Dir->get(ShouldBroadcast)) + DirectoryHasCDB.find(Dir->path())->setValue(true); } std::vector GovernedFiles;