comparison clang-tools-extra/clangd/FileDistance.cpp @ 221:79ff65ed7e25

LLVM12 Original
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 15 Jun 2021 19:15:29 +0900
parents 0572611fdcc8
children c4bab56944e8
comparison
equal deleted inserted replaced
220:42394fc6a535 221:79ff65ed7e25
56 llvm::hash_value(llvm::StringRef("/")); 56 llvm::hash_value(llvm::StringRef("/"));
57 57
58 FileDistance::FileDistance(llvm::StringMap<SourceParams> Sources, 58 FileDistance::FileDistance(llvm::StringMap<SourceParams> Sources,
59 const FileDistanceOptions &Opts) 59 const FileDistanceOptions &Opts)
60 : Opts(Opts) { 60 : Opts(Opts) {
61 llvm::DenseMap<llvm::hash_code, llvm::SmallVector<llvm::hash_code, 4>> 61 llvm::DenseMap<llvm::hash_code, llvm::SmallVector<llvm::hash_code>> DownEdges;
62 DownEdges;
63 // Compute the best distance following only up edges. 62 // Compute the best distance following only up edges.
64 // Keep track of down edges, in case we can use them to improve on this. 63 // Keep track of down edges, in case we can use them to improve on this.
65 for (const auto &S : Sources) { 64 for (const auto &S : Sources) {
66 auto Canonical = canonicalize(S.getKey()); 65 auto Canonical = canonicalize(S.getKey());
67 dlog("Source {0} = {1}, MaxUp = {2}", Canonical, S.second.Cost, 66 dlog("Source {0} = {1}, MaxUp = {2}", Canonical, S.second.Cost,
116 } 115 }
117 116
118 unsigned FileDistance::distance(llvm::StringRef Path) { 117 unsigned FileDistance::distance(llvm::StringRef Path) {
119 auto Canonical = canonicalize(Path); 118 auto Canonical = canonicalize(Path);
120 unsigned Cost = Unreachable; 119 unsigned Cost = Unreachable;
121 llvm::SmallVector<llvm::hash_code, 16> Ancestors; 120 llvm::SmallVector<llvm::hash_code> Ancestors;
122 // Walk up ancestors until we find a path we know the distance for. 121 // Walk up ancestors until we find a path we know the distance for.
123 for (llvm::StringRef Rest = Canonical; !Rest.empty(); 122 for (llvm::StringRef Rest = Canonical; !Rest.empty();
124 Rest = parent_path(Rest, llvm::sys::path::Style::posix)) { 123 Rest = parent_path(Rest, llvm::sys::path::Style::posix)) {
125 auto Hash = llvm::hash_value(Rest); 124 auto Hash = llvm::hash_value(Rest);
126 if (Hash == RootHash && !Ancestors.empty() && 125 if (Hash == RootHash && !Ancestors.empty() &&
175 } 174 }
176 return *Delegate; 175 return *Delegate;
177 } 176 }
178 177
179 static std::pair<std::string, int> scopeToPath(llvm::StringRef Scope) { 178 static std::pair<std::string, int> scopeToPath(llvm::StringRef Scope) {
180 llvm::SmallVector<llvm::StringRef, 4> Split; 179 llvm::SmallVector<llvm::StringRef> Split;
181 Scope.split(Split, "::", /*MaxSplit=*/-1, /*KeepEmpty=*/false); 180 Scope.split(Split, "::", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
182 return {"/" + llvm::join(Split, "/"), Split.size()}; 181 return {"/" + llvm::join(Split, "/"), Split.size()};
183 } 182 }
184 183
185 static FileDistance 184 static FileDistance