xref: /freebsd/contrib/llvm-project/clang/lib/Serialization/ModuleCache.cpp (revision e64bea71c21eb42e97aa615188ba91f6cce0d36d)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "clang/Serialization/ModuleCache.h"
10 
11 #include "clang/Serialization/InMemoryModuleCache.h"
12 #include "clang/Serialization/ModuleFile.h"
13 #include "llvm/Support/FileSystem.h"
14 #include "llvm/Support/LockFileManager.h"
15 #include "llvm/Support/Path.h"
16 
17 using namespace clang;
18 
19 namespace {
20 class CrossProcessModuleCache : public ModuleCache {
21   InMemoryModuleCache InMemory;
22 
23 public:
prepareForGetLock(StringRef ModuleFilename)24   void prepareForGetLock(StringRef ModuleFilename) override {
25     // FIXME: Do this in LockFileManager and only if the directory doesn't
26     // exist.
27     StringRef Dir = llvm::sys::path::parent_path(ModuleFilename);
28     llvm::sys::fs::create_directories(Dir);
29   }
30 
31   std::unique_ptr<llvm::AdvisoryLock>
getLock(StringRef ModuleFilename)32   getLock(StringRef ModuleFilename) override {
33     return std::make_unique<llvm::LockFileManager>(ModuleFilename);
34   }
35 
getModuleTimestamp(StringRef ModuleFilename)36   std::time_t getModuleTimestamp(StringRef ModuleFilename) override {
37     std::string TimestampFilename =
38         serialization::ModuleFile::getTimestampFilename(ModuleFilename);
39     llvm::sys::fs::file_status Status;
40     if (llvm::sys::fs::status(TimestampFilename, Status) != std::error_code{})
41       return 0;
42     return llvm::sys::toTimeT(Status.getLastModificationTime());
43   }
44 
updateModuleTimestamp(StringRef ModuleFilename)45   void updateModuleTimestamp(StringRef ModuleFilename) override {
46     // Overwrite the timestamp file contents so that file's mtime changes.
47     std::error_code EC;
48     llvm::raw_fd_ostream OS(
49         serialization::ModuleFile::getTimestampFilename(ModuleFilename), EC,
50         llvm::sys::fs::OF_TextWithCRLF);
51     if (EC)
52       return;
53     OS << "Timestamp file\n";
54     OS.close();
55     OS.clear_error(); // Avoid triggering a fatal error.
56   }
57 
getInMemoryModuleCache()58   InMemoryModuleCache &getInMemoryModuleCache() override { return InMemory; }
getInMemoryModuleCache() const59   const InMemoryModuleCache &getInMemoryModuleCache() const override {
60     return InMemory;
61   }
62 };
63 } // namespace
64 
createCrossProcessModuleCache()65 IntrusiveRefCntPtr<ModuleCache> clang::createCrossProcessModuleCache() {
66   return llvm::makeIntrusiveRefCnt<CrossProcessModuleCache>();
67 }
68