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/Path.h"
18 #include "llvm/Support/raw_ostream.h"
20 using namespace clang;
21 using namespace tooling;
24 /// \brief Default \c PathComparator using \c llvm::sys::fs::equivalent().
25 struct DefaultPathComparator : public PathComparator {
26 bool equivalent(StringRef FileA, StringRef FileB) const override {
27 return FileA == FileB || llvm::sys::fs::equivalent(FileA, FileB);
34 /// \brief A node of the \c FileMatchTrie.
36 /// Each node has storage for up to one path and a map mapping a path segment to
37 /// child nodes. The trie starts with an empty root node.
38 class FileMatchTrieNode {
40 /// \brief Inserts 'NewPath' into this trie. \c ConsumedLength denotes
41 /// the number of \c NewPath's trailing characters already consumed during
44 /// An insert of a path
45 /// 'p'starts at the root node and does the following:
46 /// - If the node is empty, insert 'p' into its storage and abort.
47 /// - If the node has a path 'p2' but no children, take the last path segment
48 /// 's' of 'p2', put a new child into the map at 's' an insert the rest of
50 /// - Insert a new child for the last segment of 'p' and insert the rest of
53 /// An insert operation is linear in the number of a path's segments.
54 void insert(StringRef NewPath, unsigned ConsumedLength = 0) {
55 // We cannot put relative paths into the FileMatchTrie as then a path can be
56 // a postfix of another path, violating a core assumption of the trie.
57 if (llvm::sys::path::is_relative(NewPath))
60 // This is an empty leaf. Store NewPath and return.
64 if (Children.empty()) {
65 // This is a leaf, ignore duplicate entry if 'Path' equals 'NewPath'.
68 // Make this a node and create a child-leaf with 'Path'.
69 StringRef Element(llvm::sys::path::filename(
70 StringRef(Path).drop_back(ConsumedLength)));
71 Children[Element].Path = Path;
73 StringRef Element(llvm::sys::path::filename(
74 StringRef(NewPath).drop_back(ConsumedLength)));
75 Children[Element].insert(NewPath, ConsumedLength + Element.size() + 1);
78 /// \brief Tries to find the node under this \c FileMatchTrieNode that best
79 /// matches 'FileName'.
81 /// If multiple paths fit 'FileName' equally well, \c IsAmbiguous is set to
82 /// \c true and an empty string is returned. If no path fits 'FileName', an
83 /// empty string is returned. \c ConsumedLength denotes the number of
84 /// \c Filename's trailing characters already consumed during recursion.
86 /// To find the best matching node for a given path 'p', the
87 /// \c findEquivalent() function is called recursively for each path segment
88 /// (back to fron) of 'p' until a node 'n' is reached that does not ..
89 /// - .. have children. In this case it is checked
90 /// whether the stored path is equivalent to 'p'. If yes, the best match is
91 /// found. Otherwise continue with the parent node as if this node did not
93 /// - .. a child matching the next path segment. In this case, all children of
94 /// 'n' are an equally good match for 'p'. All children are of 'n' are found
95 /// recursively and their equivalence to 'p' is determined. If none are
96 /// equivalent, continue with the parent node as if 'n' didn't exist. If one
97 /// is equivalent, the best match is found. Otherwise, report and ambigiuity
99 StringRef findEquivalent(const PathComparator& Comparator,
102 unsigned ConsumedLength = 0) const {
103 if (Children.empty()) {
104 if (Comparator.equivalent(StringRef(Path), FileName))
105 return StringRef(Path);
108 StringRef Element(llvm::sys::path::filename(FileName.drop_back(
110 llvm::StringMap<FileMatchTrieNode>::const_iterator MatchingChild =
111 Children.find(Element);
112 if (MatchingChild != Children.end()) {
113 StringRef Result = MatchingChild->getValue().findEquivalent(
114 Comparator, FileName, IsAmbiguous,
115 ConsumedLength + Element.size() + 1);
116 if (!Result.empty() || IsAmbiguous)
119 std::vector<StringRef> AllChildren;
120 getAll(AllChildren, MatchingChild);
122 for (unsigned i = 0; i < AllChildren.size(); i++) {
123 if (Comparator.equivalent(AllChildren[i], FileName)) {
124 if (Result.empty()) {
125 Result = AllChildren[i];
136 /// \brief Gets all paths under this FileMatchTrieNode.
137 void getAll(std::vector<StringRef> &Results,
138 llvm::StringMap<FileMatchTrieNode>::const_iterator Except) const {
141 if (Children.empty()) {
142 Results.push_back(StringRef(Path));
145 for (llvm::StringMap<FileMatchTrieNode>::const_iterator
146 It = Children.begin(), E = Children.end();
150 It->getValue().getAll(Results, Children.end());
154 // The stored absolute path in this node. Only valid for leaf nodes, i.e.
155 // nodes where Children.empty().
158 // The children of this node stored in a map based on the next path segment.
159 llvm::StringMap<FileMatchTrieNode> Children;
161 } // end namespace tooling
162 } // end namespace clang
164 FileMatchTrie::FileMatchTrie()
165 : Root(new FileMatchTrieNode), Comparator(new DefaultPathComparator()) {}
167 FileMatchTrie::FileMatchTrie(PathComparator *Comparator)
168 : Root(new FileMatchTrieNode), Comparator(Comparator) {}
170 FileMatchTrie::~FileMatchTrie() {
174 void FileMatchTrie::insert(StringRef NewPath) {
175 Root->insert(NewPath);
178 StringRef FileMatchTrie::findEquivalent(StringRef FileName,
179 raw_ostream &Error) const {
180 if (llvm::sys::path::is_relative(FileName)) {
181 Error << "Cannot resolve relative paths";
184 bool IsAmbiguous = false;
185 StringRef Result = Root->findEquivalent(*Comparator, FileName, IsAmbiguous);
187 Error << "Path is ambiguous";