57ccf893d9bb2d04ffe2ec3db77e8421882f90eb
[oota-llvm.git] / lib / Support / PathV2.cpp
1 //===-- 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"
15 #include "llvm/Support/ErrorHandling.h"
16 #include <cctype>
17
18 namespace {
19   using llvm::StringRef;
20
21   bool is_separator(const char value) {
22     switch(value) {
23 #ifdef LLVM_ON_WIN32
24     case '\\': // fall through
25 #endif
26     case '/': return true;
27     default: return false;
28     }
29   }
30
31 #ifdef LLVM_ON_WIN32
32   const StringRef separators = "\\/";
33   const char       prefered_separator = '\\';
34 #else
35   const StringRef separators = "/";
36   const char      prefered_separator = '/';
37 #endif
38
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.
43     // * {/,\}
44     // * {.,..}
45     // * {file,directory}name
46
47     if (path.empty())
48       return path;
49
50     // C:
51     if (path.size() >= 2 && std::isalpha(path[0]) && path[1] == ':')
52       return StringRef(path.begin(), 2);
53
54     // //net
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)
61         return path;
62       else
63         return StringRef(path.begin(), end);
64     }
65
66     // {/,\}
67     if (path[0] == '\\' || path[0] == '/')
68       return StringRef(path.begin(), 1);
69
70     if (path.startswith(".."))
71       return StringRef(path.begin(), 2);
72
73     if (path[0] == '.')
74       return StringRef(path.begin(), 1);
75
76     // * {file,directory}name
77     size_t end = path.find_first_of("\\/", 2);
78     if (end == StringRef::npos)
79       return path;
80     else
81       return StringRef(path.begin(), end);
82
83     return StringRef();
84   }
85
86   size_t filename_pos(const StringRef &str) {
87     if (str.size() == 2 &&
88         is_separator(str[0]) &&
89         is_separator(str[1]))
90       return 0;
91
92     if (str.size() > 0 && is_separator(str[str.size() - 1]))
93       return str.size() - 1;
94
95     size_t pos = str.find_last_of(separators, str.size() - 1);
96
97 #ifdef LLVM_ON_WIN32
98     if (pos == StringRef::npos)
99       pos = str.find_last_of(':', str.size() - 2);
100 #endif
101
102     if (pos == StringRef::npos ||
103         (pos == 1 && is_separator(str[0])))
104       return 0;
105
106     return pos + 1;
107   }
108
109   size_t root_dir_start(const StringRef &str) {
110     // case "c:/"
111 #ifdef LLVM_ON_WIN32
112     if (str.size() > 2 &&
113         str[1] == ':' &&
114         is_separator(str[2]))
115       return 2;
116 #endif
117
118     // case "//"
119     if (str.size() == 2 &&
120         is_separator(str[0]) &&
121         str[0] == str[1])
122       return StringRef::npos;
123
124     // case "//net"
125     if (str.size() > 3 &&
126         is_separator(str[0]) &&
127         str[0] == str[1] &&
128         !is_separator(str[2])) {
129       return str.find_first_of(separators, 2);
130     }
131
132     // case "/"
133     if (str.size() > 0 && is_separator(str[0]))
134       return 0;
135
136     return StringRef::npos;
137   }
138
139   size_t parent_path_end(const StringRef &path) {
140     size_t end_pos = filename_pos(path);
141
142     bool filename_was_sep = path.size() > 0 && is_separator(path[end_pos]);
143
144     // Skip separators except for root dir.
145     size_t root_dir_pos = root_dir_start(StringRef(path.begin(), end_pos));
146
147     while(end_pos > 0 &&
148           (end_pos - 1) != root_dir_pos &&
149           is_separator(path[end_pos - 1]))
150       --end_pos;
151
152     if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep)
153       return StringRef::npos;
154
155     return end_pos;
156   }
157 }
158
159 namespace llvm {
160 namespace sys  {
161 namespace path {
162
163 const_iterator begin(const StringRef &path) {
164   const_iterator i;
165   i.Path      = path;
166   i.Component = find_first_component(path);
167   i.Position  = 0;
168   return i;
169 }
170
171 const_iterator end(const StringRef &path) {
172   const_iterator i;
173   i.Path      = path;
174   i.Position  = path.size();
175   return i;
176 }
177
178 const_iterator &const_iterator::operator++() {
179   assert(Position < Path.size() && "Tried to increment past end!");
180
181   // Increment Position to past the current component
182   Position += Component.size();
183
184   // Check for end.
185   if (Position == Path.size()) {
186     Component = StringRef();
187     return *this;
188   }
189
190   // Both POSIX and Windows treat paths that begin with exactly two separators
191   // specially.
192   bool was_net = Component.size() > 2 &&
193     is_separator(Component[0]) &&
194     Component[1] == Component[0] &&
195     !is_separator(Component[2]);
196
197   // Handle separators.
198   if (is_separator(Path[Position])) {
199     // Root dir.
200     if (was_net
201 #ifdef LLVM_ON_WIN32
202         // c:/
203         || Component.endswith(":")
204 #endif
205         ) {
206       Component = StringRef(Path.begin() + Position, 1);
207       return *this;
208     }
209
210     // Skip extra separators.
211     while (Position != Path.size() &&
212            is_separator(Path[Position])) {
213       ++Position;
214     }
215
216     // Treat trailing '/' as a '.'.
217     if (Position == Path.size()) {
218       --Position;
219       Component = ".";
220       return *this;
221     }
222   }
223
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);
229
230   return *this;
231 }
232
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() &&
236       Path.size() > 1 &&
237       is_separator(Path[Position - 1])
238 #ifdef LLVM_ON_WIN32
239       && Path[Position - 2] != ':'
240 #endif
241       ) {
242     --Position;
243     Component = ".";
244     return *this;
245   }
246
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;
250
251   while(end_pos > 0 &&
252         (end_pos - 1) != root_dir_pos &&
253         is_separator(Path[end_pos - 1]))
254     --end_pos;
255
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;
260   return *this;
261 }
262
263 bool const_iterator::operator==(const const_iterator &RHS) const {
264   return Path.begin() == RHS.Path.begin() &&
265          Position == RHS.Position;
266 }
267
268 bool const_iterator::operator!=(const const_iterator &RHS) const {
269   return !(*this == RHS);
270 }
271
272 ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const {
273   return Position - RHS.Position;
274 }
275
276 error_code root_path(const StringRef &path, StringRef &result) {
277   const_iterator b = begin(path),
278                  pos = b,
279                  e = end(path);
280   if (b != e) {
281     bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
282     bool has_drive =
283 #ifdef LLVM_ON_WIN32
284       b->endswith(":");
285 #else
286       false;
287 #endif
288
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);
294       } else {
295         // just {C:,//net}, return the first component.
296         result = *b;
297         return make_error_code(errc::success);
298       }
299     }
300
301     // POSIX style root directory.
302     if (is_separator((*b)[0])) {
303       result = *b;
304       return make_error_code(errc::success);
305     }
306
307     // No root_path.
308     result = StringRef();
309     return make_error_code(errc::success);
310   }
311
312   // No path :(.
313   result = StringRef();
314   return make_error_code(errc::success);
315 }
316
317 error_code root_name(const StringRef &path, StringRef &result) {
318   const_iterator b = begin(path),
319                  e = end(path);
320   if (b != e) {
321     bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
322     bool has_drive =
323 #ifdef LLVM_ON_WIN32
324       b->endswith(":");
325 #else
326       false;
327 #endif
328
329     if (has_net || has_drive) {
330       // just {C:,//net}, return the first component.
331       result = *b;
332       return make_error_code(errc::success);
333     }
334   }
335
336   // No path or no name.
337   result = StringRef();
338   return make_error_code(errc::success);
339 }
340
341 error_code root_directory(const StringRef &path, StringRef &result) {
342   const_iterator b = begin(path),
343                  pos = b,
344                  e = end(path);
345   if (b != e) {
346     bool has_net = b->size() > 2 && is_separator((*b)[0]) && (*b)[1] == (*b)[0];
347     bool has_drive =
348 #ifdef LLVM_ON_WIN32
349       b->endswith(":");
350 #else
351       false;
352 #endif
353
354     if ((has_net || has_drive) &&
355         // {C:,//net}, skip to the next component.
356         (++pos != e) && is_separator((*pos)[0])) {
357       result = *pos;
358       return make_error_code(errc::success);
359     }
360
361     // POSIX style root directory.
362     if (!has_net && is_separator((*b)[0])) {
363       result = *b;
364       return make_error_code(errc::success);
365     }
366   }
367
368   // No path or no root.
369   result = StringRef();
370   return make_error_code(errc::success);
371 }
372
373 error_code relative_path(const StringRef &path, StringRef &result) {
374   StringRef root;
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);
378 }
379
380 error_code append(SmallVectorImpl<char> &path, const Twine &a,
381                                                const Twine &b,
382                                                const Twine &c,
383                                                const Twine &d) {
384   SmallString<32> a_storage;
385   SmallString<32> b_storage;
386   SmallString<32> c_storage;
387   SmallString<32> d_storage;
388
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));
394
395   for (SmallVectorImpl<StringRef>::const_iterator i = components.begin(),
396                                                   e = components.end();
397                                                   i != e; ++i) {
398     bool path_has_sep = !path.empty() && is_separator(path[path.size() - 1]);
399     bool component_has_sep = !i->empty() && is_separator((*i)[0]);
400     bool is_root_name;
401     if (error_code ec = has_root_name(*i, is_root_name)) return ec;
402
403     if (path_has_sep) {
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);
407
408       // Append it.
409       path.append(c.begin(), c.end());
410       continue;
411     }
412
413     if (!component_has_sep && !(path.empty() && is_root_name)) {
414       // Add a separator.
415       path.push_back(prefered_separator);
416     }
417
418     path.append(i->begin(), i->end());
419   }
420
421   return make_error_code(errc::success);
422 }
423
424 error_code make_absolute(SmallVectorImpl<char> &path) {
425   StringRef p(path.data(), path.size());
426
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;
430
431   // Already absolute.
432   if (rootName && rootDirectory)
433     return make_error_code(errc::success);
434
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;
438
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);
446   }
447
448   if (!rootName && rootDirectory) {
449     StringRef cdrn;
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);
456   }
457
458   if (rootName && !rootDirectory) {
459     StringRef pRootName;
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;
467
468     SmallString<128> res;
469     if (error_code ec = append(res, pRootName, bRootDirectory,
470                                     bRelativePath, pRelativePath)) return ec;
471     path.swap(res);
472     return make_error_code(errc::success);
473   }
474
475   llvm_unreachable("All rootName and rootDirectory combinations should have "
476                    "occurred above!");
477 }
478
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();
483   else
484     result = StringRef(path.data(), end_pos);
485   return make_error_code(errc::success);
486 }
487
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);
494 }
495
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);
501
502   // Erase existing extension.
503   size_t pos = p.find_last_of('.');
504   if (pos != StringRef::npos && pos >= filename_pos(p))
505     path.set_size(pos);
506
507   // Append '.' if needed.
508   if (ext.size() > 0 && ext[0] != '.')
509     path.push_back('.');
510
511   // Append extension.
512   path.append(ext.begin(), ext.end());
513   return make_error_code(errc::success);
514 }
515
516 error_code native(const Twine &path, SmallVectorImpl<char> &result) {
517   // Clear result.
518   result.set_size(0);
519 #ifdef LLVM_ON_WIN32
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(),
524                                  e = p.end();
525                                  i != e;
526                                  ++i) {
527     if (*i == '/')
528       result.push_back('\\');
529     else
530       result.push_back(*i);
531   }
532 #else
533   path.toVector(result);
534 #endif
535   return make_error_code(errc::success);
536 }
537
538 error_code filename(const StringRef &path, StringRef &result) {
539   result = *(--end(path));
540   return make_error_code(errc::success);
541 }
542
543 error_code stem(const StringRef &path, StringRef &result) {
544   StringRef fname;
545   if (error_code ec = filename(path, fname)) return ec;
546   size_t pos = fname.find_last_of('.');
547   if (pos == StringRef::npos)
548     result = fname;
549   else
550     if ((fname.size() == 1 && fname == ".") ||
551         (fname.size() == 2 && fname == ".."))
552       result = fname;
553     else
554       result = StringRef(fname.begin(), pos);
555
556   return make_error_code(errc::success);
557 }
558
559 error_code extension(const StringRef &path, StringRef &result) {
560   StringRef fname;
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();
565   else
566     if ((fname.size() == 1 && fname == ".") ||
567         (fname.size() == 2 && fname == ".."))
568       result = StringRef();
569     else
570       result = StringRef(fname.begin() + pos, fname.size() - pos);
571
572   return make_error_code(errc::success);
573 }
574
575 error_code has_root_name(const Twine &path, bool &result) {
576   SmallString<128> path_storage;
577   StringRef p = path.toStringRef(path_storage);
578
579   if (error_code ec = root_name(p, p)) return ec;
580
581   result = !p.empty();
582   return make_error_code(errc::success);
583 }
584
585 error_code has_root_directory(const Twine &path, bool &result) {
586   SmallString<128> path_storage;
587   StringRef p = path.toStringRef(path_storage);
588
589   if (error_code ec = root_directory(p, p)) return ec;
590
591   result = !p.empty();
592   return make_error_code(errc::success);
593 }
594
595 error_code has_root_path(const Twine &path, bool &result) {
596   SmallString<128> path_storage;
597   StringRef p = path.toStringRef(path_storage);
598
599   if (error_code ec = root_path(p, p)) return ec;
600
601   result = !p.empty();
602   return make_error_code(errc::success);
603 }
604
605 error_code has_filename(const Twine &path, bool &result) {
606   SmallString<128> path_storage;
607   StringRef p = path.toStringRef(path_storage);
608
609   if (error_code ec = filename(p, p)) return ec;
610
611   result = !p.empty();
612   return make_error_code(errc::success);
613 }
614
615 error_code has_parent_path(const Twine &path, bool &result) {
616   SmallString<128> path_storage;
617   StringRef p = path.toStringRef(path_storage);
618
619   if (error_code ec = parent_path(p, p)) return ec;
620
621   result = !p.empty();
622   return make_error_code(errc::success);
623 }
624
625 error_code has_stem(const Twine &path, bool &result) {
626   SmallString<128> path_storage;
627   StringRef p = path.toStringRef(path_storage);
628
629   if (error_code ec = stem(p, p)) return ec;
630
631   result = !p.empty();
632   return make_error_code(errc::success);
633 }
634
635 error_code has_extension(const Twine &path, bool &result) {
636   SmallString<128> path_storage;
637   StringRef p = path.toStringRef(path_storage);
638
639   if (error_code ec = extension(p, p)) return ec;
640
641   result = !p.empty();
642   return make_error_code(errc::success);
643 }
644
645 }
646 }
647 }
648
649 // Include the truly platform-specific parts.
650 #if defined(LLVM_ON_UNIX)
651 #include "Unix/PathV2.inc"
652 #endif
653 #if defined(LLVM_ON_WIN32)
654 #include "Windows/PathV2.inc"
655 #endif