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/FileCache.h" 13 #include "support/ThreadsafeFS.h" 14 #include "support/Trace.h" 15 #include "llvm/ADT/ScopeExit.h" 16 #include "llvm/ADT/StringMap.h" 17 #include "llvm/ADT/StringRef.h" 18 #include "llvm/Support/Path.h" 19 #include <chrono> 20 #include <mutex> 21 #include <string> 22 23 namespace clang { 24 namespace clangd { 25 namespace config { 26 27 // Threadsafe cache around reading a YAML config file from disk. 28 class FileConfigCache : public FileCache { 29 mutable llvm::SmallVector<CompiledFragment, 1> CachedValue; 30 std::string Directory; 31 32 public: 33 FileConfigCache(llvm::StringRef Path, llvm::StringRef Directory) 34 : FileCache(Path), Directory(Directory) {} 35 36 void get(const ThreadsafeFS &TFS, DiagnosticCallback DC, 37 std::chrono::steady_clock::time_point FreshTime, 38 std::vector<CompiledFragment> &Out) const { 39 read( 40 TFS, FreshTime, 41 [&](llvm::Optional<llvm::StringRef> Data) { 42 CachedValue.clear(); 43 if (Data) 44 for (auto &Fragment : Fragment::parseYAML(*Data, path(), DC)) { 45 Fragment.Source.Directory = Directory; 46 CachedValue.push_back(std::move(Fragment).compile(DC)); 47 } 48 }, 49 [&]() { llvm::copy(CachedValue, std::back_inserter(Out)); }); 50 } 51 }; 52 53 std::unique_ptr<Provider> Provider::fromYAMLFile(llvm::StringRef AbsPath, 54 llvm::StringRef Directory, 55 const ThreadsafeFS &FS) { 56 class AbsFileProvider : public Provider { 57 mutable FileConfigCache Cache; // threadsafe 58 const ThreadsafeFS &FS; 59 60 std::vector<CompiledFragment> 61 getFragments(const Params &P, DiagnosticCallback DC) const override { 62 std::vector<CompiledFragment> Result; 63 Cache.get(FS, DC, P.FreshTime, Result); 64 return Result; 65 }; 66 67 public: 68 AbsFileProvider(llvm::StringRef Path, llvm::StringRef Directory, 69 const ThreadsafeFS &FS) 70 : Cache(Path, Directory), FS(FS) { 71 assert(llvm::sys::path::is_absolute(Path)); 72 } 73 }; 74 75 return std::make_unique<AbsFileProvider>(AbsPath, Directory, FS); 76 } 77 78 std::unique_ptr<Provider> 79 Provider::fromAncestorRelativeYAMLFiles(llvm::StringRef RelPath, 80 const ThreadsafeFS &FS) { 81 class RelFileProvider : public Provider { 82 std::string RelPath; 83 const ThreadsafeFS &FS; 84 85 mutable std::mutex Mu; 86 // Keys are the (posix-style) ancestor directory, not the config within it. 87 // We only insert into this map, so pointers to values are stable forever. 88 // Mutex guards the map itself, not the values (which are threadsafe). 89 mutable llvm::StringMap<FileConfigCache> Cache; 90 91 std::vector<CompiledFragment> 92 getFragments(const Params &P, DiagnosticCallback DC) const override { 93 namespace path = llvm::sys::path; 94 95 if (P.Path.empty()) 96 return {}; 97 98 // Compute absolute paths to all ancestors (substrings of P.Path). 99 llvm::StringRef Parent = path::parent_path(P.Path); 100 llvm::SmallVector<llvm::StringRef, 8> Ancestors; 101 for (auto I = path::begin(Parent, path::Style::posix), 102 E = path::end(Parent); 103 I != E; ++I) { 104 // Avoid weird non-substring cases like phantom "." components. 105 // In practice, Component is a substring for all "normal" ancestors. 106 if (I->end() < Parent.begin() || I->end() > Parent.end()) 107 continue; 108 Ancestors.emplace_back(Parent.begin(), I->end() - Parent.begin()); 109 } 110 // Ensure corresponding cache entries exist in the map. 111 llvm::SmallVector<FileConfigCache *, 8> Caches; 112 { 113 std::lock_guard<std::mutex> Lock(Mu); 114 for (llvm::StringRef Ancestor : Ancestors) { 115 auto It = Cache.find(Ancestor); 116 // Assemble the actual config file path only once. 117 if (It == Cache.end()) { 118 llvm::SmallString<256> ConfigPath = Ancestor; 119 path::append(ConfigPath, RelPath); 120 // Use native slashes for reading the file, affects diagnostics. 121 llvm::sys::path::native(ConfigPath); 122 It = Cache.try_emplace(Ancestor, ConfigPath.str(), Ancestor).first; 123 } 124 Caches.push_back(&It->second); 125 } 126 } 127 // Finally query each individual file. 128 // This will take a (per-file) lock for each file that actually exists. 129 std::vector<CompiledFragment> Result; 130 for (FileConfigCache *Cache : Caches) 131 Cache->get(FS, DC, P.FreshTime, Result); 132 return Result; 133 }; 134 135 public: 136 RelFileProvider(llvm::StringRef RelPath, const ThreadsafeFS &FS) 137 : RelPath(RelPath), FS(FS) { 138 assert(llvm::sys::path::is_relative(RelPath)); 139 } 140 }; 141 142 return std::make_unique<RelFileProvider>(RelPath, FS); 143 } 144 145 std::unique_ptr<Provider> 146 Provider::combine(std::vector<const Provider *> Providers) { 147 class CombinedProvider : public Provider { 148 std::vector<const Provider *> Providers; 149 150 std::vector<CompiledFragment> 151 getFragments(const Params &P, DiagnosticCallback DC) const override { 152 std::vector<CompiledFragment> Result; 153 for (const auto &Provider : Providers) { 154 for (auto &Fragment : Provider->getFragments(P, DC)) 155 Result.push_back(std::move(Fragment)); 156 } 157 return Result; 158 } 159 160 public: 161 CombinedProvider(std::vector<const Provider *> Providers) 162 : Providers(std::move(Providers)) {} 163 }; 164 165 return std::make_unique<CombinedProvider>(std::move(Providers)); 166 } 167 168 Config Provider::getConfig(const Params &P, DiagnosticCallback DC) const { 169 trace::Span Tracer("getConfig"); 170 if (!P.Path.empty()) 171 SPAN_ATTACH(Tracer, "path", P.Path); 172 Config C; 173 for (const auto &Fragment : getFragments(P, DC)) 174 Fragment(P, C); 175 return C; 176 } 177 178 } // namespace config 179 } // namespace clangd 180 } // namespace clang 181