LLVM 24.0.0git
PrefixMapper.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// Prefix mapping utility.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_SUPPORT_PREFIXMAPPER_H
15#define LLVM_SUPPORT_PREFIXMAPPER_H
16
17#include "llvm/ADT/ArrayRef.h"
20#include "llvm/Support/Error.h"
21#include "llvm/Support/Path.h"
22#include <optional>
23
24namespace llvm {
25
27 std::string Old;
28 std::string New;
29
30 MappedPrefix() = default;
32 : Old(Old_.str()), New(New_.str()) {}
33
35
36 bool operator==(const MappedPrefix &RHS) const {
37 return Old == RHS.Old && New == RHS.New;
38 }
39
40 bool operator!=(const MappedPrefix &RHS) const { return !(*this == RHS); }
41
42 LLVM_ABI static std::optional<MappedPrefix>
43 getFromJoined(StringRef JoinedMapping);
44
49 LLVM_ABI static void
52 LLVM_ABI static void
55 LLVM_ABI static void
56 transformPairs(ArrayRef<std::pair<std::string, std::string>> Pairs,
58};
59
60/// Remap path prefixes.
61///
62/// Mappings are tried in order and the first match wins. A mapping only
63/// matches whole path components: \c Old must be followed by a separator or
64/// the end of the path, unless \c Old itself ends with a separator. For
65/// example, "/old=/new" remaps "/old" and "/old/a" but not "/old-a", and
66/// "/=/new" remaps every absolute path. With a Windows path style, matching is
67/// case- and separator-insensitive.
68///
69/// The rest of the path is joined to \c New with \a sys::path::append(): a
70/// separator is added if neither side has one, and leading separators are
71/// dropped from the rest if \c New ends with one. For example, "/old/a" maps to
72/// "/new/a" with either "/old=/new" or "/old/=/new/".
73///
74/// Paths are not canonicalized, so "/old/../a" maps to "/new/../a", "/old//a"
75/// maps to "/new//a", and "//old" is not matched by "/old".
77public:
78 virtual ~PrefixMapper() = default;
79
80 /// Map \p Path, and saving the new (or existing) path in \p NewPath.
81 ///
82 /// \pre \p Path is not a reference into \p NewPath.
83 /// \returns true if \c NewPath was mapped.
84 bool map(StringRef Path, SmallVectorImpl<char> &NewPath) const;
85 /// Map \p Path, and saving the new (or existing) path in \p NewPath.
86 ///
87 /// \pre \p Path is not a reference into \p NewPath.
88 /// \returns true if \c NewPath was mapped.
89 bool map(StringRef Path, std::string &NewPath) const;
90
91 /// Map \p Path, returning \a std::string.
92 std::string mapToString(StringRef Path) const;
93
94 /// Map \p Path in place.
95 /// \returns true if the path was modified.
96 bool mapInPlace(SmallVectorImpl<char> &Path) const;
97
98 /// Map \p Path in place.
99 /// \returns true if the path was modified.
100 bool mapInPlace(std::string &Path) const;
101
102protected:
103 /// Map (or unmap) \p Path. On a match, fills \p Storage with the mapped path
104 /// unless it's an exact match.
105 ///
106 /// \pre \p Path is not a reference into \p Storage.
107 virtual std::optional<StringRef>
108 mapImpl(StringRef Path, SmallVectorImpl<char> &Storage) const;
109
110public:
111 virtual void add(const MappedPrefix &MP) { Mappings.push_back(MP); }
112
113 /// A path-based reverse lexicographic sort, putting deeper paths first so
114 /// that deeper paths are prioritized over their parent paths. For example,
115 /// if both the source and build directories are remapped and one is nested
116 /// inside the other, the nested one will come first.
117 ///
118 /// FIXME: The comparison is case- and separator-sensitive, unlike matching
119 /// with a Windows path style.
120 ///
121 /// FIXME: Should probably be done implicitly, maybe by moving to a std::set
122 /// or std::map.
123 void sort();
124
125 template <class RangeT> void addRange(const RangeT &Mappings) {
126 for (const MappedPrefix &M : Mappings)
127 add(M);
128 }
129
130 template <class RangeT> void addInverseRange(const RangeT &Mappings) {
131 for (const MappedPrefix &M : Mappings)
132 add(M.getInverse());
133 }
134
135 bool empty() const { return getMappings().empty(); }
136
137 ArrayRef<MappedPrefix> getMappings() const { return Mappings; }
138
139 sys::path::Style getPathStyle() const { return PathStyle; }
140
142 : PathStyle(PathStyle) {}
143
144private:
145 sys::path::Style PathStyle;
147};
148
149} // end namespace llvm
150
151#endif // LLVM_SUPPORT_PREFIXMAPPER_H
#define LLVM_ABI
Definition Compiler.h:211
This file defines the SmallVector class.
Value * RHS
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
PrefixMapper(sys::path::Style PathStyle=sys::path::Style::native)
std::string mapToString(StringRef Path) const
Map Path, returning std::string.
virtual std::optional< StringRef > mapImpl(StringRef Path, SmallVectorImpl< char > &Storage) const
Map (or unmap) Path.
sys::path::Style getPathStyle() const
bool map(StringRef Path, SmallVectorImpl< char > &NewPath) const
Map Path, and saving the new (or existing) path in NewPath.
ArrayRef< MappedPrefix > getMappings() const
void addInverseRange(const RangeT &Mappings)
virtual void add(const MappedPrefix &MP)
bool mapInPlace(SmallVectorImpl< char > &Path) const
Map Path in place.
void addRange(const RangeT &Mappings)
bool empty() const
virtual ~PrefixMapper()=default
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
This is an optimization pass for GlobalISel generic memory operations.
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1652
MappedPrefix(StringRef Old_, StringRef New_)
static LLVM_ABI void transformPairs(ArrayRef< std::pair< std::string, std::string > > Pairs, SmallVectorImpl< MappedPrefix > &Split)
MappedPrefix getInverse() const
static LLVM_ABI std::optional< MappedPrefix > getFromJoined(StringRef JoinedMapping)
bool operator==(const MappedPrefix &RHS) const
bool operator!=(const MappedPrefix &RHS) const
static LLVM_ABI void transformJoinedIfValid(ArrayRef< StringRef > Joined, SmallVectorImpl< MappedPrefix > &Split)
static LLVM_ABI Error transformJoined(ArrayRef< StringRef > Joined, SmallVectorImpl< MappedPrefix > &Split)
MappedPrefix()=default