LLVM 24.0.0git
PrefixMapper.cpp
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
17
18using namespace llvm;
19
20std::optional<MappedPrefix>
22 auto Equals = JoinedMapping.find('=');
23 if (Equals == StringRef::npos)
24 return std::nullopt;
25 StringRef Old = JoinedMapping.substr(0, Equals);
26 StringRef New = JoinedMapping.substr(Equals + 1);
27 return MappedPrefix{Old, New};
28}
29
30template <bool StopOnInvalid, class StringT>
31static std::optional<StringRef>
34 size_t OriginalSize = Mappings.size();
35 for (StringRef Joined : JoinedMappings) {
36 if (std::optional<MappedPrefix> Split =
38 Mappings.push_back(*Split);
39 continue;
40 }
41 if (!StopOnInvalid)
42 continue;
43 Mappings.resize(OriginalSize);
44 return Joined;
45 }
46 return std::nullopt;
47}
48
49static Error makeErrorForInvalidJoin(std::optional<StringRef> Joined) {
50 if (!Joined)
51 return Error::success();
53 "invalid prefix map: '" + *Joined + "'");
54}
55
61
67
70 transformJoinedImpl</*StopOnInvalid*/ false>(Joined, Split);
71}
72
75 transformJoinedImpl</*StopOnInvalid*/ false>(Joined, Split);
76}
77
79 ArrayRef<std::pair<std::string, std::string>> Pairs,
81 for (auto &Pair : Pairs) {
82 Split.push_back(MappedPrefix{Pair.first, Pair.second});
83 }
84}
85
86/// FIXME: Copy/pasted from llvm/lib/Support/Path.cpp.
87static bool startsWith(StringRef Path, StringRef Prefix,
88 sys::path::Style PathStyle) {
89 if (PathStyle == sys::path::Style::posix ||
90 (PathStyle == sys::path::Style::native &&
92 return Path.starts_with(Prefix);
93
94 if (Path.size() < Prefix.size())
95 return false;
96
97 // Windows prefix matching : case and separator insensitive
98 for (size_t I = 0, E = Prefix.size(); I != E; ++I) {
99 bool SepPath = sys::path::is_separator(Path[I], PathStyle);
100 bool SepPrefix = sys::path::is_separator(Prefix[I], PathStyle);
101 if (SepPath != SepPrefix)
102 return false;
103 if (SepPath)
104 continue;
105 if (toLower(Path[I]) != toLower(Prefix[I]))
106 return false;
107 }
108 return true;
109}
110
111std::optional<StringRef>
113 for (const MappedPrefix &Map : Mappings) {
114 StringRef Old = Map.Old;
115 StringRef New = Map.New;
116 if (!startsWith(Path, Old, PathStyle))
117 continue;
118 StringRef Suffix = Path.drop_front(Old.size());
119 if (Suffix.empty())
120 return New; // Exact match.
121
122 // Don't remap "/old-suffix" with mapping "/old=/new".
123 bool OldEndsWithSeparator =
124 !Old.empty() && sys::path::is_separator(Old.back(), PathStyle);
125 if (!OldEndsWithSeparator &&
126 !sys::path::is_separator(Suffix.front(), PathStyle))
127 continue;
128
129 Storage.assign(New.begin(), New.end());
130 sys::path::append(Storage, PathStyle, Suffix);
131 return StringRef(Storage.begin(), Storage.size());
132 }
133 return std::nullopt;
134}
135
137 NewPath.clear();
138 std::optional<StringRef> Mapped = mapImpl(Path, NewPath);
139 if (!NewPath.empty())
140 return true;
141 bool Modified = Mapped.has_value();
142 if (!Mapped)
143 Mapped = Path;
144 NewPath.assign(Mapped->begin(), Mapped->end());
145 return Modified;
146}
147
148bool PrefixMapper::map(StringRef Path, std::string &NewPath) const {
149 SmallString<256> Storage;
150 std::optional<StringRef> Mapped = mapImpl(Path, Storage);
151 NewPath = Mapped ? Mapped->str() : Path.str();
152 return Mapped.has_value();
153}
154
155std::string PrefixMapper::mapToString(StringRef Path) const {
156 SmallString<256> Storage;
157 std::optional<StringRef> Mapped = mapImpl(Path, Storage);
158 return Mapped ? Mapped->str() : Path.str();
159}
160
162 SmallString<256> Storage;
163 std::optional<StringRef> Mapped =
164 mapImpl(StringRef(Path.begin(), Path.size()), Storage);
165 if (!Mapped)
166 return false;
167 if (Storage.empty())
168 Path.assign(Mapped->begin(), Mapped->end());
169 else
170 Storage.swap(Path);
171 return true;
172}
173
174bool PrefixMapper::mapInPlace(std::string &Path) const {
175 SmallString<256> Storage;
176 std::optional<StringRef> Mapped = mapImpl(Path, Storage);
177 if (!Mapped)
178 return false;
179 Path.assign(Mapped->begin(), Mapped->size());
180 return true;
181}
182
184 // FIXME: Compare the way startsWith() matches, so that sorting is case- and
185 // separator-insensitive with a Windows path style.
186 std::stable_sort(Mappings.begin(), Mappings.end(),
187 [](const MappedPrefix &LHS, const MappedPrefix &RHS) {
188 return LHS.Old > RHS.Old;
189 });
190}
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define I(x, y, z)
Definition MD5.cpp:57
static bool startsWith(std::string_view S, std::string_view PrefixA, std::string_view PrefixB, bool A)
static Error makeErrorForInvalidJoin(std::optional< StringRef > Joined)
static std::optional< StringRef > transformJoinedImpl(ArrayRef< StringT > JoinedMappings, SmallVectorImpl< MappedPrefix > &Mappings)
Prefix mapping utility.
This file defines the SmallString class.
This file contains some functions that are useful when dealing with strings.
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
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
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.
bool map(StringRef Path, SmallVectorImpl< char > &NewPath) const
Map Path, and saving the new (or existing) path in NewPath.
bool mapInPlace(SmallVectorImpl< char > &Path) const
Map Path in place.
void sort()
A path-based reverse lexicographic sort, putting deeper paths first so that deeper paths are prioriti...
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:28
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void assign(size_type NumElts, ValueParamT Elt)
void swap(SmallVectorImpl &RHS)
void resize(size_type N)
void push_back(const T &Elt)
LLVM_NO_SANITIZE_CFI_UNRELATED_CAST iterator begin()
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
static constexpr size_t npos
Definition StringRef.h:58
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:597
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
char back() const
Get the last character in the string.
Definition StringRef.h:153
constexpr size_t size() const
Get the string size.
Definition StringRef.h:144
char front() const
Get the first character in the string.
Definition StringRef.h:147
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition StringRef.h:290
constexpr bool is_style_posix(Style S)
Check if S uses POSIX path rules.
Definition Path.h:37
LLVM_ABI void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition Path.cpp:467
LLVM_ABI bool is_separator(char value, Style style=Style::native)
Check whether the given char is a path separator on the host OS.
Definition Path.cpp:618
This is an optimization pass for GlobalISel generic memory operations.
char toLower(char x)
Returns the corresponding lowercase character if x is uppercase.
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition Error.cpp:94
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321
static LLVM_ABI void transformPairs(ArrayRef< std::pair< std::string, std::string > > Pairs, SmallVectorImpl< MappedPrefix > &Split)
static LLVM_ABI std::optional< MappedPrefix > getFromJoined(StringRef JoinedMapping)
static LLVM_ABI void transformJoinedIfValid(ArrayRef< StringRef > Joined, SmallVectorImpl< MappedPrefix > &Split)
static LLVM_ABI Error transformJoined(ArrayRef< StringRef > Joined, SmallVectorImpl< MappedPrefix > &Split)
MappedPrefix()=default