1f4a2713aSLionel Sambuc //===--- FileMatchTrie.cpp - ----------------------------------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file contains the implementation of a FileMatchTrie.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc
14f4a2713aSLionel Sambuc #include "clang/Tooling/FileMatchTrie.h"
15f4a2713aSLionel Sambuc #include "llvm/ADT/StringMap.h"
16f4a2713aSLionel Sambuc #include "llvm/Support/FileSystem.h"
17f4a2713aSLionel Sambuc #include "llvm/Support/Path.h"
18f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
19f4a2713aSLionel Sambuc #include <sstream>
20f4a2713aSLionel Sambuc
21f4a2713aSLionel Sambuc namespace clang {
22f4a2713aSLionel Sambuc namespace tooling {
23f4a2713aSLionel Sambuc
24f4a2713aSLionel Sambuc /// \brief Default \c PathComparator using \c llvm::sys::fs::equivalent().
25f4a2713aSLionel Sambuc struct DefaultPathComparator : public PathComparator {
~DefaultPathComparatorclang::tooling::DefaultPathComparator26f4a2713aSLionel Sambuc virtual ~DefaultPathComparator() {}
equivalentclang::tooling::DefaultPathComparator27*0a6a1f1dSLionel Sambuc bool equivalent(StringRef FileA, StringRef FileB) const override {
28f4a2713aSLionel Sambuc return FileA == FileB || llvm::sys::fs::equivalent(FileA, FileB);
29f4a2713aSLionel Sambuc }
30f4a2713aSLionel Sambuc };
31f4a2713aSLionel Sambuc
32f4a2713aSLionel Sambuc /// \brief A node of the \c FileMatchTrie.
33f4a2713aSLionel Sambuc ///
34f4a2713aSLionel Sambuc /// Each node has storage for up to one path and a map mapping a path segment to
35f4a2713aSLionel Sambuc /// child nodes. The trie starts with an empty root node.
36f4a2713aSLionel Sambuc class FileMatchTrieNode {
37f4a2713aSLionel Sambuc public:
38f4a2713aSLionel Sambuc /// \brief Inserts 'NewPath' into this trie. \c ConsumedLength denotes
39f4a2713aSLionel Sambuc /// the number of \c NewPath's trailing characters already consumed during
40f4a2713aSLionel Sambuc /// recursion.
41f4a2713aSLionel Sambuc ///
42f4a2713aSLionel Sambuc /// An insert of a path
43f4a2713aSLionel Sambuc /// 'p'starts at the root node and does the following:
44f4a2713aSLionel Sambuc /// - If the node is empty, insert 'p' into its storage and abort.
45f4a2713aSLionel Sambuc /// - If the node has a path 'p2' but no children, take the last path segment
46f4a2713aSLionel Sambuc /// 's' of 'p2', put a new child into the map at 's' an insert the rest of
47f4a2713aSLionel Sambuc /// 'p2' there.
48f4a2713aSLionel Sambuc /// - Insert a new child for the last segment of 'p' and insert the rest of
49f4a2713aSLionel Sambuc /// 'p' there.
50f4a2713aSLionel Sambuc ///
51f4a2713aSLionel Sambuc /// An insert operation is linear in the number of a path's segments.
insert(StringRef NewPath,unsigned ConsumedLength=0)52f4a2713aSLionel Sambuc void insert(StringRef NewPath, unsigned ConsumedLength = 0) {
53f4a2713aSLionel Sambuc // We cannot put relative paths into the FileMatchTrie as then a path can be
54f4a2713aSLionel Sambuc // a postfix of another path, violating a core assumption of the trie.
55f4a2713aSLionel Sambuc if (llvm::sys::path::is_relative(NewPath))
56f4a2713aSLionel Sambuc return;
57f4a2713aSLionel Sambuc if (Path.empty()) {
58f4a2713aSLionel Sambuc // This is an empty leaf. Store NewPath and return.
59f4a2713aSLionel Sambuc Path = NewPath;
60f4a2713aSLionel Sambuc return;
61f4a2713aSLionel Sambuc }
62f4a2713aSLionel Sambuc if (Children.empty()) {
63f4a2713aSLionel Sambuc // This is a leaf, ignore duplicate entry if 'Path' equals 'NewPath'.
64f4a2713aSLionel Sambuc if (NewPath == Path)
65f4a2713aSLionel Sambuc return;
66f4a2713aSLionel Sambuc // Make this a node and create a child-leaf with 'Path'.
67f4a2713aSLionel Sambuc StringRef Element(llvm::sys::path::filename(
68f4a2713aSLionel Sambuc StringRef(Path).drop_back(ConsumedLength)));
69f4a2713aSLionel Sambuc Children[Element].Path = Path;
70f4a2713aSLionel Sambuc }
71f4a2713aSLionel Sambuc StringRef Element(llvm::sys::path::filename(
72f4a2713aSLionel Sambuc StringRef(NewPath).drop_back(ConsumedLength)));
73f4a2713aSLionel Sambuc Children[Element].insert(NewPath, ConsumedLength + Element.size() + 1);
74f4a2713aSLionel Sambuc }
75f4a2713aSLionel Sambuc
76f4a2713aSLionel Sambuc /// \brief Tries to find the node under this \c FileMatchTrieNode that best
77f4a2713aSLionel Sambuc /// matches 'FileName'.
78f4a2713aSLionel Sambuc ///
79f4a2713aSLionel Sambuc /// If multiple paths fit 'FileName' equally well, \c IsAmbiguous is set to
80f4a2713aSLionel Sambuc /// \c true and an empty string is returned. If no path fits 'FileName', an
81f4a2713aSLionel Sambuc /// empty string is returned. \c ConsumedLength denotes the number of
82f4a2713aSLionel Sambuc /// \c Filename's trailing characters already consumed during recursion.
83f4a2713aSLionel Sambuc ///
84f4a2713aSLionel Sambuc /// To find the best matching node for a given path 'p', the
85f4a2713aSLionel Sambuc /// \c findEquivalent() function is called recursively for each path segment
86f4a2713aSLionel Sambuc /// (back to fron) of 'p' until a node 'n' is reached that does not ..
87f4a2713aSLionel Sambuc /// - .. have children. In this case it is checked
88f4a2713aSLionel Sambuc /// whether the stored path is equivalent to 'p'. If yes, the best match is
89f4a2713aSLionel Sambuc /// found. Otherwise continue with the parent node as if this node did not
90f4a2713aSLionel Sambuc /// exist.
91f4a2713aSLionel Sambuc /// - .. a child matching the next path segment. In this case, all children of
92f4a2713aSLionel Sambuc /// 'n' are an equally good match for 'p'. All children are of 'n' are found
93f4a2713aSLionel Sambuc /// recursively and their equivalence to 'p' is determined. If none are
94f4a2713aSLionel Sambuc /// equivalent, continue with the parent node as if 'n' didn't exist. If one
95f4a2713aSLionel Sambuc /// is equivalent, the best match is found. Otherwise, report and ambigiuity
96f4a2713aSLionel Sambuc /// error.
findEquivalent(const PathComparator & Comparator,StringRef FileName,bool & IsAmbiguous,unsigned ConsumedLength=0) const97f4a2713aSLionel Sambuc StringRef findEquivalent(const PathComparator& Comparator,
98f4a2713aSLionel Sambuc StringRef FileName,
99f4a2713aSLionel Sambuc bool &IsAmbiguous,
100f4a2713aSLionel Sambuc unsigned ConsumedLength = 0) const {
101f4a2713aSLionel Sambuc if (Children.empty()) {
102f4a2713aSLionel Sambuc if (Comparator.equivalent(StringRef(Path), FileName))
103f4a2713aSLionel Sambuc return StringRef(Path);
104f4a2713aSLionel Sambuc return StringRef();
105f4a2713aSLionel Sambuc }
106f4a2713aSLionel Sambuc StringRef Element(llvm::sys::path::filename(FileName.drop_back(
107f4a2713aSLionel Sambuc ConsumedLength)));
108f4a2713aSLionel Sambuc llvm::StringMap<FileMatchTrieNode>::const_iterator MatchingChild =
109f4a2713aSLionel Sambuc Children.find(Element);
110f4a2713aSLionel Sambuc if (MatchingChild != Children.end()) {
111f4a2713aSLionel Sambuc StringRef Result = MatchingChild->getValue().findEquivalent(
112f4a2713aSLionel Sambuc Comparator, FileName, IsAmbiguous,
113f4a2713aSLionel Sambuc ConsumedLength + Element.size() + 1);
114f4a2713aSLionel Sambuc if (!Result.empty() || IsAmbiguous)
115f4a2713aSLionel Sambuc return Result;
116f4a2713aSLionel Sambuc }
117f4a2713aSLionel Sambuc std::vector<StringRef> AllChildren;
118f4a2713aSLionel Sambuc getAll(AllChildren, MatchingChild);
119f4a2713aSLionel Sambuc StringRef Result;
120f4a2713aSLionel Sambuc for (unsigned i = 0; i < AllChildren.size(); i++) {
121f4a2713aSLionel Sambuc if (Comparator.equivalent(AllChildren[i], FileName)) {
122f4a2713aSLionel Sambuc if (Result.empty()) {
123f4a2713aSLionel Sambuc Result = AllChildren[i];
124f4a2713aSLionel Sambuc } else {
125f4a2713aSLionel Sambuc IsAmbiguous = true;
126f4a2713aSLionel Sambuc return StringRef();
127f4a2713aSLionel Sambuc }
128f4a2713aSLionel Sambuc }
129f4a2713aSLionel Sambuc }
130f4a2713aSLionel Sambuc return Result;
131f4a2713aSLionel Sambuc }
132f4a2713aSLionel Sambuc
133f4a2713aSLionel Sambuc private:
134f4a2713aSLionel Sambuc /// \brief Gets all paths under this FileMatchTrieNode.
getAll(std::vector<StringRef> & Results,llvm::StringMap<FileMatchTrieNode>::const_iterator Except) const135f4a2713aSLionel Sambuc void getAll(std::vector<StringRef> &Results,
136f4a2713aSLionel Sambuc llvm::StringMap<FileMatchTrieNode>::const_iterator Except) const {
137f4a2713aSLionel Sambuc if (Path.empty())
138f4a2713aSLionel Sambuc return;
139f4a2713aSLionel Sambuc if (Children.empty()) {
140f4a2713aSLionel Sambuc Results.push_back(StringRef(Path));
141f4a2713aSLionel Sambuc return;
142f4a2713aSLionel Sambuc }
143f4a2713aSLionel Sambuc for (llvm::StringMap<FileMatchTrieNode>::const_iterator
144f4a2713aSLionel Sambuc It = Children.begin(), E = Children.end();
145f4a2713aSLionel Sambuc It != E; ++It) {
146f4a2713aSLionel Sambuc if (It == Except)
147f4a2713aSLionel Sambuc continue;
148f4a2713aSLionel Sambuc It->getValue().getAll(Results, Children.end());
149f4a2713aSLionel Sambuc }
150f4a2713aSLionel Sambuc }
151f4a2713aSLionel Sambuc
152f4a2713aSLionel Sambuc // The stored absolute path in this node. Only valid for leaf nodes, i.e.
153f4a2713aSLionel Sambuc // nodes where Children.empty().
154f4a2713aSLionel Sambuc std::string Path;
155f4a2713aSLionel Sambuc
156f4a2713aSLionel Sambuc // The children of this node stored in a map based on the next path segment.
157f4a2713aSLionel Sambuc llvm::StringMap<FileMatchTrieNode> Children;
158f4a2713aSLionel Sambuc };
159f4a2713aSLionel Sambuc
FileMatchTrie()160f4a2713aSLionel Sambuc FileMatchTrie::FileMatchTrie()
161f4a2713aSLionel Sambuc : Root(new FileMatchTrieNode), Comparator(new DefaultPathComparator()) {}
162f4a2713aSLionel Sambuc
FileMatchTrie(PathComparator * Comparator)163f4a2713aSLionel Sambuc FileMatchTrie::FileMatchTrie(PathComparator *Comparator)
164f4a2713aSLionel Sambuc : Root(new FileMatchTrieNode), Comparator(Comparator) {}
165f4a2713aSLionel Sambuc
~FileMatchTrie()166f4a2713aSLionel Sambuc FileMatchTrie::~FileMatchTrie() {
167f4a2713aSLionel Sambuc delete Root;
168f4a2713aSLionel Sambuc }
169f4a2713aSLionel Sambuc
insert(StringRef NewPath)170f4a2713aSLionel Sambuc void FileMatchTrie::insert(StringRef NewPath) {
171f4a2713aSLionel Sambuc Root->insert(NewPath);
172f4a2713aSLionel Sambuc }
173f4a2713aSLionel Sambuc
findEquivalent(StringRef FileName,raw_ostream & Error) const174f4a2713aSLionel Sambuc StringRef FileMatchTrie::findEquivalent(StringRef FileName,
175f4a2713aSLionel Sambuc raw_ostream &Error) const {
176f4a2713aSLionel Sambuc if (llvm::sys::path::is_relative(FileName)) {
177f4a2713aSLionel Sambuc Error << "Cannot resolve relative paths";
178f4a2713aSLionel Sambuc return StringRef();
179f4a2713aSLionel Sambuc }
180f4a2713aSLionel Sambuc bool IsAmbiguous = false;
181f4a2713aSLionel Sambuc StringRef Result = Root->findEquivalent(*Comparator, FileName, IsAmbiguous);
182f4a2713aSLionel Sambuc if (IsAmbiguous)
183f4a2713aSLionel Sambuc Error << "Path is ambiguous";
184f4a2713aSLionel Sambuc return Result;
185f4a2713aSLionel Sambuc }
186f4a2713aSLionel Sambuc
187f4a2713aSLionel Sambuc } // end namespace tooling
188f4a2713aSLionel Sambuc } // end namespace clang
189