File: PrefixMapper.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (223 lines) | stat: -rw-r--r-- 7,529 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//===- PrefixMapper.cpp - Prefix mapping utility --------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "llvm/Support/PrefixMapper.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/StringSaver.h"
#include "llvm/Support/VirtualFileSystem.h"

using namespace llvm;

std::optional<MappedPrefix> MappedPrefix::getFromJoined(StringRef JoinedMapping) {
  auto Equals = JoinedMapping.find('=');
  if (Equals == StringRef::npos)
    return std::nullopt;
  StringRef Old = JoinedMapping.substr(0, Equals);
  StringRef New = JoinedMapping.substr(Equals + 1);
  return MappedPrefix{Old, New};
}

template <bool StopOnInvalid, class StringT>
static std::optional<StringRef>
transformJoinedImpl(ArrayRef<StringT> JoinedMappings,
                    SmallVectorImpl<MappedPrefix> &Mappings) {
  size_t OriginalSize = Mappings.size();
  for (StringRef Joined : JoinedMappings) {
    if (std::optional<MappedPrefix> Split = MappedPrefix::getFromJoined(Joined)) {
      Mappings.push_back(*Split);
      continue;
    }
    if (!StopOnInvalid)
      continue;
    Mappings.resize(OriginalSize);
    return Joined;
  }
  return std::nullopt;
}

static Error makeErrorForInvalidJoin(std::optional<StringRef> Joined) {
  if (!Joined)
    return Error::success();
  return createStringError(inconvertibleErrorCode(),
                           "invalid prefix map: '" + *Joined + "'");
}

Error MappedPrefix::transformJoined(ArrayRef<StringRef> Joined,
                                    SmallVectorImpl<MappedPrefix> &Split) {
  return makeErrorForInvalidJoin(
      transformJoinedImpl</*StopOnInvalid*/ true>(Joined, Split));
}

Error MappedPrefix::transformJoined(ArrayRef<std::string> Joined,
                                    SmallVectorImpl<MappedPrefix> &Split) {
  return makeErrorForInvalidJoin(
      transformJoinedImpl</*StopOnInvalid*/ true>(Joined, Split));
}

void MappedPrefix::transformJoinedIfValid(
    ArrayRef<StringRef> Joined, SmallVectorImpl<MappedPrefix> &Split) {
  transformJoinedImpl</*StopOnInvalid*/ false>(Joined, Split);
}

void MappedPrefix::transformJoinedIfValid(
    ArrayRef<std::string> Joined, SmallVectorImpl<MappedPrefix> &Split) {
  transformJoinedImpl</*StopOnInvalid*/ false>(Joined, Split);
}

/// FIXME: Copy/pasted from llvm/lib/Support/Path.cpp.
static bool startsWith(StringRef Path, StringRef Prefix,
                       sys::path::Style PathStyle) {
  if (PathStyle == sys::path::Style::posix ||
      (PathStyle == sys::path::Style::native &&
       sys::path::is_style_posix(sys::path::Style::native)))
    return Path.startswith(Prefix);

  if (Path.size() < Prefix.size())
    return false;

  // Windows prefix matching : case and separator insensitive
  for (size_t I = 0, E = Prefix.size(); I != E; ++I) {
    bool SepPath = sys::path::is_separator(Path[I], PathStyle);
    bool SepPrefix = sys::path::is_separator(Prefix[I], PathStyle);
    if (SepPath != SepPrefix)
      return false;
    if (SepPath)
      continue;
    if (toLower(Path[I]) != toLower(Prefix[I]))
      return false;
  }
  return true;
}

std::optional<StringRef> PrefixMapper::mapImpl(StringRef Path,
                                          SmallVectorImpl<char> &Storage) {
  for (const MappedPrefix &Map : Mappings) {
    StringRef Old = Map.Old;
    StringRef New = Map.New;
    if (!startsWith(Path, Old, PathStyle))
      continue;
    StringRef Suffix = Path.drop_front(Old.size());
    if (Suffix.empty())
      return New; // Exact match.

    // Don't remap "/old-suffix" with mapping "/old=/new".
    if (!llvm::sys::path::is_separator(Suffix.front(), PathStyle))
      continue;

    // Drop the separator, append, and return.
    Storage.assign(New.begin(), New.end());
    llvm::sys::path::append(Storage, PathStyle, Suffix.drop_front());
    return StringRef(Storage.begin(), Storage.size());
  }
  return std::nullopt;
}

