blob: d60c1359b180b312226cb0fb601e41e6b768a35a [file] [log] [blame]
Rafael Espindolaf1fc3822013-06-26 19:33:03 +00001//===-- Path.cpp - Implement OS Path Concept ------------------------------===//
Michael J. Spencerebad2f92010-11-29 22:28:51 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Michael J. Spencerebad2f92010-11-29 22:28:51 +00006//
7//===----------------------------------------------------------------------===//
8//
Rafael Espindolaf1fc3822013-06-26 19:33:03 +00009// This file implements the operating system Path API.
Michael J. Spencerebad2f92010-11-29 22:28:51 +000010//
11//===----------------------------------------------------------------------===//
12
Zachary Turner82a0c972017-03-20 23:33:18 +000013#include "llvm/Support/Path.h"
14#include "llvm/ADT/ArrayRef.h"
Nico Weber432a3882018-04-30 14:59:11 +000015#include "llvm/Config/llvm-config.h"
Rui Ueyama5c69ff52014-09-11 22:34:32 +000016#include "llvm/Support/Endian.h"
Rafael Espindola2a826e42014-06-13 17:20:48 +000017#include "llvm/Support/Errc.h"
Michael J. Spencerebad2f92010-11-29 22:28:51 +000018#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/Support/FileSystem.h"
Aaron Ballman07e76182014-02-11 03:40:14 +000020#include "llvm/Support/Process.h"
Rafael Espindola58fe67a2017-11-13 18:33:44 +000021#include "llvm/Support/Signals.h"
Michael J. Spencerebad2f92010-11-29 22:28:51 +000022#include <cctype>
Michael J. Spencer848f46b2010-12-28 01:49:01 +000023#include <cstring>
Rafael Espindola4526b1d2013-06-18 17:01:00 +000024
25#if !defined(_MSC_VER) && !defined(__MINGW32__)
Douglas Gregora86ddf02013-03-21 21:46:10 +000026#include <unistd.h>
Rafael Espindola4526b1d2013-06-18 17:01:00 +000027#else
28#include <io.h>
Douglas Gregora86ddf02013-03-21 21:46:10 +000029#endif
Michael J. Spencerebad2f92010-11-29 22:28:51 +000030
Rafael Espindola7a0b6402014-02-24 03:07:41 +000031using namespace llvm;
Rui Ueyama3206b792015-03-02 21:19:12 +000032using namespace llvm::support::endian;
Rafael Espindola7a0b6402014-02-24 03:07:41 +000033
Michael J. Spencerebad2f92010-11-29 22:28:51 +000034namespace {
35 using llvm::StringRef;
Zhanyong Wan606bb1a2011-02-11 21:24:40 +000036 using llvm::sys::path::is_separator;
Zachary Turner5c5091f2017-03-16 22:28:04 +000037 using llvm::sys::path::Style;
Michael J. Spencerebad2f92010-11-29 22:28:51 +000038
Zachary Turner5c5091f2017-03-16 22:28:04 +000039 inline Style real_style(Style style) {
Nico Weber712e8d22018-04-29 00:45:03 +000040#ifdef _WIN32
Zachary Turner5c5091f2017-03-16 22:28:04 +000041 return (style == Style::posix) ? Style::posix : Style::windows;
Michael J. Spencerebad2f92010-11-29 22:28:51 +000042#else
Zachary Turner5c5091f2017-03-16 22:28:04 +000043 return (style == Style::windows) ? Style::windows : Style::posix;
Michael J. Spencerebad2f92010-11-29 22:28:51 +000044#endif
Zachary Turner5c5091f2017-03-16 22:28:04 +000045 }
Michael J. Spencerebad2f92010-11-29 22:28:51 +000046
Zachary Turner5c5091f2017-03-16 22:28:04 +000047 inline const char *separators(Style style) {
48 if (real_style(style) == Style::windows)
49 return "\\/";
50 return "/";
51 }
52
53 inline char preferred_separator(Style style) {
54 if (real_style(style) == Style::windows)
55 return '\\';
56 return '/';
57 }
58
59 StringRef find_first_component(StringRef path, Style style) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +000060 // Look for this first component in the following order.
61 // * empty (in this case we return an empty string)
62 // * either C: or {//,\\}net.
63 // * {/,\}
Michael J. Spencerebad2f92010-11-29 22:28:51 +000064 // * {file,directory}name
65
66 if (path.empty())
67 return path;
68
Zachary Turner5c5091f2017-03-16 22:28:04 +000069 if (real_style(style) == Style::windows) {
70 // C:
71 if (path.size() >= 2 &&
72 std::isalpha(static_cast<unsigned char>(path[0])) && path[1] == ':')
73 return path.substr(0, 2);
74 }
Michael J. Spencerebad2f92010-11-29 22:28:51 +000075
76 // //net
Zachary Turner5c5091f2017-03-16 22:28:04 +000077 if ((path.size() > 2) && is_separator(path[0], style) &&
78 path[0] == path[1] && !is_separator(path[2], style)) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +000079 // Find the next directory separator.
Zachary Turner5c5091f2017-03-16 22:28:04 +000080 size_t end = path.find_first_of(separators(style), 2);
Benjamin Kramerffa42ce2010-12-17 20:27:37 +000081 return path.substr(0, end);
Michael J. Spencerebad2f92010-11-29 22:28:51 +000082 }
83
84 // {/,\}
Zachary Turner5c5091f2017-03-16 22:28:04 +000085 if (is_separator(path[0], style))
Benjamin Kramerffa42ce2010-12-17 20:27:37 +000086 return path.substr(0, 1);
Michael J. Spencerebad2f92010-11-29 22:28:51 +000087
Michael J. Spencerebad2f92010-11-29 22:28:51 +000088 // * {file,directory}name
Zachary Turner5c5091f2017-03-16 22:28:04 +000089 size_t end = path.find_first_of(separators(style));
Benjamin Kramerffa42ce2010-12-17 20:27:37 +000090 return path.substr(0, end);
Michael J. Spencerebad2f92010-11-29 22:28:51 +000091 }
Michael J. Spencer545cbdf2010-11-30 23:28:07 +000092
Pavel Labathd20289b2018-05-09 13:21:16 +000093 // Returns the first character of the filename in str. For paths ending in
94 // '/', it returns the position of the '/'.
Zachary Turner5c5091f2017-03-16 22:28:04 +000095 size_t filename_pos(StringRef str, Style style) {
Zachary Turner5c5091f2017-03-16 22:28:04 +000096 if (str.size() > 0 && is_separator(str[str.size() - 1], style))
Michael J. Spencer545cbdf2010-11-30 23:28:07 +000097 return str.size() - 1;
98
Zachary Turner5c5091f2017-03-16 22:28:04 +000099 size_t pos = str.find_last_of(separators(style), str.size() - 1);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000100
Zachary Turner5c5091f2017-03-16 22:28:04 +0000101 if (real_style(style) == Style::windows) {
102 if (pos == StringRef::npos)
103 pos = str.find_last_of(':', str.size() - 2);
104 }
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000105
Zachary Turner5c5091f2017-03-16 22:28:04 +0000106 if (pos == StringRef::npos || (pos == 1 && is_separator(str[0], style)))
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000107 return 0;
108
109 return pos + 1;
110 }
111
Pavel Labathd20289b2018-05-09 13:21:16 +0000112 // Returns the position of the root directory in str. If there is no root
113 // directory in str, it returns StringRef::npos.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000114 size_t root_dir_start(StringRef str, Style style) {
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000115 // case "c:/"
Zachary Turner5c5091f2017-03-16 22:28:04 +0000116 if (real_style(style) == Style::windows) {
117 if (str.size() > 2 && str[1] == ':' && is_separator(str[2], style))
118 return 2;
119 }
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000120
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000121 // case "//net"
Zachary Turner5c5091f2017-03-16 22:28:04 +0000122 if (str.size() > 3 && is_separator(str[0], style) && str[0] == str[1] &&
123 !is_separator(str[2], style)) {
124 return str.find_first_of(separators(style), 2);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000125 }
126
127 // case "/"
Zachary Turner5c5091f2017-03-16 22:28:04 +0000128 if (str.size() > 0 && is_separator(str[0], style))
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000129 return 0;
130
131 return StringRef::npos;
132 }
133
Pavel Labathd20289b2018-05-09 13:21:16 +0000134 // Returns the position past the end of the "parent path" of path. The parent
135 // path will not end in '/', unless the parent is the root directory. If the
136 // path has no parent, 0 is returned.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000137 size_t parent_path_end(StringRef path, Style style) {
138 size_t end_pos = filename_pos(path, style);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000139
Zachary Turner5c5091f2017-03-16 22:28:04 +0000140 bool filename_was_sep =
141 path.size() > 0 && is_separator(path[end_pos], style);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000142
Pavel Labathd20289b2018-05-09 13:21:16 +0000143 // Skip separators until we reach root dir (or the start of the string).
144 size_t root_dir_pos = root_dir_start(path, style);
145 while (end_pos > 0 &&
146 (root_dir_pos == StringRef::npos || end_pos > root_dir_pos) &&
Zachary Turner5c5091f2017-03-16 22:28:04 +0000147 is_separator(path[end_pos - 1], style))
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000148 --end_pos;
149
Pavel Labathd20289b2018-05-09 13:21:16 +0000150 if (end_pos == root_dir_pos && !filename_was_sep) {
151 // We've reached the root dir and the input path was *not* ending in a
152 // sequence of slashes. Include the root dir in the parent path.
153 return root_dir_pos + 1;
154 }
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000155
Pavel Labathd20289b2018-05-09 13:21:16 +0000156 // Otherwise, just include before the last slash.
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000157 return end_pos;
158 }
Zhanyong Wan606bb1a2011-02-11 21:24:40 +0000159} // end unnamed namespace
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000160
Rafael Espindolae79a8722013-06-28 03:48:47 +0000161enum FSEntity {
162 FS_Dir,
163 FS_File,
164 FS_Name
165};
166
Rafael Espindolad19c2e82017-11-27 23:44:11 +0000167static std::error_code
168createUniqueEntity(const Twine &Model, int &ResultFD,
169 SmallVectorImpl<char> &ResultPath, bool MakeAbsolute,
170 unsigned Mode, FSEntity Type,
Zachary Turner1f67a3c2018-06-07 19:58:58 +0000171 sys::fs::OpenFlags Flags = sys::fs::OF_None) {
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000172
Bob Haarman112ebb62018-08-02 18:27:21 +0000173 // Limit the number of attempts we make, so that we don't infinite loop. E.g.
174 // "permission denied" could be for a specific file (so we retry with a
175 // different name) or for the whole directory (retry would always fail).
176 // Checking which is racy, so we try a number of times, then give up.
Bob Haarman9b36f512018-08-02 17:41:38 +0000177 std::error_code EC;
178 for (int Retries = 128; Retries > 0; --Retries) {
Jake Ehrlich5049c342019-03-18 20:35:18 +0000179 sys::fs::createUniquePath(Model, ResultPath, MakeAbsolute);
Bob Haarman9b36f512018-08-02 17:41:38 +0000180 // Try to open + create the file.
181 switch (Type) {
182 case FS_File: {
183 EC = sys::fs::openFileForReadWrite(Twine(ResultPath.begin()), ResultFD,
184 sys::fs::CD_CreateNew, Flags, Mode);
185 if (EC) {
186 // errc::permission_denied happens on Windows when we try to open a file
187 // that has been marked for deletion.
188 if (EC == errc::file_exists || EC == errc::permission_denied)
189 continue;
190 return EC;
191 }
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000192
Rafael Espindola281f23a2014-09-11 20:30:02 +0000193 return std::error_code();
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000194 }
Bob Haarman9b36f512018-08-02 17:41:38 +0000195
196 case FS_Name: {
197 EC = sys::fs::access(ResultPath.begin(), sys::fs::AccessMode::Exist);
198 if (EC == errc::no_such_file_or_directory)
199 return std::error_code();
200 if (EC)
201 return EC;
202 continue;
203 }
204
205 case FS_Dir: {
206 EC = sys::fs::create_directory(ResultPath.begin(), false);
207 if (EC) {
208 if (EC == errc::file_exists)
209 continue;
210 return EC;
211 }
212 return std::error_code();
213 }
214 }
215 llvm_unreachable("Invalid Type");
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000216 }
Bob Haarman9b36f512018-08-02 17:41:38 +0000217 return EC;
Rafael Espindola7a0b6402014-02-24 03:07:41 +0000218}
Rafael Espindolae79a8722013-06-28 03:48:47 +0000219
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000220namespace llvm {
221namespace sys {
222namespace path {
223
Zachary Turner5c5091f2017-03-16 22:28:04 +0000224const_iterator begin(StringRef path, Style style) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000225 const_iterator i;
226 i.Path = path;
Zachary Turner5c5091f2017-03-16 22:28:04 +0000227 i.Component = find_first_component(path, style);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000228 i.Position = 0;
Zachary Turner5c5091f2017-03-16 22:28:04 +0000229 i.S = style;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000230 return i;
231}
232
Benjamin Kramer292b44b2010-12-17 18:19:06 +0000233const_iterator end(StringRef path) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000234 const_iterator i;
235 i.Path = path;
236 i.Position = path.size();
237 return i;
238}
239
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000240const_iterator &const_iterator::operator++() {
241 assert(Position < Path.size() && "Tried to increment past end!");
242
243 // Increment Position to past the current component
244 Position += Component.size();
245
246 // Check for end.
247 if (Position == Path.size()) {
248 Component = StringRef();
249 return *this;
250 }
251
252 // Both POSIX and Windows treat paths that begin with exactly two separators
253 // specially.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000254 bool was_net = Component.size() > 2 && is_separator(Component[0], S) &&
255 Component[1] == Component[0] && !is_separator(Component[2], S);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000256
257 // Handle separators.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000258 if (is_separator(Path[Position], S)) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000259 // Root dir.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000260 if (was_net ||
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000261 // c:/
Zachary Turner5c5091f2017-03-16 22:28:04 +0000262 (real_style(S) == Style::windows && Component.endswith(":"))) {
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000263 Component = Path.substr(Position, 1);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000264 return *this;
265 }
266
267 // Skip extra separators.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000268 while (Position != Path.size() && is_separator(Path[Position], S)) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000269 ++Position;
270 }
271
Pavel Labathd20289b2018-05-09 13:21:16 +0000272 // Treat trailing '/' as a '.', unless it is the root dir.
273 if (Position == Path.size() && Component != "/") {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000274 --Position;
275 Component = ".";
276 return *this;
277 }
278 }
279
280 // Find next component.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000281 size_t end_pos = Path.find_first_of(separators(S), Position);
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000282 Component = Path.slice(Position, end_pos);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000283
284 return *this;
285}
286
Justin Bogner487e7642014-08-04 17:36:41 +0000287bool const_iterator::operator==(const const_iterator &RHS) const {
288 return Path.begin() == RHS.Path.begin() && Position == RHS.Position;
289}
290
291ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
292 return Position - RHS.Position;
293}
294
Zachary Turner5c5091f2017-03-16 22:28:04 +0000295reverse_iterator rbegin(StringRef Path, Style style) {
Justin Bogner487e7642014-08-04 17:36:41 +0000296 reverse_iterator I;
297 I.Path = Path;
298 I.Position = Path.size();
Zachary Turner5c5091f2017-03-16 22:28:04 +0000299 I.S = style;
Justin Bogner487e7642014-08-04 17:36:41 +0000300 return ++I;
301}
302
303reverse_iterator rend(StringRef Path) {
304 reverse_iterator I;
305 I.Path = Path;
306 I.Component = Path.substr(0, 0);
307 I.Position = 0;
308 return I;
309}
310
311reverse_iterator &reverse_iterator::operator++() {
Zachary Turner5c5091f2017-03-16 22:28:04 +0000312 size_t root_dir_pos = root_dir_start(Path, S);
Pavel Labathd20289b2018-05-09 13:21:16 +0000313
314 // Skip separators unless it's the root directory.
315 size_t end_pos = Position;
316 while (end_pos > 0 && (end_pos - 1) != root_dir_pos &&
317 is_separator(Path[end_pos - 1], S))
318 --end_pos;
319
320 // Treat trailing '/' as a '.', unless it is the root dir.
321 if (Position == Path.size() && !Path.empty() &&
322 is_separator(Path.back(), S) &&
323 (root_dir_pos == StringRef::npos || end_pos - 1 > root_dir_pos)) {
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000324 --Position;
325 Component = ".";
326 return *this;
327 }
328
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000329 // Find next separator.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000330 size_t start_pos = filename_pos(Path.substr(0, end_pos), S);
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000331 Component = Path.slice(start_pos, end_pos);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000332 Position = start_pos;
333 return *this;
334}
335
Justin Bogner487e7642014-08-04 17:36:41 +0000336bool reverse_iterator::operator==(const reverse_iterator &RHS) const {
337 return Path.begin() == RHS.Path.begin() && Component == RHS.Component &&
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000338 Position == RHS.Position;
339}
340
Filipe Cabecinhas78949382016-04-29 16:48:07 +0000341ptrdiff_t reverse_iterator::operator-(const reverse_iterator &RHS) const {
342 return Position - RHS.Position;
343}
344
Zachary Turner5c5091f2017-03-16 22:28:04 +0000345StringRef root_path(StringRef path, Style style) {
346 const_iterator b = begin(path, style), pos = b, e = end(path);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000347 if (b != e) {
Zachary Turner5c5091f2017-03-16 22:28:04 +0000348 bool has_net =
349 b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
350 bool has_drive = (real_style(style) == Style::windows) && b->endswith(":");
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000351
352 if (has_net || has_drive) {
Zachary Turner5c5091f2017-03-16 22:28:04 +0000353 if ((++pos != e) && is_separator((*pos)[0], style)) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000354 // {C:/,//net/}, so get the first two components.
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000355 return path.substr(0, b->size() + pos->size());
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000356 } else {
357 // just {C:,//net}, return the first component.
Michael J. Spencerf616b212010-12-07 17:04:04 +0000358 return *b;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000359 }
360 }
361
362 // POSIX style root directory.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000363 if (is_separator((*b)[0], style)) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000364 return *b;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000365 }
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000366 }
367
Michael J. Spencerf616b212010-12-07 17:04:04 +0000368 return StringRef();
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000369}
370
Zachary Turner5c5091f2017-03-16 22:28:04 +0000371StringRef root_name(StringRef path, Style style) {
372 const_iterator b = begin(path, style), e = end(path);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000373 if (b != e) {
Zachary Turner5c5091f2017-03-16 22:28:04 +0000374 bool has_net =
375 b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
376 bool has_drive = (real_style(style) == Style::windows) && b->endswith(":");
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000377
378 if (has_net || has_drive) {
379 // just {C:,//net}, return the first component.
Michael J. Spencerf616b212010-12-07 17:04:04 +0000380 return *b;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000381 }
382 }
383
384 // No path or no name.
Michael J. Spencerf616b212010-12-07 17:04:04 +0000385 return StringRef();
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000386}
387
Zachary Turner5c5091f2017-03-16 22:28:04 +0000388StringRef root_directory(StringRef path, Style style) {
389 const_iterator b = begin(path, style), pos = b, e = end(path);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000390 if (b != e) {
Zachary Turner5c5091f2017-03-16 22:28:04 +0000391 bool has_net =
392 b->size() > 2 && is_separator((*b)[0], style) && (*b)[1] == (*b)[0];
393 bool has_drive = (real_style(style) == Style::windows) && b->endswith(":");
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000394
395 if ((has_net || has_drive) &&
396 // {C:,//net}, skip to the next component.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000397 (++pos != e) && is_separator((*pos)[0], style)) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000398 return *pos;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000399 }
400
401 // POSIX style root directory.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000402 if (!has_net && is_separator((*b)[0], style)) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000403 return *b;
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000404 }
405 }
406
407 // No path or no root.
Michael J. Spencerf616b212010-12-07 17:04:04 +0000408 return StringRef();
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000409}
410
Zachary Turner5c5091f2017-03-16 22:28:04 +0000411StringRef relative_path(StringRef path, Style style) {
412 StringRef root = root_path(path, style);
Michael J. Spencere6462392012-02-29 00:06:24 +0000413 return path.substr(root.size());
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000414}
415
Zachary Turner5c5091f2017-03-16 22:28:04 +0000416void append(SmallVectorImpl<char> &path, Style style, const Twine &a,
417 const Twine &b, const Twine &c, const Twine &d) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000418 SmallString<32> a_storage;
419 SmallString<32> b_storage;
420 SmallString<32> c_storage;
421 SmallString<32> d_storage;
422
423 SmallVector<StringRef, 4> components;
424 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
425 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
426 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
427 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
428
Pawel Bylica64d08ff2015-10-22 08:12:15 +0000429 for (auto &component : components) {
Zachary Turner5c5091f2017-03-16 22:28:04 +0000430 bool path_has_sep =
431 !path.empty() && is_separator(path[path.size() - 1], style);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000432 if (path_has_sep) {
433 // Strip separators from beginning of component.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000434 size_t loc = component.find_first_not_of(separators(style));
Pawel Bylica64d08ff2015-10-22 08:12:15 +0000435 StringRef c = component.substr(loc);
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000436
437 // Append it.
438 path.append(c.begin(), c.end());
439 continue;
440 }
441
Benjamin Kramer324d96b2017-08-09 22:06:32 +0000442 bool component_has_sep =
443 !component.empty() && is_separator(component[0], style);
444 if (!component_has_sep &&
445 !(path.empty() || has_root_name(component, style))) {
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000446 // Add a separator.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000447 path.push_back(preferred_separator(style));
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000448 }
449
Pawel Bylica64d08ff2015-10-22 08:12:15 +0000450 path.append(component.begin(), component.end());
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000451 }
Michael J. Spencerebad2f92010-11-29 22:28:51 +0000452}
453
Zachary Turner5c5091f2017-03-16 22:28:04 +0000454void append(SmallVectorImpl<char> &path, const Twine &a, const Twine &b,
455 const Twine &c, const Twine &d) {
456 append(path, Style::native, a, b, c, d);
Argyrios Kyrtzidisa61736f2011-02-15 17:51:19 +0000457}
458
Zachary Turner5c5091f2017-03-16 22:28:04 +0000459void append(SmallVectorImpl<char> &path, const_iterator begin,
460 const_iterator end, Style style) {
461 for (; begin != end; ++begin)
462 path::append(path, style, *begin);
463}
464
465StringRef parent_path(StringRef path, Style style) {
466 size_t end_pos = parent_path_end(path, style);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000467 if (end_pos == StringRef::npos)
Michael J. Spencerf616b212010-12-07 17:04:04 +0000468 return StringRef();
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000469 else
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000470 return path.substr(0, end_pos);
Michael J. Spencer545cbdf2010-11-30 23:28:07 +0000471}
472
Zachary Turner5c5091f2017-03-16 22:28:04 +0000473void remove_filename(SmallVectorImpl<char> &path, Style style) {
474 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()), style);
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000475 if (end_pos != StringRef::npos)
476 path.set_size(end_pos);
Michael J. Spencer9c594092010-12-01 00:52:28 +0000477}
478
Zachary Turner5c5091f2017-03-16 22:28:04 +0000479void replace_extension(SmallVectorImpl<char> &path, const Twine &extension,
480 Style style) {
Michael J. Spencerfb3a95d2010-12-01 00:52:55 +0000481 StringRef p(path.begin(), path.size());
482 SmallString<32> ext_storage;
483 StringRef ext = extension.toStringRef(ext_storage);
484
485 // Erase existing extension.
486 size_t pos = p.find_last_of('.');
Zachary Turner5c5091f2017-03-16 22:28:04 +0000487 if (pos != StringRef::npos && pos >= filename_pos(p, style))
Michael J. Spencerfb3a95d2010-12-01 00:52:55 +0000488 path.set_size(pos);
489
490 // Append '.' if needed.
491 if (ext.size() > 0 && ext[0] != '.')
492 path.push_back('.');
493
494 // Append extension.
495 path.append(ext.begin(), ext.end());
Michael J. Spencerfb3a95d2010-12-01 00:52:55 +0000496}
497
Teresa Johnsonbbd10b42016-05-17 14:45:30 +0000498void replace_path_prefix(SmallVectorImpl<char> &Path,
Zachary Turner5c5091f2017-03-16 22:28:04 +0000499 const StringRef &OldPrefix, const StringRef &NewPrefix,
500 Style style) {
Teresa Johnsonbbd10b42016-05-17 14:45:30 +0000501 if (OldPrefix.empty() && NewPrefix.empty())
502 return;
503
504 StringRef OrigPath(Path.begin(), Path.size());
505 if (!OrigPath.startswith(OldPrefix))
506 return;
507
508 // If prefixes have the same size we can simply copy the new one over.
509 if (OldPrefix.size() == NewPrefix.size()) {
Fangrui Song75709322018-11-17 01:44:25 +0000510 llvm::copy(NewPrefix, Path.begin());
Teresa Johnsonbbd10b42016-05-17 14:45:30 +0000511 return;
512 }
513
514 StringRef RelPath = OrigPath.substr(OldPrefix.size());
515 SmallString<256> NewPath;
Zachary Turner5c5091f2017-03-16 22:28:04 +0000516 path::append(NewPath, style, NewPrefix);
517 path::append(NewPath, style, RelPath);
Teresa Johnsonbbd10b42016-05-17 14:45:30 +0000518 Path.swap(NewPath);
519}
520
Zachary Turner5c5091f2017-03-16 22:28:04 +0000521void native(const Twine &path, SmallVectorImpl<char> &result, Style style) {
Benjamin Kramerbd4ac9b2013-09-11 10:45:21 +0000522 assert((!path.isSingleStringRef() ||
523 path.getSingleStringRef().data() != result.data()) &&
524 "path and result are not allowed to overlap!");
Michael J. Spencer80025002010-12-01 02:48:27 +0000525 // Clear result.
Michael J. Spencer98c7a112010-12-07 01:23:19 +0000526 result.clear();
Michael J. Spencer80025002010-12-01 02:48:27 +0000527 path.toVector(result);
Zachary Turner5c5091f2017-03-16 22:28:04 +0000528 native(result, style);
Benjamin Kramerbd4ac9b2013-09-11 10:45:21 +0000529}
530
Zachary Turner5c5091f2017-03-16 22:28:04 +0000531void native(SmallVectorImpl<char> &Path, Style style) {
Serge Pavlov9c761a32017-03-01 09:38:15 +0000532 if (Path.empty())
533 return;
Zachary Turner5c5091f2017-03-16 22:28:04 +0000534 if (real_style(style) == Style::windows) {
535 std::replace(Path.begin(), Path.end(), '/', '\\');
536 if (Path[0] == '~' && (Path.size() == 1 || is_separator(Path[1], style))) {
537 SmallString<128> PathHome;
538 home_directory(PathHome);
539 PathHome.append(Path.begin() + 1, Path.end());
540 Path = PathHome;
541 }
542 } else {
543 for (auto PI = Path.begin(), PE = Path.end(); PI < PE; ++PI) {
544 if (*PI == '\\') {
545 auto PN = PI + 1;
546 if (PN < PE && *PN == '\\')
547 ++PI; // increment once, the for loop will move over the escaped slash
548 else
549 *PI = '/';
550 }
Rafael Espindolad649b9d2014-08-08 21:29:34 +0000551 }
552 }
Michael J. Spencer80025002010-12-01 02:48:27 +0000553}
554
Zachary Turner5c5091f2017-03-16 22:28:04 +0000555std::string convert_to_slash(StringRef path, Style style) {
556 if (real_style(style) != Style::windows)
557 return path;
558
Rui Ueyama3e649032017-01-09 01:47:15 +0000559 std::string s = path.str();
560 std::replace(s.begin(), s.end(), '\\', '/');
561 return s;
Rui Ueyama3e649032017-01-09 01:47:15 +0000562}
563
Zachary Turner5c5091f2017-03-16 22:28:04 +0000564StringRef filename(StringRef path, Style style) { return *rbegin(path, style); }
Michael J. Spencer14269202010-12-01 03:18:17 +0000565
Zachary Turner5c5091f2017-03-16 22:28:04 +0000566StringRef stem(StringRef path, Style style) {
567 StringRef fname = filename(path, style);
Michael J. Spencer956955e2010-12-01 03:18:33 +0000568 size_t pos = fname.find_last_of('.');
569 if (pos == StringRef::npos)
Michael J. Spencerf616b212010-12-07 17:04:04 +0000570 return fname;
Michael J. Spencer956955e2010-12-01 03:18:33 +0000571 else
572 if ((fname.size() == 1 && fname == ".") ||
573 (fname.size() == 2 && fname == ".."))
Michael J. Spencerf616b212010-12-07 17:04:04 +0000574 return fname;
Michael J. Spencer956955e2010-12-01 03:18:33 +0000575 else
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000576 return fname.substr(0, pos);
Michael J. Spencer956955e2010-12-01 03:18:33 +0000577}
578
Zachary Turner5c5091f2017-03-16 22:28:04 +0000579StringRef extension(StringRef path, Style style) {
580 StringRef fname = filename(path, style);
Michael J. Spencer87106c52010-12-01 03:37:41 +0000581 size_t pos = fname.find_last_of('.');
582 if (pos == StringRef::npos)
Michael J. Spencerf616b212010-12-07 17:04:04 +0000583 return StringRef();
Michael J. Spencer87106c52010-12-01 03:37:41 +0000584 else
585 if ((fname.size() == 1 && fname == ".") ||
586 (fname.size() == 2 && fname == ".."))
Michael J. Spencerf616b212010-12-07 17:04:04 +0000587 return StringRef();
Michael J. Spencer87106c52010-12-01 03:37:41 +0000588 else
Benjamin Kramerffa42ce2010-12-17 20:27:37 +0000589 return fname.substr(pos);
Michael J. Spencer87106c52010-12-01 03:37:41 +0000590}
591
Zachary Turner5c5091f2017-03-16 22:28:04 +0000592bool is_separator(char value, Style style) {
593 if (value == '/')
594 return true;
595 if (real_style(style) == Style::windows)
596 return value == '\\';
597 return false;
Zhanyong Wan606bb1a2011-02-11 21:24:40 +0000598}
599
Zachary Turner5c5091f2017-03-16 22:28:04 +0000600StringRef get_separator(Style style) {
601 if (real_style(style) == Style::windows)
602 return "\\";
603 return "/";
Yaron Keren15217202014-05-16 13:16:30 +0000604}
605
Zachary Turner5c5091f2017-03-16 22:28:04 +0000606bool has_root_name(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000607 SmallString<128> path_storage;
608 StringRef p = path.toStringRef(path_storage);
609
Zachary Turner5c5091f2017-03-16 22:28:04 +0000610 return !root_name(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000611}
612
Zachary Turner5c5091f2017-03-16 22:28:04 +0000613bool has_root_directory(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000614 SmallString<128> path_storage;
615 StringRef p = path.toStringRef(path_storage);
616
Zachary Turner5c5091f2017-03-16 22:28:04 +0000617 return !root_directory(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000618}
619
Zachary Turner5c5091f2017-03-16 22:28:04 +0000620bool has_root_path(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000621 SmallString<128> path_storage;
622 StringRef p = path.toStringRef(path_storage);
623
Zachary Turner5c5091f2017-03-16 22:28:04 +0000624 return !root_path(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000625}
626
Zachary Turner5c5091f2017-03-16 22:28:04 +0000627bool has_relative_path(const Twine &path, Style style) {
Michael J. Spencer6d4b7e72010-12-20 13:30:28 +0000628 SmallString<128> path_storage;
629 StringRef p = path.toStringRef(path_storage);
630
Zachary Turner5c5091f2017-03-16 22:28:04 +0000631 return !relative_path(p, style).empty();
Michael J. Spencer6d4b7e72010-12-20 13:30:28 +0000632}
633
Zachary Turner5c5091f2017-03-16 22:28:04 +0000634bool has_filename(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000635 SmallString<128> path_storage;
636 StringRef p = path.toStringRef(path_storage);
637
Zachary Turner5c5091f2017-03-16 22:28:04 +0000638 return !filename(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000639}
640
Zachary Turner5c5091f2017-03-16 22:28:04 +0000641bool has_parent_path(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000642 SmallString<128> path_storage;
643 StringRef p = path.toStringRef(path_storage);
644
Zachary Turner5c5091f2017-03-16 22:28:04 +0000645 return !parent_path(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000646}
647
Zachary Turner5c5091f2017-03-16 22:28:04 +0000648bool has_stem(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000649 SmallString<128> path_storage;
650 StringRef p = path.toStringRef(path_storage);
651
Zachary Turner5c5091f2017-03-16 22:28:04 +0000652 return !stem(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000653}
654
Zachary Turner5c5091f2017-03-16 22:28:04 +0000655bool has_extension(const Twine &path, Style style) {
Michael J. Spencer112a7692010-12-01 06:03:50 +0000656 SmallString<128> path_storage;
657 StringRef p = path.toStringRef(path_storage);
658
Zachary Turner5c5091f2017-03-16 22:28:04 +0000659 return !extension(p, style).empty();
Michael J. Spencer112a7692010-12-01 06:03:50 +0000660}
661
Zachary Turner5c5091f2017-03-16 22:28:04 +0000662bool is_absolute(const Twine &path, Style style) {
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000663 SmallString<128> path_storage;
664 StringRef p = path.toStringRef(path_storage);
665
Zachary Turner5c5091f2017-03-16 22:28:04 +0000666 bool rootDir = has_root_directory(p, style);
667 bool rootName =
668 (real_style(style) != Style::windows) || has_root_name(p, style);
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000669
Michael J. Spencerf616b212010-12-07 17:04:04 +0000670 return rootDir && rootName;
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000671}
672
Zachary Turner5c5091f2017-03-16 22:28:04 +0000673bool is_relative(const Twine &path, Style style) {
674 return !is_absolute(path, style);
675}
Douglas Katzmana26be4a2015-09-02 21:02:10 +0000676
Zachary Turner5c5091f2017-03-16 22:28:04 +0000677StringRef remove_leading_dotslash(StringRef Path, Style style) {
Douglas Katzmana26be4a2015-09-02 21:02:10 +0000678 // Remove leading "./" (or ".//" or "././" etc.)
Zachary Turner5c5091f2017-03-16 22:28:04 +0000679 while (Path.size() > 2 && Path[0] == '.' && is_separator(Path[1], style)) {
Douglas Katzmana26be4a2015-09-02 21:02:10 +0000680 Path = Path.substr(2);
Zachary Turner5c5091f2017-03-16 22:28:04 +0000681 while (Path.size() > 0 && is_separator(Path[0], style))
Douglas Katzmana26be4a2015-09-02 21:02:10 +0000682 Path = Path.substr(1);
683 }
684 return Path;
Michael J. Spencera72df5f2010-12-01 06:21:53 +0000685}
686
Zachary Turner5c5091f2017-03-16 22:28:04 +0000687static SmallString<256> remove_dots(StringRef path, bool remove_dot_dot,
688 Style style) {
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000689 SmallVector<StringRef, 16> components;
690
691 // Skip the root path, then look for traversal in the components.
Zachary Turner5c5091f2017-03-16 22:28:04 +0000692 StringRef rel = path::relative_path(path, style);
693 for (StringRef C :
694 llvm::make_range(path::begin(rel, style), path::end(rel))) {
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000695 if (C == ".")
696 continue;
Benjamin Kramer937dd7a2016-10-17 13:28:21 +0000697 // Leading ".." will remain in the path unless it's at the root.
698 if (remove_dot_dot && C == "..") {
699 if (!components.empty() && components.back() != "..") {
700 components.pop_back();
701 continue;
702 }
Zachary Turner5c5091f2017-03-16 22:28:04 +0000703 if (path::is_absolute(path, style))
Benjamin Kramer937dd7a2016-10-17 13:28:21 +0000704 continue;
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000705 }
706 components.push_back(C);
707 }
708
Zachary Turner5c5091f2017-03-16 22:28:04 +0000709 SmallString<256> buffer = path::root_path(path, style);
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000710 for (StringRef C : components)
Zachary Turner5c5091f2017-03-16 22:28:04 +0000711 path::append(buffer, style, C);
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000712 return buffer;
713}
714
Zachary Turner5c5091f2017-03-16 22:28:04 +0000715bool remove_dots(SmallVectorImpl<char> &path, bool remove_dot_dot,
716 Style style) {
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000717 StringRef p(path.data(), path.size());
718
Zachary Turner5c5091f2017-03-16 22:28:04 +0000719 SmallString<256> result = remove_dots(p, remove_dot_dot, style);
Mike Aizatsky662b4fd2015-11-09 18:56:31 +0000720 if (result == path)
721 return false;
722
723 path.swap(result);
724 return true;
725}
726
Michael J. Spencer9fc1d9d2010-12-01 19:32:01 +0000727} // end namespace path
728
729namespace fs {
730
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000731std::error_code getUniqueID(const Twine Path, UniqueID &Result) {
Rafael Espindola7f822a92013-07-29 21:26:49 +0000732 file_status Status;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000733 std::error_code EC = status(Path, Status);
Rafael Espindola7f822a92013-07-29 21:26:49 +0000734 if (EC)
735 return EC;
736 Result = Status.getUniqueID();
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000737 return std::error_code();
Rafael Espindola7f822a92013-07-29 21:26:49 +0000738}
739
Jake Ehrlich5049c342019-03-18 20:35:18 +0000740void createUniquePath(const Twine &Model, SmallVectorImpl<char> &ResultPath,
741 bool MakeAbsolute) {
742 SmallString<128> ModelStorage;
743 Model.toVector(ModelStorage);
744
745 if (MakeAbsolute) {
746 // Make model absolute by prepending a temp directory if it's not already.
747 if (!sys::path::is_absolute(Twine(ModelStorage))) {
748 SmallString<128> TDir;
749 sys::path::system_temp_directory(true, TDir);
750 sys::path::append(TDir, Twine(ModelStorage));
751 ModelStorage.swap(TDir);
752 }
753 }
754
755 ResultPath = ModelStorage;
756 ResultPath.push_back(0);
757 ResultPath.pop_back();
758
759 // Replace '%' with random chars.
760 for (unsigned i = 0, e = ModelStorage.size(); i != e; ++i) {
761 if (ModelStorage[i] == '%')
762 ResultPath[i] = "0123456789abcdef"[sys::Process::GetRandomNumber() & 15];
763 }
764}
765
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000766std::error_code createUniqueFile(const Twine &Model, int &ResultFd,
767 SmallVectorImpl<char> &ResultPath,
Zachary Turner8ac1c382018-06-05 19:58:26 +0000768 unsigned Mode) {
769 return createUniqueEntity(Model, ResultFd, ResultPath, false, Mode, FS_File);
770}
771
772static std::error_code createUniqueFile(const Twine &Model, int &ResultFd,
773 SmallVectorImpl<char> &ResultPath,
774 unsigned Mode, OpenFlags Flags) {
Rafael Espindolad19c2e82017-11-27 23:44:11 +0000775 return createUniqueEntity(Model, ResultFd, ResultPath, false, Mode, FS_File,
776 Flags);
Rafael Espindolac9d2e5b2013-07-05 21:01:08 +0000777}
778
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000779std::error_code createUniqueFile(const Twine &Model,
Ilya Biryukov84185762018-03-19 14:19:58 +0000780 SmallVectorImpl<char> &ResultPath,
781 unsigned Mode) {
782 int FD;
783 auto EC = createUniqueFile(Model, FD, ResultPath, Mode);
784 if (EC)
785 return EC;
786 // FD is only needed to avoid race conditions. Close it right away.
787 close(FD);
788 return EC;
Rafael Espindolac9d2e5b2013-07-05 21:01:08 +0000789}
790
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000791static std::error_code
792createTemporaryFile(const Twine &Model, int &ResultFD,
Zachary Turner8ac1c382018-06-05 19:58:26 +0000793 llvm::SmallVectorImpl<char> &ResultPath, FSEntity Type) {
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000794 SmallString<128> Storage;
795 StringRef P = Model.toNullTerminatedStringRef(Storage);
Zachary Turner5c5091f2017-03-16 22:28:04 +0000796 assert(P.find_first_of(separators(Style::native)) == StringRef::npos &&
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000797 "Model must be a simple filename.");
798 // Use P.begin() so that createUniqueEntity doesn't need to recreate Storage.
Rafael Espindolad19c2e82017-11-27 23:44:11 +0000799 return createUniqueEntity(P.begin(), ResultFD, ResultPath, true,
Zachary Turner8ac1c382018-06-05 19:58:26 +0000800 owner_read | owner_write, Type);
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000801}
802
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000803static std::error_code
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000804createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD,
Zachary Turner8ac1c382018-06-05 19:58:26 +0000805 llvm::SmallVectorImpl<char> &ResultPath, FSEntity Type) {
Rafael Espindolad3c89042013-07-25 15:00:17 +0000806 const char *Middle = Suffix.empty() ? "-%%%%%%" : "-%%%%%%.";
807 return createTemporaryFile(Prefix + Middle + Suffix, ResultFD, ResultPath,
Zachary Turner8ac1c382018-06-05 19:58:26 +0000808 Type);
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000809}
810
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000811std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
812 int &ResultFD,
Zachary Turner8ac1c382018-06-05 19:58:26 +0000813 SmallVectorImpl<char> &ResultPath) {
814 return createTemporaryFile(Prefix, Suffix, ResultFD, ResultPath, FS_File);
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000815}
816
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000817std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix,
818 SmallVectorImpl<char> &ResultPath) {
Ilya Biryukov84185762018-03-19 14:19:58 +0000819 int FD;
820 auto EC = createTemporaryFile(Prefix, Suffix, FD, ResultPath);
821 if (EC)
822 return EC;
823 // FD is only needed to avoid race conditions. Close it right away.
824 close(FD);
825 return EC;
Rafael Espindola325fa0f2013-07-05 19:56:49 +0000826}
827
828
Rafael Espindolae79a8722013-06-28 03:48:47 +0000829// This is a mkdtemp with a different pattern. We use createUniqueEntity mostly
Rafael Espindola31a24432013-06-28 10:55:41 +0000830// for consistency. We should try using mkdtemp.
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000831std::error_code createUniqueDirectory(const Twine &Prefix,
832 SmallVectorImpl<char> &ResultPath) {
Rafael Espindolae79a8722013-06-28 03:48:47 +0000833 int Dummy;
Ilya Biryukov84185762018-03-19 14:19:58 +0000834 return createUniqueEntity(Prefix + "-%%%%%%", Dummy, ResultPath, true, 0,
835 FS_Dir);
836}
837
838std::error_code
839getPotentiallyUniqueFileName(const Twine &Model,
840 SmallVectorImpl<char> &ResultPath) {
841 int Dummy;
842 return createUniqueEntity(Model, Dummy, ResultPath, false, 0, FS_Name);
843}
844
845std::error_code
846getPotentiallyUniqueTempFileName(const Twine &Prefix, StringRef Suffix,
847 SmallVectorImpl<char> &ResultPath) {
848 int Dummy;
849 return createTemporaryFile(Prefix, Suffix, Dummy, ResultPath, FS_Name);
Rafael Espindola7ffacc42013-06-27 03:45:31 +0000850}
851
Pavel Labath1ad53ca2019-01-16 09:55:32 +0000852void make_absolute(const Twine &current_directory,
853 SmallVectorImpl<char> &path) {
Michael J. Spencer92903a32010-12-07 03:57:17 +0000854 StringRef p(path.data(), path.size());
855
Zachary Turner5c5091f2017-03-16 22:28:04 +0000856 bool rootDirectory = path::has_root_directory(p);
857 bool rootName =
858 (real_style(Style::native) != Style::windows) || path::has_root_name(p);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000859
860 // Already absolute.
861 if (rootName && rootDirectory)
Pavel Labath1ad53ca2019-01-16 09:55:32 +0000862 return;
Michael J. Spencer92903a32010-12-07 03:57:17 +0000863
864 // All of the following conditions will need the current directory.
865 SmallString<128> current_dir;
Pavel Labath1ad53ca2019-01-16 09:55:32 +0000866 current_directory.toVector(current_dir);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000867
868 // Relative path. Prepend the current directory.
869 if (!rootName && !rootDirectory) {
870 // Append path to the current directory.
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000871 path::append(current_dir, p);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000872 // Set path to the result.
873 path.swap(current_dir);
Pavel Labath1ad53ca2019-01-16 09:55:32 +0000874 return;
Michael J. Spencer92903a32010-12-07 03:57:17 +0000875 }
876
877 if (!rootName && rootDirectory) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000878 StringRef cdrn = path::root_name(current_dir);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000879 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000880 path::append(curDirRootName, p);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000881 // Set path to the result.
882 path.swap(curDirRootName);
Pavel Labath1ad53ca2019-01-16 09:55:32 +0000883 return;
Michael J. Spencer92903a32010-12-07 03:57:17 +0000884 }
885
886 if (rootName && !rootDirectory) {
Michael J. Spencerf616b212010-12-07 17:04:04 +0000887 StringRef pRootName = path::root_name(p);
888 StringRef bRootDirectory = path::root_directory(current_dir);
889 StringRef bRelativePath = path::relative_path(current_dir);
890 StringRef pRelativePath = path::relative_path(p);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000891
892 SmallString<128> res;
Michael J. Spencer1e090f02010-12-07 03:57:37 +0000893 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
Michael J. Spencer92903a32010-12-07 03:57:17 +0000894 path.swap(res);
Pavel Labath1ad53ca2019-01-16 09:55:32 +0000895 return;
Michael J. Spencer92903a32010-12-07 03:57:17 +0000896 }
897
898 llvm_unreachable("All rootName and rootDirectory combinations should have "
899 "occurred above!");
900}
901
Benjamin Kramerae1d5992015-10-05 13:02:43 +0000902std::error_code make_absolute(SmallVectorImpl<char> &path) {
Pavel Labath1ad53ca2019-01-16 09:55:32 +0000903 if (path::is_absolute(path))
904 return {};
905
906 SmallString<128> current_dir;
907 if (std::error_code ec = current_path(current_dir))
908 return ec;
909
910 make_absolute(current_dir, path);
911 return {};
Benjamin Kramerae1d5992015-10-05 13:02:43 +0000912}
913
Frederic Riss6b9396c2015-08-06 21:04:55 +0000914std::error_code create_directories(const Twine &Path, bool IgnoreExisting,
915 perms Perms) {
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000916 SmallString<128> PathStorage;
917 StringRef P = Path.toStringRef(PathStorage);
Michael J. Spencer31e310c2010-12-03 05:42:11 +0000918
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000919 // Be optimistic and try to create the directory
Frederic Riss6b9396c2015-08-06 21:04:55 +0000920 std::error_code EC = create_directory(P, IgnoreExisting, Perms);
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000921 // If we succeeded, or had any error other than the parent not existing, just
922 // return it.
Rafael Espindola2a826e42014-06-13 17:20:48 +0000923 if (EC != errc::no_such_file_or_directory)
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000924 return EC;
Michael J. Spencerf616b212010-12-07 17:04:04 +0000925
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000926 // We failed because of a no_such_file_or_directory, try to create the
927 // parent.
928 StringRef Parent = path::parent_path(P);
929 if (Parent.empty())
930 return EC;
Michael J. Spencer31e310c2010-12-03 05:42:11 +0000931
Frederic Riss6b9396c2015-08-06 21:04:55 +0000932 if ((EC = create_directories(Parent, IgnoreExisting, Perms)))
Rafael Espindolab6f72b22014-02-13 16:58:19 +0000933 return EC;
934
Frederic Riss6b9396c2015-08-06 21:04:55 +0000935 return create_directory(P, IgnoreExisting, Perms);
Michael J. Spencer31e310c2010-12-03 05:42:11 +0000936}
937
Zachary Turner1adca7c2018-06-28 18:49:09 +0000938static std::error_code copy_file_internal(int ReadFD, int WriteFD) {
Justin Bognercd45f962014-06-19 19:35:39 +0000939 const size_t BufSize = 4096;
Dylan Noblesmith42836d92014-08-26 02:03:30 +0000940 char *Buf = new char[BufSize];
Justin Bognercd45f962014-06-19 19:35:39 +0000941 int BytesRead = 0, BytesWritten = 0;
942 for (;;) {
943 BytesRead = read(ReadFD, Buf, BufSize);
944 if (BytesRead <= 0)
945 break;
946 while (BytesRead) {
947 BytesWritten = write(WriteFD, Buf, BytesRead);
948 if (BytesWritten < 0)
949 break;
950 BytesRead -= BytesWritten;
951 }
952 if (BytesWritten < 0)
953 break;
954 }
Dylan Noblesmith42836d92014-08-26 02:03:30 +0000955 delete[] Buf;
Justin Bognercd45f962014-06-19 19:35:39 +0000956
957 if (BytesRead < 0 || BytesWritten < 0)
958 return std::error_code(errno, std::generic_category());
959 return std::error_code();
960}
961
Adrian Prantlc90ff5e2019-04-24 19:08:43 +0000962#ifndef __APPLE__
Zachary Turner1adca7c2018-06-28 18:49:09 +0000963std::error_code copy_file(const Twine &From, const Twine &To) {
964 int ReadFD, WriteFD;
965 if (std::error_code EC = openFileForRead(From, ReadFD, OF_None))
966 return EC;
967 if (std::error_code EC =
968 openFileForWrite(To, WriteFD, CD_CreateAlways, OF_None)) {
969 close(ReadFD);
970 return EC;
971 }
972
973 std::error_code EC = copy_file_internal(ReadFD, WriteFD);
974
975 close(ReadFD);
976 close(WriteFD);
977
978 return EC;
979}
Adrian Prantlc90ff5e2019-04-24 19:08:43 +0000980#endif
Zachary Turner1adca7c2018-06-28 18:49:09 +0000981
982std::error_code copy_file(const Twine &From, int ToFD) {
983 int ReadFD;
984 if (std::error_code EC = openFileForRead(From, ReadFD, OF_None))
985 return EC;
986
987 std::error_code EC = copy_file_internal(ReadFD, ToFD);
988
989 close(ReadFD);
990
991 return EC;
992}
993
Zachary Turner82a0c972017-03-20 23:33:18 +0000994ErrorOr<MD5::MD5Result> md5_contents(int FD) {
995 MD5 Hash;
996
997 constexpr size_t BufSize = 4096;
998 std::vector<uint8_t> Buf(BufSize);
999 int BytesRead = 0;
1000 for (;;) {
1001 BytesRead = read(FD, Buf.data(), BufSize);
1002 if (BytesRead <= 0)
1003 break;
1004 Hash.update(makeArrayRef(Buf.data(), BytesRead));
1005 }
1006
1007 if (BytesRead < 0)
1008 return std::error_code(errno, std::generic_category());
1009 MD5::MD5Result Result;
1010 Hash.final(Result);
1011 return Result;
1012}
1013
1014ErrorOr<MD5::MD5Result> md5_contents(const Twine &Path) {
1015 int FD;
Zachary Turner1f67a3c2018-06-07 19:58:58 +00001016 if (auto EC = openFileForRead(Path, FD, OF_None))
Zachary Turner82a0c972017-03-20 23:33:18 +00001017 return EC;
1018
1019 auto Result = md5_contents(FD);
1020 close(FD);
1021 return Result;
1022}
1023
Peter Collingbourne0dfdb442017-10-10 22:19:46 +00001024bool exists(const basic_file_status &status) {
Michael J. Spencer730f51a2010-12-09 17:37:02 +00001025 return status_known(status) && status.type() != file_type::file_not_found;
1026}
1027
Peter Collingbourne0dfdb442017-10-10 22:19:46 +00001028bool status_known(const basic_file_status &s) {
Michael J. Spencer730f51a2010-12-09 17:37:02 +00001029 return s.type() != file_type::status_error;
1030}
1031
Zachary Turner82dd5422017-03-07 16:10:10 +00001032file_type get_file_type(const Twine &Path, bool Follow) {
Zachary Turner990e3cd2017-03-07 03:43:17 +00001033 file_status st;
Zachary Turner82dd5422017-03-07 16:10:10 +00001034 if (status(Path, st, Follow))
Zachary Turner990e3cd2017-03-07 03:43:17 +00001035 return file_type::status_error;
1036 return st.type();
1037}
1038
Peter Collingbourne0dfdb442017-10-10 22:19:46 +00001039bool is_directory(const basic_file_status &status) {
Michael J. Spencer730f51a2010-12-09 17:37:02 +00001040 return status.type() == file_type::directory_file;
1041}
1042
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001043std::error_code is_directory(const Twine &path, bool &result) {
Michael J. Spencer0d771ed2011-01-11 01:21:55 +00001044 file_status st;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001045 if (std::error_code ec = status(path, st))
Michael J. Spencer0d771ed2011-01-11 01:21:55 +00001046 return ec;
1047 result = is_directory(st);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001048 return std::error_code();
Michael J. Spencer0d771ed2011-01-11 01:21:55 +00001049}
1050
Peter Collingbourne0dfdb442017-10-10 22:19:46 +00001051bool is_regular_file(const basic_file_status &status) {
Michael J. Spencer730f51a2010-12-09 17:37:02 +00001052 return status.type() == file_type::regular_file;
1053}
1054
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001055std::error_code is_regular_file(const Twine &path, bool &result) {
Michael J. Spencer0d771ed2011-01-11 01:21:55 +00001056 file_status st;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001057 if (std::error_code ec = status(path, st))
Michael J. Spencer0d771ed2011-01-11 01:21:55 +00001058 return ec;
1059 result = is_regular_file(st);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +00001060 return std::error_code();
Michael J. Spencer0d771ed2011-01-11 01:21:55 +00001061}
1062
Peter Collingbourne0dfdb442017-10-10 22:19:46 +00001063bool is_symlink_file(const basic_file_status &status) {
Zachary Turner7d86ee52017-03-08 17:56:08 +00001064 return status.type() == file_type::symlink_file;
1065}
1066
1067std::error_code is_symlink_file(const Twine &path, bool &result) {
1068 file_status st;
1069 if (std::error_code ec = status(path, st, false))
1070 return ec;
1071 result = is_symlink_file(st);
1072 return std::error_code();
1073}
1074
Peter Collingbourne0dfdb442017-10-10 22:19:46 +00001075bool is_other(const basic_file_status &status) {
Michael J. Spencer730f51a2010-12-09 17:37:02 +00001076 return exists(status) &&
1077 !is_regular_file(status) &&
Rafael Espindola20063062014-03-20 17:39:04 +00001078 !is_directory(status);
Michael J. Spencer730f51a2010-12-09 17:37:02 +00001079}
1080
Juergen Ributzka84ba3422014-12-18 18:19:47 +00001081std::error_code is_other(const Twine &Path, bool &Result) {
1082 file_status FileStatus;
1083 if (std::error_code EC = status(Path, FileStatus))
1084 return EC;
1085 Result = is_other(FileStatus);
1086 return std::error_code();
1087}
1088
Kristina Brooks3a55d1e2018-09-12 22:08:10 +00001089void directory_entry::replace_filename(const Twine &Filename, file_type Type,
1090 basic_file_status Status) {
1091 SmallString<128> PathStr = path::parent_path(Path);
1092 path::append(PathStr, Filename);
1093 this->Path = PathStr.str();
1094 this->Type = Type;
1095 this->Status = Status;
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +00001096}
1097
James Henderson566fdf42017-03-16 11:22:09 +00001098ErrorOr<perms> getPermissions(const Twine &Path) {
1099 file_status Status;
1100 if (std::error_code EC = status(Path, Status))
1101 return EC;
1102
1103 return Status.permissions();
1104}
1105
Aaron Ballman345012d2017-03-13 12:24:51 +00001106} // end namespace fs
1107} // end namespace sys
1108} // end namespace llvm
Michael J. Spencerebad2f92010-11-29 22:28:51 +00001109
1110// Include the truly platform-specific parts.
1111#if defined(LLVM_ON_UNIX)
Rafael Espindolaf1fc3822013-06-26 19:33:03 +00001112#include "Unix/Path.inc"
Michael J. Spencerebad2f92010-11-29 22:28:51 +00001113#endif
Nico Weber712e8d22018-04-29 00:45:03 +00001114#if defined(_WIN32)
Rafael Espindolaf1fc3822013-06-26 19:33:03 +00001115#include "Windows/Path.inc"
Michael J. Spencerebad2f92010-11-29 22:28:51 +00001116#endif
Pawel Bylica0e97e5c2015-11-02 09:49:17 +00001117
1118namespace llvm {
1119namespace sys {
Rafael Espindola2c4e9202017-11-28 01:34:20 +00001120namespace fs {
1121TempFile::TempFile(StringRef Name, int FD) : TmpName(Name), FD(FD) {}
1122TempFile::TempFile(TempFile &&Other) { *this = std::move(Other); }
1123TempFile &TempFile::operator=(TempFile &&Other) {
1124 TmpName = std::move(Other.TmpName);
1125 FD = Other.FD;
1126 Other.Done = true;
1127 return *this;
1128}
1129
1130TempFile::~TempFile() { assert(Done); }
1131
1132Error TempFile::discard() {
1133 Done = true;
Rafael Espindola2c4e9202017-11-28 01:34:20 +00001134 if (FD != -1 && close(FD) == -1) {
1135 std::error_code EC = std::error_code(errno, std::generic_category());
1136 return errorCodeToError(EC);
1137 }
1138 FD = -1;
1139
Andrew Ngd27cf272019-02-14 11:08:49 +00001140#ifdef _WIN32
1141 // On windows closing will remove the file.
1142 TmpName = "";
1143 return Error::success();
1144#else
1145 // Always try to close and remove.
1146 std::error_code RemoveEC;
1147 if (!TmpName.empty()) {
1148 RemoveEC = fs::remove(TmpName);
1149 sys::DontRemoveFileOnSignal(TmpName);
1150 if (!RemoveEC)
1151 TmpName = "";
1152 }
Rafael Espindola2c4e9202017-11-28 01:34:20 +00001153 return errorCodeToError(RemoveEC);
Andrew Ngd27cf272019-02-14 11:08:49 +00001154#endif
Rafael Espindola2c4e9202017-11-28 01:34:20 +00001155}
1156
1157Error TempFile::keep(const Twine &Name) {
1158 assert(!Done);
1159 Done = true;
1160 // Always try to close and rename.
Nico Weber712e8d22018-04-29 00:45:03 +00001161#ifdef _WIN32
Vladimir Stefanovicbeb9d972018-07-03 17:26:43 +00001162 // If we can't cancel the delete don't rename.
Peter Collingbourne881ba102018-06-13 18:03:14 +00001163 auto H = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
1164 std::error_code RenameEC = setDeleteDisposition(H, false);
Jeremy Morse01940652018-08-03 10:13:35 +00001165 if (!RenameEC) {
Rafael Espindola3ecd2042017-11-28 01:41:22 +00001166 RenameEC = rename_fd(FD, Name);
Jeremy Morse01940652018-08-03 10:13:35 +00001167 // If rename failed because it's cross-device, copy instead
1168 if (RenameEC ==
1169 std::error_code(ERROR_NOT_SAME_DEVICE, std::system_category())) {
1170 RenameEC = copy_file(TmpName, Name);
1171 setDeleteDisposition(H, true);
1172 }
1173 }
1174
Rafael Espindola20569e92017-12-05 16:40:56 +00001175 // If we can't rename, discard the temporary file.
1176 if (RenameEC)
Peter Collingbourne881ba102018-06-13 18:03:14 +00001177 setDeleteDisposition(H, true);
Rafael Espindola3ecd2042017-11-28 01:41:22 +00001178#else
Rafael Espindola2c4e9202017-11-28 01:34:20 +00001179 std::error_code RenameEC = fs::rename(TmpName, Name);
Jonas Devlieghereae1727e2018-07-29 14:56:15 +00001180 if (RenameEC) {
1181 // If we can't rename, try to copy to work around cross-device link issues.
1182 RenameEC = sys::fs::copy_file(TmpName, Name);
1183 // If we can't rename or copy, discard the temporary file.
1184 if (RenameEC)
1185 remove(TmpName);
1186 }
Rafael Espindola2c4e9202017-11-28 01:34:20 +00001187 sys::DontRemoveFileOnSignal(TmpName);
Rafael Espindola3ecd2042017-11-28 01:41:22 +00001188#endif
Rafael Espindola2c4e9202017-11-28 01:34:20 +00001189
1190 if (!RenameEC)
1191 TmpName = "";
1192
1193 if (close(FD) == -1) {
1194 std::error_code EC(errno, std::generic_category());
1195 return errorCodeToError(EC);
1196 }
1197 FD = -1;
1198
1199 return errorCodeToError(RenameEC);
1200}
1201
1202Error TempFile::keep() {
1203 assert(!Done);
1204 Done = true;
1205
Nico Weber712e8d22018-04-29 00:45:03 +00001206#ifdef _WIN32
Peter Collingbourne881ba102018-06-13 18:03:14 +00001207 auto H = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
1208 if (std::error_code EC = setDeleteDisposition(H, false))
Rafael Espindola3ecd2042017-11-28 01:41:22 +00001209 return errorCodeToError(EC);
1210#else
Rafael Espindola2c4e9202017-11-28 01:34:20 +00001211 sys::DontRemoveFileOnSignal(TmpName);
Rafael Espindola3ecd2042017-11-28 01:41:22 +00001212#endif
1213
Rafael Espindola2c4e9202017-11-28 01:34:20 +00001214 TmpName = "";
1215
1216 if (close(FD) == -1) {
1217 std::error_code EC(errno, std::generic_category());
1218 return errorCodeToError(EC);
1219 }
1220 FD = -1;
1221
1222 return Error::success();
1223}
1224
1225Expected<TempFile> TempFile::create(const Twine &Model, unsigned Mode) {
1226 int FD;
1227 SmallString<128> ResultPath;
Zachary Turner8ac1c382018-06-05 19:58:26 +00001228 if (std::error_code EC =
Zachary Turner1f67a3c2018-06-07 19:58:58 +00001229 createUniqueFile(Model, FD, ResultPath, Mode, OF_Delete))
Rafael Espindola2c4e9202017-11-28 01:34:20 +00001230 return errorCodeToError(EC);
1231
Rafael Espindola2c4e9202017-11-28 01:34:20 +00001232 TempFile Ret(ResultPath, FD);
Nico Weber712e8d22018-04-29 00:45:03 +00001233#ifndef _WIN32
Rafael Espindola2c4e9202017-11-28 01:34:20 +00001234 if (sys::RemoveFileOnSignal(ResultPath)) {
Rafael Espindola3ecd2042017-11-28 01:41:22 +00001235 // Make sure we delete the file when RemoveFileOnSignal fails.
Rafael Espindola2c4e9202017-11-28 01:34:20 +00001236 consumeError(Ret.discard());
1237 std::error_code EC(errc::operation_not_permitted);
1238 return errorCodeToError(EC);
1239 }
Rafael Espindola3ecd2042017-11-28 01:41:22 +00001240#endif
Rafael Espindola2c4e9202017-11-28 01:34:20 +00001241 return std::move(Ret);
1242}
1243}
1244
Pawel Bylica0e97e5c2015-11-02 09:49:17 +00001245} // end namsspace sys
1246} // end namespace llvm