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