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/FileSystem.h"
16 #include "llvm/Support/ErrorHandling.h"
20 using llvm::StringRef;
22 bool is_separator(const char value) {
25 case '\\': // fall through
27 case '/': return true;
28 default: return false;
33 const StringRef separators = "\\/";
34 const char prefered_separator = '\\';
36 const StringRef separators = "/";
37 const char prefered_separator = '/';
40 StringRef find_first_component(const StringRef &path) {
41 // Look for this first component in the following order.
42 // * empty (in this case we return an empty string)
43 // * either C: or {//,\\}net.
46 // * {file,directory}name
52 if (path.size() >= 2 && std::isalpha(path[0]) && path[1] == ':')
53 return StringRef(path.begin(), 2);
56 if ((path.size() > 2) &&
57 (path.startswith("\\\\") || path.startswith("//")) &&
58 (path[2] != '\\' && path[2] != '/')) {
59 // Find the next directory separator.
60 size_t end = path.find_first_of("\\/", 2);
61 if (end == StringRef::npos)
64 return StringRef(path.begin(), end);
68 if (path[0] == '\\' || path[0] == '/')
69 return StringRef(path.begin(), 1);
71 if (path.startswith(".."))
72 return StringRef(path.begin(), 2);
75 return StringRef(path.begin(), 1);
77 // * {file,directory}name
78 size_t end = path.find_first_of("\\/", 2);
79 if (end == StringRef::npos)
82 return StringRef(path.begin(), end);
87 size_t filename_pos(const StringRef &str) {
88 if (str.size() == 2 &&
89 is_separator(str[0]) &&
93 if (str.size() > 0 && is_separator(str[str.size() - 1]))
94 return str.size() - 1;
96 size_t pos = str.find_last_of(separators, str.size() - 1);
99 if (pos == StringRef::npos)
100 pos = str.find_last_of(':', str.size() - 2);
103 if (pos == StringRef::npos ||
104 (pos == 1 && is_separator(str[0])))
110 size_t root_dir_start(const StringRef &str) {
113 if (str.size() > 2 &&
115 is_separator(str[2]))
120 if (str.size() == 2 &&
121 is_separator(str[0]) &&
123 return StringRef::npos;
126 if (str.size() > 3 &&
127 is_separator(str[0]) &&
129 !is_separator(str[2])) {
130 return str.find_first_of(separators, 2);
134 if (str.size() > 0 && is_separator(str[0]))
137 return StringRef::npos;
140 size_t parent_path_end(const StringRef &path) {
141 size_t end_pos = filename_pos(path);
143 bool filename_was_sep = path.size() > 0 && is_separator(path[end_pos]);
145 // Skip separators except for root dir.
146 size_t root_dir_pos = root_dir_start(StringRef(path.begin(), end_pos));
149 (end_pos - 1) != root_dir_pos &&
150 is_separator(path[end_pos - 1]))
153 if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
154 return StringRef::npos;
164 const_iterator begin(const StringRef &path) {
167 i.Component = find_first_component(path);
172 const_iterator end(const StringRef &path) {
175 i.Position = path.size();
179 const_iterator &const_iterator::operator++() {
180 assert(Position < Path.size() && "Tried to increment past end!");
182 // Increment Position to past the current component
183 Position += Component.size();
186 if (Position == Path.size()) {
187 Component = StringRef();
191 // Both POSIX and Windows treat paths that begin with exactly two separators
193 bool was_net = Component.size() > 2 &&
194 is_separator(Component[0]) &&
195 Component[1] == Component[0] &&
196 !is_separator(Component[2]);
198 // Handle separators.
199 if (is_separator(Path[Position])) {
204 || Component.endswith(":")
207 Component = StringRef(Path.begin() + Position, 1);
211 // Skip extra separators.
212 while (Position != Path.size() &&
213 is_separator(Path[Position])) {
217 // Treat trailing '/' as a '.'.
218 if (Position == Path.size()) {
225 // Find next component.
226 size_t end_pos = Path.find_first_of(separators, Position);
227 if (end_pos == StringRef::npos)
228 end_pos = Path.size();
229 Component = StringRef(Path.begin() + Position, end_pos - Position);
234 const_iterator &const_iterator::operator--() {
235 // If we're at the end and the previous char was a '/', return '.'.
236 if (Position == Path.size() &&
238 is_separator(Path[Position - 1])
240 && Path[Position - 2] != ':'
248 // Skip separators unless it's the root directory.
249 size_t root_dir_pos = root_dir_start(Path);
250 size_t end_pos = Position;
253 (end_pos - 1) != root_dir_pos &&
254 is_separator(Path[end_pos - 1]))
257 // Find next separator.
258 size_t start_pos = filename_pos(StringRef(Path.begin(), end_pos));
259 Component = StringRef(Path.begin() + start_pos, end_pos - start_pos);
260 Position = start_pos;
264 bool const_iterator::operator==(const const_iterator &RHS) const {
265 return Path.begin() == RHS.Path.begin() &&
266 Position == RHS.Position;
269 bool const_iterator::operator!=(const const_iterator &RHS) const {
270 return !(*this == RHS);
273 ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
274 return Position - RHS.Position;
277 error_code root_path(const StringRef &path, StringRef &result) {
278 const_iterator b = begin(path),
282 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
290 if (has_net || has_drive) {
291 if ((++pos != e) && is_separator((*pos)[0])) {
292 // {C:/,//net/}, so get the first two components.
293 result = StringRef(path.begin(), b->size() + pos->size());
294 return make_error_code(errc::success);
296 // just {C:,//net}, return the first component.
298 return make_error_code(errc::success);
302 // POSIX style root directory.
303 if (is_separator((*b)[0])) {
305 return make_error_code(errc::success);
309 result = StringRef();
310 return make_error_code(errc::success);
314 result = StringRef();
315 return make_error_code(errc::success);
318 error_code root_name(const StringRef &path, StringRef &result) {
319 const_iterator b = begin(path),
322 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
330 if (has_net || has_drive) {
331 // just {C:,//net}, return the first component.
333 return make_error_code(errc::success);
337 // No path or no name.
338 result = StringRef();
339 return make_error_code(errc::success);
342 error_code root_directory(const StringRef &path, StringRef &result) {
343 const_iterator b = begin(path),
347 bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
355 if ((has_net || has_drive) &&
356 // {C:,//net}, skip to the next component.
357 (++pos != e) && is_separator((*pos)[0])) {
359 return make_error_code(errc::success);
362 // POSIX style root directory.
363 if (!has_net && is_separator((*b)[0])) {
365 return make_error_code(errc::success);
369 // No path or no root.
370 result = StringRef();
371 return make_error_code(errc::success);
374 error_code relative_path(const StringRef &path, StringRef &result) {
376 if (error_code ec = root_path(path, root)) return ec;
377 result = StringRef(path.begin() + root.size(), path.size() - root.size());
378 return make_error_code(errc::success);
381 error_code append(SmallVectorImpl<char> &path, const Twine &a,
385 SmallString<32> a_storage;
386 SmallString<32> b_storage;
387 SmallString<32> c_storage;
388 SmallString<32> d_storage;
390 SmallVector<StringRef, 4> components;
391 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
392 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
393 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
394 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
396 for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(),
397 e = components.end();
399 bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]);
400 bool component_has_sep = !i->empty() && is_separator((*i)[0]);
402 if (error_code ec = has_root_name(*i, is_root_name)) return ec;
405 // Strip separators from beginning of component.
406 size_t loc = i->find_first_not_of(separators);
407 StringRef c = StringRef(i->begin() + loc, i->size() - loc);
410 path.append(c.begin(), c.end());
414 if (!component_has_sep && !(path.empty() && is_root_name)) {
416 path.push_back(prefered_separator);
419 path.append(i->begin(), i->end());
422 return make_error_code(errc::success);
425 error_code make_absolute(SmallVectorImpl<char> &path) {
426 StringRef p(path.data(), path.size());
428 bool rootName, rootDirectory;
429 if (error_code ec = has_root_name(p, rootName)) return ec;
430 if (error_code ec = has_root_directory(p, rootDirectory)) return ec;
433 if (rootName && rootDirectory)
434 return make_error_code(errc::success);
436 // All of the following conditions will need the current directory.
437 SmallString<128> current_dir;
438 if (error_code ec = current_path(current_dir)) return ec;
440 // Relative path. Prepend the current directory.
441 if (!rootName && !rootDirectory) {
442 // Append path to the current directory.
443 if (error_code ec = append(current_dir, p)) return ec;
444 // Set path to the result.
445 path.swap(current_dir);
446 return make_error_code(errc::success);
449 if (!rootName && rootDirectory) {
451 if (error_code ec = root_name(current_dir, cdrn)) return ec;
452 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
453 if (error_code ec = append(curDirRootName, p)) return ec;
454 // Set path to the result.
455 path.swap(curDirRootName);
456 return make_error_code(errc::success);
459 if (rootName && !rootDirectory) {
461 StringRef bRootDirectory;
462 StringRef bRelativePath;
463 StringRef pRelativePath;
464 if (error_code ec = root_name(p, pRootName)) return ec;
465 if (error_code ec = root_directory(current_dir, bRootDirectory)) return ec;
466 if (error_code ec = relative_path(current_dir, bRelativePath)) return ec;
467 if (error_code ec = relative_path(p, pRelativePath)) return ec;
469 SmallString<128> res;
470 if (error_code ec = append(res, pRootName, bRootDirectory,
471 bRelativePath, pRelativePath)) return ec;
473 return make_error_code(errc::success);
476 llvm_unreachable("All rootName and rootDirectory combinations should have "
480 error_code parent_path(const StringRef &path, StringRef &result) {
481 size_t end_pos = parent_path_end(path);
482 if (end_pos == StringRef::npos)
483 result = StringRef();
485 result = StringRef(path.data(), end_pos);
486 return make_error_code(errc::success);
489 error_code remove_filename(SmallVectorImpl<char> &path) {
490 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()));
491 if (end_pos == StringRef::npos)
492 return make_error_code(errc::success);
493 path.set_size(end_pos);
494 return make_error_code(errc::success);
497 error_code replace_extension(SmallVectorImpl<char> &path,
498 const Twine &extension) {
499 StringRef p(path.begin(), path.size());
500 SmallString<32> ext_storage;
501 StringRef ext = extension.toStringRef(ext_storage);
503 // Erase existing extension.
504 size_t pos = p.find_last_of('.');
505 if (pos != StringRef::npos && pos >= filename_pos(p))
508 // Append '.' if needed.
509 if (ext.size() > 0 && ext[0] != '.')
513 path.append(ext.begin(), ext.end());
514 return make_error_code(errc::success);
517 error_code native(const Twine &path, SmallVectorImpl<char> &result) {
521 SmallString<128> path_storage;
522 StringRef p = path.toStringRef(path_storage);
523 result.reserve(p.size());
524 for (StringRef::const_iterator i = p.begin(),
529 result.push_back('\\');
531 result.push_back(*i);
534 path.toVector(result);
536 return make_error_code(errc::success);
539 error_code filename(const StringRef &path, StringRef &result) {
540 result = *(--end(path));
541 return make_error_code(errc::success);
544 error_code stem(const StringRef &path, StringRef &result) {
546 if (error_code ec = filename(path, fname)) return ec;
547 size_t pos = fname.find_last_of('.');
548 if (pos == StringRef::npos)
551 if ((fname.size() == 1 && fname == ".") ||
552 (fname.size() == 2 && fname == ".."))
555 result = StringRef(fname.begin(), pos);
557 return make_error_code(errc::success);
560 error_code extension(const StringRef &path, StringRef &result) {
562 if (error_code ec = filename(path, fname)) return ec;
563 size_t pos = fname.find_last_of('.');
564 if (pos == StringRef::npos)
565 result = StringRef();
567 if ((fname.size() == 1 && fname == ".") ||
568 (fname.size() == 2 && fname == ".."))
569 result = StringRef();
571 result = StringRef(fname.begin() + pos, fname.size() - pos);
573 return make_error_code(errc::success);
576 error_code has_root_name(const Twine &path, bool &result) {
577 SmallString<128> path_storage;
578 StringRef p = path.toStringRef(path_storage);
580 if (error_code ec = root_name(p, p)) return ec;
583 return make_error_code(errc::success);
586 error_code has_root_directory(const Twine &path, bool &result) {
587 SmallString<128> path_storage;
588 StringRef p = path.toStringRef(path_storage);
590 if (error_code ec = root_directory(p, p)) return ec;
593 return make_error_code(errc::success);
596 error_code has_root_path(const Twine &path, bool &result) {
597 SmallString<128> path_storage;
598 StringRef p = path.toStringRef(path_storage);
600 if (error_code ec = root_path(p, p)) return ec;
603 return make_error_code(errc::success);
606 error_code has_filename(const Twine &path, bool &result) {
607 SmallString<128> path_storage;
608 StringRef p = path.toStringRef(path_storage);
610 if (error_code ec = filename(p, p)) return ec;
613 return make_error_code(errc::success);
616 error_code has_parent_path(const Twine &path, bool &result) {
617 SmallString<128> path_storage;
618 StringRef p = path.toStringRef(path_storage);
620 if (error_code ec = parent_path(p, p)) return ec;
623 return make_error_code(errc::success);
626 error_code has_stem(const Twine &path, bool &result) {
627 SmallString<128> path_storage;
628 StringRef p = path.toStringRef(path_storage);
630 if (error_code ec = stem(p, p)) return ec;
633 return make_error_code(errc::success);
636 error_code has_extension(const Twine &path, bool &result) {
637 SmallString<128> path_storage;
638 StringRef p = path.toStringRef(path_storage);
640 if (error_code ec = extension(p, p)) return ec;
643 return make_error_code(errc::success);
646 error_code is_absolute(const Twine &path, bool &result) {
647 SmallString<128> path_storage;
648 StringRef p = path.toStringRef(path_storage);
650 bool rootDir = false,
652 if (error_code ec = has_root_directory(p, rootDir)) return ec;
654 if (error_code ec = has_root_name(p, rootName)) return ec;
659 result = rootDir && rootName;
660 return make_error_code(errc::success);
663 error_code is_relative(const Twine &path, bool &result) {
665 error_code ec = is_absolute(path, res);
670 } // end namespace path
674 } // end namespace fs
675 } // end namespace sys
676 } // end namespace llvm
678 // Include the truly platform-specific parts.
679 #if defined(LLVM_ON_UNIX)
680 #include "Unix/PathV2.inc"
682 #if defined(LLVM_ON_WIN32)
683 #include "Windows/PathV2.inc"