1 //===--- FileMatchTrie.cpp - ----------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the implementation of a FileMatchTrie.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Tooling/FileMatchTrie.h"
15 #include "llvm/ADT/StringMap.h"
16 #include "llvm/Support/FileSystem.h"
17 #include "llvm/Support/PathV2.h"
18 #include "llvm/Support/raw_ostream.h"
24 /// \brief Default \c PathComparator using \c llvm::sys::fs::equivalent().
25 struct DefaultPathComparator : public PathComparator {
26 virtual ~DefaultPathComparator() {}
27 virtual bool equivalent(StringRef FileA, StringRef FileB) const {
28 return FileA == FileB || llvm::sys::fs::equivalent(FileA, FileB);
32 /// \brief A node of the \c FileMatchTrie.
34 /// Each node has storage for up to one path and a map mapping a path segment to
35 /// child nodes. The trie starts with an empty root node.
36 class FileMatchTrieNode {
38 /// \brief Inserts 'NewPath' into this trie. \c ConsumedLength denotes
39 /// the number of \c NewPath's trailing characters already consumed during
42 /// An insert of a path
43 /// 'p'starts at the root node and does the following:
44 /// - If the node is empty, insert 'p' into its storage and abort.
45 /// - If the node has a path 'p2' but no children, take the last path segment
46 /// 's' of 'p2', put a new child into the map at 's' an insert the rest of
48 /// - Insert a new child for the last segment of 'p' and insert the rest of
51 /// An insert operation is linear in the number of a path's segments.
52 void insert(StringRef NewPath, unsigned ConsumedLength = 0) {
53 // We cannot put relative paths into the FileMatchTrie as then a path can be
54 // a postfix of another path, violating a core assumption of the trie.
55 if (llvm::sys::path::is_relative(NewPath))
58 // This is an empty leaf. Store NewPath and return.
62 if (Children.empty()) {
63 // This is a leaf, ignore duplicate entry if 'Path' equals 'NewPath'.
66 // Make this a node and create a child-leaf with 'Path'.
67 StringRef Element(llvm::sys::path::filename(
68 StringRef(Path).drop_back(ConsumedLength)));
69 Children[Element].Path = Path;
71 StringRef Element(llvm::sys::path::filename(
72 StringRef(NewPath).drop_back(ConsumedLength)));
73 Children[Element].insert(NewPath, ConsumedLength + Element.size() + 1);
76 /// \brief Tries to find the node under this \c FileMatchTrieNode that best
77 /// matches 'FileName'.
79 /// If multiple paths fit 'FileName' equally well, \c IsAmbiguous is set to
80 /// \c true and an empty string is returned. If no path fits 'FileName', an
81 /// empty string is returned. \c ConsumedLength denotes the number of
82 /// \c Filename's trailing characters already consumed during recursion.
84 /// To find the best matching node for a given path 'p', the
85 /// \c findEquivalent() function is called recursively for each path segment
86 /// (back to fron) of 'p' until a node 'n' is reached that does not ..
87 /// - .. have children. In this case it is checked
88 /// whether the stored path is equivalent to 'p'. If yes, the best match is
89 /// found. Otherwise continue with the parent node as if this node did not
91 /// - .. a child matching the next path segment. In this case, all children of
92 /// 'n' are an equally good match for 'p'. All children are of 'n' are found
93 /// recursively and their equivalence to 'p' is determined. If none are
94 /// equivalent, continue with the parent node as if 'n' didn't exist. If one
95 /// is equivalent, the best match is found. Otherwise, report and ambigiuity
97 StringRef findEquivalent(const PathComparator& Comparator,
100 unsigned ConsumedLength = 0) const {
101 if (Children.empty()) {
102 if (Comparator.equivalent(StringRef(Path), FileName))
103 return StringRef(Path);
106 StringRef Element(llvm::sys::path::filename(FileName.drop_back(
108 llvm::StringMap<FileMatchTrieNode>::const_iterator MatchingChild =
109 Children.find(Element);
110 if (MatchingChild != Children.end()) {
111 StringRef Result = MatchingChild->getValue().findEquivalent(
112 Comparator, FileName, IsAmbiguous,
113 ConsumedLength + Element.size() + 1);
114 if (!Result.empty() || IsAmbiguous)
117 std::vector<StringRef> AllChildren;
118 getAll(AllChildren, MatchingChild);
120 for (unsigned i = 0; i < AllChildren.size(); i++) {
121 if (Comparator.equivalent(AllChildren[i], FileName)) {
122 if (Result.empty()) {
123 Result = AllChildren[i];
134 /// \brief Gets all paths under this FileMatchTrieNode.
135 void getAll(std::vector<StringRef> &Results,
136 llvm::StringMap<FileMatchTrieNode>::const_iterator Except) const {
139 if (Children.empty()) {
140 Results.push_back(StringRef(Path));
143 for (llvm::StringMap<FileMatchTrieNode>::const_iterator
144 It = Children.begin(), E = Children.end();
148 It->getValue().getAll(Results, Children.end());
152 // The stored absolute path in this node. Only valid for leaf nodes, i.e.
153 // nodes where Children.empty().
156 // The children of this node stored in a map based on the next path segment.
157 llvm::StringMap<FileMatchTrieNode> Children;
160 FileMatchTrie::FileMatchTrie()
161 : Root(new FileMatchTrieNode), Comparator(new DefaultPathComparator()) {}
163 FileMatchTrie::FileMatchTrie(PathComparator *Comparator)
164 : Root(new FileMatchTrieNode), Comparator(Comparator) {}
166 FileMatchTrie::~FileMatchTrie() {
170 void FileMatchTrie::insert(StringRef NewPath) {
171 Root->insert(NewPath);
174 StringRef FileMatchTrie::findEquivalent(StringRef FileName,
175 raw_ostream &Error) const {
176 if (llvm::sys::path::is_relative(FileName)) {
177 Error << "Cannot resolve relative paths";
180 bool IsAmbiguous = false;
181 StringRef Result = Root->findEquivalent(*Comparator, FileName, IsAmbiguous);
183 Error << "Path is ambiguous";
187 } // end namespace tooling
188 } // end namespace clang