blob: cd9ff21d6e9cb7007e86146a69c77227eba52d3a [file] [log] [blame]
Michael J. Spencerdffde992010-11-29 22:28:51 +00001//===-- PathV2.cpp - Implement OS Path Concept ------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the operating system PathV2 API.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Support/PathV2.h"
Michael J. Spencerbee0c382010-12-01 19:32:01 +000015#include "llvm/Support/FileSystem.h"
Michael J. Spencerdffde992010-11-29 22:28:51 +000016#include "llvm/Support/ErrorHandling.h"
17#include <cctype>
18
19namespace {
20 using llvm::StringRef;
21
22 bool is_separator(const char value) {
23 switch(value) {
24#ifdef LLVM_ON_WIN32
25 case '\\': // fall through
26#endif
27 case '/': return true;
28 default: return false;
29 }
30 }
31
32#ifdef LLVM_ON_WIN32
33 const StringRef separators = "\\/";
34 const char prefered_separator = '\\';
35#else
36 const StringRef separators = "/";
37 const char prefered_separator = '/';
38#endif
39
Michael J. Spencer470ae132010-12-04 00:32:40 +000040 const llvm::error_code success;
41
Michael J. Spencerdffde992010-11-29 22:28:51 +000042 StringRef find_first_component(const StringRef &path) {
43 // Look for this first component in the following order.
44 // * empty (in this case we return an empty string)
45 // * either C: or {//,\\}net.
46 // * {/,\}
47 // * {.,..}
48 // * {file,directory}name
49
50 if (path.empty())
51 return path;
52
53 // C:
54 if (path.size() >= 2 && std::isalpha(path[0]) && path[1] == ':')
55 return StringRef(path.begin(), 2);
56
57 // //net
58 if ((path.size() > 2) &&
59 (path.startswith("\\\\") || path.startswith("//")) &&
60 (path[2] != '\\' && path[2] != '/')) {
61 // Find the next directory separator.
62 size_t end = path.find_first_of("\\/", 2);
63 if (end == StringRef::npos)
64 return path;
65 else
66 return StringRef(path.begin(), end);
67 }
68
69 // {/,\}
70 if (path[0] == '\\' || path[0] == '/')
71 return StringRef(path.begin(), 1);
72
73 if (path.startswith(".."))
74 return StringRef(path.begin(), 2);
75
76 if (path[0] == '.')
77 return StringRef(path.begin(), 1);
78
79 // * {file,directory}name
80 size_t end = path.find_first_of("\\/", 2);
81 if (end == StringRef::npos)
82 return path;
83 else
84 return StringRef(path.begin(), end);
85
86 return StringRef();
87 }
Michael J. Spencera42cf732010-11-30 23:28:07 +000088
89 size_t filename_pos(const StringRef &str) {
90 if (str.size() == 2 &&
91 is_separator(str[0]) &&
92 is_separator(str[1]))
93 return 0;
94
95 if (str.size() > 0 && is_separator(str[str.size() - 1]))
96 return str.size() - 1;
97
98 size_t pos = str.find_last_of(separators, str.size() - 1);
99
100#ifdef LLVM_ON_WIN32
101 if (pos == StringRef::npos)
102 pos = str.find_last_of(':', str.size() - 2);
103#endif
104
105 if (pos == StringRef::npos ||
106 (pos == 1 && is_separator(str[0])))
107 return 0;
108
109 return pos + 1;
110 }
111
112 size_t root_dir_start(const StringRef &str) {
113 // case "c:/"
114#ifdef LLVM_ON_WIN32
115 if (str.size() > 2 &&
116 str[1] == ':' &&
117 is_separator(str[2]))
118 return 2;
119#endif
120
121 // case "//"
122 if (str.size() == 2 &&
123 is_separator(str[0]) &&
124 str[0] == str[1])
125 return StringRef::npos;
126
127 // case "//net"
128 if (str.size() > 3 &&
129 is_separator(str[0]) &&
130 str[0] == str[1] &&
131 !is_separator(str[2])) {
132 return str.find_first_of(separators, 2);
133 }
134
135 // case "/"
136 if (str.size() > 0 && is_separator(str[0]))
137 return 0;
138
139 return StringRef::npos;
140 }
141
142 size_t parent_path_end(const StringRef &path) {
143 size_t end_pos = filename_pos(path);
144
145 bool filename_was_sep = path.size() > 0 && is_separator(path[end_pos]);
146
147 // Skip separators except for root dir.
148 size_t root_dir_pos = root_dir_start(StringRef(path.begin(), end_pos));
149
150 while(end_pos > 0 &&
151 (end_pos - 1) != root_dir_pos &&
152 is_separator(path[end_pos - 1]))
153 --end_pos;
154
155 if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
156 return StringRef::npos;
157
158 return end_pos;
159 }
Michael J. Spencerdffde992010-11-29 22:28:51 +0000160}
161
162namespace llvm {
163namespace sys {
164namespace path {
165
166const_iterator begin(const StringRef &path) {
167 const_iterator i;
168 i.Path = path;
169 i.Component = find_first_component(path);
170 i.Position = 0;
171 return i;
172}
173
174const_iterator end(const StringRef &path) {
175 const_iterator i;
176 i.Path = path;
177 i.Position = path.size();
178 return i;
179}
180
Michael J. Spencerdffde992010-11-29 22:28:51 +0000181const_iterator &const_iterator::operator++() {
182 assert(Position < Path.size() && "Tried to increment past end!");
183
184 // Increment Position to past the current component
185 Position += Component.size();
186
187 // Check for end.
188 if (Position == Path.size()) {
189 Component = StringRef();
190 return *this;
191 }
192
193 // Both POSIX and Windows treat paths that begin with exactly two separators
194 // specially.
195 bool was_net = Component.size() > 2 &&
196 is_separator(Component[0]) &&
197 Component[1] == Component[0] &&
198 !is_separator(Component[2]);
199
200 // Handle separators.
201 if (is_separator(Path[Position])) {
202 // Root dir.
203 if (was_net
204#ifdef LLVM_ON_WIN32
205 // c:/
206 || Component.endswith(":")
207#endif
208 ) {
209 Component = StringRef(Path.begin() + Position, 1);
210 return *this;
211 }
212
213 // Skip extra separators.
214 while (Position != Path.size() &&
215 is_separator(Path[Position])) {
216 ++Position;
217 }
218
219 // Treat trailing '/' as a '.'.
220 if (Position == Path.size()) {
221 --Position;
222 Component = ".";
223 return *this;
224 }
225 }
226
227 // Find next component.
228 size_t end_pos = Path.find_first_of(separators, Position);
229 if (end_pos == StringRef::npos)
230 end_pos = Path.size();
231 Component = StringRef(Path.begin() + Position, end_pos - Position);
232
233 return *this;
234}
235
Michael J. Spencera42cf732010-11-30 23:28:07 +0000236const_iterator &const_iterator::operator--() {
237 // If we're at the end and the previous char was a '/', return '.'.
238 if (Position == Path.size() &&
239 Path.size() > 1 &&
240 is_separator(Path[Position - 1])
241#ifdef LLVM_ON_WIN32
242 && Path[Position - 2] != ':'
243#endif
244 ) {
245 --Position;
246 Component = ".";
247 return *this;
248 }
249
250 // Skip separators unless it's the root directory.
251 size_t root_dir_pos = root_dir_start(Path);
252 size_t end_pos = Position;
253
254 while(end_pos > 0 &&
255 (end_pos - 1) != root_dir_pos &&
256 is_separator(Path[end_pos - 1]))
257 --end_pos;
258
259 // Find next separator.
260 size_t start_pos = filename_pos(StringRef(Path.begin(), end_pos));
261 Component = StringRef(Path.begin() + start_pos, end_pos - start_pos);
262 Position = start_pos;
263 return *this;
264}
265
Michael J. Spencerdffde992010-11-29 22:28:51 +0000266bool const_iterator::operator==(const const_iterator &RHS) const {
267 return Path.begin() == RHS.Path.begin() &&
268 Position == RHS.Position;
269}
270
271bool const_iterator::operator!=(const const_iterator &RHS) const {
272 return !(*this == RHS);
273}
274
Michael J. Spencera42cf732010-11-30 23:28:07 +0000275ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
276 return Position - RHS.Position;
277}
278
Michael J. Spencer936671b2010-12-07 03:57:37 +0000279void root_path(const StringRef &path, StringRef &result) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000280 const_iterator b = begin(path),
281 pos = b,
282 e = end(path);
283 if (b != e) {
284 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
285 bool has_drive =
286#ifdef LLVM_ON_WIN32
287 b->endswith(":");
288#else
289 false;
290#endif
291
292 if (has_net || has_drive) {
293 if ((++pos != e) && is_separator((*pos)[0])) {
294 // {C:/,//net/}, so get the first two components.
295 result = StringRef(path.begin(), b->size() + pos->size());
Michael J. Spencer936671b2010-12-07 03:57:37 +0000296 return;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000297 } else {
298 // just {C:,//net}, return the first component.
299 result = *b;
Michael J. Spencer936671b2010-12-07 03:57:37 +0000300 return;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000301 }
302 }
303
304 // POSIX style root directory.
305 if (is_separator((*b)[0])) {
306 result = *b;
Michael J. Spencer936671b2010-12-07 03:57:37 +0000307 return;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000308 }
Michael J. Spencerdffde992010-11-29 22:28:51 +0000309 }
310
Michael J. Spencerdffde992010-11-29 22:28:51 +0000311 result = StringRef();
Michael J. Spencerdffde992010-11-29 22:28:51 +0000312}
313
Michael J. Spencer936671b2010-12-07 03:57:37 +0000314void root_name(const StringRef &path, StringRef &result) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000315 const_iterator b = begin(path),
316 e = end(path);
317 if (b != e) {
318 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
319 bool has_drive =
320#ifdef LLVM_ON_WIN32
321 b->endswith(":");
322#else
323 false;
324#endif
325
326 if (has_net || has_drive) {
327 // just {C:,//net}, return the first component.
328 result = *b;
Michael J. Spencer936671b2010-12-07 03:57:37 +0000329 return;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000330 }
331 }
332
333 // No path or no name.
334 result = StringRef();
Michael J. Spencer936671b2010-12-07 03:57:37 +0000335 return;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000336}
337
Michael J. Spencer936671b2010-12-07 03:57:37 +0000338void root_directory(const StringRef &path, StringRef &result) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000339 const_iterator b = begin(path),
340 pos = b,
341 e = end(path);
342 if (b != e) {
343 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
344 bool has_drive =
345#ifdef LLVM_ON_WIN32
346 b->endswith(":");
347#else
348 false;
349#endif
350
351 if ((has_net || has_drive) &&
352 // {C:,//net}, skip to the next component.
353 (++pos != e) && is_separator((*pos)[0])) {
354 result = *pos;
Michael J. Spencer936671b2010-12-07 03:57:37 +0000355 return;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000356 }
357
358 // POSIX style root directory.
359 if (!has_net && is_separator((*b)[0])) {
360 result = *b;
Michael J. Spencer936671b2010-12-07 03:57:37 +0000361 return;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000362 }
363 }
364
365 // No path or no root.
366 result = StringRef();
Michael J. Spencer936671b2010-12-07 03:57:37 +0000367 return;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000368}
369
Michael J. Spencer936671b2010-12-07 03:57:37 +0000370void relative_path(const StringRef &path, StringRef &result) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000371 StringRef root;
Michael J. Spencer936671b2010-12-07 03:57:37 +0000372 root_path(path, root);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000373 result = StringRef(path.begin() + root.size(), path.size() - root.size());
Michael J. Spencer936671b2010-12-07 03:57:37 +0000374 return;
Michael J. Spencerdffde992010-11-29 22:28:51 +0000375}
376
Michael J. Spencer936671b2010-12-07 03:57:37 +0000377void append(SmallVectorImpl<char> &path, const Twine &a,
378 const Twine &b,
379 const Twine &c,
380 const Twine &d) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000381 SmallString<32> a_storage;
382 SmallString<32> b_storage;
383 SmallString<32> c_storage;
384 SmallString<32> d_storage;
385
386 SmallVector<StringRef, 4> components;
387 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
388 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
389 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
390 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
391
392 for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(),
393 e = components.end();
394 i != e; ++i) {
395 bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]);
396 bool component_has_sep = !i->empty() && is_separator((*i)[0]);
Bill Wendlinga6091be2010-12-04 20:20:34 +0000397 bool is_root_name = false;
Michael J. Spencer936671b2010-12-07 03:57:37 +0000398 has_root_name(*i, is_root_name);
Michael J. Spencerdffde992010-11-29 22:28:51 +0000399
400 if (path_has_sep) {
401 // Strip separators from beginning of component.
402 size_t loc = i->find_first_not_of(separators);
403 StringRef c = StringRef(i->begin() + loc, i->size() - loc);
404
405 // Append it.
406 path.append(c.begin(), c.end());
407 continue;
408 }
409
Michael J. Spencerf150e762010-12-06 04:28:23 +0000410 if (!component_has_sep && !(path.empty() || is_root_name)) {
Michael J. Spencerdffde992010-11-29 22:28:51 +0000411 // Add a separator.
412 path.push_back(prefered_separator);
413 }
414
415 path.append(i->begin(), i->end());
416 }
Michael J. Spencerdffde992010-11-29 22:28:51 +0000417}
418
Michael J. Spencer936671b2010-12-07 03:57:37 +0000419void parent_path(const StringRef &path, StringRef &result) {
Michael J. Spencera42cf732010-11-30 23:28:07 +0000420 size_t end_pos = parent_path_end(path);
421 if (end_pos == StringRef::npos)
422 result = StringRef();
423 else
424 result = StringRef(path.data(), end_pos);
Michael J. Spencera42cf732010-11-30 23:28:07 +0000425}
426
Michael J. Spencer936671b2010-12-07 03:57:37 +0000427void remove_filename(SmallVectorImpl<char> &path) {
Michael J. Spencerdbfb56b2010-12-01 00:52:28 +0000428 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()));
Michael J. Spencer936671b2010-12-07 03:57:37 +0000429 if (end_pos != StringRef::npos)
430 path.set_size(end_pos);
Michael J. Spencerdbfb56b2010-12-01 00:52:28 +0000431}
432
Michael J. Spencer936671b2010-12-07 03:57:37 +0000433void replace_extension(SmallVectorImpl<char> &path,
Michael J. Spencer52ed8672010-12-01 00:52:55 +0000434 const Twine &extension) {
435 StringRef p(path.begin(), path.size());
436 SmallString<32> ext_storage;
437 StringRef ext = extension.toStringRef(ext_storage);
438
439 // Erase existing extension.
440 size_t pos = p.find_last_of('.');
441 if (pos != StringRef::npos && pos >= filename_pos(p))
442 path.set_size(pos);
443
444 // Append '.' if needed.
445 if (ext.size() > 0 && ext[0] != '.')
446 path.push_back('.');
447
448 // Append extension.
449 path.append(ext.begin(), ext.end());
Michael J. Spencer52ed8672010-12-01 00:52:55 +0000450}
451
Michael J. Spencer936671b2010-12-07 03:57:37 +0000452void native(const Twine &path, SmallVectorImpl<char> &result) {
Michael J. Spencer722d5ad2010-12-01 02:48:27 +0000453 // Clear result.
Michael J. Spencerfbd1bbd2010-12-07 01:23:19 +0000454 result.clear();
Michael J. Spencer722d5ad2010-12-01 02:48:27 +0000455#ifdef LLVM_ON_WIN32
456 SmallString<128> path_storage;
457 StringRef p = path.toStringRef(path_storage);
458 result.reserve(p.size());
459 for (StringRef::const_iterator i = p.begin(),
460 e = p.end();
461 i != e;
462 ++i) {
463 if (*i == '/')
464 result.push_back('\\');
465 else
466 result.push_back(*i);
467 }
468#else
469 path.toVector(result);
470#endif
Michael J. Spencer722d5ad2010-12-01 02:48:27 +0000471}
472
Michael J. Spencer936671b2010-12-07 03:57:37 +0000473void filename(const StringRef &path, StringRef &result) {
Michael J. Spencera9793552010-12-01 03:18:17 +0000474 result = *(--end(path));
Michael J. Spencera9793552010-12-01 03:18:17 +0000475}
476
Michael J. Spencer936671b2010-12-07 03:57:37 +0000477void stem(const StringRef &path, StringRef &result) {
Michael J. Spencer34ab1f62010-12-01 03:18:33 +0000478 StringRef fname;
Michael J. Spencer936671b2010-12-07 03:57:37 +0000479 filename(path, fname);
Michael J. Spencer34ab1f62010-12-01 03:18:33 +0000480 size_t pos = fname.find_last_of('.');
481 if (pos == StringRef::npos)
482 result = fname;
483 else
484 if ((fname.size() == 1 && fname == ".") ||
485 (fname.size() == 2 && fname == ".."))
486 result = fname;
487 else
488 result = StringRef(fname.begin(), pos);
Michael J. Spencer34ab1f62010-12-01 03:18:33 +0000489}
490
Michael J. Spencer936671b2010-12-07 03:57:37 +0000491void extension(const StringRef &path, StringRef &result) {
Michael J. Spencer5265f222010-12-01 03:37:41 +0000492 StringRef fname;
Michael J. Spencer936671b2010-12-07 03:57:37 +0000493 filename(path, fname);
Michael J. Spencer5265f222010-12-01 03:37:41 +0000494 size_t pos = fname.find_last_of('.');
495 if (pos == StringRef::npos)
496 result = StringRef();
497 else
498 if ((fname.size() == 1 && fname == ".") ||
499 (fname.size() == 2 && fname == ".."))
500 result = StringRef();
501 else
502 result = StringRef(fname.begin() + pos, fname.size() - pos);
Michael J. Spencer5265f222010-12-01 03:37:41 +0000503}
504
Michael J. Spencer936671b2010-12-07 03:57:37 +0000505void has_root_name(const Twine &path, bool &result) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000506 SmallString<128> path_storage;
507 StringRef p = path.toStringRef(path_storage);
508
Michael J. Spencer936671b2010-12-07 03:57:37 +0000509 root_name(p, p);
Michael J. Spencerae180082010-12-01 06:03:50 +0000510
511 result = !p.empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000512}
513
Michael J. Spencer936671b2010-12-07 03:57:37 +0000514void has_root_directory(const Twine &path, bool &result) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000515 SmallString<128> path_storage;
516 StringRef p = path.toStringRef(path_storage);
517
Michael J. Spencer936671b2010-12-07 03:57:37 +0000518 root_directory(p, p);
Michael J. Spencerae180082010-12-01 06:03:50 +0000519
520 result = !p.empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000521}
522
Michael J. Spencer936671b2010-12-07 03:57:37 +0000523void has_root_path(const Twine &path, bool &result) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000524 SmallString<128> path_storage;
525 StringRef p = path.toStringRef(path_storage);
526
Michael J. Spencer936671b2010-12-07 03:57:37 +0000527 root_path(p, p);
Michael J. Spencerae180082010-12-01 06:03:50 +0000528
529 result = !p.empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000530}
531
Michael J. Spencer936671b2010-12-07 03:57:37 +0000532void has_filename(const Twine &path, bool &result) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000533 SmallString<128> path_storage;
534 StringRef p = path.toStringRef(path_storage);
535
Michael J. Spencer936671b2010-12-07 03:57:37 +0000536 filename(p, p);
Michael J. Spencerae180082010-12-01 06:03:50 +0000537
538 result = !p.empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000539}
540
Michael J. Spencer936671b2010-12-07 03:57:37 +0000541void has_parent_path(const Twine &path, bool &result) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000542 SmallString<128> path_storage;
543 StringRef p = path.toStringRef(path_storage);
544
Michael J. Spencer936671b2010-12-07 03:57:37 +0000545 parent_path(p, p);
Michael J. Spencerae180082010-12-01 06:03:50 +0000546
547 result = !p.empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000548}
549
Michael J. Spencer936671b2010-12-07 03:57:37 +0000550void has_stem(const Twine &path, bool &result) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000551 SmallString<128> path_storage;
552 StringRef p = path.toStringRef(path_storage);
553
Michael J. Spencer936671b2010-12-07 03:57:37 +0000554 stem(p, p);
Michael J. Spencerae180082010-12-01 06:03:50 +0000555
556 result = !p.empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000557}
558
Michael J. Spencer936671b2010-12-07 03:57:37 +0000559void has_extension(const Twine &path, bool &result) {
Michael J. Spencerae180082010-12-01 06:03:50 +0000560 SmallString<128> path_storage;
561 StringRef p = path.toStringRef(path_storage);
562
Michael J. Spencer936671b2010-12-07 03:57:37 +0000563 extension(p, p);
Michael J. Spencerae180082010-12-01 06:03:50 +0000564
565 result = !p.empty();
Michael J. Spencerae180082010-12-01 06:03:50 +0000566}
567
Michael J. Spencer936671b2010-12-07 03:57:37 +0000568void is_absolute(const Twine &path, bool &result) {
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000569 SmallString<128> path_storage;
570 StringRef p = path.toStringRef(path_storage);
571
572 bool rootDir = false,
573 rootName = false;
Michael J. Spencer936671b2010-12-07 03:57:37 +0000574 has_root_directory(p, rootDir);
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000575#ifdef LLVM_ON_WIN32
Michael J. Spencer936671b2010-12-07 03:57:37 +0000576 has_root_name(p, rootName);
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000577#else
578 rootName = true;
579#endif
580
581 result = rootDir && rootName;
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000582}
583
Michael J. Spencer936671b2010-12-07 03:57:37 +0000584void is_relative(const Twine &path, bool &result) {
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000585 bool res;
Michael J. Spencer936671b2010-12-07 03:57:37 +0000586 is_absolute(path, res);
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000587 result = !res;
Michael J. Spencerce2b68f2010-12-01 06:21:53 +0000588}
589
Michael J. Spencerbee0c382010-12-01 19:32:01 +0000590} // end namespace path
591
592namespace fs {
593
Michael J. Spenceree271d82010-12-07 03:57:17 +0000594error_code make_absolute(SmallVectorImpl<char> &path) {
595 StringRef p(path.data(), path.size());
596
597 bool rootName = false, rootDirectory = false;
Michael J. Spencer936671b2010-12-07 03:57:37 +0000598 path::has_root_name(p, rootName);
599 path::has_root_directory(p, rootDirectory);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000600
601 // Already absolute.
602 if (rootName && rootDirectory)
603 return success;
604
605 // All of the following conditions will need the current directory.
606 SmallString<128> current_dir;
607 if (error_code ec = current_path(current_dir)) return ec;
608
609 // Relative path. Prepend the current directory.
610 if (!rootName && !rootDirectory) {
611 // Append path to the current directory.
Michael J. Spencer936671b2010-12-07 03:57:37 +0000612 path::append(current_dir, p);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000613 // Set path to the result.
614 path.swap(current_dir);
615 return success;
616 }
617
618 if (!rootName && rootDirectory) {
619 StringRef cdrn;
Michael J. Spencer936671b2010-12-07 03:57:37 +0000620 path::root_name(current_dir, cdrn);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000621 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
Michael J. Spencer936671b2010-12-07 03:57:37 +0000622 path::append(curDirRootName, p);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000623 // Set path to the result.
624 path.swap(curDirRootName);
625 return success;
626 }
627
628 if (rootName && !rootDirectory) {
629 StringRef pRootName;
630 StringRef bRootDirectory;
631 StringRef bRelativePath;
632 StringRef pRelativePath;
Michael J. Spencer936671b2010-12-07 03:57:37 +0000633 path::root_name(p, pRootName);
634 path::root_directory(current_dir, bRootDirectory);
635 path::relative_path(current_dir, bRelativePath);
636 path::relative_path(p, pRelativePath);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000637
638 SmallString<128> res;
Michael J. Spencer936671b2010-12-07 03:57:37 +0000639 path::append(res, pRootName, bRootDirectory, bRelativePath, pRelativePath);
Michael J. Spenceree271d82010-12-07 03:57:17 +0000640 path.swap(res);
641 return success;
642 }
643
644 llvm_unreachable("All rootName and rootDirectory combinations should have "
645 "occurred above!");
646}
647
Michael J. Spencerb83769f2010-12-03 05:42:11 +0000648error_code create_directories(const Twine &path, bool &existed) {
649 SmallString<128> path_storage;
650 StringRef p = path.toStringRef(path_storage);
651
652 StringRef parent;
653 bool parent_exists;
Michael J. Spencer936671b2010-12-07 03:57:37 +0000654 path::parent_path(p, parent);
Michael J. Spencerb83769f2010-12-03 05:42:11 +0000655 if (error_code ec = fs::exists(parent, parent_exists)) return ec;
656
657 if (!parent_exists)
658 return create_directories(parent, existed);
659
660 return create_directory(p, existed);
661}
662
Michael J. Spencer753cbbb2010-12-06 04:28:42 +0000663void directory_entry::replace_filename(const Twine &filename, file_status st,
664 file_status symlink_st) {
665 SmallString<128> path(Path.begin(), Path.end());
666 path::remove_filename(path);
667 path::append(path, filename);
668 Path = path.str();
669 Status = st;
670 SymlinkStatus = symlink_st;
671}
672
Michael J. Spencerbee0c382010-12-01 19:32:01 +0000673} // end namespace fs
674} // end namespace sys
675} // end namespace llvm
Michael J. Spencerdffde992010-11-29 22:28:51 +0000676
677// Include the truly platform-specific parts.
678#if defined(LLVM_ON_UNIX)
679#include "Unix/PathV2.inc"
680#endif
681#if defined(LLVM_ON_WIN32)
682#include "Windows/PathV2.inc"
683#endif