2018-03-15 05:05:51 +08:00
|
|
|
//===- FileMatchTrie.cpp --------------------------------------------------===//
|
2012-10-09 00:08:15 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-10-09 00:08:15 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains the implementation of a FileMatchTrie.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Tooling/FileMatchTrie.h"
|
|
|
|
#include "llvm/ADT/StringMap.h"
|
2018-03-15 05:05:51 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2012-10-09 00:08:15 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2013-06-12 06:15:02 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2012-10-09 02:37:21 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2018-03-15 05:05:51 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2015-03-09 23:03:26 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace tooling;
|
2012-10-09 00:08:15 +08:00
|
|
|
|
2015-03-09 23:03:26 +08:00
|
|
|
namespace {
|
2018-03-15 05:05:51 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Default \c PathComparator using \c llvm::sys::fs::equivalent().
|
2012-10-09 00:08:15 +08:00
|
|
|
struct DefaultPathComparator : public PathComparator {
|
2014-03-15 12:29:04 +08:00
|
|
|
bool equivalent(StringRef FileA, StringRef FileB) const override {
|
2012-10-09 02:31:54 +08:00
|
|
|
return FileA == FileB || llvm::sys::fs::equivalent(FileA, FileB);
|
2012-10-09 00:08:15 +08:00
|
|
|
}
|
|
|
|
};
|
2018-03-15 05:05:51 +08:00
|
|
|
|
|
|
|
} // namespace
|
2012-10-09 00:08:15 +08:00
|
|
|
|
2015-03-09 23:03:26 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace tooling {
|
2018-03-15 05:05:51 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// A node of the \c FileMatchTrie.
|
2012-10-09 00:08:15 +08:00
|
|
|
///
|
|
|
|
/// Each node has storage for up to one path and a map mapping a path segment to
|
|
|
|
/// child nodes. The trie starts with an empty root node.
|
|
|
|
class FileMatchTrieNode {
|
|
|
|
public:
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Inserts 'NewPath' into this trie. \c ConsumedLength denotes
|
2012-10-09 00:08:15 +08:00
|
|
|
/// the number of \c NewPath's trailing characters already consumed during
|
|
|
|
/// recursion.
|
|
|
|
///
|
|
|
|
/// An insert of a path
|
|
|
|
/// 'p'starts at the root node and does the following:
|
|
|
|
/// - If the node is empty, insert 'p' into its storage and abort.
|
|
|
|
/// - If the node has a path 'p2' but no children, take the last path segment
|
|
|
|
/// 's' of 'p2', put a new child into the map at 's' an insert the rest of
|
|
|
|
/// 'p2' there.
|
|
|
|
/// - Insert a new child for the last segment of 'p' and insert the rest of
|
|
|
|
/// 'p' there.
|
|
|
|
///
|
|
|
|
/// An insert operation is linear in the number of a path's segments.
|
|
|
|
void insert(StringRef NewPath, unsigned ConsumedLength = 0) {
|
|
|
|
// We cannot put relative paths into the FileMatchTrie as then a path can be
|
|
|
|
// a postfix of another path, violating a core assumption of the trie.
|
|
|
|
if (llvm::sys::path::is_relative(NewPath))
|
|
|
|
return;
|
|
|
|
if (Path.empty()) {
|
|
|
|
// This is an empty leaf. Store NewPath and return.
|
|
|
|
Path = NewPath;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Children.empty()) {
|
|
|
|
// This is a leaf, ignore duplicate entry if 'Path' equals 'NewPath'.
|
|
|
|
if (NewPath == Path)
|
|
|
|
return;
|
|
|
|
// Make this a node and create a child-leaf with 'Path'.
|
|
|
|
StringRef Element(llvm::sys::path::filename(
|
|
|
|
StringRef(Path).drop_back(ConsumedLength)));
|
|
|
|
Children[Element].Path = Path;
|
|
|
|
}
|
|
|
|
StringRef Element(llvm::sys::path::filename(
|
|
|
|
StringRef(NewPath).drop_back(ConsumedLength)));
|
|
|
|
Children[Element].insert(NewPath, ConsumedLength + Element.size() + 1);
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Tries to find the node under this \c FileMatchTrieNode that best
|
2012-10-09 00:08:15 +08:00
|
|
|
/// matches 'FileName'.
|
|
|
|
///
|
|
|
|
/// If multiple paths fit 'FileName' equally well, \c IsAmbiguous is set to
|
|
|
|
/// \c true and an empty string is returned. If no path fits 'FileName', an
|
|
|
|
/// empty string is returned. \c ConsumedLength denotes the number of
|
|
|
|
/// \c Filename's trailing characters already consumed during recursion.
|
|
|
|
///
|
|
|
|
/// To find the best matching node for a given path 'p', the
|
|
|
|
/// \c findEquivalent() function is called recursively for each path segment
|
2018-04-06 23:14:32 +08:00
|
|
|
/// (back to front) of 'p' until a node 'n' is reached that does not ..
|
2012-10-09 00:08:15 +08:00
|
|
|
/// - .. have children. In this case it is checked
|
|
|
|
/// whether the stored path is equivalent to 'p'. If yes, the best match is
|
|
|
|
/// found. Otherwise continue with the parent node as if this node did not
|
|
|
|
/// exist.
|
|
|
|
/// - .. a child matching the next path segment. In this case, all children of
|
|
|
|
/// 'n' are an equally good match for 'p'. All children are of 'n' are found
|
|
|
|
/// recursively and their equivalence to 'p' is determined. If none are
|
|
|
|
/// equivalent, continue with the parent node as if 'n' didn't exist. If one
|
|
|
|
/// is equivalent, the best match is found. Otherwise, report and ambigiuity
|
|
|
|
/// error.
|
|
|
|
StringRef findEquivalent(const PathComparator& Comparator,
|
|
|
|
StringRef FileName,
|
|
|
|
bool &IsAmbiguous,
|
|
|
|
unsigned ConsumedLength = 0) const {
|
|
|
|
if (Children.empty()) {
|
|
|
|
if (Comparator.equivalent(StringRef(Path), FileName))
|
|
|
|
return StringRef(Path);
|
2018-03-15 05:05:51 +08:00
|
|
|
return {};
|
2012-10-09 00:08:15 +08:00
|
|
|
}
|
|
|
|
StringRef Element(llvm::sys::path::filename(FileName.drop_back(
|
|
|
|
ConsumedLength)));
|
|
|
|
llvm::StringMap<FileMatchTrieNode>::const_iterator MatchingChild =
|
|
|
|
Children.find(Element);
|
|
|
|
if (MatchingChild != Children.end()) {
|
|
|
|
StringRef Result = MatchingChild->getValue().findEquivalent(
|
|
|
|
Comparator, FileName, IsAmbiguous,
|
|
|
|
ConsumedLength + Element.size() + 1);
|
|
|
|
if (!Result.empty() || IsAmbiguous)
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
std::vector<StringRef> AllChildren;
|
|
|
|
getAll(AllChildren, MatchingChild);
|
|
|
|
StringRef Result;
|
2018-03-15 05:05:51 +08:00
|
|
|
for (const auto &Child : AllChildren) {
|
|
|
|
if (Comparator.equivalent(Child, FileName)) {
|
2012-10-09 00:08:15 +08:00
|
|
|
if (Result.empty()) {
|
2018-03-15 05:05:51 +08:00
|
|
|
Result = Child;
|
2012-10-09 00:08:15 +08:00
|
|
|
} else {
|
|
|
|
IsAmbiguous = true;
|
2018-03-15 05:05:51 +08:00
|
|
|
return {};
|
2012-10-09 00:08:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Gets all paths under this FileMatchTrieNode.
|
2012-10-09 00:08:15 +08:00
|
|
|
void getAll(std::vector<StringRef> &Results,
|
|
|
|
llvm::StringMap<FileMatchTrieNode>::const_iterator Except) const {
|
|
|
|
if (Path.empty())
|
|
|
|
return;
|
|
|
|
if (Children.empty()) {
|
|
|
|
Results.push_back(StringRef(Path));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (llvm::StringMap<FileMatchTrieNode>::const_iterator
|
|
|
|
It = Children.begin(), E = Children.end();
|
|
|
|
It != E; ++It) {
|
|
|
|
if (It == Except)
|
|
|
|
continue;
|
|
|
|
It->getValue().getAll(Results, Children.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The stored absolute path in this node. Only valid for leaf nodes, i.e.
|
|
|
|
// nodes where Children.empty().
|
|
|
|
std::string Path;
|
|
|
|
|
|
|
|
// The children of this node stored in a map based on the next path segment.
|
|
|
|
llvm::StringMap<FileMatchTrieNode> Children;
|
|
|
|
};
|
2018-03-15 05:05:51 +08:00
|
|
|
|
|
|
|
} // namespace tooling
|
|
|
|
} // namespace clang
|
2012-10-09 00:08:15 +08:00
|
|
|
|
|
|
|
FileMatchTrie::FileMatchTrie()
|
2018-03-15 05:05:51 +08:00
|
|
|
: Root(new FileMatchTrieNode), Comparator(new DefaultPathComparator()) {}
|
2012-10-09 00:08:15 +08:00
|
|
|
|
|
|
|
FileMatchTrie::FileMatchTrie(PathComparator *Comparator)
|
2018-03-15 05:05:51 +08:00
|
|
|
: Root(new FileMatchTrieNode), Comparator(Comparator) {}
|
2012-10-09 00:08:15 +08:00
|
|
|
|
|
|
|
FileMatchTrie::~FileMatchTrie() {
|
|
|
|
delete Root;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileMatchTrie::insert(StringRef NewPath) {
|
|
|
|
Root->insert(NewPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef FileMatchTrie::findEquivalent(StringRef FileName,
|
2013-01-13 03:30:44 +08:00
|
|
|
raw_ostream &Error) const {
|
2012-10-09 00:08:15 +08:00
|
|
|
if (llvm::sys::path::is_relative(FileName)) {
|
|
|
|
Error << "Cannot resolve relative paths";
|
2018-03-15 05:05:51 +08:00
|
|
|
return {};
|
2012-10-09 00:08:15 +08:00
|
|
|
}
|
|
|
|
bool IsAmbiguous = false;
|
|
|
|
StringRef Result = Root->findEquivalent(*Comparator, FileName, IsAmbiguous);
|
|
|
|
if (IsAmbiguous)
|
|
|
|
Error << "Path is ambiguous";
|
|
|
|
return Result;
|
|
|
|
}
|