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 has_root_name(const Twine &path, bool &result) {
374 SmallString<128> storage;
375 StringRef p = path.toStringRef(storage);
377 if (error_code ec = root_name(p, p)) return ec;
379 return make_error_code(errc::success);
382 error_code has_root_directory(const Twine &path, bool &result) {
383 SmallString<128> storage;
384 StringRef p = path.toStringRef(storage);
386 if (error_code ec = root_directory(p, p)) return ec;
388 return make_error_code(errc::success);
391 error_code relative_path(const StringRef &path, StringRef &result) {
393 if (error_code ec = root_path(path, root)) return ec;
394 result = StringRef(path.begin() + root.size(), path.size() - root.size());
395 return make_error_code(errc::success);
398 error_code append(SmallVectorImpl<char> &path, const Twine &a,
402 SmallString<32> a_storage;
403 SmallString<32> b_storage;
404 SmallString<32> c_storage;
405 SmallString<32> d_storage;
407 SmallVector<StringRef, 4> components;
408 if (!a.isTriviallyEmpty()) components.push_back(a.toStringRef(a_storage));
409 if (!b.isTriviallyEmpty()) components.push_back(b.toStringRef(b_storage));
410 if (!c.isTriviallyEmpty()) components.push_back(c.toStringRef(c_storage));
411 if (!d.isTriviallyEmpty()) components.push_back(d.toStringRef(d_storage));
413 for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(),
414 e = components.end();
416 bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]);
417 bool component_has_sep = !i->empty() && is_separator((*i)[0]);
419 if (error_code ec = has_root_name(*i, is_root_name)) return ec;
422 // Strip separators from beginning of component.
423 size_t loc = i->find_first_not_of(separators);
424 StringRef c = StringRef(i->begin() + loc, i->size() - loc);
427 path.append(c.begin(), c.end());
431 if (!component_has_sep && !(path.empty() && is_root_name)) {
433 path.push_back(prefered_separator);
436 path.append(i->begin(), i->end());
439 return make_error_code(errc::success);
442 error_code make_absolute(SmallVectorImpl<char> &path) {
443 StringRef p(path.data(), path.size());
445 bool rootName, rootDirectory;
446 if (error_code ec = has_root_name(p, rootName)) return ec;
447 if (error_code ec = has_root_directory(p, rootDirectory)) return ec;
450 if (rootName && rootDirectory)
451 return make_error_code(errc::success);
453 // All of the following conditions will need the current directory.
454 SmallString<128> current_dir;
455 if (error_code ec = current_path(current_dir)) return ec;
457 // Relative path. Prepend the current directory.
458 if (!rootName && !rootDirectory) {
459 // Append path to the current directory.
460 if (error_code ec = append(current_dir, p)) return ec;
461 // Set path to the result.
462 path.swap(current_dir);
463 return make_error_code(errc::success);
466 if (!rootName && rootDirectory) {
468 if (error_code ec = root_name(current_dir, cdrn)) return ec;
469 SmallString<128> curDirRootName(cdrn.begin(), cdrn.end());
470 if (error_code ec = append(curDirRootName, p)) return ec;
471 // Set path to the result.
472 path.swap(curDirRootName);
473 return make_error_code(errc::success);
476 if (rootName && !rootDirectory) {
478 StringRef bRootDirectory;
479 StringRef bRelativePath;
480 StringRef pRelativePath;
481 if (error_code ec = root_name(p, pRootName)) return ec;
482 if (error_code ec = root_directory(current_dir, bRootDirectory)) return ec;
483 if (error_code ec = relative_path(current_dir, bRelativePath)) return ec;
484 if (error_code ec = relative_path(p, pRelativePath)) return ec;
486 SmallString<128> res;
487 if (error_code ec = append(res, pRootName, bRootDirectory,
488 bRelativePath, pRelativePath)) return ec;
490 return make_error_code(errc::success);
493 llvm_unreachable("All rootName and rootDirectory combinations should have "
497 error_code parent_path(const StringRef &path, StringRef &result) {
498 size_t end_pos = parent_path_end(path);
499 if (end_pos == StringRef::npos)
500 result = StringRef();
502 result = StringRef(path.data(), end_pos);
503 return make_error_code(errc::success);
506 error_code remove_filename(SmallVectorImpl<char> &path) {
507 size_t end_pos = parent_path_end(StringRef(path.begin(), path.size()));
508 if (end_pos == StringRef::npos)
509 return make_error_code(errc::success);
510 path.set_size(end_pos);
511 return make_error_code(errc::success);
514 error_code replace_extension(SmallVectorImpl<char> &path,
515 const Twine &extension) {
516 StringRef p(path.begin(), path.size());
517 SmallString<32> ext_storage;
518 StringRef ext = extension.toStringRef(ext_storage);
520 // Erase existing extension.
521 size_t pos = p.find_last_of('.');
522 if (pos != StringRef::npos && pos >= filename_pos(p))
525 // Append '.' if needed.
526 if (ext.size() > 0 && ext[0] != '.')
530 path.append(ext.begin(), ext.end());
531 return make_error_code(errc::success);
534 error_code native(const Twine &path, SmallVectorImpl<char> &result) {
538 SmallString<128> path_storage;
539 StringRef p = path.toStringRef(path_storage);
540 result.reserve(p.size());
541 for (StringRef::const_iterator i = p.begin(),
546 result.push_back('\\');
548 result.push_back(*i);
551 path.toVector(result);
553 return make_error_code(errc::success);
556 error_code filename(const StringRef &path, StringRef &result) {
557 result = *(--end(path));
558 return make_error_code(errc::success);
565 // Include the truly platform-specific parts.
566 #if defined(LLVM_ON_UNIX)
567 #include "Unix/PathV2.inc"
569 #if defined(LLVM_ON_WIN32)
570 #include "Windows/PathV2.inc"