1 //===-- PathV2.cpp - Implement OS Path Concept ------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the operating system PathV2 API.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/PathV2.h"
15 #include "llvm/Support/ErrorHandling.h"
19 using llvm::StringRef;
21 bool is_separator(const char value) {
24 case '\\': // fall through
26 case '/': return true;
27 default: return false;
32 const StringRef separators = "\\/";
33 const char prefered_separator = '\\';
35 const StringRef separators = "/";
36 const char prefered_separator = '/';
39 StringRef find_first_component(const StringRef &path) {
40 // Look for this first component in the following order.
41 // * empty (in this case we return an empty string)
42 // * either C: or {//,\\}net.
45 // * {file,directory}name
51 if (path.size() >= 2 && std::isalpha(path[0]) && path[1] == ':')
52 return StringRef(path.begin(), 2);
55 if ((path.size() > 2) &&
56 (path.startswith("\\\\") || path.startswith("//")) &&
57 (path[2] != '\\' && path[2] != '/')) {
58 // Find the next directory separator.
59 size_t end = path.find_first_of("\\/", 2);
60 if (end == StringRef::npos)
63 return StringRef(path.begin(), end);
67 if (path[0] == '\\' || path[0] == '/')
68 return StringRef(path.begin(), 1);
70 if (path.startswith(".."))
71 return StringRef(path.begin(), 2);
74 return StringRef(path.begin(), 1);
76 // * {file,directory}name
77 size_t end = path.find_first_of("\\/", 2);
78 if (end == StringRef::npos)
81 return StringRef(path.begin(), end);
86 size_t filename_pos(const StringRef &str) {
87 if (str.size() == 2 &&
88 is_separator(str[0]) &&
92 if (str.size() > 0 && is_separator(str[str.size() - 1]))
93 return str.size() - 1;
95 size_t pos = str.find_last_of(separators, str.size() - 1);
98 if (pos == StringRef::npos)
99 pos = str.find_last_of(':', str.size() - 2);
102 if (pos == StringRef::npos ||
103 (pos == 1 && is_separator(str[0])))
109 size_t root_dir_start(const StringRef &str) {
112 if (str.size() > 2 &&
114 is_separator(str[2]))
119 if (str.size() == 2 &&
120 is_separator(str[0]) &&
122 return StringRef::npos;
125 if (str.size() > 3 &&
126 is_separator(str[0]) &&
128 !is_separator(str[2])) {
129 return str.find_first_of(separators, 2);
133 if (str.size() > 0 && is_separator(str[0]))
136 return StringRef::npos;
139 size_t parent_path_end(const StringRef &path) {
140 size_t end_pos = filename_pos(path);
142 bool filename_was_sep = path.size() > 0 && is_separator(path[end_pos]);
144 // Skip separators except for root dir.
145 size_t root_dir_pos = root_dir_start(StringRef(path.begin(), end_pos));
148 (end_pos - 1) != root_dir_pos &&
149 is_separator(path[end_pos - 1]))
152 if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
153 return StringRef::npos;
163 const_iterator begin(const StringRef &path) {
166 i.Component = find_first_component(path);
171 const_iterator end(const StringRef &path) {
174 i.Position = path.size();
178 const_iterator &const_iterator::operator++() {
179 assert(Position < Path.size() && "Tried to increment past end!");
181 // Increment Position to past the current component
182 Position += Component.size();
185 if (Position == Path.size()) {
186 Component = StringRef();
190 // Both POSIX and Windows treat paths that begin with exactly two separators
192 bool was_net = Component.size() > 2 &&
193 is_separator(Component[0]) &&
194 Component[1] == Component[0] &&
195 !is_separator(Component[2]);
197 // Handle separators.
198 if (is_separator(Path[Position])) {
203 || Component.endswith(":")
206 Component = StringRef(Path.begin() + Position, 1);
210 // Skip extra separators.
211 while (Position != Path.size() &&
212 is_separator(Path[Position])) {
216 // Treat trailing '/' as a '.'.
217 if (Position == Path.size()) {
224 // Find next component.
225 size_t end_pos = Path.find_first_of(separators, Position);
226 if (end_pos == StringRef::npos)
227 end_pos = Path.size();
228 Component = StringRef(Path.begin() + Position, end_pos - Position);
233 const_iterator &const_iterator::operator--() {
234 // If we're at the end and the previous char was a '/', return '.'.
235 if (Position == Path.size() &&
237 is_separator(Path[Position - 1])
239 && Path[Position - 2] != ':'
247 // Skip separators unless it's the root directory.
248 size_t root_dir_pos = root_dir_start(Path);
249 size_t end_pos = Position;
252 (end_pos - 1) != root_dir_pos &&
253 is_separator(Path[end_pos - 1]))
256 // Find next separator.
257 size_t start_pos = filename_pos(StringRef(Path.begin(), end_pos));
258 Component = StringRef(Path.begin() + start_pos, end_pos - start_pos);
259 Position = start_pos;
263 bool const_iterator::operator==(const const_iterator &RHS) const {
264 return Path.begin() == RHS.Path.begin() &&
265 Position == RHS.Position;
268 bool const_iterator::operator!=(const const_iterator &RHS) const {
269 return !(*this == RHS);
272 ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
273 return Position - RHS.Position;
276 error_code root_path(const StringRef &path, StringRef &result) {
277 const_iterator b = begin(path),
281 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
289 if (has_net || has_drive) {
290 if ((++pos != e) && is_separator((*pos)[0])) {
291 // {C:/,//net/}, so get the first two components.
292 result = StringRef(path.begin(), b->size() + pos->size());
293 return make_error_code(errc::success);
295 // just {C:,//net}, return the first component.
297 return make_error_code(errc::success);
301 // POSIX style root directory.
302 if (is_separator((*b)[0])) {
304 return make_error_code(errc::success);
308 result = StringRef();
309 return make_error_code(errc::success);
313 result = StringRef();
314 return make_error_code(errc::success);
317 error_code root_name(const StringRef &path, StringRef &result) {
318 const_iterator b = begin(path),
321 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
329 if (has_net || has_drive) {
330 // just {C:,//net}, return the first component.
332 return make_error_code(errc::success);
336 // No path or no name.
337 result = StringRef();
338 return make_error_code(errc::success);
341 error_code root_directory(const StringRef &path, StringRef &result) {
342 const_iterator b = begin(path),
346 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
354 if ((has_net || has_drive) &&
355 // {C:,//net}, skip to the next component.
356 (++pos != e) && is_separator((*pos)[0])) {
358 return make_error_code(errc::success);
361 // POSIX style root directory.
362 if (!has_net && is_separator((*b)[0])) {
364 return make_error_code(errc::success);
368 // No path or no root.
369 result = StringRef();
370 return make_error_code(errc::success);
373 error_code relative_path(const StringRef &path, StringRef &result) {
375 if (error_code ec = root_path(path, root)) return ec;
376 result = StringRef(path.begin() + root.size(), path.size() - root.size());
377 return make_error_code(errc::success);
380 error_code append(SmallVectorImpl<char> &path, const Twine &a,
384 SmallString<32> a_storage;
385 SmallString<32> b_storage;
386 SmallString<32> c_storage;
387 SmallString<32> d_storage;
389 SmallVector<StringRef, 4> components;
390 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
391 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
392 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
393 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
395 for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(),
396 e = components.end();
398 bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]);
399 bool component_has_sep = !i->empty() && is_separator((*i)[0]);
401 if (error_code ec = has_root_name(*i, is_root_name)) return ec;
404 // Strip separators from beginning of component.
405 size_t loc = i->find_first_not_of(separators);
406 StringRef c = StringRef(i->begin() + loc, i->size() - loc);
409 path.append(c.begin(), c.end());
413 if (!component_has_sep && !(path.empty() && is_root_name)) {
415 path.push_back(prefered_separator);
418 path.append(i->begin(), i->end());
421 return make_error_code(errc::success);
424 error_code make_absolute(SmallVectorImpl<char> &path) {
425 StringRef p(path.data(), path.size());
427 bool rootName, rootDirectory;
428 if (error_code ec = has_root_name(p, rootName)) return ec;
429 if (error_code ec = has_root_directory(p, rootDirectory)) return ec;
432 if (rootName && rootDirectory)
433 return make_error_code(errc::success);
435 // All of the following conditions will need the current directory.
436 SmallString<128> current_dir;
437 if (error_code ec = current_path(current_dir)) return ec;
439 // Relative path. Prepend the current directory.
440 if (!rootName && !rootDirectory) {
441 // Append path to the current directory.
442 if (error_code ec = append(current_dir, p)) return ec;
443 // Set path to the result.
444 path.swap(current_dir);
445 return make_error_code(errc::success);
448 if (!rootName && rootDirectory) {
450 if (error_code ec = root_name(current_dir, cdrn)) return ec;
451 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
452 if (error_code ec = append(curDirRootName, p)) return ec;
453 // Set path to the result.
454 path.swap(curDirRootName);
455 return make_error_code(errc::success);
458 if (rootName && !rootDirectory) {
460 StringRef bRootDirectory;
461 StringRef bRelativePath;
462 StringRef pRelativePath;
463 if (error_code ec = root_name(p, pRootName)) return ec;
464 if (error_code ec = root_directory(current_dir, bRootDirectory)) return ec;
465 if (error_code ec = relative_path(current_dir, bRelativePath)) return ec;
466 if (error_code ec = relative_path(p, pRelativePath)) return ec;
468 SmallString<128> res;
469 if (error_code ec = append(res, pRootName, bRootDirectory,
470 bRelativePath, pRelativePath)) return ec;
472 return make_error_code(errc::success);
475 llvm_unreachable("All rootName and rootDirectory combinations should have "
479 error_code parent_path(const StringRef &path, StringRef &result) {
480 size_t end_pos = parent_path_end(path);
481 if (end_pos == StringRef::npos)
482 result = StringRef();
484 result = StringRef(path.data(), end_pos);
485 return make_error_code(errc::success);
488 error_code remove_filename(SmallVectorImpl<char> &path) {
489 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()));
490 if (end_pos == StringRef::npos)
491 return make_error_code(errc::success);
492 path.set_size(end_pos);
493 return make_error_code(errc::success);
496 error_code replace_extension(SmallVectorImpl<char> &path,
497 const Twine &extension) {
498 StringRef p(path.begin(), path.size());
499 SmallString<32> ext_storage;
500 StringRef ext = extension.toStringRef(ext_storage);
502 // Erase existing extension.
503 size_t pos = p.find_last_of('.');
504 if (pos != StringRef::npos && pos >= filename_pos(p))
507 // Append '.' if needed.
508 if (ext.size() > 0 && ext[0] != '.')
512 path.append(ext.begin(), ext.end());
513 return make_error_code(errc::success);
516 error_code native(const Twine &path, SmallVectorImpl<char> &result) {
520 SmallString<128> path_storage;
521 StringRef p = path.toStringRef(path_storage);
522 result.reserve(p.size());
523 for (StringRef::const_iterator i = p.begin(),
528 result.push_back('\\');
530 result.push_back(*i);
533 path.toVector(result);
535 return make_error_code(errc::success);
538 error_code filename(const StringRef &path, StringRef &result) {
539 result = *(--end(path));
540 return make_error_code(errc::success);
543 error_code stem(const StringRef &path, StringRef &result) {
545 if (error_code ec = filename(path, fname)) return ec;
546 size_t pos = fname.find_last_of('.');
547 if (pos == StringRef::npos)
550 if ((fname.size() == 1 && fname == ".") ||
551 (fname.size() == 2 && fname == ".."))
554 result = StringRef(fname.begin(), pos);
556 return make_error_code(errc::success);
559 error_code extension(const StringRef &path, StringRef &result) {
561 if (error_code ec = filename(path, fname)) return ec;
562 size_t pos = fname.find_last_of('.');
563 if (pos == StringRef::npos)
564 result = StringRef();
566 if ((fname.size() == 1 && fname == ".") ||
567 (fname.size() == 2 && fname == ".."))
568 result = StringRef();
570 result = StringRef(fname.begin() + pos, fname.size() - pos);
572 return make_error_code(errc::success);
575 error_code has_root_name(const Twine &path, bool &result) {
576 SmallString<128> path_storage;
577 StringRef p = path.toStringRef(path_storage);
579 if (error_code ec = root_name(p, p)) return ec;
582 return make_error_code(errc::success);
585 error_code has_root_directory(const Twine &path, bool &result) {
586 SmallString<128> path_storage;
587 StringRef p = path.toStringRef(path_storage);
589 if (error_code ec = root_directory(p, p)) return ec;
592 return make_error_code(errc::success);
595 error_code has_root_path(const Twine &path, bool &result) {
596 SmallString<128> path_storage;
597 StringRef p = path.toStringRef(path_storage);
599 if (error_code ec = root_path(p, p)) return ec;
602 return make_error_code(errc::success);
605 error_code has_filename(const Twine &path, bool &result) {
606 SmallString<128> path_storage;
607 StringRef p = path.toStringRef(path_storage);
609 if (error_code ec = filename(p, p)) return ec;
612 return make_error_code(errc::success);
615 error_code has_parent_path(const Twine &path, bool &result) {
616 SmallString<128> path_storage;
617 StringRef p = path.toStringRef(path_storage);
619 if (error_code ec = parent_path(p, p)) return ec;
622 return make_error_code(errc::success);
625 error_code has_stem(const Twine &path, bool &result) {
626 SmallString<128> path_storage;
627 StringRef p = path.toStringRef(path_storage);
629 if (error_code ec = stem(p, p)) return ec;
632 return make_error_code(errc::success);
635 error_code has_extension(const Twine &path, bool &result) {
636 SmallString<128> path_storage;
637 StringRef p = path.toStringRef(path_storage);
639 if (error_code ec = extension(p, p)) return ec;
642 return make_error_code(errc::success);
645 error_code is_absolute(const Twine &path, bool &result) {
646 SmallString<128> path_storage;
647 StringRef p = path.toStringRef(path_storage);
649 bool rootDir = false,
651 if (error_code ec = has_root_directory(p, rootDir)) return ec;
653 if (error_code ec = has_root_name(p, rootName)) return ec;
658 result = rootDir && rootName;
659 return make_error_code(errc::success);
662 error_code is_relative(const Twine &path, bool &result) {
664 error_code ec = is_absolute(path, res);
673 // Include the truly platform-specific parts.
674 #if defined(LLVM_ON_UNIX)
675 #include "Unix/PathV2.inc"
677 #if defined(LLVM_ON_WIN32)
678 #include "Windows/PathV2.inc"