From 3f4a0d5370ae6c34afe180df96add3b8522f4af1 Mon Sep 17 00:00:00 2001 From: mattkae Date: Wed, 11 May 2022 09:23:58 -0400 Subject: initial commit --- elpa/irony-20220110.849/server/src/CompDBCache.cpp | 71 ++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 elpa/irony-20220110.849/server/src/CompDBCache.cpp (limited to 'elpa/irony-20220110.849/server/src/CompDBCache.cpp') diff --git a/elpa/irony-20220110.849/server/src/CompDBCache.cpp b/elpa/irony-20220110.849/server/src/CompDBCache.cpp new file mode 100644 index 0000000..f79ec8c --- /dev/null +++ b/elpa/irony-20220110.849/server/src/CompDBCache.cpp @@ -0,0 +1,71 @@ +#include "CompDBCache.h" + +#include + +#include + +CompDBCache::CompDBCache() + : db_(nullptr), mtime_(0) { +} + +CompDBCache::~CompDBCache() { + clear(); +} + +CXCompilationDatabase CompDBCache::fromDirectory(const std::string &buildDir, + CXCompilationDatabase_Error *error) { + assert(error != nullptr); + + const std::string jsonFilename = constructJsonDbFilename(buildDir); + const time_t mtime = modificationTime(jsonFilename); + + if (jsonFilename == filename_ && mtime != 0 && mtime == mtime_) { + // Using the cached compilation database. + // Just set the provided error code to indicate success. + *error = CXCompilationDatabase_NoError; + } else { + clear(); + + db_ = clang_CompilationDatabase_fromDirectory(buildDir.c_str(), error); + + if (mtime != 0 && *error == CXCompilationDatabase_NoError) { + // Successfully loaded a JSON compilation database. + // Cache the result. + filename_ = jsonFilename; + mtime_ = mtime; + } + } + + return db_; +} + +void CompDBCache::clear() { + if (db_) { + clang_CompilationDatabase_dispose(db_); + db_ = nullptr; + filename_.clear(); + mtime_ = 0; + } +} + +std::string CompDBCache::constructJsonDbFilename(const std::string &buildDir) const { + std::string ret = buildDir; + if (!buildDir.empty() && buildDir.back() != '/') + ret += '/'; + ret += "compile_commands.json"; + return ret; +} + +time_t CompDBCache::modificationTime(const std::string &filename) const { + time_t mtime = 0; +#ifdef _WIN32 + struct _stat st; + const int statRes = _stat(filename.c_str(), &st); +#else + struct stat st; + const int statRes = stat(filename.c_str(), &st); +#endif + if (statRes == 0) + mtime = st.st_mtime; + return mtime; +} -- cgit v1.2.1