Path: blob/main/contrib/llvm-project/clang/lib/Tooling/FileMatchTrie.cpp
35233 views
//===- FileMatchTrie.cpp --------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file contains the implementation of a FileMatchTrie.9//10//===----------------------------------------------------------------------===//1112#include "clang/Tooling/FileMatchTrie.h"13#include "llvm/ADT/StringMap.h"14#include "llvm/ADT/StringRef.h"15#include "llvm/Support/FileSystem.h"16#include "llvm/Support/Path.h"17#include "llvm/Support/raw_ostream.h"18#include <string>19#include <vector>2021using namespace clang;22using namespace tooling;2324namespace {2526/// Default \c PathComparator using \c llvm::sys::fs::equivalent().27struct DefaultPathComparator : public PathComparator {28bool equivalent(StringRef FileA, StringRef FileB) const override {29return FileA == FileB || llvm::sys::fs::equivalent(FileA, FileB);30}31};3233} // namespace3435namespace clang {36namespace tooling {3738/// A node of the \c FileMatchTrie.39///40/// Each node has storage for up to one path and a map mapping a path segment to41/// child nodes. The trie starts with an empty root node.42class FileMatchTrieNode {43public:44/// Inserts 'NewPath' into this trie. \c ConsumedLength denotes45/// the number of \c NewPath's trailing characters already consumed during46/// recursion.47///48/// An insert of a path49/// 'p'starts at the root node and does the following:50/// - If the node is empty, insert 'p' into its storage and abort.51/// - If the node has a path 'p2' but no children, take the last path segment52/// 's' of 'p2', put a new child into the map at 's' an insert the rest of53/// 'p2' there.54/// - Insert a new child for the last segment of 'p' and insert the rest of55/// 'p' there.56///57/// An insert operation is linear in the number of a path's segments.58void insert(StringRef NewPath, unsigned ConsumedLength = 0) {59// We cannot put relative paths into the FileMatchTrie as then a path can be60// a postfix of another path, violating a core assumption of the trie.61if (llvm::sys::path::is_relative(NewPath))62return;63if (Path.empty()) {64// This is an empty leaf. Store NewPath and return.65Path = std::string(NewPath);66return;67}68if (Children.empty()) {69// This is a leaf, ignore duplicate entry if 'Path' equals 'NewPath'.70if (NewPath == Path)71return;72// Make this a node and create a child-leaf with 'Path'.73StringRef Element(llvm::sys::path::filename(74StringRef(Path).drop_back(ConsumedLength)));75Children[Element].Path = Path;76}77StringRef Element(llvm::sys::path::filename(78StringRef(NewPath).drop_back(ConsumedLength)));79Children[Element].insert(NewPath, ConsumedLength + Element.size() + 1);80}8182/// Tries to find the node under this \c FileMatchTrieNode that best83/// matches 'FileName'.84///85/// If multiple paths fit 'FileName' equally well, \c IsAmbiguous is set to86/// \c true and an empty string is returned. If no path fits 'FileName', an87/// empty string is returned. \c ConsumedLength denotes the number of88/// \c Filename's trailing characters already consumed during recursion.89///90/// To find the best matching node for a given path 'p', the91/// \c findEquivalent() function is called recursively for each path segment92/// (back to front) of 'p' until a node 'n' is reached that does not ..93/// - .. have children. In this case it is checked94/// whether the stored path is equivalent to 'p'. If yes, the best match is95/// found. Otherwise continue with the parent node as if this node did not96/// exist.97/// - .. a child matching the next path segment. In this case, all children of98/// 'n' are an equally good match for 'p'. All children are of 'n' are found99/// recursively and their equivalence to 'p' is determined. If none are100/// equivalent, continue with the parent node as if 'n' didn't exist. If one101/// is equivalent, the best match is found. Otherwise, report and ambigiuity102/// error.103StringRef findEquivalent(const PathComparator& Comparator,104StringRef FileName,105bool &IsAmbiguous,106unsigned ConsumedLength = 0) const {107// Note: we support only directory symlinks for performance reasons.108if (Children.empty()) {109// As far as we do not support file symlinks, compare110// basenames here to avoid request to file system.111if (llvm::sys::path::filename(Path) ==112llvm::sys::path::filename(FileName) &&113Comparator.equivalent(StringRef(Path), FileName))114return StringRef(Path);115return {};116}117StringRef Element(llvm::sys::path::filename(FileName.drop_back(118ConsumedLength)));119llvm::StringMap<FileMatchTrieNode>::const_iterator MatchingChild =120Children.find(Element);121if (MatchingChild != Children.end()) {122StringRef Result = MatchingChild->getValue().findEquivalent(123Comparator, FileName, IsAmbiguous,124ConsumedLength + Element.size() + 1);125if (!Result.empty() || IsAmbiguous)126return Result;127}128129// If `ConsumedLength` is zero, this is the root and we have no filename130// match. Give up in this case, we don't try to find symlinks with131// different names.132if (ConsumedLength == 0)133return {};134135std::vector<StringRef> AllChildren;136getAll(AllChildren, MatchingChild);137StringRef Result;138for (const auto &Child : AllChildren) {139if (Comparator.equivalent(Child, FileName)) {140if (Result.empty()) {141Result = Child;142} else {143IsAmbiguous = true;144return {};145}146}147}148return Result;149}150151private:152/// Gets all paths under this FileMatchTrieNode.153void getAll(std::vector<StringRef> &Results,154llvm::StringMap<FileMatchTrieNode>::const_iterator Except) const {155if (Path.empty())156return;157if (Children.empty()) {158Results.push_back(StringRef(Path));159return;160}161for (llvm::StringMap<FileMatchTrieNode>::const_iterator162It = Children.begin(), E = Children.end();163It != E; ++It) {164if (It == Except)165continue;166It->getValue().getAll(Results, Children.end());167}168}169170// The stored absolute path in this node. Only valid for leaf nodes, i.e.171// nodes where Children.empty().172std::string Path;173174// The children of this node stored in a map based on the next path segment.175llvm::StringMap<FileMatchTrieNode> Children;176};177178} // namespace tooling179} // namespace clang180181FileMatchTrie::FileMatchTrie()182: Root(new FileMatchTrieNode), Comparator(new DefaultPathComparator()) {}183184FileMatchTrie::FileMatchTrie(PathComparator *Comparator)185: Root(new FileMatchTrieNode), Comparator(Comparator) {}186187FileMatchTrie::~FileMatchTrie() {188delete Root;189}190191void FileMatchTrie::insert(StringRef NewPath) {192Root->insert(NewPath);193}194195StringRef FileMatchTrie::findEquivalent(StringRef FileName,196raw_ostream &Error) const {197if (llvm::sys::path::is_relative(FileName)) {198Error << "Cannot resolve relative paths";199return {};200}201bool IsAmbiguous = false;202StringRef Result = Root->findEquivalent(*Comparator, FileName, IsAmbiguous);203if (IsAmbiguous)204Error << "Path is ambiguous";205return Result;206}207208209