diff options
Diffstat (limited to 'clangd/GlobalCompilationDatabase.h')
-rw-r--r-- | clangd/GlobalCompilationDatabase.h | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/clangd/GlobalCompilationDatabase.h b/clangd/GlobalCompilationDatabase.h index cf1e613f..b64028fc 100644 --- a/clangd/GlobalCompilationDatabase.h +++ b/clangd/GlobalCompilationDatabase.h @@ -85,6 +85,57 @@ private: /// is located. llvm::Optional<Path> CompileCommandsDir; }; + +/// A wrapper around GlobalCompilationDatabase that caches the compile commands. +/// Note that only results of getCompileCommand are cached. +class CachingCompilationDb : public GlobalCompilationDatabase { +public: + explicit CachingCompilationDb(const GlobalCompilationDatabase &InnerCDB); + + /// Gets compile command for \p File from cache or CDB if it's not in the + /// cache. + llvm::Optional<tooling::CompileCommand> + getCompileCommand(PathRef File) const override; + + /// Forwards to the inner CDB. Results of this function are not cached. + tooling::CompileCommand getFallbackCommand(PathRef File) const override; + + /// Removes an entry for \p File if it's present in the cache. + void invalidate(PathRef File); + + /// Removes all cached compile commands. + void clear(); + +private: + const GlobalCompilationDatabase &InnerCDB; + mutable std::mutex Mut; + mutable llvm::StringMap<llvm::Optional<tooling::CompileCommand>> + Cached; /* GUARDED_BY(Mut) */ +}; + +/// Gets compile args from an in-memory mapping based on a filepath. Typically +/// used by clients who provide the compile commands themselves. +class InMemoryCompilationDb : public GlobalCompilationDatabase { +public: + /// Gets compile command for \p File from the stored mapping. + llvm::Optional<tooling::CompileCommand> + getCompileCommand(PathRef File) const override; + + /// Sets the compilation command for a particular file. + /// + /// \returns True if the File had no compilation command before. + bool setCompilationCommandForFile(PathRef File, + tooling::CompileCommand CompilationCommand); + + /// Removes the compilation command for \p File if it's present in the + /// mapping. + void invalidate(PathRef File); + +private: + mutable std::mutex Mutex; + llvm::StringMap<tooling::CompileCommand> Commands; /* GUARDED_BY(Mut) */ +}; + } // namespace clangd } // namespace clang |