| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | #include "clang/Tooling/FileMatchTrie.h" |
| 14 | #include "llvm/ADT/StringMap.h" |
| 15 | #include "llvm/ADT/StringRef.h" |
| 16 | #include "llvm/Support/FileSystem.h" |
| 17 | #include "llvm/Support/Path.h" |
| 18 | #include "llvm/Support/raw_ostream.h" |
| 19 | #include <string> |
| 20 | #include <vector> |
| 21 | |
| 22 | using namespace clang; |
| 23 | using namespace tooling; |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | |
| 28 | struct DefaultPathComparator : public PathComparator { |
| 29 | bool equivalent(StringRef FileA, StringRef FileB) const override { |
| 30 | return FileA == FileB || llvm::sys::fs::equivalent(FileA, FileB); |
| 31 | } |
| 32 | }; |
| 33 | |
| 34 | } |
| 35 | |
| 36 | namespace clang { |
| 37 | namespace tooling { |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | class FileMatchTrieNode { |
| 44 | public: |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | void insert(StringRef NewPath, unsigned ConsumedLength = 0) { |
| 60 | |
| 61 | |
| 62 | if (llvm::sys::path::is_relative(NewPath)) |
| 63 | return; |
| 64 | if (Path.empty()) { |
| 65 | |
| 66 | Path = NewPath; |
| 67 | return; |
| 68 | } |
| 69 | if (Children.empty()) { |
| 70 | |
| 71 | if (NewPath == Path) |
| 72 | return; |
| 73 | |
| 74 | StringRef Element(llvm::sys::path::filename( |
| 75 | StringRef(Path).drop_back(ConsumedLength))); |
| 76 | Children[Element].Path = Path; |
| 77 | } |
| 78 | StringRef Element(llvm::sys::path::filename( |
| 79 | StringRef(NewPath).drop_back(ConsumedLength))); |
| 80 | Children[Element].insert(NewPath, ConsumedLength + Element.size() + 1); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | StringRef findEquivalent(const PathComparator& Comparator, |
| 105 | StringRef FileName, |
| 106 | bool &IsAmbiguous, |
| 107 | unsigned ConsumedLength = 0) const { |
| 108 | if (Children.empty()) { |
| 109 | if (Comparator.equivalent(StringRef(Path), FileName)) |
| 110 | return StringRef(Path); |
| 111 | return {}; |
| 112 | } |
| 113 | StringRef Element(llvm::sys::path::filename(FileName.drop_back( |
| 114 | ConsumedLength))); |
| 115 | llvm::StringMap<FileMatchTrieNode>::const_iterator MatchingChild = |
| 116 | Children.find(Element); |
| 117 | if (MatchingChild != Children.end()) { |
| 118 | StringRef Result = MatchingChild->getValue().findEquivalent( |
| 119 | Comparator, FileName, IsAmbiguous, |
| 120 | ConsumedLength + Element.size() + 1); |
| 121 | if (!Result.empty() || IsAmbiguous) |
| 122 | return Result; |
| 123 | } |
| 124 | std::vector<StringRef> AllChildren; |
| 125 | getAll(AllChildren, MatchingChild); |
| 126 | StringRef Result; |
| 127 | for (const auto &Child : AllChildren) { |
| 128 | if (Comparator.equivalent(Child, FileName)) { |
| 129 | if (Result.empty()) { |
| 130 | Result = Child; |
| 131 | } else { |
| 132 | IsAmbiguous = true; |
| 133 | return {}; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | return Result; |
| 138 | } |
| 139 | |
| 140 | private: |
| 141 | |
| 142 | void getAll(std::vector<StringRef> &Results, |
| 143 | llvm::StringMap<FileMatchTrieNode>::const_iterator Except) const { |
| 144 | if (Path.empty()) |
| 145 | return; |
| 146 | if (Children.empty()) { |
| 147 | Results.push_back(StringRef(Path)); |
| 148 | return; |
| 149 | } |
| 150 | for (llvm::StringMap<FileMatchTrieNode>::const_iterator |
| 151 | It = Children.begin(), E = Children.end(); |
| 152 | It != E; ++It) { |
| 153 | if (It == Except) |
| 154 | continue; |
| 155 | It->getValue().getAll(Results, Children.end()); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | |
| 160 | |
| 161 | std::string Path; |
| 162 | |
| 163 | |
| 164 | llvm::StringMap<FileMatchTrieNode> Children; |
| 165 | }; |
| 166 | |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | FileMatchTrie::FileMatchTrie() |
| 171 | : Root(new FileMatchTrieNode), Comparator(new DefaultPathComparator()) {} |
| 172 | |
| 173 | FileMatchTrie::FileMatchTrie(PathComparator *Comparator) |
| 174 | : Root(new FileMatchTrieNode), Comparator(Comparator) {} |
| 175 | |
| 176 | FileMatchTrie::~FileMatchTrie() { |
| 177 | delete Root; |
| 178 | } |
| 179 | |
| 180 | void FileMatchTrie::insert(StringRef NewPath) { |
| 181 | Root->insert(NewPath); |
| 182 | } |
| 183 | |
| 184 | StringRef FileMatchTrie::findEquivalent(StringRef FileName, |
| 185 | raw_ostream &Error) const { |
| 186 | if (llvm::sys::path::is_relative(FileName)) { |
| 187 | Error << "Cannot resolve relative paths"; |
| 188 | return {}; |
| 189 | } |
| 190 | bool IsAmbiguous = false; |
| 191 | StringRef Result = Root->findEquivalent(*Comparator, FileName, IsAmbiguous); |
| 192 | if (IsAmbiguous) |
| 193 | Error << "Path is ambiguous"; |
| 194 | return Result; |
| 195 | } |
| 196 | |