| 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 | #include "llvm/Support/PrefixMapper.h" |
| 15 | #include "llvm/ADT/SmallString.h" |
| 16 | #include "llvm/ADT/StringExtras.h" |
| 17 | |
| 18 | using namespace llvm; |
| 19 | |
| 20 | std::optional<MappedPrefix> |
| 21 | MappedPrefix::getFromJoined(StringRef JoinedMapping) { |
| 22 | auto Equals = JoinedMapping.find(C: '='); |
| 23 | if (Equals == StringRef::npos) |
| 24 | return std::nullopt; |
| 25 | StringRef Old = JoinedMapping.substr(Start: 0, N: Equals); |
| 26 | StringRef New = JoinedMapping.substr(Start: Equals + 1); |
| 27 | return MappedPrefix{Old, New}; |
| 28 | } |
| 29 | |
| 30 | template <bool StopOnInvalid, class StringT> |
| 31 | static std::optional<StringRef> |
| 32 | transformJoinedImpl(ArrayRef<StringT> JoinedMappings, |
| 33 | SmallVectorImpl<MappedPrefix> &Mappings) { |
| 34 | size_t OriginalSize = Mappings.size(); |
| 35 | for (StringRef Joined : JoinedMappings) { |
| 36 | if (std::optional<MappedPrefix> Split = |
| 37 | MappedPrefix::getFromJoined(JoinedMapping: Joined)) { |
| 38 | Mappings.push_back(Elt: *Split); |
| 39 | continue; |
| 40 | } |
| 41 | if (!StopOnInvalid) |
| 42 | continue; |
| 43 | Mappings.resize(N: OriginalSize); |
| 44 | return Joined; |
| 45 | } |
| 46 | return std::nullopt; |
| 47 | } |
| 48 | |
| 49 | static Error makeErrorForInvalidJoin(std::optional<StringRef> Joined) { |
| 50 | if (!Joined) |
| 51 | return Error::success(); |
| 52 | return createStringError(EC: inconvertibleErrorCode(), |
| 53 | S: "invalid prefix map: '" + *Joined + "'" ); |
| 54 | } |
| 55 | |
| 56 | Error MappedPrefix::transformJoined(ArrayRef<StringRef> Joined, |
| 57 | SmallVectorImpl<MappedPrefix> &Split) { |
| 58 | return makeErrorForInvalidJoin( |
| 59 | Joined: transformJoinedImpl</*StopOnInvalid*/ true>(JoinedMappings: Joined, Mappings&: Split)); |
| 60 | } |
| 61 | |
| 62 | Error MappedPrefix::transformJoined(ArrayRef<std::string> Joined, |
| 63 | SmallVectorImpl<MappedPrefix> &Split) { |
| 64 | return makeErrorForInvalidJoin( |
| 65 | Joined: transformJoinedImpl</*StopOnInvalid*/ true>(JoinedMappings: Joined, Mappings&: Split)); |
| 66 | } |
| 67 | |
| 68 | void MappedPrefix::transformJoinedIfValid( |
| 69 | ArrayRef<StringRef> Joined, SmallVectorImpl<MappedPrefix> &Split) { |
| 70 | transformJoinedImpl</*StopOnInvalid*/ false>(JoinedMappings: Joined, Mappings&: Split); |
| 71 | } |
| 72 | |
| 73 | void MappedPrefix::transformJoinedIfValid( |
| 74 | ArrayRef<std::string> Joined, SmallVectorImpl<MappedPrefix> &Split) { |
| 75 | transformJoinedImpl</*StopOnInvalid*/ false>(JoinedMappings: Joined, Mappings&: Split); |
| 76 | } |
| 77 | |
| 78 | void MappedPrefix::transformPairs( |
| 79 | ArrayRef<std::pair<std::string, std::string>> Pairs, |
| 80 | SmallVectorImpl<MappedPrefix> &Split) { |
| 81 | for (auto &Pair : Pairs) { |
| 82 | Split.push_back(Elt: MappedPrefix{Pair.first, Pair.second}); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /// FIXME: Copy/pasted from llvm/lib/Support/Path.cpp. |
| 87 | static bool startsWith(StringRef Path, StringRef Prefix, |
| 88 | sys::path::Style PathStyle) { |
| 89 | if (PathStyle == sys::path::Style::posix || |
| 90 | (PathStyle == sys::path::Style::native && |
| 91 | sys::path::is_style_posix(S: 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(value: Path[I], style: PathStyle); |
| 100 | bool SepPrefix = sys::path::is_separator(value: Prefix[I], style: PathStyle); |
| 101 | if (SepPath != SepPrefix) |
| 102 | return false; |
| 103 | if (SepPath) |
| 104 | continue; |
| 105 | if (toLower(x: Path[I]) != toLower(x: Prefix[I])) |
| 106 | return false; |
| 107 | } |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | std::optional<StringRef> |
| 112 | PrefixMapper::mapImpl(StringRef Path, SmallVectorImpl<char> &Storage) const { |
| 113 | for (const MappedPrefix &Map : Mappings) { |
| 114 | StringRef Old = Map.Old; |
| 115 | StringRef New = Map.New; |
| 116 | if (!startsWith(Path, Prefix: Old, PathStyle)) |
| 117 | continue; |
| 118 | StringRef Suffix = Path.drop_front(N: 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(value: Old.back(), style: PathStyle); |
| 125 | if (!OldEndsWithSeparator && |
| 126 | !sys::path::is_separator(value: Suffix.front(), style: PathStyle)) |
| 127 | continue; |
| 128 | |
| 129 | Storage.assign(in_start: New.begin(), in_end: New.end()); |
| 130 | sys::path::append(path&: Storage, style: PathStyle, a: Suffix); |
| 131 | return StringRef(Storage.begin(), Storage.size()); |
| 132 | } |
| 133 | return std::nullopt; |
| 134 | } |
| 135 | |
| 136 | bool PrefixMapper::map(StringRef Path, SmallVectorImpl<char> &NewPath) const { |
| 137 | NewPath.clear(); |
| 138 | std::optional<StringRef> Mapped = mapImpl(Path, Storage&: NewPath); |
| 139 | if (!NewPath.empty()) |
| 140 | return true; |
| 141 | bool Modified = Mapped.has_value(); |
| 142 | if (!Mapped) |
| 143 | Mapped = Path; |
| 144 | NewPath.assign(in_start: Mapped->begin(), in_end: Mapped->end()); |
| 145 | return Modified; |
| 146 | } |
| 147 | |
| 148 | bool 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 | |
| 155 | std::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 | |
| 161 | bool PrefixMapper::mapInPlace(SmallVectorImpl<char> &Path) const { |
| 162 | SmallString<256> Storage; |
| 163 | std::optional<StringRef> Mapped = |
| 164 | mapImpl(Path: StringRef(Path.begin(), Path.size()), Storage); |
| 165 | if (!Mapped) |
| 166 | return false; |
| 167 | if (Storage.empty()) |
| 168 | Path.assign(in_start: Mapped->begin(), in_end: Mapped->end()); |
| 169 | else |
| 170 | Storage.swap(RHS&: Path); |
| 171 | return true; |
| 172 | } |
| 173 | |
| 174 | bool 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(s: Mapped->begin(), n: Mapped->size()); |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | void PrefixMapper::sort() { |
| 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(first: Mappings.begin(), last: Mappings.end(), |
| 187 | comp: [](const MappedPrefix &LHS, const MappedPrefix &RHS) { |
| 188 | return LHS.Old > RHS.Old; |
| 189 | }); |
| 190 | } |
| 191 | |