bool PrefixMapper::map(StringRef Path, SmallVectorImpl<char> &NewPath) {
  NewPath.clear();
  std::optional<StringRef> Mapped = mapImpl(Path, NewPath);
  if (!NewPath.empty())
    return true;
  bool Modified = Mapped.has_value();
  if (!Mapped)
    Mapped = Path;
  NewPath.assign(Mapped->begin(), Mapped->end());
  return Modified;
}

bool PrefixMapper::map(StringRef Path, std::string &NewPath) {
  SmallString<256> Storage;
  std::optional<StringRef> Mapped = mapImpl(Path, Storage);
  NewPath = Mapped ? Mapped->str() : Path.str();
  return Mapped.has_value();
}

std::string PrefixMapper::mapToString(StringRef Path) {
  SmallString<256> Storage;
  std::optional<StringRef> Mapped = mapImpl(Path, Storage);
  return Mapped ? Mapped->str() : Path.str();
}

bool PrefixMapper::mapInPlace(SmallVectorImpl<char> &Path) {
  SmallString<256> Storage;
  std::optional<StringRef> Mapped =
      mapImpl(StringRef(Path.begin(), Path.size()), Storage);
  if (!Mapped)
    return false;
  if (Storage.empty())
    Path.assign(Mapped->begin(), Mapped->end());
  else
    Storage.swap(Path);
  return true;
}

bool PrefixMapper::mapInPlace(std::string &Path) {
  SmallString<256> Storage;
  std::optional<StringRef> Mapped = mapImpl(Path, Storage);
  if (!Mapped)
    return false;
  Path.assign(Mapped->begin(), Mapped->size());
  return true;
}

void PrefixMapper::sort() {
  // FIXME: Only works for posix right now since it doesn't handle case- and
  // separator-insensitivity.
  std::stable_sort(Mappings.begin(), Mappings.end(),
                   [](const MappedPrefix &LHS, const MappedPrefix &RHS) {
                     return LHS.Old > RHS.Old;
                   });
}

TreePathPrefixMapper::TreePathPrefixMapper(
    IntrusiveRefCntPtr<vfs::FileSystem> FS, sys::path::Style PathStyle)
    : PrefixMapper(PathStyle), FS(std::move(FS)) {}

TreePathPrefixMapper::~TreePathPrefixMapper() = default;

std::optional<StringRef>
TreePathPrefixMapper::mapImpl(StringRef Path, SmallVectorImpl<char> &Storage) {
  StringRef TreePath = getTreePath(Path);
  std::optional<StringRef> Mapped = PrefixMapper::mapImpl(TreePath, Storage);
  if (Mapped)
    return *Mapped;
  if (TreePath != Path)
    return TreePath;
  return std::nullopt;
}

StringRef TreePathPrefixMapper::getTreePath(StringRef Path) {
  if (Path.empty())
    return Path;
  auto Entry = FS->getDirectoryEntry(Path, /*FollowSymlinks=*/false);
  if (!Entry) {
    consumeError(Entry.takeError());
    return Path;
  }
  return (*Entry)->getTreePath();
}

void TreePathPrefixMapper::add(const MappedPrefix &Mapping) {
  // Add the original mapping. If it contains a non-canonical path, this will
  // only affect the behaviour when later mapping a path that cannot be
  // canonicalized, since a non-canonical prefix cannot match a canonical path.
  PrefixMapper::add(Mapping);
  StringRef Old = getTreePath(Mapping.Old);
  StringRef New = Mapping.New;
  // Add the canonical prefix mapping, if different.
  if (Old != Mapping.Old)
    PrefixMapper::add(MappedPrefix{Old, New});
}

StringRef
TreePathPrefixMapper::mapDirEntry(const vfs::CachedDirectoryEntry &Entry,
                                  SmallVectorImpl<char> &Storage) {
  StringRef TreePath = Entry.getTreePath();
  std::optional<StringRef> Mapped = PrefixMapper::mapImpl(TreePath, Storage);
  return Mapped ? *Mapped : TreePath;
}