1 //===--- ConfigProvider.cpp - Loading of user configuration ---------------===//
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 "ConfigProvider.h"
10 #include "Config.h"
11 #include "ConfigFragment.h"
12 #include "support/ThreadsafeFS.h"
13 #include "support/Trace.h"
14 #include "llvm/ADT/StringMap.h"
15 #include "llvm/Support/Path.h"
16 #include <mutex>
17 
18 namespace clang {
19 namespace clangd {
20 namespace config {
21 
22 // Threadsafe cache around reading a YAML config file from disk.
23 class FileConfigCache {
24   std::mutex Mu;
25   llvm::SmallVector<CompiledFragment, 1> CachedValue;
26   llvm::sys::TimePoint<> MTime = {};
27   unsigned Size = -1;
28 
29   void updateCacheLocked(const llvm::vfs::Status &Stat,
30                          llvm::vfs::FileSystem &FS, DiagnosticCallback DC) {
31     if (Size == Stat.getSize() && MTime == Stat.getLastModificationTime())
32       return; // Already valid.
33 
34     Size = Stat.getSize();
35     MTime = Stat.getLastModificationTime();
36     CachedValue.clear();
37 
38     auto Buf = FS.getBufferForFile(Path);
39     // If stat() succeeds but we failed to read, don't cache failure.
40     if (!Buf) {
41       Size = -1;
42       MTime = {};
43       return;
44     }
45 
46     // If file changed between stat and open, we don't know its mtime.
47     // For simplicity, don't cache the value in this case (use a bad key).
48     if (Buf->get()->getBufferSize() != Size) {
49       Size = -1;
50       MTime = {};
51     }
52 
53     // Finally parse and compile the actual fragments.
54     for (auto &Fragment :
55          Fragment::parseYAML(Buf->get()->getBuffer(), Path, DC))
56       CachedValue.push_back(std::move(Fragment).compile(DC));
57   }
58 
59 public:
60   // Must be set before the cache is used. Not a constructor param to allow
61   // computing ancestor-relative paths to be deferred.
62   std::string Path;
63 
64   // Retrieves up-to-date config fragments from disk.
65   // A cached result may be reused if the mtime and size are unchanged.
66   // (But several concurrent read()s can miss the cache after a single change).
67   // Future performance ideas:
68   // - allow caches to be reused based on short elapsed walltime
69   // - allow latency-sensitive operations to skip revalidating the cache
70   void read(const ThreadsafeFS &TFS, DiagnosticCallback DC,
71             std::vector<CompiledFragment> &Out) {
72     assert(llvm::sys::path::is_absolute(Path));
73     auto FS = TFS.view(/*CWD=*/llvm::None);
74     auto Stat = FS->status(Path);
75     if (!Stat || !Stat->isRegularFile()) {
76       // No point taking the lock to clear the cache. We know what to return.
77       // If the file comes back we'll invalidate the cache at that point.
78       return;
79     }
80 
81     std::lock_guard<std::mutex> Lock(Mu);
82     updateCacheLocked(*Stat, *FS, DC);
83     llvm::copy(CachedValue, std::back_inserter(Out));
84   }
85 };
86 
87 std::unique_ptr<Provider> Provider::fromYAMLFile(llvm::StringRef AbsPath,
88                                                  const ThreadsafeFS &FS) {
89   class AbsFileProvider : public Provider {
90     mutable FileConfigCache Cache; // threadsafe
91     const ThreadsafeFS &FS;
92 
93     std::vector<CompiledFragment>
94     getFragments(const Params &P, DiagnosticCallback DC) const override {
95       std::vector<CompiledFragment> Result;
96       Cache.read(FS, DC, Result);
97       return Result;
98     };
99 
100   public:
101     AbsFileProvider(llvm::StringRef Path, const ThreadsafeFS &FS) : FS(FS) {
102       assert(llvm::sys::path::is_absolute(Path));
103       Cache.Path = Path.str();
104     }
105   };
106 
107   return std::make_unique<AbsFileProvider>(AbsPath, FS);
108 }
109 
110 std::unique_ptr<Provider>
111 Provider::fromAncestorRelativeYAMLFiles(llvm::StringRef RelPath,
112                                         const ThreadsafeFS &FS) {
113   class RelFileProvider : public Provider {
114     std::string RelPath;
115     const ThreadsafeFS &FS;
116 
117     mutable std::mutex Mu;
118     // Keys are the ancestor directory, not the actual config path within it.
119     // We only insert into this map, so pointers to values are stable forever.
120     // Mutex guards the map itself, not the values (which are threadsafe).
121     mutable llvm::StringMap<FileConfigCache> Cache;
122 
123     std::vector<CompiledFragment>
124     getFragments(const Params &P, DiagnosticCallback DC) const override {
125       namespace path = llvm::sys::path;
126 
127       if (P.Path.empty())
128         return {};
129 
130       // Compute absolute paths to all ancestors (substrings of P.Path).
131       llvm::StringRef Parent = path::parent_path(P.Path);
132       llvm::SmallVector<llvm::StringRef, 8> Ancestors;
133       for (auto I = path::begin(Parent, path::Style::posix),
134                 E = path::end(Parent);
135            I != E; ++I) {
136         // Avoid weird non-substring cases like phantom "." components.
137         // In practice, Component is a substring for all "normal" ancestors.
138         if (I->end() < Parent.begin() && I->end() > Parent.end())
139           continue;
140         Ancestors.emplace_back(Parent.begin(), I->end() - Parent.begin());
141       }
142       // Ensure corresponding cache entries exist in the map.
143       llvm::SmallVector<FileConfigCache *, 8> Caches;
144       {
145         std::lock_guard<std::mutex> Lock(Mu);
146         for (llvm::StringRef Ancestor : Ancestors) {
147           auto R = Cache.try_emplace(Ancestor);
148           // Assemble the actual config file path only once.
149           if (R.second) {
150             llvm::SmallString<256> ConfigPath = Ancestor;
151             path::append(ConfigPath, RelPath);
152             R.first->second.Path = ConfigPath.str().str();
153           }
154           Caches.push_back(&R.first->second);
155         }
156       }
157       // Finally query each individual file.
158       // This will take a (per-file) lock for each file that actually exists.
159       std::vector<CompiledFragment> Result;
160       for (FileConfigCache *Cache : Caches)
161         Cache->read(FS, DC, Result);
162       return Result;
163     };
164 
165   public:
166     RelFileProvider(llvm::StringRef RelPath, const ThreadsafeFS &FS)
167         : RelPath(RelPath), FS(FS) {
168       assert(llvm::sys::path::is_relative(RelPath));
169     }
170   };
171 
172   return std::make_unique<RelFileProvider>(RelPath, FS);
173 }
174 
175 std::unique_ptr<Provider>
176 Provider::combine(std::vector<std::unique_ptr<Provider>> Providers) {
177   struct CombinedProvider : Provider {
178     std::vector<std::unique_ptr<Provider>> Providers;
179 
180     std::vector<CompiledFragment>
181     getFragments(const Params &P, DiagnosticCallback DC) const override {
182       std::vector<CompiledFragment> Result;
183       for (const auto &Provider : Providers) {
184         for (auto &Fragment : Provider->getFragments(P, DC))
185           Result.push_back(std::move(Fragment));
186       }
187       return Result;
188     }
189   };
190   auto Result = std::make_unique<CombinedProvider>();
191   Result->Providers = std::move(Providers);
192   return Result;
193 }
194 
195 Config Provider::getConfig(const Params &P, DiagnosticCallback DC) const {
196   trace::Span Tracer("getConfig");
197   if (!P.Path.empty())
198     SPAN_ATTACH(Tracer, "path", P.Path);
199   Config C;
200   for (const auto &Fragment : getFragments(P, DC))
201     Fragment(P, C);
202   return C;
203 }
204 
205 } // namespace config
206 } // namespace clangd
207 } // namespace clang
208